purescript-httpurple/test/Test/HTTPure/HeadersSpec.purs

123 lines
4.4 KiB
Haskell
Raw Normal View History

2017-10-26 21:19:30 +00:00
module Test.HTTPure.HeadersSpec where
import Prelude
import Effect.Class as EffectClass
import Data.Maybe as Maybe
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 ((!!))
2017-10-26 21:19:30 +00:00
import Test.HTTPure.TestHelpers as TestHelpers
import Test.HTTPure.TestHelpers ((?=))
2017-10-26 21:19:30 +00:00
lookupSpec :: TestHelpers.Test
2017-07-18 01:51:43 +00:00
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 Just the string" do
Headers.header "x-test" "test" !! "x-test" ?= Maybe.Just "test"
2017-07-18 01:51:43 +00:00
Spec.describe "when searching with uppercase" do
Spec.it "is Just the string" do
Headers.header "x-test" "test" !! "X-Test" ?= Maybe.Just "test"
2017-07-18 01:51:43 +00:00
Spec.describe "when the string is not in the header set" do
Spec.it "is Nothing" do
((Headers.empty !! "X-Test") :: Maybe.Maybe String) ?= Maybe.Nothing
2017-09-26 06:08:07 +00:00
2017-10-26 21:19:30 +00:00
showSpec :: TestHelpers.Test
2017-09-26 06:08:07 +00:00
showSpec = Spec.describe "show" do
Spec.it "is a string representing the headers in HTTP format" do
let mock = Headers.header "Test1" "1" <> Headers.header "Test2" "2"
show mock ?= "Test1: 1\nTest2: 2\n\n"
2017-09-26 06:08:07 +00:00
2017-10-26 21:19:30 +00:00
eqSpec :: TestHelpers.Test
2017-09-26 06:08:07 +00:00
eqSpec = Spec.describe "eq" do
Spec.describe "when the two Headers contain the same keys and values" do
Spec.it "is true" do
Headers.header "Test1" "test1" == Headers.header "Test1" "test1" ?= true
2017-09-26 06:08:07 +00:00
Spec.describe "when the two Headers contain different keys and values" do
Spec.it "is false" do
Headers.header "Test1" "test1" == Headers.header "Test2" "test2" ?= false
Spec.describe "when the two Headers contain only different values" do
Spec.it "is false" do
Headers.header "Test1" "test1" == Headers.header "Test1" "test2" ?= false
2017-09-26 06:08:07 +00:00
Spec.describe "when the one Headers contains additional keys and values" do
Spec.it "is false" do
let mock = Headers.header "Test1" "1" <> Headers.header "Test2" "2"
Headers.header "Test1" "1" == mock ?= false
2017-09-26 06:08:07 +00:00
2017-10-26 21:19:30 +00:00
appendSpec :: TestHelpers.Test
2017-09-27 19:34:00 +00:00
appendSpec = Spec.describe "append" do
Spec.describe "when there are multiple keys" do
Spec.it "appends the headers correctly" do
let mock1 = Headers.header "Test1" "1" <> Headers.header "Test2" "2"
let mock2 = Headers.header "Test3" "3" <> Headers.header "Test4" "4"
let mock3 = Headers.headers
[ Tuple.Tuple "Test1" "1"
, Tuple.Tuple "Test2" "2"
, Tuple.Tuple "Test3" "3"
, Tuple.Tuple "Test4" "4"
]
mock1 <> mock2 ?= mock3
2017-09-27 19:34:00 +00:00
Spec.describe "when there is a duplicated key" do
Spec.it "uses the last appended value" do
let mock = Headers.header "Test" "foo" <> Headers.header "Test" "bar"
mock ?= Headers.header "Test" "bar"
2017-09-27 19:34:00 +00:00
2017-10-26 21:19:30 +00:00
readSpec :: TestHelpers.Test
2017-09-26 06:08:07 +00:00
readSpec = Spec.describe "read" do
Spec.describe "with no headers" do
Spec.it "is an empty Map" do
request <- TestHelpers.mockRequest "" "" "" "" []
Headers.read request ?= Headers.empty
2017-09-26 06:08:07 +00:00
Spec.describe "with headers" do
Spec.it "is a Map with the contents of the headers" do
2017-09-26 06:08:07 +00:00
let testHeader = [Tuple.Tuple "X-Test" "test"]
request <- TestHelpers.mockRequest "" "" "" "" testHeader
2017-09-26 06:08:07 +00:00
Headers.read request ?= Headers.headers testHeader
2017-07-18 01:51:43 +00:00
2017-10-26 21:19:30 +00:00
writeSpec :: TestHelpers.Test
writeSpec = Spec.describe "write" do
Spec.it "writes the headers to the response" do
header <- EffectClass.liftEffect do
2017-10-26 21:19:30 +00:00
mock <- TestHelpers.mockResponse
Headers.write mock $ Headers.header "X-Test" "test"
2017-10-26 21:19:30 +00:00
pure $ TestHelpers.getResponseHeader "X-Test" mock
header ?= "test"
2017-10-26 21:19:30 +00:00
emptySpec :: TestHelpers.Test
emptySpec = Spec.describe "empty" do
Spec.it "is an empty Map in an empty Headers" do
show Headers.empty ?= "\n"
2017-10-26 21:19:30 +00:00
headerSpec :: TestHelpers.Test
headerSpec = Spec.describe "header" do
Spec.it "creates a singleton Headers" do
show (Headers.header "X-Test" "test") ?= "X-Test: test\n\n"
2017-10-26 21:19:30 +00:00
headersFunctionSpec :: TestHelpers.Test
headersFunctionSpec = Spec.describe "headers" do
Spec.it "is equivalent to using Headers.header with <>" do
test ?= expected
where
test =
Headers.headers
[ Tuple.Tuple "X-Test-1" "1"
, Tuple.Tuple "X-Test-2" "2"
]
expected = Headers.header "X-Test-1" "1" <> Headers.header "X-Test-2" "2"
2017-10-26 21:19:30 +00:00
headersSpec :: TestHelpers.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
emptySpec
headerSpec
headersFunctionSpec