hspec-wai-json 0.6.0 → 0.6.1
raw patch · 3 files changed
+75/−17 lines, 3 filesdep +case-insensitive
Dependencies added: case-insensitive
Files
- hspec-wai-json.cabal +23/−16
- src/Test/Hspec/Wai/JSON.hs +8/−1
- test/Test/Hspec/Wai/JSONSpec.hs +44/−0
hspec-wai-json.cabal view
@@ -1,13 +1,21 @@+-- This file has been generated from package.yml by hpack version 0.2.0.+--+-- see: https://github.com/sol/hpack+ name: hspec-wai-json-version: 0.6.0+version: 0.6.1+homepage: https://github.com/hspec/hspec-wai#readme+bug-reports: https://github.com/hspec/hspec-wai/issues license: MIT license-file: LICENSE copyright: (c) 2012-2014 Fujimura Daisuke, (c) 2014 Simon Hengel-author: Fujimura Daisuke <me@fujimuradaisuke.com>, Simon Hengel <sol@typeful.net>-maintainer: Fujimura Daisuke <me@fujimuradaisuke.com>, Simon Hengel <sol@typeful.net>+author: Fujimura Daisuke <me@fujimuradaisuke.com>,+ Simon Hengel <sol@typeful.net>+maintainer: Fujimura Daisuke <me@fujimuradaisuke.com>,+ Simon Hengel <sol@typeful.net> build-type: Simple-cabal-version: >= 1.8+cabal-version: >= 1.10 category: Testing synopsis: Testing JSON APIs with hspec-wai description: Testing JSON APIs with hspec-wai@@ -17,10 +25,7 @@ location: https://github.com/hspec/hspec-wai library- ghc-options:- -Wall- hs-source-dirs:- src+ hs-source-dirs: src exposed-modules: Test.Hspec.Wai.JSON build-depends:@@ -30,18 +35,20 @@ , template-haskell , aeson , aeson-qq >= 0.7.3+ , case-insensitive+ ghc-options: -Wall+ default-language: Haskell2010 test-suite spec- type:- exitcode-stdio-1.0- ghc-options:- -Wall- hs-source-dirs:- test- main-is:- Spec.hs+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ main-is: Spec.hs+ other-modules:+ Test.Hspec.Wai.JSONSpec build-depends: base == 4.* , hspec-wai-json , hspec-wai , hspec+ ghc-options: -Wall+ default-language: Haskell2010
src/Test/Hspec/Wai/JSON.hs view
@@ -5,11 +5,13 @@ , FromValue(..) ) where +import Control.Arrow (second) import Data.List import Data.ByteString.Lazy (ByteString) import qualified Data.ByteString.Lazy as BL import Data.Aeson (Value, encode) import Data.Aeson.QQ+import qualified Data.CaseInsensitive as CI import Language.Haskell.TH.Quote import Test.Hspec.Wai@@ -57,9 +59,14 @@ fromValue v = ResponseMatcher 200 [MatchHeader p] (Just body) where body = fromValue v+ permissibleHeaders = addIfASCII ("Content-Type", "application/json") [("Content-Type", "application/json; charset=utf-8")]+ addIfASCII h = if BL.all (< 128) body then (h :) else id- p headers = if any (`elem` permissibleHeaders) headers++ mkCI = map (second CI.mk)++ p headers = if any (`elem` mkCI permissibleHeaders) (mkCI headers) then Nothing else (Just . unlines) ("missing header:" : (intersperse " OR" $ map formatHeader permissibleHeaders))
+ test/Test/Hspec/Wai/JSONSpec.hs view
@@ -0,0 +1,44 @@+{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}+module Test.Hspec.Wai.JSONSpec (main, spec) where++import Test.Hspec++import Test.Hspec.Wai+import Test.Hspec.Wai.JSON++main :: IO ()+main = hspec spec++spec :: Spec+spec = do+ describe "json" $ do+ context "when matching Content-Type header" $ do+ context "when JSON is ASCII" $ do+ let [MatchHeader matcher] = matchHeaders [json|{foo: 23}|]+ it "accepts 'application/json'" $ do+ matcher [("Content-Type", "application/json")] `shouldBe` Nothing++ it "accepts 'application/json; charset=utf-8'" $ do+ matcher [("Content-Type", "application/json; charset=utf-8")] `shouldBe` Nothing++ it "ignores case" $ do+ matcher [("Content-Type", "application/JSON; charset=UTF-8")] `shouldBe` Nothing++ it "rejects other headers" $ do+ matcher [("Content-Type", "foobar")] `shouldBe` (Just . unlines) [+ "missing header:"+ , " Content-Type: application/json"+ , " OR"+ , " Content-Type: application/json; charset=utf-8"+ ]++ context "when JSON contains Unicode" $ do+ let [MatchHeader matcher] = matchHeaders [json|{foo: #{"\955" :: String}}|]+ it "rejects 'application/json'" $ do+ matcher [("Content-Type", "application/json")] `shouldBe` (Just . unlines) [+ "missing header:"+ , " Content-Type: application/json; charset=utf-8"+ ]++ it "accepts 'application/json; charset=utf-8'" $ do+ matcher [("Content-Type", "application/json; charset=utf-8")] `shouldBe` Nothing