From cbef19fae94ce912ff01d1c11cc334595e1f4772 Mon Sep 17 00:00:00 2001 From: Maximilian Tagher Date: Tue, 19 Nov 2019 23:11:13 -0800 Subject: [PATCH] [yesod-test] Add testModifySite --- yesod-test/ChangeLog.md | 4 ++++ yesod-test/Yesod/Test.hs | 30 ++++++++++++++++++++++++------ yesod-test/test/main.hs | 2 +- yesod-test/yesod-test.cabal | 2 +- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/yesod-test/ChangeLog.md b/yesod-test/ChangeLog.md index ba1aa905..fe40bf15 100644 --- a/yesod-test/ChangeLog.md +++ b/yesod-test/ChangeLog.md @@ -1,5 +1,9 @@ # ChangeLog for yesod-test +## 1.6.8 + +Add `testModifySite` function [#](https://github.com/yesodweb/yesod/pull/) + ## 1.6.7 Add `addBasicAuthHeader` function [#1632](https://github.com/yesodweb/yesod/pull/1632) diff --git a/yesod-test/Yesod/Test.hs b/yesod-test/Yesod/Test.hs index fcae9ffb..80b814f8 100644 --- a/yesod-test/Yesod/Test.hs +++ b/yesod-test/Yesod/Test.hs @@ -344,15 +344,33 @@ yesodSpecApp site getApp yspecs = yit :: String -> YesodExample site () -> YesodSpec site yit label example = tell [YesodSpecItem label example] --- | Modifies the site ('yedSite') of the test +-- | Modifies the site ('yedSite') of the test, and creates a new WAI app ('yedApp') for it. -- --- TODO documentation here +-- yesod-test allows sending requests to your application to test that it handles them correctly. +-- In rare cases, you may wish to modify that application in the middle of a test. +-- This may be useful if you wish to, for example, test your application under a certain configuration, +-- then change that configuration to see if your app responds differently. -- --- TODO @since -testModifySite :: YesodDispatch site => (site -> site) -> Middleware -> YesodExample site () -testModifySite newSiteFn middleware = do +-- ==== __Examples__ +-- +-- > post SendEmailR +-- > -- Assert email not created in database +-- > testModifySite (\site -> pure (site { siteSettingsStoreEmail = True }, id)) +-- > post SendEmailR +-- > -- Assert email created in database +-- +-- > testModifySite (\site -> do +-- > middleware <- makeLogware site +-- > pure (site { appRedisConnection = Nothing }, middleware) +-- > ) +-- +-- @since 1.6.8 +testModifySite :: YesodDispatch site + => (site -> IO (site, Middleware)) -- ^ A function from the existing site, to a new site and middleware for a WAI app. + -> YesodExample site () +testModifySite newSiteFn = do currentSite <- getTestYesod - let newSite = newSiteFn currentSite + (newSite, middleware) <- liftIO $ newSiteFn currentSite app <- liftIO $ toWaiAppPlain newSite modifySIO $ \yed -> yed { yedSite = newSite, yedApp = middleware app } diff --git a/yesod-test/test/main.hs b/yesod-test/test/main.hs index 57a93c17..f0f5b8e4 100644 --- a/yesod-test/test/main.hs +++ b/yesod-test/test/main.hs @@ -453,7 +453,7 @@ main = hspec $ do yit "can change site value" $ do get ("/get-integer" :: Text) bodyContains "0" - testModifySite (\site -> site { routedAppInteger = 1 }) id + testModifySite (\site -> pure (site { routedAppInteger = 1 }, id)) get ("/get-integer" :: Text) bodyContains "1" diff --git a/yesod-test/yesod-test.cabal b/yesod-test/yesod-test.cabal index 24ac3208..ac770758 100644 --- a/yesod-test/yesod-test.cabal +++ b/yesod-test/yesod-test.cabal @@ -1,5 +1,5 @@ name: yesod-test -version: 1.6.7 +version: 1.6.8 license: MIT license-file: LICENSE author: Nubis