2017-10-26 21:19:30 +00:00
|
|
|
module Test.HTTPure.RequestSpec where
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2017-07-18 05:31:46 +00:00
|
|
|
import Prelude
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2017-09-26 06:08:07 +00:00
|
|
|
import Data.Tuple as Tuple
|
2018-07-08 23:16:48 +00:00
|
|
|
import Foreign.Object as Object
|
2017-07-10 10:17:13 +00:00
|
|
|
import Test.Spec as Spec
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2017-07-18 01:51:43 +00:00
|
|
|
import HTTPure.Headers as Headers
|
2017-09-26 06:08:07 +00:00
|
|
|
import HTTPure.Method as Method
|
2017-07-14 06:28:57 +00:00
|
|
|
import HTTPure.Request as Request
|
2019-04-25 17:13:04 +00:00
|
|
|
import HTTPure.Version as Version
|
2017-07-14 06:28:57 +00:00
|
|
|
|
2017-10-26 21:19:30 +00:00
|
|
|
import Test.HTTPure.TestHelpers as TestHelpers
|
|
|
|
import Test.HTTPure.TestHelpers ((?=))
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2017-10-26 21:19:30 +00:00
|
|
|
fromHTTPRequestSpec :: TestHelpers.Test
|
2017-07-14 06:28:57 +00:00
|
|
|
fromHTTPRequestSpec = Spec.describe "fromHTTPRequest" do
|
2017-09-26 06:08:07 +00:00
|
|
|
Spec.it "contains the correct method" do
|
|
|
|
mock <- mockRequest
|
|
|
|
mock.method ?= Method.Post
|
|
|
|
Spec.it "contains the correct path" do
|
|
|
|
mock <- mockRequest
|
|
|
|
mock.path ?= [ "test" ]
|
|
|
|
Spec.it "contains the correct query" do
|
|
|
|
mock <- mockRequest
|
2018-07-08 23:16:48 +00:00
|
|
|
mock.query ?= Object.singleton "a" "b"
|
2017-09-26 06:08:07 +00:00
|
|
|
Spec.it "contains the correct headers" do
|
|
|
|
mock <- mockRequest
|
|
|
|
mock.headers ?= Headers.headers mockHeaders
|
|
|
|
Spec.it "contains the correct body" do
|
|
|
|
mock <- mockRequest
|
|
|
|
mock.body ?= "body"
|
2019-04-25 17:13:04 +00:00
|
|
|
Spec.it "contains the correct httpVersion" do
|
|
|
|
mock <- mockRequest
|
|
|
|
mock.httpVersion ?= Version.HTTP1_1
|
2017-07-18 01:51:43 +00:00
|
|
|
where
|
2017-09-26 06:08:07 +00:00
|
|
|
mockHeaders = [ Tuple.Tuple "Test" "test" ]
|
|
|
|
mockHTTPRequest =
|
2019-04-25 17:13:04 +00:00
|
|
|
TestHelpers.mockRequest "1.1" "POST" "/test?a=b" "body" mockHeaders
|
2017-09-26 06:08:07 +00:00
|
|
|
mockRequest = mockHTTPRequest >>= Request.fromHTTPRequest
|
2017-07-10 10:17:13 +00:00
|
|
|
|
2017-10-26 21:19:30 +00:00
|
|
|
fullPathSpec :: TestHelpers.Test
|
2017-09-29 16:52:21 +00:00
|
|
|
fullPathSpec = Spec.describe "fullPath" do
|
|
|
|
Spec.describe "without query parameters" do
|
|
|
|
Spec.it "is correct" do
|
|
|
|
mock <- mockRequest "/foo/bar"
|
|
|
|
Request.fullPath mock ?= "/foo/bar"
|
|
|
|
Spec.describe "with empty path segments" do
|
|
|
|
Spec.it "strips the empty segments" do
|
|
|
|
mock <- mockRequest "//foo////bar/"
|
|
|
|
Request.fullPath mock ?= "/foo/bar"
|
|
|
|
Spec.describe "with only query parameters" do
|
|
|
|
Spec.it "is correct" do
|
|
|
|
mock <- mockRequest "?a=b&c=d"
|
|
|
|
Request.fullPath mock ?= "/?a=b&c=d"
|
|
|
|
Spec.describe "with only empty query parameters" do
|
2018-11-06 17:45:37 +00:00
|
|
|
Spec.it "is has the default value of '' for the empty parameters" do
|
2017-09-29 16:52:21 +00:00
|
|
|
mock <- mockRequest "?a"
|
2018-11-06 17:45:37 +00:00
|
|
|
Request.fullPath mock ?= "/?a="
|
|
|
|
Spec.describe "with query parameters that have special characters" do
|
|
|
|
Spec.it "percent encodes query params" do
|
|
|
|
mock <- mockRequest "?a=%3Fx%3Dtest"
|
|
|
|
Request.fullPath mock ?= "/?a=%3Fx%3Dtest"
|
2017-09-29 16:52:21 +00:00
|
|
|
Spec.describe "with empty query parameters" do
|
|
|
|
Spec.it "strips out the empty arameters" do
|
|
|
|
mock <- mockRequest "?a=b&&&"
|
|
|
|
Request.fullPath mock ?= "/?a=b"
|
|
|
|
Spec.describe "with a mix of segments and query parameters" do
|
|
|
|
Spec.it "is correct" do
|
|
|
|
mock <- mockRequest "/foo///bar/?&a=b&&c"
|
2018-11-06 17:45:37 +00:00
|
|
|
Request.fullPath mock ?= "/foo/bar?a=b&c="
|
2017-09-29 16:52:21 +00:00
|
|
|
where
|
2019-04-25 17:13:04 +00:00
|
|
|
mockHTTPRequest path = TestHelpers.mockRequest "" "POST" path "body" []
|
2017-09-29 16:52:21 +00:00
|
|
|
mockRequest path = mockHTTPRequest path >>= Request.fromHTTPRequest
|
|
|
|
|
2017-10-26 21:19:30 +00:00
|
|
|
requestSpec :: TestHelpers.Test
|
2017-07-14 06:28:57 +00:00
|
|
|
requestSpec = Spec.describe "Request" do
|
|
|
|
fromHTTPRequestSpec
|
2017-09-29 16:52:21 +00:00
|
|
|
fullPathSpec
|