1 USING: http http.server http.client http.client.private tools.test multiline
2 io.streams.string io.encodings.utf8 io.encodings.8-bit
3 io.encodings.binary io.encodings.string kernel arrays splitting
4 sequences assocs io.sockets db db.sqlite continuations urls
5 hashtables accessors namespaces ;
8 [ "text/plain" latin1 ] [ "text/plain" parse-content-type ] unit-test
10 [ "text/html" utf8 ] [ "text/html; charset=UTF-8" parse-content-type ] unit-test
12 [ "application/octet-stream" binary ] [ "application/octet-stream" parse-content-type ] unit-test
14 [ { } ] [ "" parse-cookie ] unit-test
15 [ { } ] [ "" parse-set-cookie ] unit-test
17 ! Make sure that totally invalid cookies don't confuse us
18 [ { } ] [ "hello world; how are you" parse-cookie ] unit-test
20 : lf>crlf "\n" split "\r\n" join ;
22 STRING: read-request-test-1
27 Content-type: application/octet-stream
34 { url T{ url { path "/bar" } } }
37 { header H{ { "some-header" "1; 2" } { "content-length" "4" } { "content-type" "application/octet-stream" } } }
38 { post-data T{ post-data { data "blah" } { content-type "application/octet-stream" } } }
42 read-request-test-1 lf>crlf [
47 STRING: read-request-test-1'
50 content-type: application/octet-stream
56 read-request-test-1' 1array [
57 read-request-test-1 lf>crlf
58 [ read-request ] with-string-reader
59 [ write-request ] with-string-writer
61 string-lines "\n" join
64 STRING: read-request-test-2
72 { url T{ url { host "www.sex.com" } { path "/bar" } } }
75 { header H{ { "host" "www.sex.com" } } }
79 read-request-test-2 lf>crlf [
84 STRING: read-request-test-3
89 [ read-request-test-3 lf>crlf [ read-request ] with-string-reader ]
90 [ "Bad request: URL" = ]
93 STRING: read-request-test-4
95 Host: "www.amazon.com"
100 read-request-test-4 lf>crlf [ read-request ] with-string-reader
104 STRING: read-response-test-1
105 HTTP/1.1 404 not found
106 Content-Type: text/html; charset=UTF-8
115 { message "not found" }
116 { header H{ { "content-type" "text/html; charset=UTF-8" } } }
118 { content-type "text/html" }
119 { content-charset utf8 }
122 read-response-test-1 lf>crlf
123 [ read-response ] with-string-reader
127 STRING: read-response-test-1'
128 HTTP/1.1 404 not found
129 content-type: text/html; charset=UTF-8
134 read-response-test-1' 1array [
135 URL" http://localhost/" url set
136 read-response-test-1 lf>crlf
137 [ read-response ] with-string-reader
138 [ write-response ] with-string-writer
140 string-lines "\n" join
144 "rmid=732423sdfs73242; path=/; domain=.example.net; expires=Fri, 31-Dec-2010 23:59:59 GMT"
145 dup parse-set-cookie first unparse-set-cookie =
150 dup parse-set-cookie first unparse-set-cookie =
153 STRING: read-response-test-2
154 HTTP/1.1 200 Content follows
155 Set-Cookie: oo="bar; a=b"; httponly=yes; sid=123456
161 read-response-test-2 lf>crlf
162 [ read-response ] with-string-reader
166 STRING: read-response-test-3
167 HTTP/1.1 200 Content follows
168 Set-Cookie: oo="bar; a=b"; comment="your mom"; httponly=yes
174 read-response-test-3 lf>crlf
175 [ read-response ] with-string-reader
180 USING: http.server http.server.static furnace.sessions furnace.alloy
181 furnace.actions furnace.auth furnace.auth.login furnace.db http.client
182 io.servers.connection io.files io.files.temp io.directories io io.encodings.ascii
183 accessors namespaces threads
184 http.server.responses http.server.redirection furnace.redirection
185 http.server.dispatchers db.tuples ;
189 [ stop-this-server "Goodbye" "text/html" <content> ] >>display
190 "quit" add-responder ;
192 : test-db-file "test.db" temp-file ;
194 : test-db test-db-file <sqlite-db> ;
196 [ test-db-file delete-file ] ignore-errors
202 : test-httpd ( responder -- )
209 sockets>> first addr>> port>>
210 ] with-scope "port" set ;
216 "resource:basis/http/test" <static> >>default
217 "nested" add-responder
219 [ URL" redirect-loop" <temporary-redirect> ] >>display
220 "redirect-loop" add-responder
225 : add-port ( url -- url' )
226 >url clone "port" get >>port ;
229 "resource:basis/http/test/foo.html" ascii file-contents
230 "http://localhost/nested/foo.html" add-port http-get nip =
233 [ "http://localhost/redirect-loop" add-port http-get nip ]
234 [ too-many-redirects? ] must-fail-with
237 "http://localhost/quit" add-port http-get nip
240 ! HTTP client redirect bug
244 <action> [ "quit" <temporary-redirect> ] >>display
245 "redirect" add-responder
251 "http://localhost/redirect" add-port http-get nip
256 [ "http://localhost/quit" add-port http-get 2drop ] ignore-errors
268 <action> "" add-responder
270 test-db <db-persistence>
275 : 404? [ download-failed? ] [ response>> code>> 404 = ] bi and ;
277 ! This should give a 404 not an infinite redirect loop
278 [ "http://localhost/d/blah" add-port http-get nip ] [ 404? ] must-fail-with
280 ! This should give a 404 not an infinite redirect loop
281 [ "http://localhost/blah/" add-port http-get nip ] [ 404? ] must-fail-with
283 [ "Goodbye" ] [ "http://localhost/quit" add-port http-get nip ] unit-test
287 <action> [ [ "Hi" write ] "text/plain" <content> ] >>display
292 test-db <db-persistence>
297 [ "Hi" ] [ "http://localhost/" add-port http-get nip ] unit-test
299 [ "Goodbye" ] [ "http://localhost/quit" add-port http-get nip ] unit-test
301 USING: html.components html.elements html.forms
302 xml xml.utilities validators
303 furnace furnace.conversations ;
310 [ a get-global "a" set-value ] >>init
311 [ [ <html> "a" <field> render </html> ] "text/html" <content> ] >>display
312 [ { { "a" [ v-integer ] } } validate-params ] >>validate
313 [ "a" value a set-global URL" " <redirect> ] >>submit
318 test-db <db-persistence>
325 : test-a string>xml "input" tag-named "value" swap at ;
328 "http://localhost/" add-port http-get
329 swap dup cookies>> "cookies" set session-id-key get-cookie
330 value>> "session-id" set test-a
336 "http://localhost" add-port "__u" set
337 "session-id" get session-id-key set
339 "http://localhost/" add-port <post-request> "cookies" get >>cookies http-request nip test-a
342 [ 4 ] [ a get-global ] unit-test
348 "http://localhost" add-port "__u" set
349 "session-id" get session-id-key set
351 "http://localhost/" add-port <post-request> "cookies" get >>cookies http-request nip test-a
354 [ 4 ] [ a get-global ] unit-test
356 [ "Goodbye" ] [ "http://localhost/quit" add-port http-get nip ] unit-test
359 [ f ] [ <404> dup clone "b" "a" set-header drop "a" header ] unit-test
360 [ f ] [ <404> dup clone "b" "a" <cookie> put-cookie drop "a" get-cookie ] unit-test