packages feed

hspec-yesod 0.2.0 → 0.2.0.1

raw patch · 4 files changed

+43/−51 lines, 4 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

Files

ChangeLog.md view
@@ -1,5 +1,11 @@ # ChangeLog for hspec-yesod +## 0.2.0.1++- Fixes unused import warnings in `Test.Hspec.Yesod.Internal`+- `statusIs` now also prints the first 1024 characters of the request body, in certain circumstances.+- `statusIs` output broken across multiple lines+ ## 0.2.0  - `statusIs` now prints the request method, path, and query string on failing requests
hspec-yesod.cabal view
@@ -1,5 +1,5 @@ name:               hspec-yesod-version:            0.2.0+version:            0.2.0.1 license:            MIT license-file:       LICENSE author:             Nubis <nubis@woobiz.com.ar>, Matt Parsons <parsonsmatt@gmail.com>
src/Test/Hspec/Yesod.hs view
@@ -591,7 +591,7 @@ assertEqualNoShow msg a b = liftIO $ HUnit.assertBool msg (a == b)  -- | Assert the last response status is as expected.--- If the status code doesn't match, a portion of the body is also printed to aid in debugging.+-- If the status code doesn't match, the request and a portion of the body is also printed to aid in debugging. -- -- ==== __Examples__ --@@ -601,17 +601,43 @@ statusIs number = do   latestRequest <- requireLatestRequest   withResponse $ \(SResponse status headers body) -> do-    let mContentType = lookup hContentType headers-        isUTF8ContentType = maybe False YT.Internal.contentTypeHeaderIsUtf8 mContentType+    let mResponseContentType = lookup hContentType headers+        isResponseUTF8ContentType = maybe False YT.Internal.contentTypeHeaderIsUtf8 mResponseContentType -    liftIO $ flip HUnit.assertBool (H.statusCode status == number) $ concat-      [ "Expected status was ", show number-      , " but received status was ", show $ H.statusCode status-      , " for " <> (T.unpack $ formatRequestBuilderDataForDebugging latestRequest)-      , if isUTF8ContentType-          then ". For debugging, the body was: " <> (T.unpack $ YT.Internal.getBodyTextPreview body)-          else ""+        responsePreview = T.unpack $ YT.Internal.getBodyTextPreview body+        previewFinal = if responsePreview == "" then "<empty response>" else +                if isResponseUTF8ContentType then T.unpack $ YT.Internal.getBodyTextPreview body+                    else "<content-type not suitable for printing>"++    liftIO $ flip HUnit.assertBool (H.statusCode status == number) $ unlines+      [ "Expected status: " <> show number+      , "But status was: " <> (show $ H.statusCode status)+      , "Request: " <> (T.unpack $ formatRequestBuilderDataForDebugging latestRequest)+      , "Request body: " <> (T.unpack $ getRequestBodyPreview latestRequest)+      , "Response body: " <> previewFinal       ]++  where+    getRequestBodyPreview :: RequestBuilderData site -> T.Text+    getRequestBodyPreview RequestBuilderData{..} = +        let mRequestContentType = lookup hContentType rbdHeaders+            isRequestUTF8ContentType = maybe False YT.Internal.contentTypeHeaderIsUtf8 mRequestContentType+        in case rbdPostData of+                    MultipleItemsPostData xs -> case xs of+                        [] -> "<empty body>"+                        _ -> "<POST param preview not supported>"+                    BinaryPostData lbs -> if isRequestUTF8ContentType then +                        getRequestTextPreview lbs+                        else "<empty body>"++    getRequestTextPreview :: BSL8.ByteString -> T.Text+    getRequestTextPreview body =+      let characterLimit = 1024+          textBody = TL.toStrict $ decodeUtf8 body+      in if T.length textBody < characterLimit+            then textBody+            else T.take characterLimit textBody <> "... (use `rbdPostData` from `yedRequest` to see complete request body)"+  -- | Assert the given header key/value pair was returned. --
src/Test/Hspec/Yesod/Internal.hs view
@@ -7,50 +7,10 @@     , RequestPart(..)     ) where -import Control.Monad.Catch (finally)-import Test.Hspec.Core.Spec-import Test.Hspec.Core.Hooks-import qualified Data.List as DL-import qualified Data.ByteString.Char8 as BS8-import Data.ByteString (ByteString) import qualified Data.Text as T-import qualified Data.Text.Encoding as TE-import qualified Data.Text.Encoding.Error as TErr import qualified Data.ByteString.Lazy.Char8 as BSL8-import qualified Test.HUnit as HUnit import qualified Network.HTTP.Types as H-import qualified Network.Socket as Sock-import Data.CaseInsensitive (CI)-import qualified Data.CaseInsensitive as CI-import Network.Wai import Network.Wai.Test hiding (assertHeader, assertNoHeader, request)-import Control.Monad.IO.Class-import qualified Control.Monad.State.Class as MS-import Control.Monad.State.Class hiding (get)-import System.IO-import Yesod.Core.Unsafe (runFakeHandler)-import Yesod.Core-import qualified Data.Text.Lazy as TL-import Data.Text.Lazy.Encoding (encodeUtf8, decodeUtf8, decodeUtf8With)-import Text.XML.Cursor hiding (element)-import qualified Text.XML.Cursor as C-import qualified Text.HTML.DOM as HD-import qualified Data.Map as M-import qualified Web.Cookie as Cookie-import qualified Blaze.ByteString.Builder as Builder-import Data.Time.Clock (getCurrentTime)-import Control.Applicative ((<$>))-import Text.Show.Pretty (ppShow)-import Data.Monoid (mempty)-import Data.ByteArray.Encoding (convertToBase, Base(..))-import Network.HTTP.Types.Header (hContentType)-import Data.Aeson (eitherDecode')-import Control.Monad-import qualified Yesod.Test.TransversingCSS as YT.CSS-import Yesod.Test.TransversingCSS (HtmlLBS, Query)-import qualified Yesod.Test.Internal.SIO as YT.SIO-import Yesod.Test.Internal.SIO (SIO, execSIO, runSIO)-import qualified Yesod.Test.Internal as YT.Internal (getBodyTextPreview, contentTypeHeaderIsUtf8)  data RequestBuilderData site = RequestBuilderData     { rbdPostData :: RBDPostData