1 USING: destructors http http.server http.server.requests http.client
2 http.client.private tools.test multiline fry io.streams.string io.crlf
3 io.encodings.utf8 io.encodings.8-bit io.encodings.binary io.encodings.string
4 io.encodings.ascii kernel arrays splitting sequences assocs io.sockets db
5 db.sqlite make continuations urls hashtables accessors namespaces xml.data
6 io.encodings.8-bit.latin1 random combinators.short-circuit literals ;
9 { "text/plain" "UTF-8" } [ "text/plain" parse-content-type ] unit-test
11 { "text/html" "ASCII" } [ "text/html; charset=ASCII" parse-content-type ] unit-test
13 { "text/html" "utf-8" } [ "text/html; charset=\"utf-8\"" parse-content-type ] unit-test
15 { "application/octet-stream" f } [ "application/octet-stream" parse-content-type ] unit-test
17 { "localhost" f } [ "localhost" parse-host ] unit-test
18 { "localhost" 8888 } [ "localhost:8888" parse-host ] unit-test
19 { "::1" 8888 } [ "::1:8888" parse-host ] unit-test
20 { "127.0.0.1" 8888 } [ "127.0.0.1:8888" parse-host ] unit-test
22 { "localhost" } [ T{ url { protocol "http" } { host "localhost" } } unparse-host ] unit-test
23 { "localhost" } [ T{ url { protocol "http" } { host "localhost" } { port 80 } } unparse-host ] unit-test
24 { "localhost" } [ T{ url { protocol "https" } { host "localhost" } { port 443 } } unparse-host ] unit-test
25 { "localhost:8080" } [ T{ url { protocol "http" } { host "localhost" } { port 8080 } } unparse-host ] unit-test
26 { "localhost:8443" } [ T{ url { protocol "https" } { host "localhost" } { port 8443 } } unparse-host ] unit-test
28 STRING: read-request-test-1
33 Content-type: application/octet-stream
40 { url T{ url { path "/bar" } } }
41 { proxy-url T{ url } }
44 { header H{ { "some-header" "1; 2" } { "content-length" "4" } { "content-type" "application/octet-stream" } } }
45 { post-data T{ post-data { data "blah" } { content-type "application/octet-stream" } } }
50 read-request-test-1 lf>crlf [
55 STRING: read-request-test-1'
58 content-type: application/octet-stream
64 ${ read-request-test-1' } [
65 read-request-test-1 lf>crlf
66 [ read-request ] with-string-reader
67 [ write-request ] with-string-writer
69 string-lines "\n" join
72 STRING: read-request-test-2
80 { url T{ url { host "www.sex.com" } { path "/bar" } } }
81 { proxy-url T{ url } }
84 { header H{ { "host" "www.sex.com" } } }
89 read-request-test-2 lf>crlf [
94 STRING: read-request-test-2'
102 { url T{ url { host "www.sex.com" } { port 101 } { path "/bar" } } }
103 { proxy-url T{ url } }
106 { header H{ { "host" "www.sex.com:101" } } }
111 read-request-test-2' lf>crlf [
116 STRING: read-request-test-3
121 STRING: read-request-test-4
123 Host: "www.amazon.com"
128 read-request-test-4 lf>crlf [ read-request ] with-string-reader
132 STRING: read-response-test-1
133 HTTP/1.1 404 not found
134 Content-Type: text/html; charset=UTF-8
143 { message "not found" }
144 { header H{ { "content-type" "text/html; charset=UTF-8" } } }
146 { content-type "text/html" }
147 { content-charset "UTF-8" }
148 { content-encoding utf8 }
151 read-response-test-1 lf>crlf
152 [ read-response ] with-string-reader
156 STRING: read-response-test-1'
157 HTTP/1.1 404 not found
158 content-type: text/html; charset=UTF-8
162 ${ read-response-test-1' } [
163 URL" http://localhost/" url set
164 read-response-test-1 lf>crlf
165 [ read-response ] with-string-reader
166 [ write-response ] with-string-writer
168 string-lines "\n" join
172 "rmid=732423sdfs73242; path=/; domain=.example.net; expires=Fri, 31-Dec-2010 23:59:59 GMT"
173 dup parse-set-cookie first unparse-set-cookie =
178 dup parse-set-cookie first unparse-set-cookie =
181 STRING: read-response-test-2
182 HTTP/1.1 200 Content follows
183 Set-Cookie: oo="bar; a=b"; httponly=yes; sid=123456
189 read-response-test-2 lf>crlf
190 [ read-response ] with-string-reader
194 STRING: read-response-test-3
195 HTTP/1.1 200 Content follows
196 Set-Cookie: oo="bar; a=b"; comment="your mom"; httponly=yes
202 read-response-test-3 lf>crlf
203 [ read-response ] with-string-reader
208 USING: http.server.static furnace.sessions furnace.alloy
209 furnace.actions furnace.auth furnace.auth.login furnace.db
210 io.servers io.files io.files.temp io.directories io
212 http.server.responses http.server.redirection furnace.redirection
213 http.server.dispatchers db.tuples ;
215 : add-quit-action ( responder -- responder )
217 [ stop-this-server "Goodbye" "text/html" <content> ] >>display
218 "quit" add-responder ;
220 : test-db-file ( -- path ) "test.db" temp-file ;
222 : test-db ( -- db ) test-db-file <sqlite-db> ;
224 : add-addr ( url -- url' )
225 >url clone "addr" get set-url-addr ;
227 : stop-test-httpd ( -- )
228 "http://localhost/quit" add-addr http-get nip
232 test-db-file ?delete-file
239 : test-with-dispatcher ( dispatcher quot -- )
242 <http-server> 0 >>insecure f >>secure
244 server-addrs random "addr" set @
245 ] with-threaded-server
246 ] with-scope ; inline
250 :: test-with-db-persistence ( db-persistence quot -- )
252 quot test-with-dispatcher
253 ] with-disposal ; inline
258 "vocab:http/test" <static> >>default
259 "nested" add-responder
261 [ URL" redirect-loop" <temporary-redirect> ] >>display
262 "redirect-loop" add-responder [
265 "vocab:http/test/foo.html" ascii file-contents
266 "http://localhost/nested/foo.html" add-addr http-get nip =
269 [ "http://localhost/redirect-loop" add-addr http-get nip ]
270 [ too-many-redirects? ] must-fail-with
273 "http://localhost/quit" add-addr http-get nip
276 ] test-with-dispatcher
278 ! HTTP client redirect bug
281 <action> [ "quit" <temporary-redirect> ] >>display
282 "redirect" add-responder [
285 "http://localhost/redirect" add-addr http-get nip
289 [ stop-test-httpd ] ignore-errors
292 ] test-with-dispatcher
295 : 404? ( response -- ? )
298 [ response>> response? ]
299 [ response>> code>> 404 = ]
309 <action> "" add-responder
311 test-db <db-persistence> [
313 ! This should give a 404 not an infinite redirect loop
314 [ "http://localhost/d/blah" add-addr http-get nip ] [ 404? ] must-fail-with
316 ! This should give a 404 not an infinite redirect loop
317 [ "http://localhost/blah/" add-addr http-get nip ] [ 404? ] must-fail-with
319 [ "Goodbye" ] [ "http://localhost/quit" add-addr http-get nip ] unit-test
321 ] test-with-db-persistence
324 <action> [ [ "Hi" write ] "text/plain" <content> ] >>display
329 test-db <db-persistence> [
331 [ "Hi" ] [ "http://localhost/" add-addr http-get nip ] unit-test
333 [ "Goodbye" ] [ "http://localhost/quit" add-addr http-get nip ] unit-test
335 ] test-with-db-persistence
337 USING: html.components html.forms
338 xml xml.traversal validators
339 furnace furnace.conversations ;
343 : test-a ( xml -- value )
344 string>xml body>> "input" deep-tag-named "value" attr ;
348 [ a get-global "a" set-value ] >>init
349 [ [ "<html>" write "a" <field> render "</html>" write ] "text/html" <content> ] >>display
350 [ { { "a" [ v-integer ] } } validate-params ] >>validate
351 [ "a" value a set-global URL" " <redirect> ] >>submit
356 test-db <db-persistence> [
361 "http://localhost/" add-addr http-get
362 swap dup cookies>> "cookies" set session-id-key get-cookie
363 value>> "session-id" set test-a
369 "http://localhost" add-addr "__u" ,,
370 "session-id" get session-id-key ,,
372 "http://localhost/" add-addr <post-request> "cookies" get >>cookies
373 http-request nip test-a
376 [ 4 ] [ a get-global ] unit-test
382 "http://localhost" add-addr "__u" ,,
383 "session-id" get session-id-key ,,
385 "http://localhost/" add-addr <post-request> "cookies" get >>cookies
386 http-request nip test-a
389 [ 4 ] [ a get-global ] unit-test
391 [ "Goodbye" ] [ "http://localhost/quit" add-addr http-get nip ] unit-test
393 ] test-with-db-persistence
396 { f } [ <404> dup clone "b" "a" set-header drop "a" header ] unit-test
397 { f } [ <404> dup clone "b" "a" <cookie> put-cookie drop "a" get-cookie ] unit-test
400 { "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" } [
401 <request> "Aladdin" "open sesame" set-basic-auth "Authorization" header
404 ! Test a corner case with static responder
407 "vocab:http/test/foo.html" <static> >>default [
409 "http://localhost/" add-addr http-get nip
410 "vocab:http/test/foo.html" ascii file-contents =
413 [ ] [ stop-test-httpd ] unit-test
415 ] test-with-dispatcher
417 ! Check behavior of 307 redirect (reported by Chris Double)
421 [ "b" <temporary-redirect> ] >>submit
425 request get post-data>> data>> "data" =
426 [ "OK" "text/plain" <content> ] [ "OOPS" throw ] if
430 [ "OK" ] [ "data" "http://localhost/a" add-addr http-post nip ] unit-test
432 ! Check that download throws errors (reported by Chris Double)
435 "http://localhost/tweet_my_twat" add-addr download
436 ] with-temp-directory
439 [ ] [ stop-test-httpd ] unit-test
441 ] test-with-dispatcher
443 ! Check that index.fhtml works
445 "resource:basis/http/test/" <static> enable-fhtml >>default
448 [ "OK\n" ] [ "http://localhost/" add-addr http-get nip ] unit-test
450 [ ] [ stop-test-httpd ] unit-test
452 ] test-with-dispatcher
454 ! Check that just closing the socket without sending anything works
457 [ ] [ "addr" get binary [ ] with-client ] unit-test
459 [ ] [ stop-test-httpd ] unit-test
461 ] test-with-dispatcher