diff --git a/basis/http/server/requests/requests-tests.factor b/basis/http/server/requests/requests-tests.factor index 6388626e02..62410aa469 100644 --- a/basis/http/server/requests/requests-tests.factor +++ b/basis/http/server/requests/requests-tests.factor @@ -1,34 +1,104 @@ -USING: accessors assocs http http.server.requests io.streams.string kernel -sequences tools.test urls ; +USING: accessors assocs http http.server http.server.requests +io.streams.limited io.streams.string kernel multiline namespaces sequences +splitting tools.test urls ; 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" } [ - { - "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 + test-post-no-content-type string>request [ post-data>> data>> ] [ header>> "content-length" of ] bi ] 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" } [ - { - "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 + test-post-0-content-length string>request [ post-data>> data>> ] [ header>> "content-length" of ] bi ] 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 ! In the interest of robustness, servers SHOULD ignore any empty ! line(s) received where a Request-Line is expected. In other words, if