factor/basis/http/http-tests.factor

367 lines
8.7 KiB
Factor
Raw Normal View History

USING: http http.server http.client http.client.private tools.test multiline
2008-09-05 21:39:45 -04:00
io.streams.string io.encodings.utf8 io.encodings.8-bit
io.encodings.binary io.encodings.string kernel arrays splitting
sequences assocs io.sockets db db.sqlite continuations urls
2009-01-29 16:15:50 -05:00
hashtables accessors namespaces xml.data ;
2008-03-01 17:00:45 -05:00
IN: http.tests
2007-09-20 18:09:08 -04:00
2008-06-25 04:25:08 -04:00
[ "text/plain" latin1 ] [ "text/plain" parse-content-type ] unit-test
[ "text/html" utf8 ] [ "text/html; charset=UTF-8" parse-content-type ] unit-test
[ "application/octet-stream" binary ] [ "application/octet-stream" parse-content-type ] unit-test
2008-11-24 21:26:29 -05:00
[ { } ] [ "" parse-cookie ] unit-test
[ { } ] [ "" parse-set-cookie ] unit-test
! Make sure that totally invalid cookies don't confuse us
[ { } ] [ "hello world; how are you" parse-cookie ] unit-test
2008-04-23 01:53:42 -04:00
: lf>crlf "\n" split "\r\n" join ;
2008-02-25 15:53:18 -05:00
STRING: read-request-test-1
2008-06-12 19:53:53 -04:00
POST /bar HTTP/1.1
2008-02-25 15:53:18 -05:00
Some-Header: 1
Some-Header: 2
Content-Length: 4
Content-type: application/octet-stream
2008-02-25 15:53:18 -05:00
blah
;
[
2008-09-05 21:39:45 -04:00
T{ request
2008-09-06 03:16:44 -04:00
{ url T{ url { path "/bar" } } }
2008-09-05 21:39:45 -04:00
{ method "POST" }
{ version "1.1" }
{ header H{ { "some-header" "1; 2" } { "content-length" "4" } { "content-type" "application/octet-stream" } } }
{ post-data T{ post-data { data "blah" } { content-type "application/octet-stream" } } }
2008-09-05 21:39:45 -04:00
{ cookies V{ } }
2008-02-25 15:53:18 -05:00
}
] [
2008-04-23 01:53:42 -04:00
read-request-test-1 lf>crlf [
2008-02-25 15:53:18 -05:00
read-request
] with-string-reader
] unit-test
STRING: read-request-test-1'
POST /bar HTTP/1.1
2008-02-25 15:53:18 -05:00
content-length: 4
content-type: application/octet-stream
2008-02-29 01:57:38 -05:00
some-header: 1; 2
2008-02-25 15:53:18 -05:00
blah
;
read-request-test-1' 1array [
2008-04-23 01:53:42 -04:00
read-request-test-1 lf>crlf
2008-02-25 15:53:18 -05:00
[ read-request ] with-string-reader
[ write-request ] with-string-writer
! normalize crlf
string-lines "\n" join
] unit-test
STRING: read-request-test-2
2008-06-12 19:53:53 -04:00
HEAD /bar HTTP/1.1
2008-02-25 15:53:18 -05:00
Host: www.sex.com
2008-04-23 01:53:42 -04:00
2008-02-25 15:53:18 -05:00
;
[
2008-09-05 21:39:45 -04:00
T{ request
2008-09-06 06:12:28 -04:00
{ url T{ url { host "www.sex.com" } { path "/bar" } } }
2008-09-05 21:39:45 -04:00
{ method "HEAD" }
{ version "1.1" }
{ header H{ { "host" "www.sex.com" } } }
{ cookies V{ } }
2008-02-25 15:53:18 -05:00
}
] [
2008-04-23 01:53:42 -04:00
read-request-test-2 lf>crlf [
2008-02-25 15:53:18 -05:00
read-request
] with-string-reader
] unit-test
STRING: read-request-test-3
GET nested HTTP/1.0
;
[ read-request-test-3 lf>crlf [ read-request ] with-string-reader ]
[ "Bad request: URL" = ]
must-fail-with
STRING: read-request-test-4
GET /blah HTTP/1.0
Host: "www.amazon.com"
;
[ "www.amazon.com" ]
[
read-request-test-4 lf>crlf [ read-request ] with-string-reader
"host" header
] unit-test
2008-02-25 15:53:18 -05:00
STRING: read-response-test-1
2008-02-29 01:57:38 -05:00
HTTP/1.1 404 not found
Content-Type: text/html; charset=UTF-8
2008-02-25 15:53:18 -05:00
blah
;
[
2008-09-05 21:39:45 -04:00
T{ response
{ version "1.1" }
{ code 404 }
{ message "not found" }
{ header H{ { "content-type" "text/html; charset=UTF-8" } } }
{ cookies { } }
{ content-type "text/html" }
{ content-charset utf8 }
2008-02-25 15:53:18 -05:00
}
] [
2008-04-23 01:53:42 -04:00
read-response-test-1 lf>crlf
2008-02-25 15:53:18 -05:00
[ read-response ] with-string-reader
] unit-test
STRING: read-response-test-1'
2008-02-29 01:57:38 -05:00
HTTP/1.1 404 not found
content-type: text/html; charset=UTF-8
2008-02-25 15:53:18 -05:00
;
read-response-test-1' 1array [
2008-11-24 21:26:29 -05:00
URL" http://localhost/" url set
2008-04-23 01:53:42 -04:00
read-response-test-1 lf>crlf
2008-02-25 15:53:18 -05:00
[ read-response ] with-string-reader
[ write-response ] with-string-writer
! normalize crlf
string-lines "\n" join
] unit-test
2008-02-29 01:57:38 -05:00
[ t ] [
"rmid=732423sdfs73242; path=/; domain=.example.net; expires=Fri, 31-Dec-2010 23:59:59 GMT"
dup parse-set-cookie first unparse-set-cookie =
] unit-test
[ t ] [
"a="
dup parse-set-cookie first unparse-set-cookie =
] unit-test
STRING: read-response-test-2
HTTP/1.1 200 Content follows
Set-Cookie: oo="bar; a=b"; httponly=yes; sid=123456
;
[ 2 ] [
read-response-test-2 lf>crlf
[ read-response ] with-string-reader
cookies>> length
] unit-test
STRING: read-response-test-3
HTTP/1.1 200 Content follows
Set-Cookie: oo="bar; a=b"; comment="your mom"; httponly=yes
;
[ 1 ] [
read-response-test-3 lf>crlf
[ read-response ] with-string-reader
cookies>> length
2008-02-29 01:57:38 -05:00
] unit-test
2008-03-07 18:21:20 -05:00
! Live-fire exercise
USING: http.server http.server.static furnace.sessions furnace.alloy
2008-06-16 04:34:17 -04:00
furnace.actions furnace.auth furnace.auth.login furnace.db http.client
io.servers.connection io.files io.files.temp io.directories io io.encodings.ascii
2008-06-02 16:00:03 -04:00
accessors namespaces threads
2008-06-17 06:25:21 -04:00
http.server.responses http.server.redirection furnace.redirection
http.server.dispatchers db.tuples ;
2008-03-07 18:21:20 -05:00
2008-04-25 04:23:47 -04:00
: add-quit-action
<action>
2008-10-02 09:30:38 -04:00
[ stop-this-server "Goodbye" "text/html" <content> ] >>display
2008-04-25 04:23:47 -04:00
"quit" add-responder ;
2008-10-02 17:37:04 -04:00
: test-db-file "test.db" temp-file ;
2008-04-26 02:44:45 -04:00
2008-10-02 17:37:04 -04:00
: test-db test-db-file <sqlite-db> ;
[ test-db-file delete-file ] ignore-errors
2008-05-01 17:24:50 -04:00
2008-04-26 02:44:45 -04:00
test-db [
init-furnace-tables
2008-04-26 02:44:45 -04:00
] with-db
: test-httpd ( responder -- )
[
main-responder set
<http-server>
0 >>insecure
f >>secure
dup start-server*
sockets>> first addr>> port>>
] with-scope "port" set ;
2008-06-25 17:58:19 -04:00
2008-03-07 18:21:20 -05:00
[ ] [
<dispatcher>
add-quit-action
2008-03-07 18:21:20 -05:00
<dispatcher>
"resource:basis/http/test" <static> >>default
"nested" add-responder
<action>
[ URL" redirect-loop" <temporary-redirect> ] >>display
"redirect-loop" add-responder
2008-03-07 18:21:20 -05:00
test-httpd
2008-03-07 18:21:20 -05:00
] unit-test
: add-port ( url -- url' )
>url clone "port" get >>port ;
2008-03-07 18:21:20 -05:00
[ t ] [
2008-09-04 21:11:28 -04:00
"resource:basis/http/test/foo.html" ascii file-contents
"http://localhost/nested/foo.html" add-port http-get nip =
2008-04-14 05:34:26 -04:00
] unit-test
[ "http://localhost/redirect-loop" add-port http-get nip ]
[ too-many-redirects? ] must-fail-with
2008-03-07 18:21:20 -05:00
[ "Goodbye" ] [
"http://localhost/quit" add-port http-get nip
2008-03-07 18:21:20 -05:00
] unit-test
2008-04-25 04:23:47 -04:00
2008-09-04 17:56:41 -04:00
! HTTP client redirect bug
[ ] [
<dispatcher>
add-quit-action
<action> [ "quit" <temporary-redirect> ] >>display
"redirect" add-responder
2008-09-04 17:56:41 -04:00
test-httpd
2008-09-04 17:56:41 -04:00
] unit-test
[ "Goodbye" ] [
"http://localhost/redirect" add-port http-get nip
2008-09-04 17:56:41 -04:00
] unit-test
[ ] [
[ "http://localhost/quit" add-port http-get 2drop ] ignore-errors
2008-09-04 17:56:41 -04:00
] unit-test
2008-04-25 04:23:47 -04:00
! Dispatcher bugs
[ ] [
<dispatcher>
<action> <protected>
"Test" <login-realm>
<sessions>
"" add-responder
add-quit-action
2008-04-25 04:23:47 -04:00
<dispatcher>
<action> "" add-responder
"d" add-responder
test-db <db-persistence>
2008-04-25 04:23:47 -04:00
test-httpd
2008-04-25 04:23:47 -04:00
] unit-test
: 404? [ download-failed? ] [ response>> code>> 404 = ] bi and ;
! This should give a 404 not an infinite redirect loop
[ "http://localhost/d/blah" add-port http-get nip ] [ 404? ] must-fail-with
2008-04-25 04:23:47 -04:00
! This should give a 404 not an infinite redirect loop
[ "http://localhost/blah/" add-port http-get nip ] [ 404? ] must-fail-with
2008-04-25 04:23:47 -04:00
[ "Goodbye" ] [ "http://localhost/quit" add-port http-get nip ] unit-test
2008-04-25 04:23:47 -04:00
[ ] [
<dispatcher>
<action> [ [ "Hi" write ] "text/plain" <content> ] >>display
"Test" <login-realm>
<sessions>
"" add-responder
add-quit-action
test-db <db-persistence>
test-httpd
2008-04-25 04:23:47 -04:00
] unit-test
[ "Hi" ] [ "http://localhost/" add-port http-get nip ] unit-test
2008-04-25 04:23:47 -04:00
[ "Goodbye" ] [ "http://localhost/quit" add-port http-get nip ] unit-test
2009-01-31 21:54:49 -05:00
USING: html.components html.forms
2009-02-05 22:17:03 -05:00
xml xml.traversal validators
2008-07-10 00:41:45 -04:00
furnace furnace.conversations ;
SYMBOL: a
[ ] [
<dispatcher>
<action>
[ a get-global "a" set-value ] >>init
2009-02-01 03:02:09 -05:00
[ [ "<html>" write "a" <field> render "</html>" write ] "text/html" <content> ] >>display
[ { { "a" [ v-integer ] } } validate-params ] >>validate
[ "a" value a set-global URL" " <redirect> ] >>submit
<conversations>
<sessions>
>>default
add-quit-action
test-db <db-persistence>
test-httpd
] unit-test
3 a set-global
2009-01-31 21:54:49 -05:00
: test-a ( xml -- value )
string>xml body>> "input" deep-tag-named "value" attr ;
[ "3" ] [
"http://localhost/" add-port http-get
swap dup cookies>> "cookies" set session-id-key get-cookie
value>> "session-id" set test-a
] unit-test
[ "4" ] [
[
"4" "a" set
"http://localhost" add-port "__u" set
"session-id" get session-id-key set
] H{ } make-assoc
"http://localhost/" add-port <post-request> "cookies" get >>cookies http-request nip test-a
] unit-test
[ 4 ] [ a get-global ] unit-test
! Test flash scope
[ "xyz" ] [
[
"xyz" "a" set
"http://localhost" add-port "__u" set
"session-id" get session-id-key set
] H{ } make-assoc
"http://localhost/" add-port <post-request> "cookies" get >>cookies http-request nip test-a
] unit-test
[ 4 ] [ a get-global ] unit-test
[ "Goodbye" ] [ "http://localhost/quit" add-port http-get nip ] unit-test
2008-06-16 02:35:06 -04:00
! Test cloning
[ f ] [ <404> dup clone "b" "a" set-header drop "a" header ] unit-test
[ f ] [ <404> dup clone "b" "a" <cookie> put-cookie drop "a" get-cookie ] unit-test
! Test basic auth
[ "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" ] [ <request> "Aladdin" "open sesame" set-basic-auth "Authorization" header ] unit-test