hspec-wai-json (empty) → 0.5.0
raw patch · 5 files changed
+137/−0 lines, 5 filesdep +aesondep +aeson-qqdep +basesetup-changed
Dependencies added: aeson, aeson-qq, base, bytestring, hspec-wai, hspec-wai-json, hspec2, template-haskell
Files
- LICENSE +19/−0
- Setup.lhs +3/−0
- hspec-wai-json.cabal +47/−0
- src/Test/Hspec/Wai/JSON.hs +67/−0
- test/Spec.hs +1/−0
+ LICENSE view
@@ -0,0 +1,19 @@+Copyright (c) 2012-2014 Fujimura Daisuke, Simon Hengel++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in+all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+THE SOFTWARE.
+ Setup.lhs view
@@ -0,0 +1,3 @@+#!/usr/bin/env runhaskell+> import Distribution.Simple+> main = defaultMain
+ hspec-wai-json.cabal view
@@ -0,0 +1,47 @@+name: hspec-wai-json+version: 0.5.0+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>+build-type: Simple+cabal-version: >= 1.8+category: Testing+synopsis: Testing JSON APIs with hspec-wai+description: Testing JSON APIs with hspec-wai++source-repository head+ type: git+ location: https://github.com/hspec/hspec-wai++library+ ghc-options:+ -Wall+ hs-source-dirs:+ src+ exposed-modules:+ Test.Hspec.Wai.JSON+ build-depends:+ base == 4.*+ , hspec-wai == 0.5.*+ , bytestring+ , template-haskell+ , aeson+ , aeson-qq >= 0.7.3++test-suite spec+ type:+ exitcode-stdio-1.0+ ghc-options:+ -Wall+ hs-source-dirs:+ test+ main-is:+ Spec.hs+ build-depends:+ base == 4.*+ , hspec-wai-json+ , hspec-wai+ , hspec2
+ src/Test/Hspec/Wai/JSON.hs view
@@ -0,0 +1,67 @@+{-# LANGUAGE OverloadedStrings, TemplateHaskell #-}+module Test.Hspec.Wai.JSON (+-- $setup+ json+, FromValue(..)+) where++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 Language.Haskell.TH.Quote++import Test.Hspec.Wai+import Test.Hspec.Wai.Internal (formatHeader)++-- $setup+-- The examples in this module assume that you have the @QuasiQuotes@ language+-- extension enabled and that "Data.ByteString.Lazy.Char8" is imported+-- qualified as @L@:+--+-- >>> :set -XQuasiQuotes+-- >>> import Data.ByteString.Lazy.Char8 as L++-- | A `QuasiQuoter` for constructing JSON values.+--+-- The constructed value is polymorph and unifies to instances of `FromValue`.+--+-- When used as a `ResponseMatcher` it matches a response with+--+-- * a status code of @200@+--+-- * a @Content-Type@ header with value @application/json@+--+-- * the specified JSON as response body+--+-- When used as a @ByteString@ it creates a ByteString from the specified JSON+-- that can be used as a request body for e.g. @POST@ and @PUT@ requests.+--+-- Example:+--+-- >>> L.putStrLn [json|[23, {foo: 42}]|]+-- [23,{"foo":42}]+json :: QuasiQuoter+json = QuasiQuoter {+ quoteExp = \input -> [|fromValue $(quoteExp aesonQQ input)|]+, quotePat = const $ error "No quotePat defined for Test.Hspec.Wai.JSON.json"+, quoteType = const $ error "No quoteType defined for Test.Hspec.Wai.JSON.json"+, quoteDec = const $ error "No quoteDec defined for Test.Hspec.Wai.JSON.json"+}++class FromValue a where+ fromValue :: Value -> a++instance FromValue ResponseMatcher where+ 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+ then Nothing+ else (Just . unlines) ("missing header:" : (intersperse " OR" $ map formatHeader permissibleHeaders))++instance FromValue ByteString where+ fromValue = encode
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}