summaryrefslogtreecommitdiff
path: root/tests/run.scm
blob: 347c18f054a461b54097c4464e465d601e881a45 (plain)
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
(import test)

(include "../http-client.scm")
(import http-client)

(import (chicken port))

(include "testlib.scm")

(test-begin "http-client")

;; TODO: This is messy and hard to read
(test-group "simple GET requests"
  (test-group "an empty response"
    (let* ((log (with-server-response
                 (lambda ()
                   (test "Response is EOF"
                         #!eof
                         (with-input-from-request
                          "http://example.com/some/path#more"
                          #f read-string)))
                 "HTTP/1.0 200 OK\r\nContent-Length: 0\r\n"))
           (req (log-request log)))

      (test "Request method" 'GET (request-method req))
      (test "URI is path without fragment"
            "/some/path" (uri->string (request-uri req)))
      (test "host header gets set"
            '("example.com" . #f)
            (header-value 'host (request-headers req)))
      (test "HTTP request is version 1.1"
            '(1 1)
            (list (request-major req) (request-minor req)))))

  (test-group "a response with trailing garbage"
    (with-server-response
     (lambda ()
       (test "Response excludes garbage data"
             "foo"
             (with-input-from-request
              "http://example.com" #f read-string)))
     (conc "HTTP/1.0 200 OK\r\nContent-Length: 3\r\n"
           "\r\nfoobar")))

  ;; This is (mostly) an intarweb test...
  (test-group "a short chunked response with trailing garbage"
    (with-server-response
     (lambda ()
       (test "Response is the chunked data"
             "one, two three"
             (with-input-from-request "http://example.com"
                                      #f read-string)))
     (conc "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n"
           "\r\n5\r\none, \r\n2\r\ntw\r\n7\r\no three\r\n0\r\n"
           "IGNORED TRAILING GARBAGE")))

  (test-group "400 series"
    (with-server-response
     (lambda ()
       (test-error* "404 results in client error"
                    (exn http client-error)
                    (with-input-from-request "http://example.com" #f #f)))
     (conc "HTTP/1.0 404 Not Found\r\n"))))


(test-group "request body encoding"
  (test-group "simple string body"
    (let* ((log (with-server-response
                 (lambda ()
                   (test "Response is read back"
                         "Your response, sir"
                         (with-input-from-request
                          "http://example.com" "testing" read-string)))
                 "HTTP/1.0 200 OK\r\n\r\nYour response, sir"))
           (req (log-request log)))

      (test "Request method" 'POST (request-method req))
      (test "Content type is not set"
            #f
            (header-value 'content-type (request-headers req)))
      (test "Content-length is string length"
            7 (header-value 'content-length (request-headers req)))
      (test "String was sent as body" "testing" (log-body log))))

  (test-group "string body with custom request method"
    (let* ((log (with-server-response
                 (lambda ()
                   (let* ((uri (uri-reference "http://example.com"))
                          (req (make-request uri: uri method: 'LALA)))
                     (test "Response is read back"
                           "Your response, sir"
                           (with-input-from-request
                            req "testing" read-string))))
                 "HTTP/1.0 200 OK\r\n\r\nYour response, sir"))
           (req (log-request log)))

      (test "Request method is custom" 'LALA (request-method req))
      (test "Content type is not set"
            #f
            (header-value 'content-type (request-headers req)))
      (test "Content-length is string length"
            7 (header-value 'content-length (request-headers req)))
      (test "String was sent as body" "testing" (log-body log))))

  (test-group "string body using HTTP/1.0"
    (let* ((log (with-server-response
                 (lambda ()
                   (let* ((uri (uri-reference "http://example.com"))
                          (req (make-request uri: uri method: 'LALA
                                             major: 1 minor: 0)))
                     (test "Response is read back"
                           "Your response, sir"
                           (with-input-from-request
                            req "testing" read-string))))
                 "HTTP/1.0 200 OK\r\n\r\nYour response, sir"))
           (req (log-request log)))

      (test "Request method is custom" 'LALA (request-method req))
      (test "Version is correct"
            '(1 . 0)
            (cons (request-major req) (request-minor req)))
      (test "Content type is not set"
            #f
            (header-value 'content-type (request-headers req)))
      (test "Content-length is set"
            7 (header-value 'content-length (request-headers req)))
      (test "String was sent as body" "testing" (log-body log))))

  (test-group "alist form data body"
    (let* ((log (with-server-response
                 (lambda ()
                   (with-input-from-request
                    "http://example.com"
                    '((lala . "testing")
                      (another . "data")
                      ("more" . stuff))
                    read-string))
                 "HTTP/1.0 200 OK\r\n\r\n"))
           (req (log-request log)))

      (test "Request method" 'POST (request-method req))
      (test "Content type is form encoding"
            'application/x-www-form-urlencoded
            (header-value 'content-type (request-headers req)))
      (test "Content-length was set correctly"
            36 (header-value 'content-length (request-headers req)))
      (test "Body was sent correctly"
            "lala=testing&another=data&more=stuff" (log-body log))))

  (test-group "alist form data body with string port"
    (let* ((string-port (open-input-string "the file's contents"))
           (log (with-server-response
                 (lambda ()
                   (with-input-from-request
                    "http://example.com"
                    `((lala . "testing")
                      (the-file file: ,string-port
                                filename: "str")
                      ("more" . stuff))
                    read-string))
                 "HTTP/1.0 200 OK\r\n\r\n"))
           (req (log-request log))
           (h (request-headers req))
           (boundary (header-param 'boundary 'content-type h))
           (expected-data
            (conc
             "--" boundary "\r\n"
             "Content-Disposition: form-data; name=\"lala\"\r\n\r\n"
             "testing\r\n"
             "--" boundary "\r\n"
             "Content-Disposition: form-data; name=\"the-file\"; "
             "filename=\"str\"\r\n"
             "Content-Type: application/octet-stream\r\n\r\n"
             "the file's contents\r\n"
             "--" boundary "\r\n"
             "Content-Disposition: form-data; name=\"more\"\r\n\r\n"
             "stuff\r\n"
             "--" boundary "--\r\n")))

      (test "Request method" 'POST (request-method req))
      (test "Content type is multipart"
            'multipart/form-data
            (header-value 'content-type h))
      (test "Content-length was set"
            504 (header-value 'content-length h))
      (test "Version is the default HTTP version of 1.1"
            '(1 . 1)
            (cons (request-major req) (request-minor req)))
      (test "Transfer encoding is not set"
            #f
            (header-value 'transfer-encoding (request-headers req)))
      (test "Body contains the file and other data, delimited by the boundary"
            expected-data (log-body log))))

  (test-group "alist form data body with string port using HTTP/1.0"
    (let* ((string-port (open-input-string "the file's contents"))
           (uri (uri-reference "http://example.com"))
           (req (make-request uri: uri method: 'POST
                              major: 1 minor: 0))
           (log (with-server-response
                 (lambda ()
                   (with-input-from-request
                    req
                    `((lala . "testing")
                      (the-file file: ,string-port
                                filename: "str")
                      ("more" . stuff))
                    read-string))
                 "HTTP/1.0 200 OK\r\n\r\n"))
           (req (log-request log))
           (h (request-headers req))
           (boundary (header-param 'boundary 'content-type h))
           (expected-data
            (conc
             "--" boundary "\r\n"
             "Content-Disposition: form-data; name=\"lala\"\r\n\r\n"
             "testing\r\n"
             "--" boundary "\r\n"
             "Content-Disposition: form-data; name=\"the-file\"; "
             "filename=\"str\"\r\n"
             "Content-Type: application/octet-stream\r\n\r\n"
             "the file's contents\r\n"
             "--" boundary "\r\n"
             "Content-Disposition: form-data; name=\"more\"\r\n\r\n"
             "stuff\r\n"
             "--" boundary "--\r\n")))

      (test "Request method" 'POST (request-method req))
      (test "Content type is multipart"
            'multipart/form-data
            (header-value 'content-type h))
      (test "Content-length was set"
            504 (header-value 'content-length h))
      (test "Version is correct"
            '(1 . 0)
            (cons (request-major req) (request-minor req)))
      (test "Transfer encoding is not set"
            #f
            (header-value 'transfer-encoding (request-headers req)))
      (test "Body contains the file and other data, delimited by the boundary"
            expected-data (log-body log))))

  (test-group "alist form data body with custom port"
    (let* ((string-port (open-input-string "the file's contents"))
           (custom-port (make-input-port (lambda () (read-char string-port)) (constantly #t) (lambda () (close-input-port string-port))))
           (log (with-server-response
                 (lambda ()
                   (with-input-from-request
                    "http://example.com"
                    `((lala . "testing")
                      (the-file file: ,custom-port
                                filename: "str")
                      ("more" . stuff))
                    read-string))
                 "HTTP/1.0 200 OK\r\n\r\n"))
           (req (log-request log))
           (h (request-headers req))
           (boundary (header-param 'boundary 'content-type h))
           (expected-data
            (conc
             "--" boundary "\r\n"
             "Content-Disposition: form-data; name=\"lala\"\r\n\r\n"
             "testing\r\n"
             "--" boundary "\r\n"
             "Content-Disposition: form-data; name=\"the-file\"; "
             "filename=\"str\"\r\n"
             "Content-Type: application/octet-stream\r\n\r\n"
             "the file's contents\r\n"
             "--" boundary "\r\n"
             "Content-Disposition: form-data; name=\"more\"\r\n\r\n"
             "stuff\r\n"
             "--" boundary "--\r\n")))

      (test "Request method" 'POST (request-method req))
      (test "Content type is multipart"
            'multipart/form-data
            (header-value 'content-type h))
      (test "Content-length was not set"
            #f (header-value 'content-length h))
      (test "Version is the default HTTP version of 1.1"
            '(1 . 1)
            (cons (request-major req) (request-minor req)))
      (test "Transfer encoding is chunked"
            'chunked
            (header-value 'transfer-encoding (request-headers req)))
      (test "Body contains the file and other data, delimited by the boundary"
            expected-data (log-body log))))

  (test-group "alist form data body with custom port using HTTP/1.0"
    (let* ((string-port (open-input-string "the file's contents"))
           (custom-port (make-input-port (lambda () (read-char string-port)) (constantly #t) (lambda () (close-input-port string-port))))
           (uri (uri-reference "http://example.com"))
           (req (make-request uri: uri method: 'POST
                              major: 1 minor: 0))
           (log (with-server-response
                 (lambda ()
                   (with-input-from-request
                    req
                    `((lala . "testing")
                      (the-file file: ,custom-port
                                filename: "str")
                      ("more" . stuff))
                    read-string))
                 "HTTP/1.0 200 OK\r\n\r\n"))
           (req (log-request log))
           (h (request-headers req))
           (boundary (header-param 'boundary 'content-type h))
           (expected-data
            (conc
             "--" boundary "\r\n"
             "Content-Disposition: form-data; name=\"lala\"\r\n\r\n"
             "testing\r\n"
             "--" boundary "\r\n"
             "Content-Disposition: form-data; name=\"the-file\"; "
             "filename=\"str\"\r\n"
             "Content-Type: application/octet-stream\r\n\r\n"
             "the file's contents\r\n"
             "--" boundary "\r\n"
             "Content-Disposition: form-data; name=\"more\"\r\n\r\n"
             "stuff\r\n"
             "--" boundary "--\r\n")))

      (test "Request method" 'POST (request-method req))
      (test "Content type is multipart"
            'multipart/form-data
            (header-value 'content-type h))
      (test "Content-length was not set"
            #f (header-value 'content-length h))
      (test "Version is correct"
            '(1 . 0)
            (cons (request-major req) (request-minor req)))
      (test "Transfer encoding is not set"
            #f
            (header-value 'transfer-encoding (request-headers req)))
      (test "Body contains the file and other data, delimited by the boundary"
            expected-data (log-body log))))

  (test-group "alist form data body with filename"
    (let* ((tmpfile (create-temporary-file))
           (log (with-server-response
                 (lambda ()
                   (with-output-to-file tmpfile
                     (lambda () (display "the file's contents")))
                   (with-input-from-request
                    "http://example.com"
                    `((lala . "testing")
                      (the-file file: ,tmpfile filename: "tmpfile")
                      ("more" . stuff))
                    read-string))
                 "HTTP/1.0 200 OK\r\n\r\n"))
           (req (log-request log))
           (h (request-headers req))
           (boundary (header-param 'boundary 'content-type h))
           (expected-data
            (conc
             "--" boundary "\r\n"
             "Content-Disposition: form-data; name=\"lala\"\r\n\r\n"
             "testing\r\n"
             "--" boundary "\r\n"
             "Content-Disposition: form-data; name=\"the-file\"; "
             "filename=\"tmpfile\"\r\n"
             "Content-Type: application/octet-stream\r\n\r\n"
             "the file's contents\r\n"
             "--" boundary "\r\n"
             "Content-Disposition: form-data; name=\"more\"\r\n\r\n"
             "stuff\r\n"
             "--" boundary "--\r\n")))

      (test "Request method" 'POST (request-method req))
      (test "Content type is multipart"
            'multipart/form-data
            (header-value 'content-type h))
      (test "Content-length was set to the entire body size"
            (string-length expected-data)
            (header-value 'content-length h))
      (test "Body contains the file and other data, delimited by the boundary"
            expected-data (log-body log))))

  (test-group "custom writer procedure"
    (let* ((log (with-server-response
                 (lambda ()
                   (test "Response is read back"
                         "Your response, sir"
                         (with-input-from-request
                          "http://example.com"
                          (lambda ()
                            (display "test, ")
                            (display "test, 123"))
                          read-string)))
                 "HTTP/1.0 200 OK\r\n\r\nYour response, sir"))
           (req (log-request log)))

      (test "Request method" 'POST (request-method req))
      (test "Content type is not set"
            #f
            (header-value 'content-type (request-headers req)))
      (test "Transfer encoding is chunked"
            'chunked
            (header-value 'transfer-encoding (request-headers req)))
      (test "Content-length is not set"
            #f (header-value 'content-length (request-headers req)))
      (test "All writes were received"
            "test, test, 123" (log-body log))))

  (test-group "custom writer procedure with content-length header"
    (let* ((req (make-request uri: (uri-reference "http://example.com")
                              headers: (headers `((content-length 15)))
                              method: 'POST))
           (log (with-server-response
                 (lambda ()
                   (test "Response is read back"
                         "Your response, sir"
                         (with-input-from-request
                          req
                          (lambda ()
                            (display "test, ")
                            (display "test, 123"))
                          read-string)))
                 "HTTP/1.0 200 OK\r\n\r\nYour response, sir"))
           (req (log-request log)))

      (test "Request method" 'POST (request-method req))
      (test "Content type is not set"
            #f
            (header-value 'content-type (request-headers req)))
      (test "Transfer encoding is not set"
            #f
            (header-value 'transfer-encoding (request-headers req)))
      (test "Content-length is taken from user-supplied header"
            15 (header-value 'content-length (request-headers req)))
      (test "All writes were received"
            "test, test, 123" (log-body log))))

  (test-group "custom writer procedure with http/1.0 and no content-length"
    (let* ((req (make-request uri: (uri-reference "http://example.com")
                              method: 'POST major: 1 minor: 0))
           (log (with-server-response
                 (lambda ()
                   (test "Response is read back"
                         "Your response, sir"
                         (with-input-from-request
                          req
                          (lambda ()
                            (display "test, ")
                            (display "test, 123"))
                          read-string)))
                 "HTTP/1.0 200 OK\r\n\r\nYour response, sir"))
           (req (log-request log)))

      (test "Request method" 'POST (request-method req))
      (test "Content type is not set"
            #f
            (header-value 'content-type (request-headers req)))
      (test "Transfer encoding is not set"
            #f
            (header-value 'transfer-encoding (request-headers req)))
      (test "Content-length is not set"
            #f (header-value 'content-length (request-headers req)))
      ;; We could set connection: close, but for HTTP/1.0 that doesn't
      ;; really exist
      (test "Connection is not set"
            #f (header-value 'connection (request-headers req)))
      (test "All writes were received"
            "test, test, 123" (log-body log)))))

(test-group "Redirects"
  (test-group "single permanent GET redirect"
    (let* ((logs (with-server-responses
                  (lambda ()
                    (test "Final response matches final request"
                          "Got here"
                          (with-input-from-request
                           "http://example.com/some/path#more"
                           #f read-string)))
                  (conc "HTTP/1.0 301 Moved Permanently\r\n"
                        "Location: http://example.org/different/path\r\n"
                        "Content-Length: 8\r\n\r\nIgnored!")
                  (conc "HTTP/1.0 200 OK\r\nContent-Length: 8\r\n\r\n"
                        "Got here")))
           (req1 (log-request (car logs)))
           (req2 (log-request (cadr logs))))

      (test "Redirected URI is new path"
            "/different/path" (uri->string (request-uri req2)))
      (test "host header gets set on second request"
            '("example.org" . #f)
            (header-value 'host (request-headers req2)))
      (test "HTTP request is version 1.1 (even though response was 1.0)"
            '(1 1)
            (list (request-major req2) (request-minor req2)))))

  (test-group "single permanent POST redirect"
    (let* ((logs (with-server-responses
                  (lambda ()
                    (test "Final response matches final request"
                          "Got here"
                          (with-input-from-request
                           "http://example.com/some/path#more"
                           '((foo . "bar")) read-string)))
                  (conc "HTTP/1.0 301 Moved Permanently\r\n"
                        "Location: http://example.org/different/path\r\n"
                        "Content-Length: 8\r\n\r\nIgnored!")
                  (conc "HTTP/1.0 200 OK\r\nContent-Length: 8\r\n\r\n"
                        "Got here")))
           (req1 (log-request (car logs)))
           (req2 (log-request (cadr logs))))

      (test "Redirected URI is new path"
            "/different/path" (uri->string (request-uri req2)))
      (test "HTTP method is still POST" 'POST (request-method req2))
      (test "Correct content-length on both requests"
            '(7 7)
            (list (header-value 'content-length (request-headers req1))
                  (header-value 'content-length (request-headers req2))))
      (test "Body got sent to target" "foo=bar" (log-body (cadr logs)))))

  (test-group "single \"see other\" POST redirect"
    (let* ((logs (with-server-responses
                  (lambda ()
                    (test "Final response matches final request"
                          "Got here"
                          (with-input-from-request
                           "http://example.com/some/path#more"
                           '((foo . "bar")) read-string)))
                  (conc "HTTP/1.0 303 See Other\r\n"
                        "Location: http://example.org/different/path\r\n"
                        "Content-Length: 8\r\n\r\nIgnored!")
                  (conc "HTTP/1.0 200 OK\r\nContent-Length: 8\r\n\r\n"
                        "Got here")))
           (req1 (log-request (car logs)))
           (req2 (log-request (cadr logs))))

      (test "Redirected URI is new path"
            "/different/path" (uri->string (request-uri req2)))
      (test "HTTP method switched to GET" 'GET (request-method req2))
      (test "Zero content-length on target"
            0
            (header-value 'content-length (request-headers req2)))
      (test "No body got sent to target" "" (log-body (cadr logs)))))

  (test-group "Multiple redirects, just below maximum"
    (parameterize ((max-redirect-depth 3))
      (let* ((logs (with-server-responses
                    (lambda ()
                      (test "Final response matches final request"
                            "Got here"
                            (with-input-from-request
                             "http://example.com/some/path#more"
                             #f read-string)))
                    (conc "HTTP/1.0 301 Moved Permanently\r\n"
                          "Location: http://example.org/different/path\r\n"
                          "Content-Length: 8\r\n\r\nIgnored!")
                    (conc "HTTP/1.0 301 Moved Permanently\r\n"
                          "Location: http://example.org/new/path\r\n"
                          "Content-Length: 8\r\n\r\nIgnored!")
                    (conc "HTTP/1.0 301 Moved Permanently\r\n"
                          "Location: http://example.net/newer/path\r\n"
                          "Content-Length: 8\r\n\r\nIgnored!")
                    (conc "HTTP/1.0 200 OK\r\nContent-Length: 8\r\n\r\n"
                          "Got here")))
             (req (log-request (last logs))))

        (test "Redirected URI is new path"
              "/newer/path" (uri->string (request-uri req)))
        (test "host header gets set on last request"
              '("example.net" . #f)
              (header-value 'host (request-headers req)))
        (test "HTTP request is still version 1.1"
              '(1 1) (list (request-major req) (request-minor req))))))

  (test-group "exceeding maximum redirects"
    (parameterize ((max-redirect-depth 2))
      (test-error* "results in a client redirect error"
                   (exn http redirect-depth-exceeded)
                   (with-server-responses
                    (lambda ()
                      (with-input-from-request
                       "http://example.com" #f read-string))
                    (conc "HTTP/1.0 301 Moved Permanently\r\n"
                          "Location: http://example.org/different/path\r\n"
                          "Content-Length: 8\r\n\r\nIgnored!")
                    (conc "HTTP/1.0 301 Moved Permanently\r\n"
                          "Location: http://example.org/new/path\r\n"
                          "Content-Length: 8\r\n\r\nIgnored!")
                    (conc "HTTP/1.0 301 Moved Permanently\r\n"
                          "Location: http://example.net/newer/path\r\n"
                          "Content-Length: 8\r\n\r\nIgnored!")
                    (conc "HTTP/1.0 200 OK\r\nContent-Length: 19\r\n\r\n"
                          "Should not get here"))))))

(test-group "Retries"
  (test-group "premature disconnect by server"
    (test-group "just below maximum retries"
      (parameterize ((max-retry-attempts 3))
        (let* ((logs (with-server-responses
                      (lambda ()
                        (test "Final response matches final request"
                              "It worked at last"
                              (with-input-from-request
                               "http://example.com/blah" #f read-string)))
                      ;; Empty responses
                      "" ;; 0 retries
                      "" ;; 1 retry
                      "" ;; 2 retries
                      (conc "HTTP/1.0 200 OK\r\n"
                            "Content-Length: 17\r\n\r\n"
                            "It worked at last")))
               (req (log-request (last logs))))

          ;; Just a few random checks
          (test "URI is still OK"
                "/blah" (uri->string (request-uri req)))
          (test "host header is also OK"
                '("example.com" . #f)
                (header-value 'host (request-headers req)))
          (test "HTTP request is version 1.1"
                '(1 1) (list (request-major req) (request-minor req)))
          (test "No body got sent (GET)" #f (log-body (last logs))))))

    (test-group "exceeding maximum retries"
      (parameterize ((max-retry-attempts 3))
        (test-error* "results in a premature disconnection error"
                     (exn http premature-disconnection)
                     (with-server-responses
                      (lambda ()
                        (with-input-from-request
                         "http://example.com/" #f read-string))
                      ;; Empty responses
                      ""     ;; 0 retries
                      ""     ;; 1 retry
                      ""     ;; 2 retries
                      "")))) ;; 3 retries

    (test-group "no retries when retry-request? returns #f"
      (parameterize ((max-retry-attempts 5)
                     (retry-request? (lambda (r) #f)))
        (test-error* "results in a premature-disconnection error"
                     (exn http premature-disconnection)
                     (with-server-responses
                      (lambda ()
                        (with-input-from-request
                         "http://foo:bar@example.com/" #f read-string))
                      ;; Empty responses
                      ""      ;; 0 retries
                      ""))))) ;; 1 retry

  (test-group "unauthorized"
    (test-group "just below maximum retries"
      (parameterize ((max-retry-attempts 2))
        (let* ((logs (with-server-responses
                      (lambda ()
                        (test "Final response is ok"
                              "You got the password right"
                              (with-input-from-request
                               "http://foo:bar@example.com/blah" #f read-string)))
                      (conc "HTTP/1.0 401 Unauthorized\r\n"
                            "WWW-Authenticate: basic realm=\"x\"\r\n"
                            "Content-Length: 7\r\n\r\n"
                            "Retry 0")
                      (conc "HTTP/1.0 401 Unauthorized\r\n"
                            "WWW-Authenticate: basic realm=\"x\"\r\n"
                            "Content-Length: 7\r\n\r\n"
                            "Retry 1")
                      (conc "HTTP/1.0 200 OK\r\n"
                            "Content-Length: 26\r\n\r\n"
                            "You got the password right")))
               (req (log-request (last logs))))

          ;; Just a few random checks
          (test "URI is still OK"
                "/blah" (uri->string (request-uri req)))
          (test "host header is also OK"
                '("example.com" . #f)
                (header-value 'host (request-headers req)))
          (test "HTTP request is version 1.1"
                '(1 1) (list (request-major req) (request-minor req)))
          (test "No body got sent (GET)" #f (log-body (last logs))))))

    (test-group "exceeding maximum retries"
      (parameterize ((max-retry-attempts 2))
        (test-error* "results in a client error"
                     (exn http client-error)
                     (with-server-responses
                      (lambda ()
                        (with-input-from-request
                         "http://foo:bar@example.com/" #f read-string))
                      (conc "HTTP/1.0 401 Unauthorized\r\n"
                            "WWW-Authenticate: basic realm=\"x\"\r\n"
                            "Content-Length: 7\r\n\r\n"
                            "Retry 0")
                      (conc "HTTP/1.0 401 Unauthorized\r\n"
                            "WWW-Authenticate: basic realm=\"x\"\r\n"
                            "Content-Length: 7\r\n\r\n"
                            "Retry 1")
                      (conc "HTTP/1.0 401 Unauthorized\r\n"
                            "WWW-Authenticate: basic realm=\"x\"\r\n"
                            "Content-Length: 7\r\n\r\n"
                            "Retry 2")))))

    ;; TODO: Figure out some way to test the retries when there's a
    ;; net i/o error.

    (test-group "retries are OK for unauthorized when retry-request? returns #f"
      (parameterize ((max-retry-attempts 5)
                     (retry-request? (lambda (r) #f)))
        (let* ((logs (with-server-responses
                      (lambda ()
                        (test "Final response is ok"
                              "You got the password right"
                              (with-input-from-request
                               "http://foo:bar@example.com/blah" #f read-string)))
                      (conc "HTTP/1.0 401 Unauthorized\r\n"
                            "WWW-Authenticate: basic realm=\"x\"\r\n"
                            "Content-Length: 7\r\n\r\n"
                            "Retry 0")
                      (conc "HTTP/1.0 200 OK\r\n"
                            "Content-Length: 26\r\n\r\n"
                            "You got the password right")))
               (req (log-request (last logs))))

          (test "URI is still OK"
                "/blah" (uri->string (request-uri req)))
          (test "host header is also OK"
                '("example.com" . #f)
                (header-value 'host (request-headers req)))
          (test "HTTP request is version 1.1"
                '(1 1) (list (request-major req) (request-minor req)))
          (test "No body got sent (GET)" #f (log-body (last logs))))))))

(test-group "url normalization"
  (let* ((logs (with-server-responses
                (lambda ()
                  ;; Reported by Caolan McMahon in #1448: URI paths
                  ;; would be re-encoded in a lossy way, dropping
                  ;; special characters.
                  (with-input-from-request
                   "https://img.discogs.com/dMvk8q681FkVCkhv3qRvTfwlLZk=/fit-in/300x300/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/R-8062430-1454420247-1268.jpeg.jpg" #f read-string))
                (conc "HTTP/1.0 200 OK\r\n\r\n")))
         (req (log-request (last logs))))
    (test "URI path was not mangled"
          "/dMvk8q681FkVCkhv3qRvTfwlLZk=/fit-in/300x300/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/R-8062430-1454420247-1268.jpeg.jpg"
          (uri->string (request-uri req)))))

(test-group "error handling"
  (with-server-responses
   (lambda ()
     (test-error* "Invalid uri"
                  (exn http bad-uri)
                  (with-input-from-request "%" #f read-string))))
  ;; TODO: Why shouldn't empty POST be allowed?
  (with-server-responses
   (lambda ()
     (test-error* "Invalid form data"
                  (exn http form-data-error)
                  (with-input-from-request
                   "http://example.com" '() read-string)))))


(test-end "http-client")

(test-exit)