| 1 |
;;;; web-server.test --- HTTP server -*- mode: scheme; coding: utf-8; -*- |
| 2 |
;;;; |
| 3 |
;;;; Copyright (C) 2019-2020, 2022, 2023 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 |
|
| 19 |
|
| 20 |
(define-module (test-suite web-client) |
| 21 |
#:use-module (web client) |
| 22 |
#:use-module (web request) |
| 23 |
#:use-module (web response) |
| 24 |
#:use-module (web server) |
| 25 |
#:use-module (web uri) |
| 26 |
#:use-module (rnrs bytevectors) |
| 27 |
#:use-module (ice-9 binary-ports) |
| 28 |
#:use-module (ice-9 match) |
| 29 |
#:use-module (ice-9 threads) |
| 30 |
#:use-module (srfi srfi-11) |
| 31 |
#:use-module (test-suite lib)) |
| 32 |
|
| 33 |
(define (handle-request request body) |
| 34 |
(match (cons (request-method request) |
| 35 |
(split-and-decode-uri-path |
| 36 |
(uri-path (request-uri request)))) |
| 37 |
(('GET) ;root |
| 38 |
(values '((content-type . (text/plain (charset . "UTF-8")))) |
| 39 |
"Hello, λ world!")) |
| 40 |
(('GET "latin1") |
| 41 |
(values '((content-type . (text/plain (charset . "ISO-8859-1")))) |
| 42 |
"Écrit comme ça en Latin-1.")) |
| 43 |
(('GET "user-agent") |
| 44 |
(values '((content-type . (text/plain))) |
| 45 |
(lambda (port) |
| 46 |
(display (assq-ref (request-headers request) 'user-agent) |
| 47 |
port)))) |
| 48 |
(('GET "quit") |
| 49 |
(values '() |
| 50 |
(lambda (port) (pk 'quit) (throw 'quit)))) |
| 51 |
(('GET _ ...) |
| 52 |
(values (build-response #:code 404) "not found")) |
| 53 |
(_ |
| 54 |
(values (build-response #:code 403 |
| 55 |
#:headers |
| 56 |
'((content-type . (application/octet-stream)))) |
| 57 |
(string->utf8 "forbidden"))))) |
| 58 |
|
| 59 |
(define %port-number 8885) |
| 60 |
(define %server-base-uri "http://localhost:8885") |
| 61 |
|
| 62 |
(when (provided? 'threads) |
| 63 |
;; Run a local publishing server in a separate thread. |
| 64 |
(call-with-new-thread |
| 65 |
(lambda () |
| 66 |
(run-server handle-request 'http `(#:port ,%port-number))))) |
| 67 |
|
| 68 |
(define-syntax-rule (expect method path code args ...) |
| 69 |
(if (provided? 'threads) |
| 70 |
(let-values (((response body) |
| 71 |
(method (string-append %server-base-uri path) |
| 72 |
#:decode-body? #t |
| 73 |
#:keep-alive? #f args ...))) |
| 74 |
(and (= code (response-code response)) |
| 75 |
body)) |
| 76 |
(throw 'unresolved))) |
| 77 |
|
| 78 |
|
| 79 |
(pass-if "server is listening" |
| 80 |
;; First, wait until the server is listening, up to a few seconds. |
| 81 |
(if (provided? 'threads) |
| 82 |
(let ((socket (socket AF_INET SOCK_STREAM 0))) |
| 83 |
(let loop ((n 1)) |
| 84 |
(define success? |
| 85 |
(catch 'system-error |
| 86 |
(lambda () |
| 87 |
(format (current-error-port) |
| 88 |
"connecting to the server, attempt #~a~%" n) |
| 89 |
(connect socket AF_INET INADDR_LOOPBACK %port-number) |
| 90 |
(close-port socket) |
| 91 |
#t) |
| 92 |
(lambda args |
| 93 |
(if (and (= ECONNREFUSED (system-error-errno args)) |
| 94 |
(<= n 15)) |
| 95 |
#f |
| 96 |
(apply throw args))))) |
| 97 |
|
| 98 |
(or success? |
| 99 |
(begin |
| 100 |
(sleep 1) |
| 101 |
(loop (+ n 1)))))) |
| 102 |
(throw 'unresolved))) |
| 103 |
|
| 104 |
(pass-if-equal "GET /" |
| 105 |
"Hello, λ world!" |
| 106 |
(expect http-get "/" 200)) |
| 107 |
|
| 108 |
(pass-if-equal "GET /latin1" |
| 109 |
"Écrit comme ça en Latin-1." |
| 110 |
(expect http-get "/latin1" 200)) |
| 111 |
|
| 112 |
(pass-if-equal "GET /user-agent" |
| 113 |
"GNU Guile" |
| 114 |
(expect http-get "/user-agent" 200 |
| 115 |
#:headers `((user-agent . "GNU Guile")))) |
| 116 |
|
| 117 |
(pass-if-equal "GET /does-not-exist" |
| 118 |
"not found" |
| 119 |
(expect http-get "/does-not-exist" 404)) |
| 120 |
|
| 121 |
(pass-if-equal "GET with keep-alive" |
| 122 |
'("Hello, λ world!" |
| 123 |
"Écrit comme ça en Latin-1." |
| 124 |
"GNU Guile") |
| 125 |
(if (provided? 'threads) |
| 126 |
(let ((port (open-socket-for-uri %server-base-uri))) |
| 127 |
(define result |
| 128 |
(map (lambda (path) |
| 129 |
(let-values (((response body) |
| 130 |
(http-get (string-append %server-base-uri path) |
| 131 |
#:port port |
| 132 |
#:keep-alive? #t |
| 133 |
#:headers |
| 134 |
'((user-agent . "GNU Guile"))))) |
| 135 |
(and (= (response-code response) 200) |
| 136 |
body))) |
| 137 |
'("/" "/latin1" "/user-agent"))) |
| 138 |
(close-port port) |
| 139 |
result) |
| 140 |
(throw 'unresolved))) |
| 141 |
|
| 142 |
(pass-if-equal "POST /" |
| 143 |
"forbidden" |
| 144 |
(utf8->string (expect http-post "/" 403))) |