purescript-httpurple/test/HTTPure/RequestSpec.purs

73 lines
2.5 KiB
Haskell
Raw Normal View History

module HTTPure.RequestSpec where
import Prelude
2017-09-26 06:08:07 +00:00
import Data.Tuple as Tuple
import Data.StrMap as StrMap
2017-07-10 10:17:13 +00:00
import Test.Spec as Spec
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
import HTTPure.Request as Request
2017-07-10 10:17:13 +00:00
import HTTPure.SpecHelpers as SpecHelpers
import HTTPure.SpecHelpers ((?=))
fromHTTPRequestSpec :: SpecHelpers.Test
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
mock.query ?= StrMap.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"
2017-07-18 01:51:43 +00:00
where
2017-09-26 06:08:07 +00:00
mockHeaders = [ Tuple.Tuple "Test" "test" ]
mockHTTPRequest =
SpecHelpers.mockRequest "POST" "/test?a=b" "body" mockHeaders
mockRequest = mockHTTPRequest >>= Request.fromHTTPRequest
2017-07-10 10:17:13 +00:00
fullPathSpec :: SpecHelpers.Test
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
Spec.it "is has the default value of 'true' for the empty parameters" do
mock <- mockRequest "?a"
Request.fullPath mock ?= "/?a=true"
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"
Request.fullPath mock ?= "/foo/bar?a=b&c=true"
where
mockHTTPRequest path = SpecHelpers.mockRequest "POST" path "body" []
mockRequest path = mockHTTPRequest path >>= Request.fromHTTPRequest
2017-07-10 10:17:13 +00:00
requestSpec :: SpecHelpers.Test
requestSpec = Spec.describe "Request" do
fromHTTPRequestSpec
fullPathSpec