|
| 1 | +#lang racket/base |
| 2 | +(require rackunit |
| 3 | + racket/port |
| 4 | + racket/promise |
| 5 | + net/url |
| 6 | + web-server/http |
| 7 | + (prefix-in logger: web-server/dispatchers/dispatch-logresp) |
| 8 | + (prefix-in logger*: web-server/dispatchers/dispatch-log) |
| 9 | + (prefix-in lift: web-server/dispatchers/dispatch-lift) |
| 10 | + "../util.rkt") |
| 11 | + |
| 12 | +(let ([bad-logger 'this-should-fail]) |
| 13 | + (check-exn exn:fail:contract? |
| 14 | + (lambda () |
| 15 | + (logger:make #:format bad-logger |
| 16 | + #:log-path (open-output-nowhere))))) |
| 17 | + |
| 18 | +(let () |
| 19 | + (define-values (ip op) (make-pipe)) |
| 20 | + |
| 21 | + (define (make-dispatcher formatter code) |
| 22 | + (logger:make #:format formatter |
| 23 | + #:log-path op |
| 24 | + (lift:make |
| 25 | + (λ (req) |
| 26 | + (response/xexpr #:code code |
| 27 | + '(hello world)))))) |
| 28 | + |
| 29 | + (define dispatcher/req+resp/200 |
| 30 | + (make-dispatcher (logger:log-format->format 'apache-default) |
| 31 | + 200)) |
| 32 | + |
| 33 | + (define dispatcher/req+resp/404 |
| 34 | + (make-dispatcher (logger:log-format->format 'apache-default) |
| 35 | + 404)) |
| 36 | + |
| 37 | + (define dispatcher/req/200 |
| 38 | + (make-dispatcher (logger*:log-format->format 'apache-default) |
| 39 | + 200)) |
| 40 | + |
| 41 | + (define req |
| 42 | + (request #"GET" |
| 43 | + (string->url "whatever") |
| 44 | + (list) |
| 45 | + (delay (list)) |
| 46 | + #f |
| 47 | + "localhost" |
| 48 | + 80 |
| 49 | + "nada")) |
| 50 | + |
| 51 | + (define conn (fake-connection-for-bytes #"")) |
| 52 | + |
| 53 | + (test-case "req+resp 200" |
| 54 | + (dispatcher/req+resp/200 conn req) |
| 55 | + ;; the [...] part of the regexp matches a time-dependent piece of |
| 56 | + ;; the log data |
| 57 | + (check-regexp-match #px"^nada - - \\[[^\\]].+\\] \"GET whatever HTTP/1.1\" 200 -$" |
| 58 | + (read-line ip))) |
| 59 | + |
| 60 | + (test-case "req+resp 404" |
| 61 | + (dispatcher/req+resp/404 conn req) |
| 62 | + (check-regexp-match #px"^nada - - \\[[^\\]].+\\] \"GET whatever HTTP/1.1\" 404 -$" |
| 63 | + (read-line ip))) |
| 64 | + |
| 65 | + (test-case "req 200" |
| 66 | + (dispatcher/req/200 conn req) |
| 67 | + (check-regexp-match #px"^nada - - \\[[^\\]].+\\] \"GET whatever HTTP/1.1\" - -$" |
| 68 | + (read-line ip)))) |
0 commit comments