hspec-wai-json 0.8.0 → 0.9.0
raw patch · 3 files changed
+68/−28 lines, 3 filesdep ~hspec-waiPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: hspec-wai
API changes (from Hackage documentation)
Files
- hspec-wai-json.cabal +13/−10
- src/Test/Hspec/Wai/JSON.hs +33/−1
- test/Test/Hspec/Wai/JSONSpec.hs +22/−17
hspec-wai-json.cabal view
@@ -1,9 +1,9 @@--- This file has been generated from package.yaml by hpack version 0.15.0.+-- This file has been generated from package.yaml by hpack version 0.19.3. -- -- see: https://github.com/sol/hpack name: hspec-wai-json-version: 0.8.0+version: 0.9.0 homepage: https://github.com/hspec/hspec-wai#readme bug-reports: https://github.com/hspec/hspec-wai/issues license: MIT@@ -29,14 +29,16 @@ src exposed-modules: Test.Hspec.Wai.JSON+ other-modules:+ Paths_hspec_wai_json build-depends:- base == 4.*- , hspec-wai == 0.8.0+ aeson+ , aeson-qq >=0.7.3+ , base ==4.* , bytestring- , template-haskell- , aeson- , aeson-qq >= 0.7.3 , case-insensitive+ , hspec-wai ==0.9.0+ , template-haskell ghc-options: -Wall default-language: Haskell2010 @@ -47,10 +49,11 @@ main-is: Spec.hs other-modules: Test.Hspec.Wai.JSONSpec+ Paths_hspec_wai_json build-depends:- base == 4.*- , hspec-wai-json- , hspec-wai+ base ==4.* , hspec+ , hspec-wai+ , hspec-wai-json ghc-options: -Wall default-language: Haskell2010
src/Test/Hspec/Wai/JSON.hs view
@@ -6,6 +6,8 @@ ) where import Data.ByteString.Lazy (ByteString)+import qualified Data.ByteString.Char8 as B+import Data.Char import Data.Aeson (Value, decode, encode) import Data.Aeson.QQ import Language.Haskell.TH.Quote@@ -52,7 +54,37 @@ fromValue :: Value -> a instance FromValue ResponseMatcher where- fromValue = ResponseMatcher 200 ["Content-Type" <:> "application/json"] . equalsJSON+ fromValue = ResponseMatcher 200 [matchHeader] . equalsJSON+ where+ matchHeader = MatchHeader $ \headers _body ->+ case lookup "Content-Type" headers of+ Just h | isJSON h -> Nothing+ _ -> Just $ unlines [+ "missing header:"+ , formatHeader ("Content-Type", "application/json")+ ]+ isJSON c = media == "application/json" && parameters `elem` ignoredParameters+ where+ (media, parameters) = let (m, p) = breakAt ';' c in (strip m, strip p)++ -- Technically, no parameters are required nor optional for+ -- application/json. However, as charset=utf-8 is widely added by+ -- other software and compliant recipients should ignore any charset+ -- (as per http://www.iana.org/assignments/media-types/application/json)+ -- we ignore charset=utf-8 here.+ --+ -- This is a decision made for pragmatism!+ --+ -- I'm still pretty much against ignoring any other charsets. Adding+ -- a charset parameter is non-standard and hspec-wai is not just a+ -- compliant recipients but a testing software. Hence I take the+ -- stance that the job of a testing software is not just to accept+ -- what a compliant client would accept but also to enforce standard+ -- conformance.+ ignoredParameters = ["", "charset=utf-8"]++ breakAt c = fmap (B.drop 1) . B.break (== c)+ strip = B.reverse . B.dropWhile isSpace . B.reverse . B.dropWhile isSpace equalsJSON :: Value -> MatchBody equalsJSON expected = MatchBody matcher
test/Test/Hspec/Wai/JSONSpec.hs view
@@ -1,5 +1,6 @@ {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE NoMonomorphismRestriction #-} module Test.Hspec.Wai.JSONSpec (main, spec) where import Test.Hspec@@ -34,24 +35,28 @@ ]) context "when matching Content-Type header" $ do- let [MatchHeader matcher] = matchHeaders [json|{foo: 23}|]+ let+ body = [json|{foo: 23}|]+ [MatchHeader matcher] = matchHeaders body+ match = (`matcher` body) - context "when body is ASCII" $ do- let- body = [json|{foo: 23}|]- match = (`matcher` body)+ it "accepts 'application/json'" $ do+ match [("Content-Type", "application/json")] `shouldBe` Nothing - it "accepts 'application/json'" $ do- match [("Content-Type", "application/json")] `shouldBe` Nothing+ it "ignores 'charset=utf-8'" $ do+ match [("Content-Type", "application/json;charset=utf-8")] `shouldBe` Nothing - it "accepts 'application/json; charset=utf-8'" $ do- match [("Content-Type", "application/json; charset=utf-8")] `shouldBe` (Just . unlines) [- "missing header:"- , " Content-Type: application/json"- ]+ it "ignores whitespace" $ do+ match [("Content-Type", "application/json ; charset=utf-8")] `shouldBe` Nothing - it "rejects other headers" $ do- match [("Content-Type", "foobar")] `shouldBe` (Just . unlines) [- "missing header:"- , " Content-Type: application/json"- ]+ it "requires a Content-Type header" $ do+ match [] `shouldBe` (Just. unlines) [+ "missing header:"+ , " Content-Type: application/json"+ ]++ it "rejects other values for Content-Type" $ do+ match [("Content-Type", "foobar")] `shouldBe` (Just . unlines) [+ "missing header:"+ , " Content-Type: application/json"+ ]