purescript-httpurple/test/HTTPure/RequestSpec.purs

127 lines
4.8 KiB
Haskell
Raw Normal View History

module HTTPure.RequestSpec where
import Prelude
2017-07-18 05:25:14 +00:00
import Control.Monad.Eff.Class as EffClass
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-07-18 01:51:43 +00:00
import HTTPure.Headers as Headers
import HTTPure.Request as Request
2017-07-10 10:17:13 +00:00
import HTTPure.SpecHelpers as SpecHelpers
showSpec :: SpecHelpers.Test
showSpec = Spec.describe "show" do
Spec.describe "with a POST" do
Spec.it "is the method and the path" do
show (Request.Post none "test" "") `Assertions.shouldEqual` "POST: test"
Spec.describe "with a PUT" do
Spec.it "is the method and the path" do
show (Request.Put none "test" "") `Assertions.shouldEqual` "PUT: test"
Spec.describe "with a DELETE" do
Spec.it "is the method and the path" do
show (Request.Delete none "test") `Assertions.shouldEqual` "DELETE: test"
Spec.describe "with a GET" do
Spec.it "is the method and the path" do
show (Request.Get none "test") `Assertions.shouldEqual` "GET: test"
where
none = StrMap.empty
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
(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 _ _) ->
Headers.lookup headers "X-Test" `Assertions.shouldEqual` "test"
a -> Assertions.fail $ "expected a Post, got " <> show a
Spec.it "has the correct path" do
2017-07-18 05:25:14 +00:00
response <- mock "POST" "test" "" StrMap.empty
case response of
(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
a -> Assertions.fail $ "expected the body 'test', got " <> show a
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
(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 _ _) ->
Headers.lookup headers "X-Test" `Assertions.shouldEqual` "test"
a -> Assertions.fail $ "expected a Put, got " <> show a
Spec.it "has the correct path" do
2017-07-18 05:25:14 +00:00
response <- mock "PUT" "test" "" StrMap.empty
case response of
(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
a -> Assertions.fail $ "expected the body 'test', got " <> show a
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
(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 _) ->
Headers.lookup headers "X-Test" `Assertions.shouldEqual` "test"
a -> Assertions.fail $ "expected a Delete, got " <> show a
Spec.it "has the correct path" do
2017-07-18 05:25:14 +00:00
response <- mock "DELETE" "test" "" StrMap.empty
case response of
(Request.Delete _ "test") -> pure unit
a -> Assertions.fail $ "expected the path 'test', got " <> show a
2017-07-10 10:17:13 +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
(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 _) ->
Headers.lookup headers "X-Test" `Assertions.shouldEqual` "test"
a -> Assertions.fail $ "expected a Get, got " <> show a
Spec.it "has the correct path" do
2017-07-18 05:25:14 +00:00
response <- mock "GET" "test" "" StrMap.empty
case response of
(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
requestSpec = Spec.describe "Request" do
showSpec
fromHTTPRequestSpec