|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (test-suite web-client) |
|
#:use-module (web client) |
|
#:use-module (web request) |
|
#:use-module (web response) |
|
#:use-module (web server) |
|
#:use-module (web uri) |
|
#:use-module (rnrs bytevectors) |
|
#:use-module (ice-9 binary-ports) |
|
#:use-module (ice-9 match) |
|
#:use-module (ice-9 threads) |
|
#:use-module (srfi srfi-11) |
|
#:use-module (test-suite lib)) |
|
|
|
(define (handle-request request body) |
|
(match (cons (request-method request) |
|
(split-and-decode-uri-path |
|
(uri-path (request-uri request)))) |
|
(('GET) |
|
(values '((content-type . (text/plain (charset . "UTF-8")))) |
|
"Hello, 位 world!")) |
|
(('GET "latin1") |
|
(values '((content-type . (text/plain (charset . "ISO-8859-1")))) |
|
"脡crit comme 莽a en Latin-1.")) |
|
(('GET "user-agent") |
|
(values '((content-type . (text/plain))) |
|
(lambda (port) |
|
(display (assq-ref (request-headers request) 'user-agent) |
|
port)))) |
|
(('GET "quit") |
|
(values '() |
|
(lambda (port) (pk 'quit) (throw 'quit)))) |
|
(('GET _ ...) |
|
(values (build-response #:code 404) "not found")) |
|
(_ |
|
(values (build-response #:code 403 |
|
#:headers |
|
'((content-type . (application/octet-stream)))) |
|
(string->utf8 "forbidden"))))) |
|
|
|
(define %port-number 8885) |
|
(define %server-base-uri "http://localhost:8885") |
|
|
|
(when (provided? 'threads) |
|
|
|
(call-with-new-thread |
|
(lambda () |
|
(run-server handle-request 'http `(#:port ,%port-number))))) |
|
|
|
(define-syntax-rule (expect method path code args ...) |
|
(if (provided? 'threads) |
|
(let-values (((response body) |
|
(method (string-append %server-base-uri path) |
|
#:decode-body? #t |
|
#:keep-alive? #f args ...))) |
|
(and (= code (response-code response)) |
|
body)) |
|
(throw 'unresolved))) |
|
|
|
|
|
(pass-if "server is listening" |
|
|
|
(if (provided? 'threads) |
|
(let ((socket (socket AF_INET SOCK_STREAM 0))) |
|
(let loop ((n 1)) |
|
(define success? |
|
(catch 'system-error |
|
(lambda () |
|
(format (current-error-port) |
|
"connecting to the server, attempt #~a~%" n) |
|
(connect socket AF_INET INADDR_LOOPBACK %port-number) |
|
(close-port socket) |
|
#t) |
|
(lambda args |
|
(if (and (= ECONNREFUSED (system-error-errno args)) |
|
(<= n 15)) |
|
#f |
|
(apply throw args))))) |
|
|
|
(or success? |
|
(begin |
|
(sleep 1) |
|
(loop (+ n 1)))))) |
|
(throw 'unresolved))) |
|
|
|
(pass-if-equal "GET /" |
|
"Hello, 位 world!" |
|
(expect http-get "/" 200)) |
|
|
|
(pass-if-equal "GET /latin1" |
|
"脡crit comme 莽a en Latin-1." |
|
(expect http-get "/latin1" 200)) |
|
|
|
(pass-if-equal "GET /user-agent" |
|
"GNU Guile" |
|
(expect http-get "/user-agent" 200 |
|
#:headers `((user-agent . "GNU Guile")))) |
|
|
|
(pass-if-equal "GET /does-not-exist" |
|
"not found" |
|
(expect http-get "/does-not-exist" 404)) |
|
|
|
(pass-if-equal "GET with keep-alive" |
|
'("Hello, 位 world!" |
|
"脡crit comme 莽a en Latin-1." |
|
"GNU Guile") |
|
(if (provided? 'threads) |
|
(let ((port (open-socket-for-uri %server-base-uri))) |
|
(define result |
|
(map (lambda (path) |
|
(let-values (((response body) |
|
(http-get (string-append %server-base-uri path) |
|
#:port port |
|
#:keep-alive? #t |
|
#:headers |
|
'((user-agent . "GNU Guile"))))) |
|
(and (= (response-code response) 200) |
|
body))) |
|
'("/" "/latin1" "/user-agent"))) |
|
(close-port port) |
|
result) |
|
(throw 'unresolved))) |
|
|
|
(pass-if-equal "POST /" |
|
"forbidden" |
|
(utf8->string (expect http-post "/" 403))) |
|
|