purescript-httpurple/test/HTTPure/HeadersSpec.purs

120 lines
3.9 KiB
Haskell
Raw Normal View History

module HTTPure.HeadersSpec where
import Prelude
import Control.Monad.Eff.Class as EffClass
2017-09-26 06:08:07 +00:00
import Data.Tuple as Tuple
import Test.Spec as Spec
import HTTPure.Headers as Headers
2017-09-26 06:08:07 +00:00
import HTTPure.Lookup ((!!))
import HTTPure.SpecHelpers as SpecHelpers
import HTTPure.SpecHelpers ((?=))
2017-07-18 01:51:43 +00:00
lookupSpec :: SpecHelpers.Test
lookupSpec = Spec.describe "lookup" do
Spec.describe "when the string is in the header set" do
Spec.describe "when searching with lowercase" do
Spec.it "is the string" do
2017-09-26 06:08:07 +00:00
mockHeaders !! "x-test" ?= "test"
2017-07-18 01:51:43 +00:00
Spec.describe "when searching with uppercase" do
Spec.it "is the string" do
2017-09-26 06:08:07 +00:00
mockHeaders !! "X-Test" ?= "test"
2017-07-18 01:51:43 +00:00
Spec.describe "when the string is not in the header set" do
Spec.it "is an empty string" do
2017-09-26 06:08:07 +00:00
(Headers.headers []) !! "X-Test" ?= ""
2017-07-18 01:51:43 +00:00
where
2017-09-26 06:08:07 +00:00
mockHeaders = Headers.headers [Tuple.Tuple "x-test" "test"]
showSpec :: SpecHelpers.Test
showSpec = Spec.describe "show" do
Spec.it "is a string representing the headers in HTTP format" do
show mockHeaders ?= "Test1: test1\nTest2: test2\n\n"
where
mockHeaders =
Headers.headers
[ Tuple.Tuple "Test1" "test1"
, Tuple.Tuple "Test2" "test2"
]
eqSpec :: SpecHelpers.Test
eqSpec = Spec.describe "eq" do
Spec.describe "when the two Headers contain the same keys and values" do
Spec.it "is true" do
eq mockHeaders1 mockHeaders2 ?= true
Spec.describe "when the two Headers contain different keys and values" do
Spec.it "is false" do
eq mockHeaders1 mockHeaders3 ?= false
Spec.describe "when the one Headers contains additional keys and values" do
Spec.it "is false" do
eq mockHeaders1 mockHeaders4 ?= false
where
mockHeaders1 = Headers.headers [ Tuple.Tuple "Test1" "test1" ]
mockHeaders2 = Headers.headers [ Tuple.Tuple "Test1" "test1" ]
mockHeaders3 = Headers.headers [ Tuple.Tuple "Test2" "test2" ]
mockHeaders4 =
Headers.headers
[ Tuple.Tuple "Test1" "test1"
, Tuple.Tuple "Test2" "test2"
]
2017-09-27 19:34:00 +00:00
appendSpec :: SpecHelpers.Test
appendSpec = Spec.describe "append" do
Spec.describe "when there are multiple keys" do
Spec.it "appends the headers correctly" do
mockHeaders1 <> mockHeaders2 ?= mockHeaders3
Spec.describe "when there is a duplicated key" do
Spec.it "uses the last appended value" do
mockHeadersDupe1 <> mockHeadersDupe2 ?= mockHeadersDupe2
where
mockHeadersDupe1 = Headers.headers [ Tuple.Tuple "Test" "foo" ]
mockHeadersDupe2 = Headers.headers [ Tuple.Tuple "Test" "bar" ]
mockHeaders1 =
Headers.headers
[ Tuple.Tuple "Test1" "test1"
, Tuple.Tuple "Test2" "test2"
]
mockHeaders2 =
Headers.headers
[ Tuple.Tuple "Test3" "test3"
, Tuple.Tuple "Test4" "test4"
]
mockHeaders3 =
Headers.headers
[ Tuple.Tuple "Test1" "test1"
, Tuple.Tuple "Test2" "test2"
, Tuple.Tuple "Test3" "test3"
, Tuple.Tuple "Test4" "test4"
]
2017-09-26 06:08:07 +00:00
readSpec :: SpecHelpers.Test
readSpec = Spec.describe "read" do
Spec.describe "with no headers" do
Spec.it "is an empty StrMap" do
request <- SpecHelpers.mockRequest "" "" "" []
Headers.read request ?= Headers.headers []
Spec.describe "with headers" do
Spec.it "is an StrMap with the contents of the headers" do
let testHeader = [Tuple.Tuple "X-Test" "test"]
request <- SpecHelpers.mockRequest "" "" "" testHeader
Headers.read request ?= Headers.headers testHeader
2017-07-18 01:51:43 +00:00
writeSpec :: SpecHelpers.Test
writeSpec = Spec.describe "write" do
Spec.it "writes the headers to the response" do
header <- EffClass.liftEff do
mock <- SpecHelpers.mockResponse
2017-09-26 06:08:07 +00:00
Headers.write mock $ Headers.headers [Tuple.Tuple "X-Test" "test"]
pure $ SpecHelpers.getResponseHeader "X-Test" mock
header ?= "test"
headersSpec :: SpecHelpers.Test
headersSpec = Spec.describe "Headers" do
2017-07-18 01:51:43 +00:00
lookupSpec
2017-09-26 06:08:07 +00:00
showSpec
eqSpec
2017-09-27 19:34:00 +00:00
appendSpec
2017-09-26 06:08:07 +00:00
readSpec
writeSpec