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