File: stub_web_service_spec.rb

package info (click to toggle)
ruby-sham-rack 1.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 172 kB
  • sloc: ruby: 712; makefile: 4
file content (114 lines) | stat: -rw-r--r-- 2,154 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require "spec_helper"

require "sham_rack/stub_web_service"
require "rack/test"

RSpec.describe ShamRack::StubWebService do

  include Rack::Test::Methods

  attr_reader :app

  before(:each) do
    @app = ShamRack::StubWebService.new
  end

  describe "#last_request" do

    it "returns the last request" do
      get '/foo/bar'
      expect(@app.last_request.path_info).to eq("/foo/bar")
    end

  end

  describe "with no handlers registered" do

    describe "any request" do

      before do
        get "/foo/456"
      end

      it "returns a 404" do
        expect(last_response.status).to eq(404)
      end

    end

  end

  describe "with two handlers registered" do

    before(:each) do

      @app.handle do |request|
        [200, {}, ["response from first handler"]] if request.get?
      end

      @app.handle do |request|
        [200, {}, ["response from second handler"]] if request.path_info == "/stuff"
      end

    end

    describe "a request matching the first handler" do

      before do
        get "/foo/456"
      end

      it "receives a response from the first handler" do
        expect(last_response.body).to eq("response from first handler")
      end

    end

    describe "a request matching the second handler" do

      before do
        post "/stuff"
      end

      it "receives a response from the second handler" do
        expect(last_response.body).to eq("response from second handler")
      end

    end

    describe "a request matching both handlers" do

      before do
        get "/stuff"
      end

      it "receives a response from the second handler" do
        expect(last_response.body).to eq("response from second handler")
      end

    end

  end

  describe ".register_resource" do

    before do
      @app.register_resource("/stuff?foo=bar", "STUFF", "text/plain", 202)
      get "/stuff?foo=bar"
    end

    it "sets body" do
      expect(last_response.body).to eq("STUFF")
    end

    it "sets content-type" do
      expect(last_response.content_type).to eq("text/plain")
    end

    it "sets status code" do
      expect(last_response.status).to eq(202)
    end

  end

end