2017-10-26 21:19:30 +00:00
|
|
|
module Test.HTTPure.QuerySpec where
|
2017-09-26 06:08:07 +00:00
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
2017-09-26 07:14:06 +00:00
|
|
|
import Data.Tuple as Tuple
|
2018-07-08 23:16:48 +00:00
|
|
|
import Foreign.Object as Object
|
2017-09-26 06:08:07 +00:00
|
|
|
import Test.Spec as Spec
|
|
|
|
|
|
|
|
import HTTPure.Query as Query
|
|
|
|
|
2017-10-26 21:19:30 +00:00
|
|
|
import Test.HTTPure.TestHelpers as TestHelpers
|
|
|
|
import Test.HTTPure.TestHelpers ((?=))
|
2017-09-26 06:08:07 +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
|
2017-09-26 07:14:06 +00:00
|
|
|
Spec.describe "with no query string" do
|
2018-07-08 23:16:48 +00:00
|
|
|
Spec.it "is an empty Map" do
|
2019-04-25 17:13:04 +00:00
|
|
|
req <- TestHelpers.mockRequest "" "" "/test" "" []
|
2018-07-08 23:16:48 +00:00
|
|
|
Query.read req ?= Object.empty
|
2017-09-26 07:14:06 +00:00
|
|
|
Spec.describe "with an empty query string" do
|
2018-07-08 23:16:48 +00:00
|
|
|
Spec.it "is an empty Map" do
|
2019-04-25 17:13:04 +00:00
|
|
|
req <- TestHelpers.mockRequest "" "" "/test?" "" []
|
2018-07-08 23:16:48 +00:00
|
|
|
Query.read req ?= Object.empty
|
2017-09-26 07:14:06 +00:00
|
|
|
Spec.describe "with a query parameter in the query string" do
|
2018-07-08 23:16:48 +00:00
|
|
|
Spec.it "is a correct Map" do
|
2019-04-25 17:13:04 +00:00
|
|
|
req <- TestHelpers.mockRequest "" "" "/test?a=b" "" []
|
2018-07-08 23:16:48 +00:00
|
|
|
Query.read req ?= Object.singleton "a" "b"
|
2017-09-26 07:14:06 +00:00
|
|
|
Spec.describe "with empty fields in the query string" do
|
|
|
|
Spec.it "ignores the empty fields" do
|
2019-04-25 17:13:04 +00:00
|
|
|
req <- TestHelpers.mockRequest "" "" "/test?&&a=b&&" "" []
|
2018-07-08 23:16:48 +00:00
|
|
|
Query.read req ?= Object.singleton "a" "b"
|
2017-09-26 07:14:06 +00:00
|
|
|
Spec.describe "with duplicated params" do
|
|
|
|
Spec.it "takes the last param value" do
|
2019-04-25 17:13:04 +00:00
|
|
|
req <- TestHelpers.mockRequest "" "" "/test?a=b&a=c" "" []
|
2018-07-08 23:16:48 +00:00
|
|
|
Query.read req ?= Object.singleton "a" "c"
|
2017-09-26 07:14:06 +00:00
|
|
|
Spec.describe "with empty params" do
|
2018-11-06 17:45:37 +00:00
|
|
|
Spec.it "uses '' as the value" do
|
2019-04-25 17:13:04 +00:00
|
|
|
req <- TestHelpers.mockRequest "" "" "/test?a" "" []
|
2018-11-06 17:45:37 +00:00
|
|
|
Query.read req ?= Object.singleton "a" ""
|
2017-09-26 07:14:06 +00:00
|
|
|
Spec.describe "with complex params" do
|
2018-07-08 23:16:48 +00:00
|
|
|
Spec.it "is the correct Map" do
|
2019-04-25 17:13:04 +00:00
|
|
|
req <- TestHelpers.mockRequest "" "" "/test?&&a&b=c&b=d&&&e=f&g=&" "" []
|
2017-09-29 14:25:31 +00:00
|
|
|
Query.read req ?= expectedComplexResult
|
2018-11-06 17:45:37 +00:00
|
|
|
Spec.describe "with urlencoded params" do
|
|
|
|
Spec.it "decodes valid keys and values" do
|
2019-04-25 17:13:04 +00:00
|
|
|
req <- TestHelpers.mockRequest "" "" "/test?foo%20bar=%3Fx%3Dtest" "" []
|
2018-11-06 17:45:37 +00:00
|
|
|
Query.read req ?= Object.singleton "foo bar" "?x=test"
|
|
|
|
Spec.it "passes invalid keys and values through" do
|
2019-04-25 17:13:04 +00:00
|
|
|
req <- TestHelpers.mockRequest "" "" "/test?%%=%C3" "" []
|
2018-11-06 17:45:37 +00:00
|
|
|
Query.read req ?= Object.singleton "%%" "%C3"
|
|
|
|
Spec.it "converts + to a space" do
|
2019-04-25 17:13:04 +00:00
|
|
|
req <- TestHelpers.mockRequest "" "" "/test?foo=bar+baz" "" []
|
2018-11-06 17:45:37 +00:00
|
|
|
Query.read req ?= Object.singleton "foo" "bar baz"
|
2017-09-26 07:14:06 +00:00
|
|
|
where
|
|
|
|
expectedComplexResult =
|
2018-07-08 23:16:48 +00:00
|
|
|
Object.fromFoldable
|
2018-11-06 17:45:37 +00:00
|
|
|
[ Tuple.Tuple "a" ""
|
2017-09-26 07:14:06 +00:00
|
|
|
, Tuple.Tuple "b" "d"
|
|
|
|
, Tuple.Tuple "e" "f"
|
2018-11-06 17:45:37 +00:00
|
|
|
, Tuple.Tuple "g" ""
|
2017-09-26 07:14:06 +00:00
|
|
|
]
|
2017-09-26 06:08:07 +00:00
|
|
|
|
2017-10-26 21:19:30 +00:00
|
|
|
querySpec :: TestHelpers.Test
|
2017-09-26 06:08:07 +00:00
|
|
|
querySpec = Spec.describe "Query" do
|
|
|
|
readSpec
|