File: test_httprequest.rb

package info (click to toggle)
ruby-webrick 1.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 748 kB
  • sloc: ruby: 7,783; sh: 4; makefile: 4
file content (704 lines) | stat: -rw-r--r-- 20,958 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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# frozen_string_literal: false
require "webrick"
require "stringio"
require "test/unit"

class TestWEBrickHTTPRequest < Test::Unit::TestCase
  def teardown
    WEBrick::Utils::TimeoutHandler.terminate
    super
  end

  def test_simple_request
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert(req.meta_vars) # fails if @header was not initialized and iteration is attempted on the nil reference
  end

  def test_parse_09
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /
      foobar    # HTTP/0.9 request don't have header nor entity body.
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal("GET", req.request_method)
    assert_equal("/", req.unparsed_uri)
    assert_equal(WEBrick::HTTPVersion.new("0.9"), req.http_version)
    assert_equal(WEBrick::Config::HTTP[:ServerName], req.host)
    assert_equal(80, req.port)
    assert_equal(false, req.keep_alive?)
    assert_equal(nil, req.body)
    assert(req.query.empty?)
  end

  def test_parse_10
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET / HTTP/1.0

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal("GET", req.request_method)
    assert_equal("/", req.unparsed_uri)
    assert_equal(WEBrick::HTTPVersion.new("1.0"), req.http_version)
    assert_equal(WEBrick::Config::HTTP[:ServerName], req.host)
    assert_equal(80, req.port)
    assert_equal(false, req.keep_alive?)
    assert_equal(nil, req.body)
    assert(req.query.empty?)
  end

  def test_parse_11
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /path HTTP/1.1

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal("GET", req.request_method)
    assert_equal("/path", req.unparsed_uri)
    assert_equal("", req.script_name)
    assert_equal("/path", req.path_info)
    assert_equal(WEBrick::HTTPVersion.new("1.1"), req.http_version)
    assert_equal(WEBrick::Config::HTTP[:ServerName], req.host)
    assert_equal(80, req.port)
    assert_equal(true, req.keep_alive?)
    assert_equal(nil, req.body)
    assert(req.query.empty?)
  end

  def test_request_uri_too_large
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /#{"a"*2084} HTTP/1.1
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    assert_raise(WEBrick::HTTPStatus::RequestURITooLarge){
      req.parse(StringIO.new(msg))
    }
  end

  def test_invalid_content_length_header
    ['', ' ', ' +1', ' -1', ' a'].each do |cl|
      msg = <<~HTTP.gsub("\n", "\r\n")
        GET / HTTP/1.1
        Content-Length:#{cl}

      HTTP
      req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
      assert_raise(WEBrick::HTTPStatus::BadRequest){
        req.parse(StringIO.new(msg))
      }
    end
  end

  def test_bare_lf_request_line
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET / HTTP/1.1
      Content-Length: 0\r
      \r
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    assert_raise(WEBrick::HTTPStatus::EOFError){
      req.parse(StringIO.new(msg))
    }
  end

  def test_bare_lf_header
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET / HTTP/1.1\r
      Content-Length: 0
      \r
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    assert_raise(WEBrick::HTTPStatus::BadRequest){
      req.parse(StringIO.new(msg))
    }
  end

  def test_header_vt_ff_whitespace
    msg = <<~HTTP
      GET / HTTP/1.1\r
      Foo: \x0b1\x0c\r
      \r
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal("\x0b1\x0c", req["Foo"])

    msg = <<~HTTP
      GET / HTTP/1.1\r
      Foo: \x0b1\x0c\r
       \x0b2\x0c\r
      \r
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal("\x0b1\x0c \x0b2\x0c", req["Foo"])
  end

  def test_bare_cr_request_line
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET / HTTP/1.1\r\r
      Content-Length: 0\r
      \r
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    assert_raise(WEBrick::HTTPStatus::BadRequest){
      req.parse(StringIO.new(msg))
    }
  end

  def test_bare_cr_header
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET / HTTP/1.1\r
      Content-Type: foo\rbar\r
      \r
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    assert_raise(WEBrick::HTTPStatus::BadRequest){
      req.parse(StringIO.new(msg))
    }
  end

  def test_invalid_request_lines
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET  / HTTP/1.1\r
      Content-Length: 0\r
      \r
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    assert_raise(WEBrick::HTTPStatus::BadRequest){
      req.parse(StringIO.new(msg))
    }

    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /  HTTP/1.1\r
      Content-Length: 0\r
      \r
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    assert_raise(WEBrick::HTTPStatus::BadRequest){
      req.parse(StringIO.new(msg))
    }

    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /\r HTTP/1.1\r
      Content-Length: 0\r
      \r
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    assert_raise(WEBrick::HTTPStatus::BadRequest){
      req.parse(StringIO.new(msg))
    }

    msg = <<~HTTP.gsub("\n", "\r\n")
      GET / HTTP/1.1 \r
      Content-Length: 0\r
      \r
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    assert_raise(WEBrick::HTTPStatus::BadRequest){
      req.parse(StringIO.new(msg))
    }
  end

  def test_duplicate_content_length_header
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET / HTTP/1.1
      Content-Length: 1
      Content-Length: 2

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    assert_raise(WEBrick::HTTPStatus::BadRequest){
      req.parse(StringIO.new(msg))
    }
  end

  def test_content_length_and_transfer_encoding_headers_smuggling
    msg = <<~HTTP.gsub("\n", "\r\n")
      POST /user HTTP/1.1
      Content-Length: 28
      Transfer-Encoding: chunked

      0

      GET /admin HTTP/1.1

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_raise(WEBrick::HTTPStatus::BadRequest){
      req.body
    }
  end

  def test_parse_headers
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /path HTTP/1.1
      Host: test.ruby-lang.org:8080
      Connection: close
      Accept: text/*;q=0.3, text/html;q=0.7, text/html;level=1,
              text/html;level=2;q=0.4, */*;q=0.5
      Accept-Encoding: compress;q=0.5
      Accept-Encoding: gzip;q=1.0, identity; q=0.4, *;q=0
      Accept-Language: en;q=0.5, *; q=0
      Accept-Language: ja
      Content-Type: text/plain
      Content-Length: 8
      X-Empty-Header:

      foobar
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal(
      URI.parse("http://test.ruby-lang.org:8080/path"), req.request_uri)
    assert_equal("test.ruby-lang.org", req.host)
    assert_equal(8080, req.port)
    assert_equal(false, req.keep_alive?)
    assert_equal(
      %w(text/html;level=1 text/html */* text/html;level=2 text/*),
      req.accept)
    assert_equal(%w(gzip compress identity *), req.accept_encoding)
    assert_equal(%w(ja en *), req.accept_language)
    assert_equal(8, req.content_length)
    assert_equal("text/plain", req.content_type)
    assert_equal("foobar\r\n", req.body)
    assert_equal("", req["x-empty-header"])
    assert_equal(nil, req["x-no-header"])
    assert(req.query.empty?)
  end

  def test_parse_header2()
    msg = <<~HTTP.gsub("\n", "\r\n")
      POST /foo/bar/../baz?q=a HTTP/1.0
      Content-Length: 10
      User-Agent:
        FOO   BAR
        BAZ

      hogehoge
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal("POST", req.request_method)
    assert_equal("/foo/baz", req.path)
    assert_equal("", req.script_name)
    assert_equal("/foo/baz", req.path_info)
    assert_equal("10", req['content-length'])
    assert_equal("FOO   BAR BAZ", req['user-agent'])
    assert_equal("hogehoge\r\n", req.body)
  end

  def test_parse_headers3
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /path HTTP/1.1
      Host: test.ruby-lang.org

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal(URI.parse("http://test.ruby-lang.org/path"), req.request_uri)
    assert_equal("test.ruby-lang.org", req.host)
    assert_equal(80, req.port)

    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /path HTTP/1.1
      Host: 192.168.1.1

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal(URI.parse("http://192.168.1.1/path"), req.request_uri)
    assert_equal("192.168.1.1", req.host)
    assert_equal(80, req.port)

    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /path HTTP/1.1
      Host: [fe80::208:dff:feef:98c7]

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal(URI.parse("http://[fe80::208:dff:feef:98c7]/path"),
                 req.request_uri)
    assert_equal("[fe80::208:dff:feef:98c7]", req.host)
    assert_equal(80, req.port)

    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /path HTTP/1.1
      Host: 192.168.1.1:8080

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal(URI.parse("http://192.168.1.1:8080/path"), req.request_uri)
    assert_equal("192.168.1.1", req.host)
    assert_equal(8080, req.port)

    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /path HTTP/1.1
      Host: [fe80::208:dff:feef:98c7]:8080

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal(URI.parse("http://[fe80::208:dff:feef:98c7]:8080/path"),
                 req.request_uri)
    assert_equal("[fe80::208:dff:feef:98c7]", req.host)
    assert_equal(8080, req.port)
  end

  def test_parse_get_params
    param = "foo=1;foo=2;foo=3;bar=x"
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /path?#{param} HTTP/1.1
      Host: test.ruby-lang.org:8080

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    query = req.query
    assert_equal("1", query["foo"])
    assert_equal(["1", "2", "3"], query["foo"].to_ary)
    assert_equal(["1", "2", "3"], query["foo"].list)
    assert_equal("x", query["bar"])
    assert_equal(["x"], query["bar"].list)
  end

  def test_parse_post_params
    param = "foo=1;foo=2;foo=3;bar=x"
    msg = <<~HTTP.gsub("\n", "\r\n")
      POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
      Host: test.ruby-lang.org:8080
      Content-Length: #{param.size}
      Content-Type: application/x-www-form-urlencoded

      #{param}
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    query = req.query
    assert_equal("1", query["foo"])
    assert_equal(["1", "2", "3"], query["foo"].to_ary)
    assert_equal(["1", "2", "3"], query["foo"].list)
    assert_equal("x", query["bar"])
    assert_equal(["x"], query["bar"].list)
  end

  def test_chunked
    crlf = "\x0d\x0a"
    expect = File.binread(__FILE__).freeze
    msg = <<~HTTP.gsub("\n", "\r\n")
      POST /path HTTP/1.1
      Host: test.ruby-lang.org:8080
      Transfer-Encoding: chunked

    HTTP
    File.open(__FILE__){|io|
      while chunk = io.read(100)
        msg << chunk.size.to_s(16) << crlf
        msg << chunk << crlf
      end
    }
    msg << "0" << crlf
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal(expect, req.body)

    # chunked req.body_reader
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    dst = StringIO.new
    IO.copy_stream(req.body_reader, dst)
    assert_equal(expect, dst.string)
  end

  def test_bad_chunked
    msg = <<~HTTP
      POST /path HTTP/1.1\r
      Transfer-Encoding: chunked\r
      \r
      01x1\r
      \r
      1
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_raise(WEBrick::HTTPStatus::BadRequest){ req.body }

    # chunked req.body_reader
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    dst = StringIO.new
    assert_raise(WEBrick::HTTPStatus::BadRequest) do
      IO.copy_stream(req.body_reader, dst)
    end
  end

  def test_bad_chunked_extra_data
    msg = <<~HTTP
      POST /path HTTP/1.1\r
      Transfer-Encoding: chunked\r
      \r
      3\r
      ABCthis-all-gets-ignored\r
      0\r
      \r
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_raise(WEBrick::HTTPStatus::BadRequest){ req.body }

    # chunked req.body_reader
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    dst = StringIO.new
    assert_raise(WEBrick::HTTPStatus::BadRequest) do
      IO.copy_stream(req.body_reader, dst)
    end
  end

  def test_null_byte_in_header
    msg = <<~HTTP.gsub("\n", "\r\n")
      POST /path HTTP/1.1\r
      Evil: evil\x00\r
      \r
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    assert_raise(WEBrick::HTTPStatus::BadRequest){ req.parse(StringIO.new(msg)) }
  end

  def test_forwarded
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /foo HTTP/1.1
      Host: localhost:10080
      User-Agent: w3m/0.5.2
      X-Forwarded-For: 123.123.123.123
      X-Forwarded-Host: forward.example.com
      X-Forwarded-Server: server.example.com
      Connection: Keep-Alive

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal("server.example.com", req.server_name)
    assert_equal("http://forward.example.com/foo", req.request_uri.to_s)
    assert_equal("forward.example.com", req.host)
    assert_equal(80, req.port)
    assert_equal("123.123.123.123", req.remote_ip)
    assert(!req.ssl?)

    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /foo HTTP/1.1
      Host: localhost:10080
      User-Agent: w3m/0.5.2
      X-Forwarded-For: 192.168.1.10, 172.16.1.1, 123.123.123.123
      X-Forwarded-Host: forward.example.com:8080
      X-Forwarded-Server: server.example.com
      Connection: Keep-Alive

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal("server.example.com", req.server_name)
    assert_equal("http://forward.example.com:8080/foo", req.request_uri.to_s)
    assert_equal("forward.example.com", req.host)
    assert_equal(8080, req.port)
    assert_equal("123.123.123.123", req.remote_ip)
    assert(!req.ssl?)

    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /foo HTTP/1.1
      Host: localhost:10080
      Client-IP: 234.234.234.234
      X-Forwarded-Proto: https, http
      X-Forwarded-For: 192.168.1.10, 10.0.0.1, 123.123.123.123
      X-Forwarded-Host: forward.example.com
      X-Forwarded-Server: server.example.com
      X-Requested-With: XMLHttpRequest
      Connection: Keep-Alive

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal("server.example.com", req.server_name)
    assert_equal("https://forward.example.com/foo", req.request_uri.to_s)
    assert_equal("forward.example.com", req.host)
    assert_equal(443, req.port)
    assert_equal("234.234.234.234", req.remote_ip)
    assert(req.ssl?)

    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /foo HTTP/1.1
      Host: localhost:10080
      Client-IP: 234.234.234.234
      X-Forwarded-Proto: https
      X-Forwarded-For: 192.168.1.10
      X-Forwarded-Host: forward1.example.com:1234, forward2.example.com:5678
      X-Forwarded-Server: server1.example.com, server2.example.com
      X-Requested-With: XMLHttpRequest
      Connection: Keep-Alive

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal("server1.example.com", req.server_name)
    assert_equal("https://forward1.example.com:1234/foo", req.request_uri.to_s)
    assert_equal("forward1.example.com", req.host)
    assert_equal(1234, req.port)
    assert_equal("234.234.234.234", req.remote_ip)
    assert(req.ssl?)

    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /foo HTTP/1.1
      Host: localhost:10080
      Client-IP: 234.234.234.234
      X-Forwarded-Proto: https
      X-Forwarded-For: 192.168.1.10
      X-Forwarded-Host: [fd20:8b1e:b255:8154:250:56ff:fea8:4d84], forward2.example.com:5678
      X-Forwarded-Server: server1.example.com, server2.example.com
      X-Requested-With: XMLHttpRequest
      Connection: Keep-Alive

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal("server1.example.com", req.server_name)
    assert_equal("https://[fd20:8b1e:b255:8154:250:56ff:fea8:4d84]/foo", req.request_uri.to_s)
    assert_equal("[fd20:8b1e:b255:8154:250:56ff:fea8:4d84]", req.host)
    assert_equal(443, req.port)
    assert_equal("234.234.234.234", req.remote_ip)
    assert(req.ssl?)

    msg = <<~HTTP.gsub("\n", "\r\n")
      GET /foo HTTP/1.1
      Host: localhost:10080
      Client-IP: 234.234.234.234
      X-Forwarded-Proto: https
      X-Forwarded-For: 192.168.1.10
      X-Forwarded-Host: [fd20:8b1e:b255:8154:250:56ff:fea8:4d84]:1234, forward2.example.com:5678
      X-Forwarded-Server: server1.example.com, server2.example.com
      X-Requested-With: XMLHttpRequest
      Connection: Keep-Alive

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert_equal("server1.example.com", req.server_name)
    assert_equal("https://[fd20:8b1e:b255:8154:250:56ff:fea8:4d84]:1234/foo", req.request_uri.to_s)
    assert_equal("[fd20:8b1e:b255:8154:250:56ff:fea8:4d84]", req.host)
    assert_equal(1234, req.port)
    assert_equal("234.234.234.234", req.remote_ip)
    assert(req.ssl?)
  end

  def test_continue_sent
    msg = <<~HTTP.gsub("\n", "\r\n")
      POST /path HTTP/1.1
      Expect: 100-continue

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert req['expect']
    l = msg.size
    req.continue
    assert_not_equal l, msg.size
    assert_match(/HTTP\/1.1 100 continue\r\n\r\n\z/, msg)
    assert !req['expect']
  end

  def test_continue_not_sent
    msg = <<~HTTP.gsub("\n", "\r\n")
      POST /path HTTP/1.1

    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new(msg))
    assert !req['expect']
    l = msg.size
    req.continue
    assert_equal l, msg.size
  end

  def test_bad_messages
    param = "foo=1;foo=2;foo=3;bar=x"
    msg = <<~HTTP.gsub("\n", "\r\n")
      POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
      Host: test.ruby-lang.org:8080
      Content-Type: application/x-www-form-urlencoded

      #{param}
    HTTP
    assert_raise(WEBrick::HTTPStatus::LengthRequired){
      req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
      req.parse(StringIO.new(msg))
      req.body
    }

    msg = <<~HTTP.gsub("\n", "\r\n")
      POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
      Host: test.ruby-lang.org:8080
      Content-Length: 100000

      body is too short.
    HTTP
    assert_raise(WEBrick::HTTPStatus::BadRequest){
      req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
      req.parse(StringIO.new(msg))
      req.body
    }

    msg = <<~HTTP.gsub("\n", "\r\n")
      POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
      Host: test.ruby-lang.org:8080
      Transfer-Encoding: foobar

      body is too short.
    HTTP
    assert_raise(WEBrick::HTTPStatus::NotImplemented){
      req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
      req.parse(StringIO.new(msg))
      req.body
    }
  end

  def test_eof_raised_when_line_is_nil
    assert_raise(WEBrick::HTTPStatus::EOFError) {
      req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
      req.parse(StringIO.new(""))
    }
  end

  def test_eof_raised_with_missing_line_between_headers_and_body
    msg = <<~HTTP.gsub("\n", "\r\n")
      GET / HTTP/1.0
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    assert_raise(WEBrick::HTTPStatus::EOFError) {
      req.parse(StringIO.new(msg))
    }

    msg = <<~HTTP.gsub("\n", "\r\n")
      GET / HTTP/1.0
      Foo: 1
    HTTP
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    assert_raise(WEBrick::HTTPStatus::EOFError) {
      req.parse(StringIO.new(msg))
    }
  end

  def test_cookie_join
    req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
    req.parse(StringIO.new("GET / HTTP/1.1\r\ncookie: a=1\r\ncookie: b=2\r\n\r\n"))
    assert_equal 2, req.cookies.length
    assert_equal 'a=1; b=2', req['cookie']
  end
end