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 xml.data ;
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 : lf>crlf "\n" split "\r\n" join ;
16 STRING: read-request-test-1
21 Content-type: application/octet-stream
28 { url T{ url { path "/bar" } } }
31 { header H{ { "some-header" "1; 2" } { "content-length" "4" } { "content-type" "application/octet-stream" } } }
32 { post-data T{ post-data { data "blah" } { content-type "application/octet-stream" } } }
36 read-request-test-1 lf>crlf [
41 STRING: read-request-test-1'
44 content-type: application/octet-stream
50 read-request-test-1' 1array [
51 read-request-test-1 lf>crlf
52 [ read-request ] with-string-reader
53 [ write-request ] with-string-writer
55 string-lines "\n" join
58 STRING: read-request-test-2
66 { url T{ url { host "www.sex.com" } { path "/bar" } } }
69 { header H{ { "host" "www.sex.com" } } }
73 read-request-test-2 lf>crlf [
78 STRING: read-request-test-3
83 [ read-request-test-3 lf>crlf [ read-request ] with-string-reader ]
84 [ "Bad request: URL" = ]
87 STRING: read-request-test-4
89 Host: "www.amazon.com"
94 read-request-test-4 lf>crlf [ read-request ] with-string-reader
98 STRING: read-response-test-1
99 HTTP/1.1 404 not found
100 Content-Type: text/html; charset=UTF-8
109 { message "not found" }
110 { header H{ { "content-type" "text/html; charset=UTF-8" } } }
112 { content-type "text/html" }
113 { content-charset utf8 }
116 read-response-test-1 lf>crlf
117 [ read-response ] with-string-reader
121 STRING: read-response-test-1'
122 HTTP/1.1 404 not found
123 content-type: text/html; charset=UTF-8
128 read-response-test-1' 1array [
129 URL" http://localhost/" url set
130 read-response-test-1 lf>crlf
131 [ read-response ] with-string-reader
132 [ write-response ] with-string-writer
134 string-lines "\n" join
138 "rmid=732423sdfs73242; path=/; domain=.example.net; expires=Fri, 31-Dec-2010 23:59:59 GMT"
139 dup parse-set-cookie first unparse-set-cookie =
144 dup parse-set-cookie first unparse-set-cookie =
147 STRING: read-response-test-2
148 HTTP/1.1 200 Content follows
149 Set-Cookie: oo="bar; a=b"; httponly=yes; sid=123456
155 read-response-test-2 lf>crlf
156 [ read-response ] with-string-reader
160 STRING: read-response-test-3
161 HTTP/1.1 200 Content follows
162 Set-Cookie: oo="bar; a=b"; comment="your mom"; httponly=yes
168 read-response-test-3 lf>crlf
169 [ read-response ] with-string-reader
174 USING: http.server http.server.static furnace.sessions furnace.alloy
175 furnace.actions furnace.auth furnace.auth.login furnace.db http.client
176 io.servers.connection io.files io.files.temp io.directories io io.encodings.ascii
177 accessors namespaces threads
178 http.server.responses http.server.redirection furnace.redirection
179 http.server.dispatchers db.tuples ;
183 [ stop-this-server "Goodbye" "text/html" <content> ] >>display
184 "quit" add-responder ;
186 : test-db-file "test.db" temp-file ;
188 : test-db test-db-file <sqlite-db> ;
190 [ test-db-file delete-file ] ignore-errors
196 : test-httpd ( responder -- )
203 sockets>> first addr>> port>>
204 ] with-scope "port" set ;
210 "vocab:http/test" <static> >>default
211 "nested" add-responder
213 [ URL" redirect-loop" <temporary-redirect> ] >>display
214 "redirect-loop" add-responder
219 : add-port ( url -- url' )
220 >url clone "port" get >>port ;
223 "vocab:http/test/foo.html" ascii file-contents
224 "http://localhost/nested/foo.html" add-port http-get nip =
227 [ "http://localhost/redirect-loop" add-port http-get nip ]
228 [ too-many-redirects? ] must-fail-with
231 "http://localhost/quit" add-port http-get nip
234 ! HTTP client redirect bug
238 <action> [ "quit" <temporary-redirect> ] >>display
239 "redirect" add-responder
245 "http://localhost/redirect" add-port http-get nip
250 [ "http://localhost/quit" add-port http-get 2drop ] ignore-errors
262 <action> "" add-responder
264 test-db <db-persistence>
269 : 404? [ download-failed? ] [ response>> code>> 404 = ] bi and ;
271 ! This should give a 404 not an infinite redirect loop
272 [ "http://localhost/d/blah" add-port http-get nip ] [ 404? ] must-fail-with
274 ! This should give a 404 not an infinite redirect loop
275 [ "http://localhost/blah/" add-port http-get nip ] [ 404? ] must-fail-with
277 [ "Goodbye" ] [ "http://localhost/quit" add-port http-get nip ] unit-test
281 <action> [ [ "Hi" write ] "text/plain" <content> ] >>display
286 test-db <db-persistence>
291 [ "Hi" ] [ "http://localhost/" add-port http-get nip ] unit-test
293 [ "Goodbye" ] [ "http://localhost/quit" add-port http-get nip ] unit-test
295 USING: html.components html.forms
296 xml xml.traversal validators
297 furnace furnace.conversations ;
304 [ a get-global "a" set-value ] >>init
305 [ [ "<html>" write "a" <field> render "</html>" write ] "text/html" <content> ] >>display
306 [ { { "a" [ v-integer ] } } validate-params ] >>validate
307 [ "a" value a set-global URL" " <redirect> ] >>submit
312 test-db <db-persistence>
319 : test-a ( xml -- value )
320 string>xml body>> "input" deep-tag-named "value" attr ;
323 "http://localhost/" add-port http-get
324 swap dup cookies>> "cookies" set session-id-key get-cookie
325 value>> "session-id" set test-a
331 "http://localhost" add-port "__u" set
332 "session-id" get session-id-key set
334 "http://localhost/" add-port <post-request> "cookies" get >>cookies http-request nip test-a
337 [ 4 ] [ a get-global ] unit-test
343 "http://localhost" add-port "__u" set
344 "session-id" get session-id-key set
346 "http://localhost/" add-port <post-request> "cookies" get >>cookies http-request nip test-a
349 [ 4 ] [ a get-global ] unit-test
351 [ "Goodbye" ] [ "http://localhost/quit" add-port http-get nip ] unit-test
354 [ f ] [ <404> dup clone "b" "a" set-header drop "a" header ] unit-test
355 [ f ] [ <404> dup clone "b" "a" <cookie> put-cookie drop "a" get-cookie ] unit-test
358 [ "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" ] [ <request> "Aladdin" "open sesame" set-basic-auth "Authorization" header ] unit-test