2017-05-25 19:12:29 +00:00
|
|
|
module HTTPure.RequestSpec where
|
|
|
|
|
2017-07-18 05:31:46 +00:00
|
|
|
import Prelude
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2017-07-18 05:25:14 +00:00
|
|
|
import Control.Monad.Eff.Class as EffClass
|
2017-07-14 06:28:57 +00:00
|
|
|
import Data.StrMap as StrMap
|
2017-07-10 10:17:13 +00:00
|
|
|
import Test.Spec as Spec
|
|
|
|
import Test.Spec.Assertions as Assertions
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2017-07-18 01:51:43 +00:00
|
|
|
import HTTPure.Headers as Headers
|
2017-07-14 06:28:57 +00:00
|
|
|
import HTTPure.Request as Request
|
|
|
|
|
2017-07-10 10:17:13 +00:00
|
|
|
import HTTPure.SpecHelpers as SpecHelpers
|
2017-07-19 05:21:36 +00:00
|
|
|
import HTTPure.SpecHelpers ((?=))
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2017-07-14 06:28:57 +00:00
|
|
|
showSpec :: SpecHelpers.Test
|
|
|
|
showSpec = Spec.describe "show" do
|
|
|
|
Spec.describe "with a POST" do
|
|
|
|
Spec.it "is the method and the path" do
|
2017-07-19 05:21:36 +00:00
|
|
|
show (Request.Post StrMap.empty "test" "") ?= "POST: test"
|
2017-07-14 06:28:57 +00:00
|
|
|
Spec.describe "with a PUT" do
|
|
|
|
Spec.it "is the method and the path" do
|
2017-07-19 05:21:36 +00:00
|
|
|
show (Request.Put StrMap.empty "test" "") ?= "PUT: test"
|
2017-07-14 06:28:57 +00:00
|
|
|
Spec.describe "with a DELETE" do
|
|
|
|
Spec.it "is the method and the path" do
|
2017-07-19 05:21:36 +00:00
|
|
|
show (Request.Delete StrMap.empty "test") ?= "DELETE: test"
|
|
|
|
Spec.describe "with a HEAD" do
|
|
|
|
Spec.it "is the method and the path" do
|
|
|
|
show (Request.Head StrMap.empty "test") ?= "HEAD: test"
|
|
|
|
Spec.describe "with a CONNECT" do
|
|
|
|
Spec.it "is the method and the path" do
|
|
|
|
show (Request.Connect StrMap.empty "test" "") ?= "CONNECT: test"
|
|
|
|
Spec.describe "with a OPTIONS" do
|
|
|
|
Spec.it "is the method and the path" do
|
|
|
|
show (Request.Options StrMap.empty "test") ?= "OPTIONS: test"
|
|
|
|
Spec.describe "with a TRACE" do
|
|
|
|
Spec.it "is the method and the path" do
|
|
|
|
show (Request.Trace StrMap.empty "test") ?= "TRACE: test"
|
|
|
|
Spec.describe "with a PATH" do
|
|
|
|
Spec.it "is the method and the path" do
|
|
|
|
show (Request.Patch StrMap.empty "test" "") ?= "PATCH: test"
|
2017-07-14 06:28:57 +00:00
|
|
|
Spec.describe "with a GET" do
|
|
|
|
Spec.it "is the method and the path" do
|
2017-07-19 05:21:36 +00:00
|
|
|
show (Request.Get StrMap.empty "test") ?= "GET: test"
|
2017-07-14 06:28:57 +00:00
|
|
|
|
|
|
|
fromHTTPRequestSpec :: SpecHelpers.Test
|
|
|
|
fromHTTPRequestSpec = Spec.describe "fromHTTPRequest" do
|
|
|
|
|
|
|
|
Spec.describe "with a POST" do
|
|
|
|
Spec.it "is a Post" do
|
2017-07-18 05:25:14 +00:00
|
|
|
response <- mock "POST" "" "" StrMap.empty
|
|
|
|
case response of
|
2017-07-14 06:28:57 +00:00
|
|
|
(Request.Post _ _ _) -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected a Post, got " <> show a
|
2017-07-18 01:51:43 +00:00
|
|
|
Spec.it "has the correct headers" do
|
2017-07-18 05:25:14 +00:00
|
|
|
response <- mock "POST" "" "" mockHeader
|
|
|
|
case response of
|
2017-07-18 01:51:43 +00:00
|
|
|
(Request.Post headers _ _) ->
|
2017-07-19 05:21:36 +00:00
|
|
|
Headers.lookup headers "X-Test" ?= "test"
|
2017-07-18 01:51:43 +00:00
|
|
|
a -> Assertions.fail $ "expected a Post, got " <> show a
|
2017-07-14 06:28:57 +00:00
|
|
|
Spec.it "has the correct path" do
|
2017-07-18 05:25:14 +00:00
|
|
|
response <- mock "POST" "test" "" StrMap.empty
|
|
|
|
case response of
|
2017-07-14 06:28:57 +00:00
|
|
|
(Request.Post _ "test" _) -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected the path 'test', got " <> show a
|
2017-07-18 05:25:14 +00:00
|
|
|
Spec.it "has the correct body" do
|
|
|
|
response <- mock "POST" "" "test" StrMap.empty
|
|
|
|
case response of
|
|
|
|
(Request.Post _ _ "test") -> pure unit
|
2017-07-20 22:48:59 +00:00
|
|
|
(Request.Post _ _ body) ->
|
|
|
|
Assertions.fail $ "expected the body 'test', got " <> body
|
|
|
|
a -> Assertions.fail $ "expected a post, got " <> show a
|
2017-07-14 06:28:57 +00:00
|
|
|
|
|
|
|
Spec.describe "with a PUT" do
|
|
|
|
Spec.it "is a Put" do
|
2017-07-18 05:25:14 +00:00
|
|
|
response <- mock "PUT" "" "" StrMap.empty
|
|
|
|
case response of
|
2017-07-14 06:28:57 +00:00
|
|
|
(Request.Put _ _ _) -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected a Put, got " <> show a
|
2017-07-18 01:51:43 +00:00
|
|
|
Spec.it "has the correct headers" do
|
2017-07-18 05:25:14 +00:00
|
|
|
response <- mock "PUT" "" "" mockHeader
|
|
|
|
case response of
|
2017-07-18 01:51:43 +00:00
|
|
|
(Request.Put headers _ _) ->
|
2017-07-19 05:21:36 +00:00
|
|
|
Headers.lookup headers "X-Test" ?= "test"
|
2017-07-18 01:51:43 +00:00
|
|
|
a -> Assertions.fail $ "expected a Put, got " <> show a
|
2017-07-14 06:28:57 +00:00
|
|
|
Spec.it "has the correct path" do
|
2017-07-18 05:25:14 +00:00
|
|
|
response <- mock "PUT" "test" "" StrMap.empty
|
|
|
|
case response of
|
2017-07-14 06:28:57 +00:00
|
|
|
(Request.Put _ "test" _) -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected the path 'test', got " <> show a
|
2017-07-18 05:25:14 +00:00
|
|
|
Spec.it "has the correct body" do
|
|
|
|
response <- mock "PUT" "" "test" StrMap.empty
|
|
|
|
case response of
|
|
|
|
(Request.Put _ _ "test") -> pure unit
|
2017-07-20 22:48:59 +00:00
|
|
|
(Request.Put _ _ body) ->
|
|
|
|
Assertions.fail $ "expected the body 'test', got " <> body
|
|
|
|
a -> Assertions.fail $ "expected a put, got " <> show a
|
2017-07-14 06:28:57 +00:00
|
|
|
|
|
|
|
Spec.describe "with a DELETE" do
|
|
|
|
Spec.it "is a Delete" do
|
2017-07-18 05:25:14 +00:00
|
|
|
response <- mock "DELETE" "" "" StrMap.empty
|
|
|
|
case response of
|
2017-07-14 06:28:57 +00:00
|
|
|
(Request.Delete _ _) -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected a Delete, got " <> show a
|
2017-07-18 01:51:43 +00:00
|
|
|
Spec.it "has the correct headers" do
|
2017-07-18 05:25:14 +00:00
|
|
|
response <- mock "DELETE" "" "" mockHeader
|
|
|
|
case response of
|
2017-07-18 01:51:43 +00:00
|
|
|
(Request.Delete headers _) ->
|
2017-07-19 05:21:36 +00:00
|
|
|
Headers.lookup headers "X-Test" ?= "test"
|
2017-07-18 01:51:43 +00:00
|
|
|
a -> Assertions.fail $ "expected a Delete, got " <> show a
|
2017-07-14 06:28:57 +00:00
|
|
|
Spec.it "has the correct path" do
|
2017-07-18 05:25:14 +00:00
|
|
|
response <- mock "DELETE" "test" "" StrMap.empty
|
|
|
|
case response of
|
2017-07-14 06:28:57 +00:00
|
|
|
(Request.Delete _ "test") -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected the path 'test', got " <> show a
|
2017-07-10 10:17:13 +00:00
|
|
|
|
2017-07-19 05:21:36 +00:00
|
|
|
Spec.describe "with a HEAD" do
|
|
|
|
Spec.it "is a Head" do
|
|
|
|
response <- mock "HEAD" "" "" StrMap.empty
|
|
|
|
case response of
|
|
|
|
(Request.Head _ _) -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected a Head, got " <> show a
|
|
|
|
Spec.it "has the correct headers" do
|
|
|
|
response <- mock "HEAD" "" "" mockHeader
|
|
|
|
case response of
|
|
|
|
(Request.Head headers _) ->
|
|
|
|
Headers.lookup headers "X-Test" ?= "test"
|
|
|
|
a -> Assertions.fail $ "expected a Head, got " <> show a
|
|
|
|
Spec.it "has the correct path" do
|
|
|
|
response <- mock "HEAD" "test" "" StrMap.empty
|
|
|
|
case response of
|
|
|
|
(Request.Head _ "test") -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected the path 'test', got " <> show a
|
|
|
|
|
|
|
|
Spec.describe "with a CONNECT" do
|
|
|
|
Spec.it "is a Connect" do
|
|
|
|
response <- mock "CONNECT" "" "" StrMap.empty
|
|
|
|
case response of
|
|
|
|
(Request.Connect _ _ _) -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected a Connect, got " <> show a
|
|
|
|
Spec.it "has the correct headers" do
|
|
|
|
response <- mock "CONNECT" "" "" mockHeader
|
|
|
|
case response of
|
|
|
|
(Request.Connect headers _ _) ->
|
|
|
|
Headers.lookup headers "X-Test" ?= "test"
|
|
|
|
a -> Assertions.fail $ "expected a Connect, got " <> show a
|
|
|
|
Spec.it "has the correct path" do
|
|
|
|
response <- mock "CONNECT" "test" "" StrMap.empty
|
|
|
|
case response of
|
|
|
|
(Request.Connect _ "test" _) -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected the path 'test', got " <> show a
|
|
|
|
Spec.it "has the correct body" do
|
|
|
|
response <- mock "CONNECT" "" "test" StrMap.empty
|
|
|
|
case response of
|
|
|
|
(Request.Connect _ _ "test") -> pure unit
|
2017-07-20 22:48:59 +00:00
|
|
|
(Request.Connect _ _ body) ->
|
|
|
|
Assertions.fail $ "expected the body 'test', got " <> body
|
|
|
|
a -> Assertions.fail $ "expected a connect, got " <> show a
|
2017-07-19 05:21:36 +00:00
|
|
|
|
|
|
|
Spec.describe "with a OPTIONS" do
|
|
|
|
Spec.it "is a Options" do
|
|
|
|
response <- mock "OPTIONS" "" "" StrMap.empty
|
|
|
|
case response of
|
|
|
|
(Request.Options _ _) -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected a Options, got " <> show a
|
|
|
|
Spec.it "has the correct headers" do
|
|
|
|
response <- mock "OPTIONS" "" "" mockHeader
|
|
|
|
case response of
|
|
|
|
(Request.Options headers _) ->
|
|
|
|
Headers.lookup headers "X-Test" ?= "test"
|
|
|
|
a -> Assertions.fail $ "expected a Options, got " <> show a
|
|
|
|
Spec.it "has the correct path" do
|
|
|
|
response <- mock "OPTIONS" "test" "" StrMap.empty
|
|
|
|
case response of
|
|
|
|
(Request.Options _ "test") -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected the path 'test', got " <> show a
|
|
|
|
|
|
|
|
Spec.describe "with a TRACE" do
|
|
|
|
Spec.it "is a Trace" do
|
|
|
|
response <- mock "TRACE" "" "" StrMap.empty
|
|
|
|
case response of
|
|
|
|
(Request.Trace _ _) -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected a Trace, got " <> show a
|
|
|
|
Spec.it "has the correct headers" do
|
|
|
|
response <- mock "TRACE" "" "" mockHeader
|
|
|
|
case response of
|
|
|
|
(Request.Trace headers _) ->
|
|
|
|
Headers.lookup headers "X-Test" ?= "test"
|
|
|
|
a -> Assertions.fail $ "expected a Trace, got " <> show a
|
|
|
|
Spec.it "has the correct path" do
|
|
|
|
response <- mock "TRACE" "test" "" StrMap.empty
|
|
|
|
case response of
|
|
|
|
(Request.Trace _ "test") -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected the path 'test', got " <> show a
|
|
|
|
|
|
|
|
Spec.describe "with a PATCH" do
|
|
|
|
Spec.it "is a Patch" do
|
|
|
|
response <- mock "PATCH" "" "" StrMap.empty
|
|
|
|
case response of
|
|
|
|
(Request.Patch _ _ _) -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected a Patch, got " <> show a
|
|
|
|
Spec.it "has the correct headers" do
|
|
|
|
response <- mock "PATCH" "" "" mockHeader
|
|
|
|
case response of
|
|
|
|
(Request.Patch headers _ _) ->
|
|
|
|
Headers.lookup headers "X-Test" ?= "test"
|
|
|
|
a -> Assertions.fail $ "expected a Patch, got " <> show a
|
|
|
|
Spec.it "has the correct path" do
|
|
|
|
response <- mock "PATCH" "test" "" StrMap.empty
|
|
|
|
case response of
|
|
|
|
(Request.Patch _ "test" _) -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected the path 'test', got " <> show a
|
|
|
|
Spec.it "has the correct body" do
|
|
|
|
response <- mock "PATCH" "" "test" StrMap.empty
|
|
|
|
case response of
|
|
|
|
(Request.Patch _ _ "test") -> pure unit
|
2017-07-20 22:48:59 +00:00
|
|
|
(Request.Patch _ _ body) ->
|
|
|
|
Assertions.fail $ "expected the body 'test', got " <> body
|
|
|
|
a -> Assertions.fail $ "expected a patch, got " <> show a
|
2017-07-19 05:21:36 +00:00
|
|
|
|
2017-07-14 06:28:57 +00:00
|
|
|
Spec.describe "with a GET" do
|
|
|
|
Spec.it "is a Get" do
|
2017-07-18 05:25:14 +00:00
|
|
|
response <- mock "GET" "" "" StrMap.empty
|
|
|
|
case response of
|
2017-07-14 06:28:57 +00:00
|
|
|
(Request.Get _ _) -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected a Get, got " <> show a
|
2017-07-18 01:51:43 +00:00
|
|
|
Spec.it "has the correct headers" do
|
2017-07-18 05:25:14 +00:00
|
|
|
response <- mock "GET" "" "" mockHeader
|
|
|
|
case response of
|
2017-07-18 01:51:43 +00:00
|
|
|
(Request.Get headers _) ->
|
2017-07-19 05:21:36 +00:00
|
|
|
Headers.lookup headers "X-Test" ?= "test"
|
2017-07-18 01:51:43 +00:00
|
|
|
a -> Assertions.fail $ "expected a Get, got " <> show a
|
2017-07-14 06:28:57 +00:00
|
|
|
Spec.it "has the correct path" do
|
2017-07-18 05:25:14 +00:00
|
|
|
response <- mock "GET" "test" "" StrMap.empty
|
|
|
|
case response of
|
2017-07-14 06:28:57 +00:00
|
|
|
(Request.Get _ "test") -> pure unit
|
|
|
|
a -> Assertions.fail $ "expected the path 'test', got " <> show a
|
2017-07-18 01:51:43 +00:00
|
|
|
|
|
|
|
where
|
2017-07-18 05:25:14 +00:00
|
|
|
mock method path body headers = do
|
|
|
|
let req = SpecHelpers.mockRequest method path body headers
|
|
|
|
EffClass.liftEff req >>= Request.fromHTTPRequest
|
2017-07-18 01:51:43 +00:00
|
|
|
mockHeader = StrMap.singleton "x-test" "test"
|
2017-07-10 10:17:13 +00:00
|
|
|
|
|
|
|
requestSpec :: SpecHelpers.Test
|
2017-07-14 06:28:57 +00:00
|
|
|
requestSpec = Spec.describe "Request" do
|
|
|
|
showSpec
|
|
|
|
fromHTTPRequestSpec
|