web-server.test 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ;;;; web-server.test --- HTTP server -*- mode: scheme; coding: utf-8; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2019, 2020 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-client)
  19. #:use-module (web client)
  20. #:use-module (web request)
  21. #:use-module (web response)
  22. #:use-module (web server)
  23. #:use-module (web uri)
  24. #:use-module (rnrs bytevectors)
  25. #:use-module (ice-9 binary-ports)
  26. #:use-module (ice-9 match)
  27. #:use-module (ice-9 threads)
  28. #:use-module (srfi srfi-11)
  29. #:use-module (test-suite lib))
  30. (define (handle-request request body)
  31. (match (cons (request-method request)
  32. (split-and-decode-uri-path
  33. (uri-path (request-uri request))))
  34. (('GET) ;root
  35. (values '((content-type . (text/plain (charset . "UTF-8"))))
  36. "Hello, λ world!"))
  37. (('GET "latin1")
  38. (values '((content-type . (text/plain (charset . "ISO-8859-1"))))
  39. "Écrit comme ça en Latin-1."))
  40. (('GET "user-agent")
  41. (values '((content-type . (text/plain)))
  42. (lambda (port)
  43. (display (assq-ref (request-headers request) 'user-agent)
  44. port))))
  45. (('GET "quit")
  46. (values '()
  47. (lambda (port) (pk 'quit) (throw 'quit))))
  48. (('GET _ ...)
  49. (values (build-response #:code 404) "not found"))
  50. (_
  51. (values (build-response #:code 403
  52. #:headers
  53. '((content-type . (application/octet-stream))))
  54. (string->utf8 "forbidden")))))
  55. (define %port-number 8885)
  56. (define %server-base-uri "http://localhost:8885")
  57. (when (provided? 'threads)
  58. ;; Run a local publishing server in a separate thread.
  59. (call-with-new-thread
  60. (lambda ()
  61. (run-server handle-request 'http `(#:port ,%port-number)))))
  62. (define-syntax-rule (expect method path code args ...)
  63. (if (provided? 'threads)
  64. (let-values (((response body)
  65. (method (string-append %server-base-uri path)
  66. #:decode-body? #t
  67. #:keep-alive? #f args ...)))
  68. (and (= code (response-code response))
  69. body))
  70. (throw 'unresolved)))
  71. (pass-if-equal "GET /"
  72. "Hello, λ world!"
  73. (expect http-get "/" 200))
  74. (pass-if-equal "GET /latin1"
  75. "Écrit comme ça en Latin-1."
  76. (expect http-get "/latin1" 200))
  77. (pass-if-equal "GET /user-agent"
  78. "GNU Guile"
  79. (expect http-get "/user-agent" 200
  80. #:headers `((user-agent . "GNU Guile"))))
  81. (pass-if-equal "GET /does-not-exist"
  82. "not found"
  83. (expect http-get "/does-not-exist" 404))
  84. (pass-if-equal "GET with keep-alive"
  85. '("Hello, λ world!"
  86. "Écrit comme ça en Latin-1."
  87. "GNU Guile")
  88. (if (provided? 'threads)
  89. (let ((port (open-socket-for-uri %server-base-uri)))
  90. (define result
  91. (map (lambda (path)
  92. (let-values (((response body)
  93. (http-get (string-append %server-base-uri path)
  94. #:port port
  95. #:keep-alive? #t
  96. #:headers
  97. '((user-agent . "GNU Guile")))))
  98. (and (= (response-code response) 200)
  99. body)))
  100. '("/" "/latin1" "/user-agent")))
  101. (close-port port)
  102. result)
  103. (throw 'unresolved)))
  104. (pass-if-equal "POST /"
  105. "forbidden"
  106. (utf8->string (expect http-post "/" 403)))