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 -- )
240 [ main-responder ] dip '[
241 <http-server> 0 >>insecure f >>secure
243 server-addrs random "addr" set @
244 ] with-threaded-server
245 ] with-variable ; inline
249 :: test-with-db-persistence ( db-persistence quot -- )
251 quot test-with-dispatcher
252 ] with-disposal ; inline
257 "vocab:http/test" <static> >>default
258 "nested" add-responder
260 [ URL" redirect-loop" <temporary-redirect> ] >>display
261 "redirect-loop" add-responder [
264 "vocab:http/test/foo.html" ascii file-contents
265 "http://localhost/nested/foo.html" add-addr http-get nip =
268 [ "http://localhost/redirect-loop" add-addr http-get nip ]
269 [ too-many-redirects? ] must-fail-with
272 "http://localhost/quit" add-addr http-get nip
275 ] test-with-dispatcher
277 ! HTTP client redirect bug
280 <action> [ "quit" <temporary-redirect> ] >>display
281 "redirect" add-responder [
284 "http://localhost/redirect" add-addr http-get nip
288 [ stop-test-httpd ] ignore-errors
291 ] test-with-dispatcher
294 : 404? ( response -- ? )
297 [ response>> response? ]
298 [ response>> code>> 404 = ]
308 <action> "" add-responder
310 test-db <db-persistence> [
312 ! This should give a 404 not an infinite redirect loop
313 [ "http://localhost/d/blah" add-addr http-get nip ] [ 404? ] must-fail-with
315 ! This should give a 404 not an infinite redirect loop
316 [ "http://localhost/blah/" add-addr http-get nip ] [ 404? ] must-fail-with
318 [ "Goodbye" ] [ "http://localhost/quit" add-addr http-get nip ] unit-test
320 ] test-with-db-persistence
323 <action> [ [ "Hi" write ] "text/plain" <content> ] >>display
328 test-db <db-persistence> [
330 [ "Hi" ] [ "http://localhost/" add-addr http-get nip ] unit-test
332 [ "Goodbye" ] [ "http://localhost/quit" add-addr http-get nip ] unit-test
334 ] test-with-db-persistence
336 USING: html.components html.forms
337 xml xml.traversal validators
338 furnace furnace.conversations ;
342 : test-a ( xml -- value )
343 string>xml body>> "input" deep-tag-named "value" attr ;
347 [ a get-global "a" set-value ] >>init
348 [ [ "<!DOCTYPE html><html>" write "a" <field> render "</html>" write ] "text/html" <content> ] >>display
349 [ { { "a" [ v-integer ] } } validate-params ] >>validate
350 [ "a" value a set-global URL" " <redirect> ] >>submit
355 test-db <db-persistence> [
360 "http://localhost/" add-addr http-get
361 swap dup cookies>> "cookies" set session-id-key get-cookie
362 value>> "session-id" set test-a
368 "http://localhost" add-addr "__u" ,,
369 "session-id" get session-id-key ,,
371 "http://localhost/" add-addr <post-request> "cookies" get >>cookies
372 http-request nip test-a
375 [ 4 ] [ a get-global ] unit-test
381 "http://localhost" add-addr "__u" ,,
382 "session-id" get session-id-key ,,
384 "http://localhost/" add-addr <post-request> "cookies" get >>cookies
385 http-request nip test-a
388 [ 4 ] [ a get-global ] unit-test
390 [ "Goodbye" ] [ "http://localhost/quit" add-addr http-get nip ] unit-test
392 ] test-with-db-persistence
395 { f } [ <404> dup clone "b" "a" set-header drop "a" header ] unit-test
396 { f } [ <404> dup clone "b" "a" <cookie> put-cookie drop "a" get-cookie ] unit-test
399 { "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" } [
400 <request> "Aladdin" "open sesame" set-basic-auth "Authorization" header
403 ! Test a corner case with static responder
406 "vocab:http/test/foo.html" <static> >>default [
408 "http://localhost/" add-addr http-get nip
409 "vocab:http/test/foo.html" ascii file-contents =
412 [ ] [ stop-test-httpd ] unit-test
414 ] test-with-dispatcher
416 ! Check behavior of 307 redirect (reported by Chris Double)
420 [ "b" <temporary-redirect> ] >>submit
424 request get post-data>> data>> "data" =
425 [ "OK" "text/plain" <content> ] [ "OOPS" throw ] if
429 [ "OK" ] [ "data" "http://localhost/a" add-addr http-post nip ] unit-test
431 ! Check that download throws errors (reported by Chris Double)
434 "http://localhost/tweet_my_twat" add-addr download
435 ] with-temp-directory
438 [ ] [ stop-test-httpd ] unit-test
440 ] test-with-dispatcher
442 ! Check that index.fhtml works
444 "resource:basis/http/test/" <static> enable-fhtml >>default
447 [ "OK\n" ] [ "http://localhost/" add-addr http-get nip ] unit-test
449 [ ] [ stop-test-httpd ] unit-test
451 ] test-with-dispatcher
453 ! Check that just closing the socket without sending anything works
456 [ ] [ "addr" get binary [ ] with-client ] unit-test
458 [ ] [ stop-test-httpd ] unit-test
460 ] test-with-dispatcher