From 57b7ad8eda16dc140464e5ffae7d659a25c52600 Mon Sep 17 00:00:00 2001 From: Chris Allen Date: Thu, 17 Mar 2016 14:18:38 -0500 Subject: [PATCH 1/4] better error provenance for stuff invoking withResponse' --- yesod-test/Yesod/Test.hs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/yesod-test/Yesod/Test.hs b/yesod-test/Yesod/Test.hs index 51912397..86e48933 100644 --- a/yesod-test/Yesod/Test.hs +++ b/yesod-test/Yesod/Test.hs @@ -277,15 +277,21 @@ yit label example = tell [YesodSpecItem label example] -- response-level assertions withResponse' :: MonadIO m => (state -> Maybe SResponse) + -> [T.Text] -> (SResponse -> ST.StateT state m a) -> ST.StateT state m a -withResponse' getter f = maybe err f . getter =<< ST.get - where err = failure "There was no response, you should make a request" +withResponse' getter errTrace f = maybe err f . getter =<< ST.get + where err = failure msg + msg = if null errTrace + then "There was no response, you should make a request." + else + "There was no response, you should make a request. A request was requested because: " + <> T.intercalate "\n-" errTrace -- | Performs a given action using the last response. Use this to create -- response-level assertions withResponse :: (SResponse -> YesodExample site a) -> YesodExample site a -withResponse = withResponse' yedResponse +withResponse = withResponse' yedResponse [] -- | Use HXT to parse a value from an HTML tag. -- Check for usage examples in this module's source. @@ -295,16 +301,17 @@ parseHTML html = fromDocument $ HD.parseLBS html -- | Query the last response using CSS selectors, returns a list of matched fragments htmlQuery' :: MonadIO m => (state -> Maybe SResponse) + -> [T.Text] -> Query -> ST.StateT state m [HtmlLBS] -htmlQuery' getter query = withResponse' getter $ \ res -> +htmlQuery' getter errTrace query = withResponse' getter ("Tried to invoke htmlQuery' in order to read HTML of a previous response." : errTrace) $ \ res -> case findBySelector (simpleBody res) query of Left err -> failure $ query <> " did not parse: " <> T.pack (show err) Right matches -> return $ map (encodeUtf8 . TL.pack) matches -- | Query the last response using CSS selectors, returns a list of matched fragments htmlQuery :: Query -> YesodExample site [HtmlLBS] -htmlQuery = htmlQuery' yedResponse +htmlQuery = htmlQuery' yedResponse [] -- | Asserts that the two given values are equal. assertEqual :: (Eq a) => String -> a -> a -> YesodExample site () @@ -569,7 +576,7 @@ fileByLabel label path mime = do -- > addToken_ "#formID" addToken_ :: Query -> RequestBuilder site () addToken_ scope = do - matches <- htmlQuery' rbdResponse $ scope <> "input[name=_token][type=hidden][value]" + matches <- htmlQuery' rbdResponse ["Tried to get CSRF token with addToken'"] $ scope <> "input[name=_token][type=hidden][value]" case matches of [] -> failure $ "No CSRF token found in the current page" element:[] -> addPostParam "_token" $ head $ attribute "value" $ parseHTML element From 7e10d874923c57849decde4d6605e45ac5923f64 Mon Sep 17 00:00:00 2001 From: Chris Allen Date: Thu, 17 Mar 2016 14:33:15 -0500 Subject: [PATCH 2/4] better error formatting, bumping version --- yesod-test/Yesod/Test.hs | 4 ++-- yesod-test/yesod-test.cabal | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/yesod-test/Yesod/Test.hs b/yesod-test/Yesod/Test.hs index 86e48933..0ec0d5d3 100644 --- a/yesod-test/Yesod/Test.hs +++ b/yesod-test/Yesod/Test.hs @@ -285,8 +285,8 @@ withResponse' getter errTrace f = maybe err f . getter =<< ST.get msg = if null errTrace then "There was no response, you should make a request." else - "There was no response, you should make a request. A request was requested because: " - <> T.intercalate "\n-" errTrace + "There was no response, you should make a request. A request was requested because: \n - " + <> T.intercalate "\n - " errTrace -- | Performs a given action using the last response. Use this to create -- response-level assertions diff --git a/yesod-test/yesod-test.cabal b/yesod-test/yesod-test.cabal index a10d3a6d..ede0f998 100644 --- a/yesod-test/yesod-test.cabal +++ b/yesod-test/yesod-test.cabal @@ -1,5 +1,5 @@ name: yesod-test -version: 1.5.0.1 +version: 1.6.0.0 license: MIT license-file: LICENSE author: Nubis From 776007ffa37fc3185ead6143556ca75d17e53df9 Mon Sep 17 00:00:00 2001 From: Chris Allen Date: Thu, 17 Mar 2016 14:35:15 -0500 Subject: [PATCH 3/4] better wording --- yesod-test/Yesod/Test.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yesod-test/Yesod/Test.hs b/yesod-test/Yesod/Test.hs index 0ec0d5d3..33d60364 100644 --- a/yesod-test/Yesod/Test.hs +++ b/yesod-test/Yesod/Test.hs @@ -285,7 +285,7 @@ withResponse' getter errTrace f = maybe err f . getter =<< ST.get msg = if null errTrace then "There was no response, you should make a request." else - "There was no response, you should make a request. A request was requested because: \n - " + "There was no response, you should make a request. A response was needed because: \n - " <> T.intercalate "\n - " errTrace -- | Performs a given action using the last response. Use this to create From 289471d1221de4d4e3377b10e03ca9bf2e6319cc Mon Sep 17 00:00:00 2001 From: Chris Allen Date: Sun, 20 Mar 2016 12:50:17 -0500 Subject: [PATCH 4/4] appropriate version bump --- yesod-test/yesod-test.cabal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yesod-test/yesod-test.cabal b/yesod-test/yesod-test.cabal index ede0f998..907624e5 100644 --- a/yesod-test/yesod-test.cabal +++ b/yesod-test/yesod-test.cabal @@ -1,5 +1,5 @@ name: yesod-test -version: 1.6.0.0 +version: 1.5.1.0 license: MIT license-file: LICENSE author: Nubis