web-server.test 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ;;;; web-server.test --- HTTP server -*- mode: scheme; coding: utf-8; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2019-2020, 2022 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 "server is listening"
  72. ;; First, wait until the server is listening, up to a few seconds.
  73. (let ((socket (socket AF_INET SOCK_STREAM 0)))
  74. (let loop ((n 1))
  75. (define success?
  76. (catch 'system-error
  77. (lambda ()
  78. (format (current-error-port)
  79. "connecting to the server, attempt #~a~%" n)
  80. (connect socket AF_INET INADDR_LOOPBACK %port-number)
  81. (close-port socket)
  82. #t)
  83. (lambda args
  84. (if (and (= ECONNREFUSED (system-error-errno args))
  85. (<= n 15))
  86. #f
  87. (apply throw args)))))
  88. (or success?
  89. (begin
  90. (sleep 1)
  91. (loop (+ n 1)))))))
  92. (pass-if-equal "GET /"
  93. "Hello, λ world!"
  94. (expect http-get "/" 200))
  95. (pass-if-equal "GET /latin1"
  96. "Écrit comme ça en Latin-1."
  97. (expect http-get "/latin1" 200))
  98. (pass-if-equal "GET /user-agent"
  99. "GNU Guile"
  100. (expect http-get "/user-agent" 200
  101. #:headers `((user-agent . "GNU Guile"))))
  102. (pass-if-equal "GET /does-not-exist"
  103. "not found"
  104. (expect http-get "/does-not-exist" 404))
  105. (pass-if-equal "GET with keep-alive"
  106. '("Hello, λ world!"
  107. "Écrit comme ça en Latin-1."
  108. "GNU Guile")
  109. (if (provided? 'threads)
  110. (let ((port (open-socket-for-uri %server-base-uri)))
  111. (define result
  112. (map (lambda (path)
  113. (let-values (((response body)
  114. (http-get (string-append %server-base-uri path)
  115. #:port port
  116. #:keep-alive? #t
  117. #:headers
  118. '((user-agent . "GNU Guile")))))
  119. (and (= (response-code response) 200)
  120. body)))
  121. '("/" "/latin1" "/user-agent")))
  122. (close-port port)
  123. result)
  124. (throw 'unresolved)))
  125. (pass-if-equal "POST /"
  126. "forbidden"
  127. (utf8->string (expect http-post "/" 403)))