File: allowing_and_disabling_net_connect.rb

package info (click to toggle)
ruby-webmock 3.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,172 kB
  • sloc: ruby: 12,829; makefile: 6
file content (313 lines) | stat: -rw-r--r-- 13,428 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
shared_context "allowing and disabling net connect" do |*adapter_info|
  describe "when net connect" do
    describe "is allowed", net_connect: true do
      before(:each) do
        WebMock.allow_net_connect!
      end

      it "should make a real web request if request is not stubbed" do
        expect(http_request(:get, webmock_server_url).status).to eq("200")
      end

      it "should make a real https request if request is not stubbed" do
        unless http_library == :httpclient
          result = http_request(:get, "https://www.google.com/").body
          if result.respond_to? :encode
            result = result.encode(
              'UTF-8',
              'binary',
              invalid: :replace,
              undef: :replace,
              replace: ''
            )
          end
          expect(result).to match(/.*google.*/)
        end
      end

      it "should return stubbed response if request was stubbed" do
        stub_request(:get, "www.example.com").to_return(body: "abc")
        expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
      end
    end

    describe "is not allowed" do
      before(:each) do
        WebMock.disable_net_connect!
      end

      it "should return stubbed response if request was stubbed" do
        stub_request(:get, "www.example.com").to_return(body: "abc")
        expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
      end

      it "should return stubbed response if request with path was stubbed" do
        stub_request(:get, "www.example.com/hello_world").to_return(body: "abc")
        expect(http_request(:get, "http://www.example.com/hello_world").body).to eq("abc")
      end

      it "should raise exception if request was not stubbed" do
        expect {
          http_request(:get, "http://www.example.com/")
        }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
      end
    end

    describe "is not allowed with exception for localhost" do
      before(:each) do
        WebMock.disable_net_connect!(allow_localhost: true)
      end

      it "should return stubbed response if request was stubbed" do
        stub_request(:get, "www.example.com").to_return(body: "abc")
        expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
      end

      it "should raise exception if request was not stubbed" do
        expect {
          http_request(:get, "http://www.example.com/")
        }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
      end

      it "should make a real request to localhost" do
        expect {
          http_request(:get, "http://localhost:12345/")
        }.to raise_error(connection_refused_exception_class)
      end

      it "should make a real request to 127.0.0.1" do
        expect {
          http_request(:get, "http://127.0.0.1:12345/")
        }.to raise_error(connection_refused_exception_class)
      end

      it "should make a real request to 0.0.0.0" do
        expect {
          http_request(:get, "http://0.0.0.0:12345/")
        }.to raise_error(connection_refused_exception_class)
      end
    end

    describe "is not allowed, with exceptions" do
      describe "allowing by host string" do
        before :each do
          WebMock.disable_net_connect!(allow: 'https://httpstat.us')
        end

        context "when the host is not allowed" do
          it "should return stubbed response if request was stubbed" do
            stub_request(:get, 'disallowed.example.com/foo').to_return(body: "abc")
            expect(http_request(:get, 'http://disallowed.example.com/foo').body).to eq("abc")
          end

          it "should raise exception if request was not stubbed" do
            expect {
              http_request(:get, 'http://disallowed.example.com/')
            }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://disallowed.example.com))
          end
        end

        context "when the host is allowed" do
          it "should return stubbed response if request was stubbed" do
            stub_request(:get, 'https://httpstat.us/200').to_return(body: "abc")
            expect(http_request(:get, "https://httpstat.us/200").body).to eq("abc")
          end

          # WARNING: this makes a real HTTP request!
          it "should make a real request to allowed host", net_connect: true do
            expect(http_request(:get, "https://httpstat.us/200").status).to eq('200')
          end
        end
      end

      describe "allowing by host:port string" do
        def replace_with_different_port(uri)
          uri.sub(%r{:(\d+)}){|m0, m1| ':' + ($~[1].to_i + 1).to_s }
        end

        let(:allowed_host_with_port) { WebMockServer.instance.host_with_port }
        let(:disallowed_host_with_port) { replace_with_different_port(allowed_host_with_port) }

        before :each do
          WebMock.disable_net_connect!(allow: allowed_host_with_port)
        end

        context "when the host is not allowed" do
          it "should return stubbed response if request was stubbed" do
            request_url = "http://#{disallowed_host_with_port}/foo"
            stub_request(:get, request_url).to_return(body: "abc")
            expect(http_request(:get, request_url).body).to eq("abc")
          end

          it "should raise exception if request was not stubbed" do
            request_url = "http://#{disallowed_host_with_port}/foo"
            expect {
              http_request(:get, request_url)
            }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET #{request_url}))
          end
        end

        context "when the host is allowed" do
          it "should return stubbed response if request was stubbed" do
            request_url = "http://#{allowed_host_with_port}/foo"
            stub_request(:get, request_url).to_return(body: "abc")
            expect(http_request(:get, request_url).body).to eq('abc')
          end

          it "should make a real request to allowed host", net_connect: true do
            request_url = "http://#{allowed_host_with_port}/foo"
            expect(http_request(:get, request_url).status).to eq('200')
          end
        end
      end

      describe "allowing by scheme:host string" do
        before :each do
          WebMock.disable_net_connect!(allow: 'https://www.google.pl')
        end

        context "when the host and scheme is not allowed" do
          it "should return stubbed response if request was stubbed" do
            stub_request(:get, 'https://disallowed.example.com/foo').to_return(body: "abc")
            expect(http_request(:get, 'https://disallowed.example.com/foo').body).to eq("abc")
          end

          it "should raise exception if request was not stubbed" do
            expect {
              http_request(:get, 'https://disallowed.example.com/')
            }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET https://disallowed.example.com))
          end

          it "should raise exception if request was made to different port" do
            expect {
              http_request(:get, 'https://www.google.pl:80/')
            }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET https://www.google.pl:80))
          end

          it "should raise exception if request was made to different scheme" do
            expect {
              http_request(:get, 'http://www.google.pl/')
            }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.google.pl))
          end
        end

        context "when the host is allowed" do
          it "should return stubbed response if request was stubbed" do
            stub_request(:get, 'https://www.google.pl').to_return(body: "abc")
            expect(http_request(:get, "https://www.google.pl/").body).to eq("abc")
          end

          it "should make a real request to allowed host with scheme", net_connect: true do
            method = http_library == :httpclient ? :head : :get #https://github.com/nahi/httpclient/issues/299
            expect(http_request(method, "https://www.google.pl/").status).to eq('200')
          end

          it "should make a real request to allowed host with scheme and port", net_connect: true do
            method = http_library == :httpclient ? :head : :get
            expect(http_request(method, "https://www.google.pl:443/").status).to eq('200')
          end
        end
      end

      describe "allowing by regular expression" do
        before :each do
          WebMock.disable_net_connect!(allow: %r{httpstat})
        end

        context "when the host is not allowed" do
          it "should return stubbed response if request was stubbed" do
            stub_request(:get, 'disallowed.example.com/foo').to_return(body: "abc")
            expect(http_request(:get, 'http://disallowed.example.com/foo').body).to eq("abc")
          end

          it "should raise exception if request was not stubbed" do
            expect {
              http_request(:get, 'http://disallowed.example.com/')
            }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://disallowed.example.com))
          end
        end

        context "when the host is allowed" do
          it "should return stubbed response if request was stubbed" do
            stub_request(:get, 'https://httpstat.us/200').to_return(body: "abc")
            expect(http_request(:get, "https://httpstat.us/200").body).to eq("abc")
          end

          # WARNING: this makes a real HTTP request!
          it "should make a real request to allowed host", net_connect: true do
            expect(http_request(:get, "https://httpstat.us/200").status).to eq('200')
          end

          it "should make a real request if request is allowed by path regexp and url contains default port", net_connect: true do
            WebMock.disable_net_connect!(allow: %r{www.google.pl/webhp})
            method = http_library == :httpclient ? :head : :get
            expect(http_request(method, 'https://www.google.pl:443/webhp').status).to eq('200')
          end
        end
      end

      describe "allowing by a callable" do
        before :each do
          WebMock.disable_net_connect!(allow: lambda{|url| url.to_str.include?('httpstat') })
        end

        context "when the host is not allowed" do
          it "should return stubbed response if request was stubbed" do
            stub_request(:get, 'disallowed.example.com/foo').to_return(body: "abc")
            expect(http_request(:get, 'http://disallowed.example.com/foo').body).to eq("abc")
          end

          it "should raise exception if request was not stubbed" do
            expect {
              http_request(:get, 'http://disallowed.example.com/')
            }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://disallowed.example.com))
          end
        end

        context "when the host is allowed" do
          it "should return stubbed response if request was stubbed" do
            stub_request(:get, 'https://httpstat.us/200').to_return(body: "abc")
            expect(http_request(:get, "https://httpstat.us/200").body).to eq("abc")
          end

          # WARNING: this makes a real HTTP request!
          it "should make a real request to allowed host", net_connect: true do
            expect(http_request(:get, "https://httpstat.us/200").status).to eq('200')
          end
        end
      end

      describe "allowing by a list of the above" do
        before :each do
          WebMock.disable_net_connect!(allow: [lambda{|_| false }, %r{foobar}, 'https://httpstat.us'])
        end

        context "when the host is not allowed" do
          it "should return stubbed response if request was stubbed" do
            stub_request(:get, 'disallowed.example.com/foo').to_return(body: "abc")
            expect(http_request(:get, 'http://disallowed.example.com/foo').body).to eq("abc")
          end

          it "should raise exception if request was not stubbed" do
            expect {
              http_request(:get, 'http://disallowed.example.com/')
            }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://disallowed.example.com))
          end
        end

        context "when the host is allowed" do
          it "should return stubbed response if request was stubbed" do
            stub_request(:get, 'https://httpstat.us/200').to_return(body: "abc")
            expect(http_request(:get, "https://httpstat.us/200").body).to eq("abc")
          end

          # WARNING: this makes a real HTTP request!
          it "should make a real request to allowed host", net_connect: true do
            expect(http_request(:get, "https://httpstat.us/200").status).to eq('200')
          end
        end
      end

    end
  end
end