]> gitweb.factorcode.org Git - factor.git/blob - basis/http/server/server-tests.factor
167eb7f127bef33cf51692ac80c4e551abfdedf5
[factor.git] / basis / http / server / server-tests.factor
1 USING: accessors assocs continuations http http.server
2 http.server.requests io.encodings.utf8 io.encodings.binary
3 io.streams.string kernel math peg sequences tools.test urls ;
4
5 { t } [ [ \ + first ] [ <500> ] recover response? ] unit-test
6
7 { "text/plain; charset=ASCII" } [
8     <response>
9         "text/plain" >>content-type
10         "ASCII" >>content-charset
11     unparse-content-type
12 ] unit-test
13
14 { "text/xml; charset=UTF-8" } [
15     <response>
16         "text/xml" >>content-type
17     unparse-content-type
18 ] unit-test
19
20 { "image/jpeg" } [
21     <response>
22         "image/jpeg" >>content-type
23     unparse-content-type
24 ] unit-test
25
26 { "application/octet-stream" } [
27     <response>
28     unparse-content-type
29 ] unit-test
30
31 ! RFC 2616: Section 19.3
32 ! The line terminator for message-header fields is the sequence CRLF.
33 ! However, we recommend that applications, when parsing such headers,
34 ! recognize a single LF as a line terminator and ignore the leading CR.
35 { t } [
36     {
37         "GET / HTTP/1.1"
38         "connection: close"
39         "host: 127.0.0.1:55532"
40         "user-agent: Factor http.client"
41     } [ "\n" join ] [ "\r\n" join ] bi
42     [ [ read-request ] with-string-reader ] same?
43 ] unit-test
44
45 ! RFC 2616: Section 4.1
46 ! In the interest of robustness, servers SHOULD ignore any empty
47 ! line(s) received where a Request-Line is expected. In other words, if
48 ! the server is reading the protocol stream at the beginning of a
49 ! message and receives a CRLF first, it should ignore the CRLF.
50 {
51     T{ request
52         { method "GET" }
53         { url URL" /" }
54         { proxy-url URL" " }
55         { version "1.0" }
56         { header H{ } }
57         { cookies V{ } }
58         { redirects 10 }
59     }
60 } [
61     "\r\n\r\n\r\nGET / HTTP/1.0\r\n\r\n"
62     [ read-request ] with-string-reader
63 ] unit-test
64
65 ! RFC 1945; Section 4.1
66 ! Implement a version of Simple-Request, although rather than
67 ! parse version 0.9, we parse 1.0 to return a Full-Response.
68 {
69     T{ request
70         { method "GET" }
71         { url URL" /" }
72         { proxy-url URL" " }
73         { version "1.0" }
74         { header H{ } }
75         { cookies V{ } }
76         { redirects 10 }
77     }
78 } [
79     "\r\n\r\n\r\nGET /\r\n\r\n"
80     [ read-request ] with-string-reader
81 ] unit-test
82
83 ! Don't rethrow parse-errors with an empty request string. They are
84 ! expected from certain browsers when the server serves a certificate
85 ! that the browser can't verify.
86 { } [
87     0 "" f <parse-error> \ bad-request-line boa handle-client-error
88 ] unit-test
89
90 [
91     0 "not empty" f <parse-error> handle-client-error
92 ] [ parse-error? ] must-fail-with