http.server.requests.tests: more tests for POST requests handling
parent
9058fbff01
commit
99012bb20a
|
@ -1,34 +1,104 @@
|
||||||
USING: accessors assocs http http.server.requests io.streams.string kernel
|
USING: accessors assocs http http.server http.server.requests
|
||||||
sequences tools.test urls ;
|
io.streams.limited io.streams.string kernel multiline namespaces sequences
|
||||||
|
splitting tools.test urls ;
|
||||||
IN: http.server.requests.tests
|
IN: http.server.requests.tests
|
||||||
|
|
||||||
! content-length in combination with POST
|
: normalize-nl ( str -- str' )
|
||||||
|
"\n" "\r\n" replace ;
|
||||||
|
|
||||||
|
: string>request ( str -- request )
|
||||||
|
normalize-nl
|
||||||
|
[ request-limit get limited-input read-request ] with-string-reader ;
|
||||||
|
|
||||||
|
! POST requests
|
||||||
|
STRING: test-post-no-content-type
|
||||||
|
POST / HTTP/1.1
|
||||||
|
connection: close
|
||||||
|
host: 127.0.0.1:55532
|
||||||
|
user-agent: Factor http.client
|
||||||
|
content-length: 7
|
||||||
|
|
||||||
|
foo=bar
|
||||||
|
;
|
||||||
{ "foo=bar" "7" } [
|
{ "foo=bar" "7" } [
|
||||||
{
|
test-post-no-content-type string>request
|
||||||
"POST / HTTP/1.1"
|
|
||||||
"connection: close"
|
|
||||||
"host: 127.0.0.1:55532"
|
|
||||||
"user-agent: Factor http.client"
|
|
||||||
"content-length: 7"
|
|
||||||
""
|
|
||||||
"foo=bar"
|
|
||||||
} "\n" join [ read-request ] with-string-reader
|
|
||||||
[ post-data>> data>> ] [ header>> "content-length" of ] bi
|
[ post-data>> data>> ] [ header>> "content-length" of ] bi
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
|
STRING: test-post-0-content-length
|
||||||
|
POST / HTTP/1.1
|
||||||
|
connection: close
|
||||||
|
host: 127.0.0.1:55532
|
||||||
|
user-agent: Factor http.client
|
||||||
|
content-length: 0
|
||||||
|
|
||||||
|
|
||||||
|
;
|
||||||
{ f "0" } [
|
{ f "0" } [
|
||||||
{
|
test-post-0-content-length string>request
|
||||||
"POST / HTTP/1.1"
|
|
||||||
"connection: close"
|
|
||||||
"host: 127.0.0.1:55532"
|
|
||||||
"user-agent: Factor http.client"
|
|
||||||
"content-length: 0"
|
|
||||||
""
|
|
||||||
""
|
|
||||||
} "\n" join [ read-request ] with-string-reader
|
|
||||||
[ post-data>> data>> ] [ header>> "content-length" of ] bi
|
[ post-data>> data>> ] [ header>> "content-length" of ] bi
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
|
! Should work no problem.
|
||||||
|
STRING: test-post-wrong-content-length
|
||||||
|
POST / HTTP/1.1
|
||||||
|
connection: close
|
||||||
|
host: 127.0.0.1:55532
|
||||||
|
user-agent: Factor http.client
|
||||||
|
Content-Type: application/x-www-form-urlencoded; charset=utf-8
|
||||||
|
content-length: 190
|
||||||
|
|
||||||
|
foo=bar
|
||||||
|
;
|
||||||
|
{ H{ { "foo" "bar" } } } [
|
||||||
|
test-post-wrong-content-length string>request post-data>> params>>
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
STRING: test-post-urlencoded
|
||||||
|
POST / HTTP/1.1
|
||||||
|
Accept: */*
|
||||||
|
Accept-Encoding: gzip, deflate
|
||||||
|
Connection: keep-alive
|
||||||
|
Content-Length: 15
|
||||||
|
Content-Type: application/x-www-form-urlencoded; charset=utf-8
|
||||||
|
Host: news.ycombinator.com
|
||||||
|
User-Agent: HTTPie/0.9.0-dev
|
||||||
|
|
||||||
|
name=John+Smith
|
||||||
|
;
|
||||||
|
{ H{ { "name" "John Smith" } } } [
|
||||||
|
test-post-urlencoded string>request post-data>> params>>
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
! multipart/form-data
|
||||||
|
STRING: test-multipart/form-data
|
||||||
|
POST / HTTP/1.1
|
||||||
|
Accept: */*
|
||||||
|
Accept-Encoding: gzip, deflate
|
||||||
|
Connection: keep-alive
|
||||||
|
Content-Length: 151
|
||||||
|
Content-Type: multipart/form-data; boundary=768de80194d942619886d23f1337aa15
|
||||||
|
Host: localhost:8000
|
||||||
|
User-Agent: HTTPie/0.9.0-dev
|
||||||
|
|
||||||
|
--768de80194d942619886d23f1337aa15
|
||||||
|
Content-Disposition: form-data; name="text"; filename="upload.txt"
|
||||||
|
|
||||||
|
hello
|
||||||
|
--768de80194d942619886d23f1337aa15--
|
||||||
|
|
||||||
|
;
|
||||||
|
{
|
||||||
|
"upload.txt"
|
||||||
|
H{
|
||||||
|
{ "content-disposition"
|
||||||
|
"form-data; name=\"text\"; filename=\"upload.txt\"" }
|
||||||
|
}
|
||||||
|
} [
|
||||||
|
test-multipart/form-data string>request post-data>> params>> "text" of
|
||||||
|
[ filename>> ] [ headers>> ] bi
|
||||||
|
] unit-test
|
||||||
|
|
||||||
! RFC 2616: Section 4.1
|
! RFC 2616: Section 4.1
|
||||||
! In the interest of robustness, servers SHOULD ignore any empty
|
! In the interest of robustness, servers SHOULD ignore any empty
|
||||||
! line(s) received where a Request-Line is expected. In other words, if
|
! line(s) received where a Request-Line is expected. In other words, if
|
||||||
|
|
Loading…
Reference in New Issue