File: callbacks.rb

package info (click to toggle)
ruby-webmock 3.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,196 kB
  • sloc: ruby: 12,905; makefile: 6
file content (148 lines) | stat: -rw-r--r-- 5,055 bytes parent folder | download
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
shared_context "callbacks" do |*adapter_info|
  describe "when after_request callback is declared" do
    before(:each) do
      @called = nil
      WebMock.reset_callbacks
      stub_request(:get, "http://www.example.com")
    end

    it "should not invoke callback unless request is made" do
      WebMock.after_request {
        @called = true
      }
      expect(@called).to eq(nil)
    end

    it "should invoke a callback after request is made" do
      WebMock.after_request {
        @called = true
      }
      http_request(:get, "http://www.example.com/")
      expect(@called).to eq(true)
    end

    it "should not invoke a callback if this http library should be ignored" do
      WebMock.after_request(except: [http_library()]) {
        @called = true
      }
      http_request(:get, "http://www.example.com/")
      expect(@called).to eq(nil)
    end

    it "should invoke a callback even if other http libraries should be ignored" do
      WebMock.after_request(except: [:other_lib]) {
        @called = true
      }
      http_request(:get, "http://www.example.com/")
      expect(@called).to eq(true)
    end

    it "should pass request signature to the callback" do
      WebMock.after_request(except: [:other_lib])  do |request_signature, _|
        @request_signature = request_signature
      end
      http_request(:get, "http://www.example.com/")
      expect(@request_signature.uri.to_s).to eq("http://www.example.com:80/")
    end

    after(:each) do
      WebMock::StubRegistry.instance.global_stubs.clear
    end

    it 'passes the same request signature instance to the callback that was passed to the global stub callback' do
      global_stub_request_sig = after_request_request_sig = nil
      WebMock.globally_stub_request do |request_sig|
        global_stub_request_sig = request_sig
        nil
      end

      WebMock.after_request do |request_sig, _|
        after_request_request_sig = request_sig
      end

      http_request(:get, "http://www.example.com/")
      expect(global_stub_request_sig).to be(after_request_request_sig)
    end

    context "passing response to callback" do
      context "when request is stubbed" do
        before(:each) do
          stub_request(:get, "http://www.example.com").
          to_return(
            status: [200, "hello"],
            headers: {'Content-Length' => '666', 'Hello' => 'World'},
            body: "foo bar"
          )
          WebMock.after_request(except: [:other_lib])  do |_, response|
            @response = response
          end
          http_request(:get, "http://www.example.com/")
        end

        it "should pass response to callback with the status and message" do
          expect(@response.status).to eq([200, "hello"])
        end

        it "should pass response to callback with headers" do
          expect(@response.headers).to eq({
            'Content-Length' => '666',
            'Hello' => 'World'
          })
        end

        it "should pass response to callback with body" do
          expect(@response.body).to eq("foo bar")
        end
      end

      describe "when request is not stubbed", net_connect: true do
        before(:each) do
          WebMock.reset!
          WebMock.allow_net_connect!
          WebMock.after_request(except: [:other_lib])  do |_, response|
            @response = response
          end
          http_request(:get, "#{HTTP_STATUS_SERVICE}/201", headers: { "Accept" => "*" })
        end

        it "should pass real response to callback with status and message" do
          expect(@response.status[0]).to eq(201)
          expect(@response.status[1]).to eq("Created") unless adapter_info.include?(:no_status_message)
        end

        it "should pass real response to callback with headers" do
          expect(@response.headers["Server"]).to eq( "Kestrel")
          expect(@response.headers["Content-Length"]).to eq("11") unless adapter_info.include?(:no_content_length_header)
        end

        it "should pass response to callback with body" do
          expect(@response.body.size).to eq(11)
        end
      end
    end

    it "should invoke multiple callbacks in order of their declarations" do
      WebMock.after_request { @called = 1 }
      WebMock.after_request { @called += 1 }
      http_request(:get, "http://www.example.com/")
      expect(@called).to eq(2)
    end

    it "should invoke callbacks only for real requests if requested", net_connect: true do
      WebMock.after_request(real_requests_only: true) { @called = true }
      http_request(:get, "http://www.example.com/")
      expect(@called).to eq(nil)
      WebMock.allow_net_connect!
      http_request(:get, "http://www.example.net/")
      expect(@called).to eq(true)
    end

    it "should not invoke any callbacks after callbacks were reset" do
      WebMock.after_request { @called = 1 }
      WebMock.reset_callbacks
      stub_request(:get, "http://www.example.com/")
      http_request(:get, "http://www.example.com/")
      expect(@called).to eq(nil)
    end
  end
end