web-response.test 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ;;;; web-response.test --- HTTP responses -*- mode: scheme; coding: utf-8; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2010, 2011 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 (srfi srfi-19)
  23. #:use-module (test-suite lib))
  24. ;; The newlines are equivalent to \n. From www.gnu.org.
  25. (define example-1
  26. "HTTP/1.1 200 OK\r
  27. Date: Wed, 03 Nov 2010 22:27:07 GMT\r
  28. Server: Apache/2.0.55\r
  29. Accept-Ranges: bytes\r
  30. Cache-Control: max-age=543234\r
  31. Expires: Thu, 28 Oct 2010 15:33:13 GMT\r
  32. Vary: Accept-Encoding\r
  33. Content-Encoding: gzip\r
  34. Content-Length: 36\r
  35. Content-Type: text/html; charset=utf-8\r
  36. \r
  37. abcdefghijklmnopqrstuvwxyz0123456789")
  38. (define (responses-equal? r1 body1 r2 body2)
  39. (and (equal? (response-version r1) (response-version r2))
  40. (equal? (response-code r1) (response-code r2))
  41. (equal? (response-reason-phrase r1) (response-reason-phrase r2))
  42. (equal? (response-headers r1) (response-headers r2))
  43. (equal? body1 body2)))
  44. (with-test-prefix "example-1"
  45. (let ((r #f) (body #f))
  46. (pass-if "read-response"
  47. (begin
  48. (set! r (read-response (open-input-string example-1)))
  49. (response? r)))
  50. (pass-if "read-response-body"
  51. (begin
  52. (set! body (read-response-body r))
  53. #t))
  54. (pass-if (equal? (response-version r) '(1 . 1)))
  55. (pass-if (equal? (response-code r) 200))
  56. (pass-if (equal? (response-reason-phrase r) "OK"))
  57. (pass-if (equal? body
  58. (string->utf8
  59. "abcdefghijklmnopqrstuvwxyz0123456789")))
  60. (pass-if "checking all headers"
  61. (equal?
  62. (response-headers r)
  63. `((date . ,(string->date "Wed, 03 Nov 2010 22:27:07 +0000"
  64. "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  65. (server . "Apache/2.0.55")
  66. (accept-ranges . (bytes))
  67. (cache-control . ((max-age . 543234)))
  68. (expires . ,(string->date "Thu, 28 Oct 2010 15:33:13 GMT +0000"
  69. "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  70. (vary . (accept-encoding))
  71. (content-encoding . (gzip))
  72. (content-length . 36)
  73. (content-type . (text/html (charset . "utf-8"))))))
  74. (pass-if "write then read"
  75. (call-with-values
  76. (lambda ()
  77. (with-input-from-string
  78. (with-output-to-string
  79. (lambda ()
  80. (let ((r (write-response r (current-output-port))))
  81. (write-response-body r body))))
  82. (lambda ()
  83. (let ((r (read-response (current-input-port))))
  84. (values r (read-response-body r))))))
  85. (lambda (r* body*)
  86. (responses-equal? r body r* body*))))
  87. (pass-if "by accessor"
  88. (equal? (response-content-encoding r) '(gzip)))))