web-response.test 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ;;;; web-response.test --- HTTP responses -*- mode: scheme; coding: utf-8; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2010-2016 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (test-suite web-response)
  19. #:use-module (web uri)
  20. #:use-module (web response)
  21. #:use-module (rnrs bytevectors)
  22. #:use-module (rnrs io ports)
  23. #:use-module (srfi srfi-19)
  24. #:use-module (test-suite lib))
  25. ;; The newlines are equivalent to \n. From www.gnu.org.
  26. (define example-1
  27. "HTTP/1.1 200 OK\r
  28. Date: Wed, 03 Nov 2010 22:27:07 GMT\r
  29. Server: Apache/2.0.55\r
  30. Accept-Ranges: bytes\r
  31. Cache-Control: max-age=543234\r
  32. Expires: Thu, 28 Oct 2010 15:33:13 GMT\r
  33. Vary: Accept-Encoding\r
  34. Content-Encoding: gzip\r
  35. Content-Length: 36\r
  36. Content-Type: text/html; charset=utf-8\r
  37. \r
  38. abcdefghijklmnopqrstuvwxyz0123456789
  39. -> Here is trailing garbage that should be ignored because it is
  40. beyond Content-Length.")
  41. (define example-2
  42. "HTTP/1.1 200 OK\r
  43. Transfer-Encoding: chunked\r
  44. Content-Type: text/plain
  45. \r
  46. 1c\r
  47. Lorem ipsum dolor sit amet, \r
  48. 1d\r
  49. consectetur adipisicing elit,\r
  50. 43\r
  51. sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\r
  52. 0\r\n")
  53. (define (responses-equal? r1 body1 r2 body2)
  54. (and (equal? (response-version r1) (response-version r2))
  55. (equal? (response-code r1) (response-code r2))
  56. (equal? (response-reason-phrase r1) (response-reason-phrase r2))
  57. (equal? (response-headers r1) (response-headers r2))
  58. (equal? body1 body2)))
  59. (with-test-prefix "example-1"
  60. (let ((r #f) (body #f))
  61. (pass-if "read-response"
  62. (begin
  63. (set! r (read-response (open-input-string example-1)))
  64. (response? r)))
  65. (pass-if "read-response-body"
  66. (begin
  67. (set! body (read-response-body r))
  68. #t))
  69. (pass-if-equal '(1 . 1) (response-version r))
  70. (pass-if-equal 200 (response-code r))
  71. (pass-if-equal "OK" (response-reason-phrase r))
  72. (pass-if-equal (string->utf8 "abcdefghijklmnopqrstuvwxyz0123456789")
  73. body)
  74. (pass-if-equal "checking all headers"
  75. `((date . ,(string->date "Wed, 03 Nov 2010 22:27:07 +0000"
  76. "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  77. (server . "Apache/2.0.55")
  78. (accept-ranges . (bytes))
  79. (cache-control . ((max-age . 543234)))
  80. (expires . ,(string->date "Thu, 28 Oct 2010 15:33:13 GMT +0000"
  81. "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  82. (vary . (accept-encoding))
  83. (content-encoding . (gzip))
  84. (content-length . 36)
  85. (content-type . (text/html (charset . "utf-8"))))
  86. (response-headers r))
  87. (pass-if "write then read"
  88. (call-with-values
  89. (lambda ()
  90. (with-input-from-string
  91. (with-output-to-string
  92. (lambda ()
  93. (let ((r (write-response r (current-output-port))))
  94. (write-response-body r body))))
  95. (lambda ()
  96. (let ((r (read-response (current-input-port))))
  97. (values r (read-response-body r))))))
  98. (lambda (r* body*)
  99. (responses-equal? r body r* body*))))
  100. (pass-if-equal "by accessor"
  101. '(gzip)
  102. (response-content-encoding r))
  103. (pass-if-equal "response-body-port"
  104. `("UTF-8" ,body)
  105. (with-fluids ((%default-port-encoding #f))
  106. (let* ((r (read-response (open-input-string example-1)))
  107. (p (response-body-port r)))
  108. (list (port-encoding p) (get-bytevector-all p)))))
  109. (pass-if "response-body-port + close"
  110. (with-fluids ((%default-port-encoding #f))
  111. (let* ((r (read-response (open-input-string example-1)))
  112. (p (response-body-port r #:keep-alive? #f)))
  113. ;; Before, calling 'close-port' here would yield a
  114. ;; wrong-arg-num error when calling the delimited input port's
  115. ;; 'close' procedure.
  116. (close-port p)
  117. (port-closed? p))))))
  118. (with-test-prefix "example-2"
  119. (let* ((r (read-response (open-input-string example-2)))
  120. (b (read-response-body r)))
  121. (pass-if-equal '((chunked))
  122. (response-transfer-encoding r))
  123. (pass-if-equal
  124. (string->utf8
  125. (string-append
  126. "Lorem ipsum dolor sit amet, consectetur adipisicing elit,"
  127. " sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."))
  128. b)
  129. (pass-if-equal "response-body-port"
  130. `("ISO-8859-1" ,(utf8->string b)) ; no `charset', hence ISO-8859-1
  131. (with-fluids ((%default-port-encoding #f))
  132. (let* ((r (read-response (open-input-string example-2)))
  133. (p (response-body-port r)))
  134. (list (port-encoding p) (get-string-all p)))))))