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 ;
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" } } }
43 { header H{ { "some-header" "1; 2" } { "content-length" "4" } { "content-type" "application/octet-stream" } } }
44 { post-data T{ post-data { data "blah" } { content-type "application/octet-stream" } } }
49 read-request-test-1 lf>crlf [
54 STRING: read-request-test-1'
57 content-type: application/octet-stream
63 read-request-test-1' 1array [
64 read-request-test-1 lf>crlf
65 [ read-request ] with-string-reader
66 [ write-request ] with-string-writer
68 string-lines "\n" join
71 STRING: read-request-test-2
79 { url T{ url { host "www.sex.com" } { path "/bar" } } }
82 { header H{ { "host" "www.sex.com" } } }
87 read-request-test-2 lf>crlf [
92 STRING: read-request-test-2'
100 { url T{ url { host "www.sex.com" } { port 101 } { path "/bar" } } }
103 { header H{ { "host" "www.sex.com:101" } } }
108 read-request-test-2' lf>crlf [
113 STRING: read-request-test-3
118 STRING: read-request-test-4
120 Host: "www.amazon.com"
125 read-request-test-4 lf>crlf [ read-request ] with-string-reader
129 STRING: read-response-test-1
130 HTTP/1.1 404 not found
131 Content-Type: text/html; charset=UTF-8
140 { message "not found" }
141 { header H{ { "content-type" "text/html; charset=UTF-8" } } }
143 { content-type "text/html" }
144 { content-charset "UTF-8" }
145 { content-encoding utf8 }
148 read-response-test-1 lf>crlf
149 [ read-response ] with-string-reader
153 STRING: read-response-test-1'
154 HTTP/1.1 404 not found
155 content-type: text/html; charset=UTF-8
160 read-response-test-1' 1array [
161 URL" http://localhost/" url set
162 read-response-test-1 lf>crlf
163 [ read-response ] with-string-reader
164 [ write-response ] with-string-writer
166 string-lines "\n" join
170 "rmid=732423sdfs73242; path=/; domain=.example.net; expires=Fri, 31-Dec-2010 23:59:59 GMT"
171 dup parse-set-cookie first unparse-set-cookie =
176 dup parse-set-cookie first unparse-set-cookie =
179 STRING: read-response-test-2
180 HTTP/1.1 200 Content follows
181 Set-Cookie: oo="bar; a=b"; httponly=yes; sid=123456
187 read-response-test-2 lf>crlf
188 [ read-response ] with-string-reader
192 STRING: read-response-test-3
193 HTTP/1.1 200 Content follows
194 Set-Cookie: oo="bar; a=b"; comment="your mom"; httponly=yes
200 read-response-test-3 lf>crlf
201 [ read-response ] with-string-reader
206 USING: http.server.static furnace.sessions furnace.alloy
207 furnace.actions furnace.auth furnace.auth.login furnace.db
208 io.servers io.files io.files.temp io.directories io
210 http.server.responses http.server.redirection furnace.redirection
211 http.server.dispatchers db.tuples ;
213 : add-quit-action ( responder -- responder )
215 [ stop-this-server "Goodbye" "text/html" <content> ] >>display
216 "quit" add-responder ;
218 : test-db-file ( -- path ) "test.db" temp-file ;
220 : test-db ( -- db ) test-db-file <sqlite-db> ;
222 : add-addr ( url -- url' )
223 >url clone "addr" get set-url-addr ;
225 : stop-test-httpd ( -- )
226 "http://localhost/quit" add-addr http-get nip
230 [ test-db-file delete-file ] ignore-errors
237 : test-with-dispatcher ( dispatcher quot -- )
240 <http-server> 0 >>insecure f >>secure
242 server-addrs random "addr" set @
243 ] with-threaded-server
244 ] with-scope ; inline
248 :: test-with-db-persistence ( db-persistence quot -- )
250 quot test-with-dispatcher
251 ] with-disposal ; inline
256 "vocab:http/test" <static> >>default
257 "nested" add-responder
259 [ URL" redirect-loop" <temporary-redirect> ] >>display
260 "redirect-loop" add-responder [
263 "vocab:http/test/foo.html" ascii file-contents
264 "http://localhost/nested/foo.html" add-addr http-get nip =
267 [ "http://localhost/redirect-loop" add-addr http-get nip ]
268 [ too-many-redirects? ] must-fail-with
271 "http://localhost/quit" add-addr http-get nip
274 ] test-with-dispatcher
276 ! HTTP client redirect bug
279 <action> [ "quit" <temporary-redirect> ] >>display
280 "redirect" add-responder [
283 "http://localhost/redirect" add-addr http-get nip
287 [ stop-test-httpd ] ignore-errors
290 ] test-with-dispatcher
293 : 404? ( response -- ? )
296 [ response>> response? ]
297 [ response>> code>> 404 = ]
307 <action> "" add-responder
309 test-db <db-persistence> [
311 ! This should give a 404 not an infinite redirect loop
312 [ "http://localhost/d/blah" add-addr http-get nip ] [ 404? ] must-fail-with
314 ! This should give a 404 not an infinite redirect loop
315 [ "http://localhost/blah/" add-addr http-get nip ] [ 404? ] must-fail-with
317 [ "Goodbye" ] [ "http://localhost/quit" add-addr http-get nip ] unit-test
319 ] test-with-db-persistence
322 <action> [ [ "Hi" write ] "text/plain" <content> ] >>display
327 test-db <db-persistence> [
329 [ "Hi" ] [ "http://localhost/" add-addr http-get nip ] unit-test
331 [ "Goodbye" ] [ "http://localhost/quit" add-addr http-get nip ] unit-test
333 ] test-with-db-persistence
335 USING: html.components html.forms
336 xml xml.traversal validators
337 furnace furnace.conversations ;
341 : test-a ( xml -- value )
342 string>xml body>> "input" deep-tag-named "value" attr ;
346 [ a get-global "a" set-value ] >>init
347 [ [ "<html>" write "a" <field> render "</html>" write ] "text/html" <content> ] >>display
348 [ { { "a" [ v-integer ] } } validate-params ] >>validate
349 [ "a" value a set-global URL" " <redirect> ] >>submit
354 test-db <db-persistence> [
359 "http://localhost/" add-addr http-get
360 swap dup cookies>> "cookies" set session-id-key get-cookie
361 value>> "session-id" set test-a
367 "http://localhost" add-addr "__u" ,,
368 "session-id" get session-id-key ,,
370 "http://localhost/" add-addr <post-request> "cookies" get >>cookies
371 http-request nip test-a
374 [ 4 ] [ a get-global ] unit-test
380 "http://localhost" add-addr "__u" ,,
381 "session-id" get session-id-key ,,
383 "http://localhost/" add-addr <post-request> "cookies" get >>cookies
384 http-request nip test-a
387 [ 4 ] [ a get-global ] unit-test
389 [ "Goodbye" ] [ "http://localhost/quit" add-addr http-get nip ] unit-test
391 ] test-with-db-persistence
394 [ f ] [ <404> dup clone "b" "a" set-header drop "a" header ] unit-test
395 [ f ] [ <404> dup clone "b" "a" <cookie> put-cookie drop "a" get-cookie ] unit-test
398 [ "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" ] [
399 <request> "Aladdin" "open sesame" set-basic-auth "Authorization" header
402 ! Test a corner case with static responder
405 "vocab:http/test/foo.html" <static> >>default [
407 "http://localhost/" add-addr http-get nip
408 "vocab:http/test/foo.html" ascii file-contents =
411 [ ] [ stop-test-httpd ] unit-test
413 ] test-with-dispatcher
415 ! Check behavior of 307 redirect (reported by Chris Double)
419 [ "b" <temporary-redirect> ] >>submit
423 request get post-data>> data>> "data" =
424 [ "OK" "text/plain" <content> ] [ "OOPS" throw ] if
428 [ "OK" ] [ "data" "http://localhost/a" add-addr http-post nip ] unit-test
430 ! Check that download throws errors (reported by Chris Double)
433 "http://localhost/tweet_my_twat" add-addr download
434 ] with-temp-directory
437 [ ] [ stop-test-httpd ] unit-test
439 ] test-with-dispatcher
441 ! Check that index.fhtml works
443 "resource:basis/http/test/" <static> enable-fhtml >>default
446 [ "OK\n\n" ] [ "http://localhost/" add-addr http-get nip ] unit-test
448 [ ] [ stop-test-httpd ] unit-test
450 ] test-with-dispatcher
452 ! Check that just closing the socket without sending anything works
455 [ ] [ "addr" get binary [ ] with-client ] unit-test
457 [ ] [ stop-test-httpd ] unit-test
459 ] test-with-dispatcher