File: client_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (489 lines) | stat: -rw-r--r-- 15,874 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
require "../spec_helper"
require "../../socket/spec_helper"
require "openssl"
require "http/client"
require "http/server"
require "http/log"
require "log/spec"

# TODO: Windows networking in the interpreter requires #12495
{% if flag?(:interpreted) && flag?(:win32) %}
  pending HTTP::Client
  {% skip_file %}
{% end %}

private def test_server(host, port, read_time = 0.seconds, content_type = "text/plain", write_response = true, &)
  server = TCPServer.new(host, port)
  begin
    spawn do
      io = server.accept
      sleep read_time
      if write_response
        response = HTTP::Client::Response.new(200, headers: HTTP::Headers{"Content-Type" => content_type}, body: "OK")
        response.to_io(io)
        io.flush
      end
    end

    yield server
  ensure
    server.close
  end
end

private class TestClient < HTTP::Client
  def set_defaults(request)
    super
  end
end

module HTTP
  describe Client do
    typeof(Client.new("host"))
    typeof(Client.new("host", port: 8080))
    typeof(Client.new("host", tls: true))
    typeof(Client.new(URI.new))
    typeof(Client.new(URI.parse("http://www.example.com")))

    {% for method in %w(get post put head delete patch options) %}
      typeof(Client.{{method.id}} "url")
      typeof(Client.new("host").{{method.id}}("uri"))
      typeof(Client.new("host").{{method.id}}("uri", headers: Headers {"Content-Type" => "text/plain"}))
      typeof(Client.new("host").{{method.id}}("uri", body: "body"))
    {% end %}

    typeof(Client.post "url", form: {"a" => "b"})
    typeof(Client.post("url", form: {"a" => "b"}) { })
    typeof(Client.put "url", form: {"a" => "b"})
    typeof(Client.put("url", form: {"a" => "b"}) { })
    typeof(Client.new("host").basic_auth("username", "password"))
    typeof(Client.new("host").before_request { |req| })
    typeof(Client.new("host").close)
    typeof(Client.new("host").compress = true)
    typeof(Client.new("host").compress?)
    typeof(Client.get(URI.parse("http://www.example.com")))
    typeof(Client.get(URI.parse("http://www.example.com")))
    typeof(Client.get("http://www.example.com"))
    typeof(Client.post("http://www.example.com", body: IO::Memory.new))
    typeof(Client.new("host").post("/", body: IO::Memory.new))
    typeof(Client.post("http://www.example.com", body: Bytes[65]))
    typeof(Client.new("host").post("/", body: Bytes[65]))

    describe "from String" do
      it "raises when not a host" do
        ["http://www.example.com",
         "www.example.com:8080",
         "example.com/path",
         "example.com?query",
         "http://example.com:bad_port",
         "user:pass@domain"].each do |string|
          expect_raises(ArgumentError, "The string passed to create an HTTP::Client must be just a host, not #{string.inspect}") do
            Client.new(string)
          end
        end
      end
    end

    describe "from URI" do
      it "has sane defaults" do
        cl = Client.new(URI.parse("http://example.com"))
        cl.tls?.should be_nil
        cl.port.should eq(80)
      end

      {% if !flag?(:without_openssl) %}
        it "detects HTTPS" do
          cl = Client.new(URI.parse("https://example.com"))
          cl.tls?.should be_truthy
          cl.port.should eq(443)
        end

        it "keeps context" do
          ctx = OpenSSL::SSL::Context::Client.new
          cl = Client.new(URI.parse("https://example.com"), ctx)
          cl.tls.should be(ctx)
        end

        it "doesn't take context for HTTP" do
          ctx = OpenSSL::SSL::Context::Client.new
          expect_raises(ArgumentError, "TLS context given") do
            Client.new(URI.parse("http://example.com"), ctx)
          end
        end

        it "allows for specified ports" do
          cl = Client.new(URI.parse("https://example.com:9999"))
          cl.tls?.should be_truthy
          cl.port.should eq(9999)
        end
      {% else %}
        it "raises when trying to activate TLS" do
          expect_raises(Exception, "TLS is disabled") do
            Client.new "example.org", 443, tls: true
          end
        end
      {% end %}

      it "raises error if not http schema" do
        expect_raises(ArgumentError, "Unsupported scheme: ssh") do
          Client.new(URI.parse("ssh://example.com"))
        end
      end

      it "raises error if URI is missing host" do
        expect_raises(ArgumentError, "must have host") do
          Client.new(URI.parse("http:/"))
        end
      end

      it "yields to a block" do
        Client.new(URI.parse("http://example.com")) do |client|
          typeof(client)
        end
      end
    end

    context "from a host" do
      it "yields to a block" do
        Client.new("example.com") do |client|
          typeof(client)
        end
      end
    end

    pending_ipv6 "sends the host header ipv6 with brackets" do
      server = HTTP::Server.new do |context|
        context.response.print context.request.headers["Host"]
      end
      address = server.bind_unused_port "::1"

      run_server(server) do
        HTTP::Client.get("http://[::1]:#{address.port}/").body.should eq("[::1]:#{address.port}")
      end
    end

    it "sends a 'connection: close' header on one-shot request" do
      server = HTTP::Server.new do |context|
        context.response.print context.request.headers["connection"]
      end
      address = server.bind_unused_port "127.0.0.1"

      run_server(server) do
        HTTP::Client.get("http://127.0.0.1:#{address.port}/").body.should eq("close")
      end
    end

    it "sends a 'connection: close' header on one-shot request with block" do
      server = HTTP::Server.new do |context|
        context.response.print context.request.headers["connection"]
      end
      address = server.bind_unused_port "127.0.0.1"

      run_server(server) do
        HTTP::Client.get("http://127.0.0.1:#{address.port}/") do |response|
          response.body_io.gets_to_end
        end.should eq("close")
      end
    end

    it "ensures closing the response when breaking out of block" do
      server = HTTP::Server.new { }
      address = server.bind_unused_port "127.0.0.1"

      run_server(server) do
        client = HTTP::Client.new(address.address, address.port)
        response = nil

        exc = Exception.new("")
        expect_raises Exception do
          client.get("/") do |r|
            response = r
            raise exc
          end
        end.should be exc

        response.try(&.body_io?.try(&.closed?)).should be_true
      end
    end

    it "will retry a broken socket" do
      server = HTTP::Server.new do |context|
        context.response.output.print "foo"
        context.response.output.close
        io = context.response.@io.as(Socket)
        io.linger = 0 # with linger 0 the socket will be RST on close
        io.close
      end
      address = server.bind_unused_port "127.0.0.1"

      run_server(server) do
        client = HTTP::Client.new("127.0.0.1", address.port)
        client.get(path: "/").body.should eq "foo"
        client.get(path: "/").body.should eq "foo"
        client.get(path: "/") do |resp|
          resp.body_io.gets_to_end.should eq "foo"
        end
      end
    end

    it "will retry once on connection error" do
      requests = 0
      server = HTTP::Server.new do |context|
        requests += 1
        io = context.response.@io.as(Socket)
        io.linger = 0 # with linger 0 the socket will be RST on close
        io.close
      end
      address = server.bind_unused_port "127.0.0.1"

      run_server(server) do
        client = HTTP::Client.new("127.0.0.1", address.port)
        expect_raises(IO::Error) do
          client.get(path: "/")
        end
        requests.should eq 2
      end
    end

    it "will not retry if IO::Error in request handling" do
      requests = 0
      server = HTTP::Server.new do |context|
        requests += 1
        context.response.puts "foo"
      end
      address = server.bind_unused_port "127.0.0.1"

      run_server(server) do
        client = HTTP::Client.new("127.0.0.1", address.port)
        expect_raises(IO::Error) do
          client.get(path: "/") do
            raise IO::Error.new
          end
        end
        requests.should eq 1
      end
    end

    it "will not retry when closed (non-block) (#12464)" do
      requests = 0

      client = HTTP::Client.new("127.0.0.1", 0)
      client.before_request do
        requests += 1
        raise IO::Error.new("foobar")
      end

      expect_raises(IO::Error, "foobar") do
        client.not_nil!.get(path: "/")
      end
      requests.should eq 1
    end

    it "will not retry when closed (block) (#12464)" do
      requests = 0

      client = HTTP::Client.new("127.0.0.1", 0)
      client.before_request do
        requests += 1
        raise IO::Error.new("foobar")
      end

      expect_raises(IO::Error, "foobar") do
        client.not_nil!.get(path: "/") { }
      end
      requests.should eq 1
    end

    it "retry does not affect implicit compression (#11354)" do
      server = HTTP::Server.new do |context|
        context.response.headers["Content-Encoding"] = "gzip"
        context.response.output.print "\u001F\x8B\b\u0000\u0000\u0000\u0000\u0000\u0004\u0003+\xCFH,I-K-\u0002\u0000\xB3C\u0011N\b\u0000\u0000\u0000"
        context.response.output.close
        io = context.response.@io.as(Socket)
        io.linger = 0 # with linger 0 the socket will be RST on close
        io.close
      end
      address = server.bind_unused_port "127.0.0.1"

      run_server(server) do
        client = HTTP::Client.new("127.0.0.1", address.port)
        # First request establishes the server connection, but the server
        # immediately closes it after sending the response.
        client.get(path: "/")

        # Second request tries to re-use the connection which fails (due to the
        # server's hang up) and then it retries by establishing a new connection.
        client.get(path: "/").body.should eq "whatever"
      end
    end

    it "doesn't read the body if request was HEAD" do
      resp_get = test_server("localhost", 0, 0.seconds) do |server|
        client = Client.new("localhost", server.local_address.port)
        break client.get("/")
      end

      test_server("localhost", 0, 0.seconds) do |server|
        client = Client.new("localhost", server.local_address.port)
        resp_head = client.head("/")
        resp_head.headers.should eq(resp_get.headers)
        resp_head.body.should eq("")
      end
    end

    it "raises if URI is missing scheme" do
      expect_raises(ArgumentError, "Missing scheme") do
        HTTP::Client.get URI.parse("www.example.com")
      end
    end

    it "raises if URI is missing host" do
      expect_raises(ArgumentError, "must have host") do
        HTTP::Client.get URI.parse("http://")
      end
    end

    it "tests read_timeout" do
      test_server("localhost", 0, 0.seconds) do |server|
        client = Client.new("localhost", server.local_address.port)
        client.read_timeout = 1.second
        client.get("/")
      end

      # Here we don't want to write a response on the server side because
      # it doesn't make sense to try to write because the client will already
      # timeout on read. Writing a response could lead on an exception in
      # the server if the socket is closed.
      test_server("localhost", 0, 0.5.seconds, write_response: false) do |server|
        client = Client.new("localhost", server.local_address.port)
        expect_raises(IO::TimeoutError, {% if flag?(:win32) %} "WSARecv timed out" {% else %} "Read timed out" {% end %}) do
          client.read_timeout = 1.millisecond
          client.get("/?sleep=1")
        end
      end
    end

    it "tests write_timeout" do
      # Here we don't want to write a response on the server side because
      # it doesn't make sense to try to write because the client will already
      # timeout on read. Writing a response could lead on an exception in
      # the server if the socket is closed.
      test_server("localhost", 0, 0.seconds, write_response: false) do |server|
        client = Client.new("localhost", server.local_address.port)
        expect_raises(IO::TimeoutError, {% if flag?(:win32) %} "WSASend timed out" {% else %} "Write timed out" {% end %}) do
          client.write_timeout = 1.millisecond
          client.post("/", body: "a" * 5_000_000)
        end
      end
    end

    it "tests connect_timeout" do
      test_server("localhost", 0, 0.seconds) do |server|
        client = Client.new("localhost", server.local_address.port)
        client.connect_timeout = 0.5.seconds
        client.get("/")
      end
    end

    it "tests empty Content-Type" do
      test_server("localhost", 0, content_type: "") do |server|
        client = Client.new("localhost", server.local_address.port)
        client.get("/")
      end
    end

    describe "#set_defaults" do
      it "sets default Host header" do
        client = TestClient.new "www.example.com"
        request = HTTP::Request.new("GET", "/")
        client.set_defaults(request)
        request.hostname.should eq "www.example.com"

        request = HTTP::Request.new("GET", "/", HTTP::Headers{"Host" => "other.example.com"})
        client.set_defaults(request)
        request.hostname.should eq "other.example.com"
      end
    end

    it "works with IO" do
      io_response = IO::Memory.new <<-HTTP.gsub('\n', "\r\n")
      HTTP/1.1 200 OK
      Content-Type: text/plain
      Content-Length: 3

      Hi!
      HTTP
      io_request = IO::Memory.new
      io = IO::Stapled.new(io_response, io_request)
      client = Client.new(io)
      response = client.get("/")
      response.body.should eq("Hi!")

      io_request.rewind
      request = HTTP::Request.from_io(io_request).as(HTTP::Request)
      request.hostname.should eq("")
    end

    it "can specify host and port when initialized with IO" do
      client = Client.new(IO::Memory.new, "host", 1234)
      client.host.should eq("host")
      client.port.should eq(1234)
    end

    it "cannot reconnect when initialized with IO" do
      io = IO::Memory.new
      client = Client.new(io)
      client.close
      io.closed?.should be_true
      expect_raises(Exception, "This HTTP::Client cannot be reconnected") do
        client.get("/")
      end
    end

    describe "logging" do
      it "emit logs" do
        test_server("localhost", 0, content_type: "") do |server|
          client = Client.new("localhost", server.local_address.port)
          Log.capture("http.client") do |logs|
            client.get("/")

            logs.check(:debug, "Performing request")
            logs.entry.data[:method].should eq("GET")
            logs.entry.data[:host].should eq("localhost")
            logs.entry.data[:port].should eq(server.local_address.port)
            logs.entry.data[:resource].should eq("/")
          end
        end
      end

      it "emit logs with block" do
        test_server("localhost", 0, content_type: "") do |server|
          Client.new("localhost", server.local_address.port) do |client|
            Log.capture("http.client") do |logs|
              client.get("/") do |response|
                logs.check(:debug, "Performing request")
                logs.entry.data[:method].should eq("GET")
                logs.entry.data[:host].should eq("localhost")
                logs.entry.data[:port].should eq(server.local_address.port)
                logs.entry.data[:resource].should eq("/")
              end
            end
          end
        end
      end
    end

    it "can be subclassed" do
      expect_raises(Exception, "from subclass") do
        SubClient.get("http://localhost")
      end
    end
  end

  class SubClient < HTTP::Client
    def around_exec(request, &)
      raise "from subclass"
      yield
    end
  end
end