WIP - Add testModifySite
This commit is contained in:
parent
0d4c435e42
commit
e39eaeef92
@ -45,6 +45,9 @@ module Yesod.Test
|
|||||||
, ydescribe
|
, ydescribe
|
||||||
, yit
|
, yit
|
||||||
|
|
||||||
|
-- * Modify test site
|
||||||
|
, testModifySite
|
||||||
|
|
||||||
-- * Modify test state
|
-- * Modify test state
|
||||||
, testSetCookie
|
, testSetCookie
|
||||||
, testDeleteCookie
|
, testDeleteCookie
|
||||||
@ -341,6 +344,18 @@ yesodSpecApp site getApp yspecs =
|
|||||||
yit :: String -> YesodExample site () -> YesodSpec site
|
yit :: String -> YesodExample site () -> YesodSpec site
|
||||||
yit label example = tell [YesodSpecItem label example]
|
yit label example = tell [YesodSpecItem label example]
|
||||||
|
|
||||||
|
-- | Modifies the site ('yedSite') of the test
|
||||||
|
--
|
||||||
|
-- TODO documentation here
|
||||||
|
--
|
||||||
|
-- TODO @since
|
||||||
|
testModifySite :: YesodDispatch site => (site -> site) -> Middleware -> YesodExample site ()
|
||||||
|
testModifySite newSiteFn middleware = do
|
||||||
|
currentSite <- getTestYesod
|
||||||
|
let newSite = newSiteFn currentSite
|
||||||
|
app <- liftIO $ toWaiAppPlain newSite
|
||||||
|
modifySIO $ \yed -> yed { yedSite = newSite, yedApp = middleware app }
|
||||||
|
|
||||||
-- | Sets a cookie
|
-- | Sets a cookie
|
||||||
--
|
--
|
||||||
-- ==== __Examples__
|
-- ==== __Examples__
|
||||||
|
|||||||
@ -42,6 +42,7 @@ import Network.HTTP.Types.Status (status301, status303, status403, status422, un
|
|||||||
import UnliftIO.Exception (tryAny, SomeException, try)
|
import UnliftIO.Exception (tryAny, SomeException, try)
|
||||||
import qualified Web.Cookie as Cookie
|
import qualified Web.Cookie as Cookie
|
||||||
import Data.Maybe (isNothing)
|
import Data.Maybe (isNothing)
|
||||||
|
import qualified Data.Text as T
|
||||||
|
|
||||||
parseQuery_ :: Text -> [[SelectorGroup]]
|
parseQuery_ :: Text -> [[SelectorGroup]]
|
||||||
parseQuery_ = either error id . parseQuery
|
parseQuery_ = either error id . parseQuery
|
||||||
@ -49,14 +50,24 @@ parseQuery_ = either error id . parseQuery
|
|||||||
findBySelector_ :: HtmlLBS -> Query -> [String]
|
findBySelector_ :: HtmlLBS -> Query -> [String]
|
||||||
findBySelector_ x = either error id . findBySelector x
|
findBySelector_ x = either error id . findBySelector x
|
||||||
|
|
||||||
data RoutedApp = RoutedApp
|
data RoutedApp = RoutedApp { routedAppInteger :: Integer }
|
||||||
|
|
||||||
|
defaultRoutedApp :: RoutedApp
|
||||||
|
defaultRoutedApp = RoutedApp 0
|
||||||
|
|
||||||
mkYesod "RoutedApp" [parseRoutes|
|
mkYesod "RoutedApp" [parseRoutes|
|
||||||
/ HomeR GET POST
|
/ HomeR GET POST
|
||||||
/resources ResourcesR POST
|
/resources ResourcesR POST
|
||||||
/resources/#Text ResourceR GET
|
/resources/#Text ResourceR GET
|
||||||
|
/get-integer IntegerR GET
|
||||||
|]
|
|]
|
||||||
|
|
||||||
|
-- data ParamaterizedApp = ParamaterizedApp { parameterizedAppInteger :: Integer }
|
||||||
|
|
||||||
|
-- mkYesod "ParamaterizedApp" [parseRoutes|
|
||||||
|
-- /get-integer ParameterizedGetInteger GET
|
||||||
|
-- |]
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = hspec $ do
|
main = hspec $ do
|
||||||
describe "CSS selector parsing" $ do
|
describe "CSS selector parsing" $ do
|
||||||
@ -378,7 +389,7 @@ main = hspec $ do
|
|||||||
testModifyCookies (\_ -> Map.empty)
|
testModifyCookies (\_ -> Map.empty)
|
||||||
get ("cookie/check-no-cookie" :: Text)
|
get ("cookie/check-no-cookie" :: Text)
|
||||||
statusIs 200
|
statusIs 200
|
||||||
describe "CSRF with cookies/headers" $ yesodSpec RoutedApp $ do
|
describe "CSRF with cookies/headers" $ yesodSpec defaultRoutedApp $ do
|
||||||
yit "Should receive a CSRF cookie and add its value to the headers" $ do
|
yit "Should receive a CSRF cookie and add its value to the headers" $ do
|
||||||
get ("/" :: Text)
|
get ("/" :: Text)
|
||||||
statusIs 200
|
statusIs 200
|
||||||
@ -420,7 +431,7 @@ main = hspec $ do
|
|||||||
r <- followRedirect
|
r <- followRedirect
|
||||||
liftIO $ assertBool "expected a Left when not a redirect" $ isLeft r
|
liftIO $ assertBool "expected a Left when not a redirect" $ isLeft r
|
||||||
|
|
||||||
describe "route parsing in tests" $ yesodSpec RoutedApp $ do
|
describe "route parsing in tests" $ yesodSpec defaultRoutedApp $ do
|
||||||
yit "parses location header into a route" $ do
|
yit "parses location header into a route" $ do
|
||||||
-- get CSRF token
|
-- get CSRF token
|
||||||
get HomeR
|
get HomeR
|
||||||
@ -444,6 +455,14 @@ main = hspec $ do
|
|||||||
loc <- getLocation
|
loc <- getLocation
|
||||||
liftIO $ assertBool "expected a Left when not a redirect" $ isLeft loc
|
liftIO $ assertBool "expected a Left when not a redirect" $ isLeft loc
|
||||||
|
|
||||||
|
describe "modifying site value" $ yesodSpec defaultRoutedApp $ do
|
||||||
|
yit "can change site value" $ do
|
||||||
|
get ("/get-integer" :: Text)
|
||||||
|
bodyContains "0"
|
||||||
|
testModifySite (\site -> site { routedAppInteger = 1 }) id
|
||||||
|
get ("/get-integer" :: Text)
|
||||||
|
bodyContains "1"
|
||||||
|
|
||||||
describe "Basic Authentication" $ yesodSpec app $ do
|
describe "Basic Authentication" $ yesodSpec app $ do
|
||||||
yit "rejects no header" $ do
|
yit "rejects no header" $ do
|
||||||
get ("checkBasicAuth" :: Text)
|
get ("checkBasicAuth" :: Text)
|
||||||
@ -571,6 +590,9 @@ cookieApp = liteApp $ do
|
|||||||
instance Yesod RoutedApp where
|
instance Yesod RoutedApp where
|
||||||
yesodMiddleware = defaultYesodMiddleware . defaultCsrfMiddleware
|
yesodMiddleware = defaultYesodMiddleware . defaultCsrfMiddleware
|
||||||
|
|
||||||
|
-- instance Yesod ParameterizedApp where
|
||||||
|
-- yesodMiddleware = defaultYesodMiddleware . defaultCsrfMiddleware
|
||||||
|
|
||||||
getHomeR :: Handler Html
|
getHomeR :: Handler Html
|
||||||
getHomeR = defaultLayout
|
getHomeR = defaultLayout
|
||||||
[whamlet|
|
[whamlet|
|
||||||
@ -598,3 +620,8 @@ getResourceR i = defaultLayout
|
|||||||
<p>
|
<p>
|
||||||
Read item #{i}.
|
Read item #{i}.
|
||||||
|]
|
|]
|
||||||
|
|
||||||
|
getIntegerR :: Handler Text
|
||||||
|
getIntegerR = do
|
||||||
|
app <- getYesod
|
||||||
|
pure $ T.pack $ show (routedAppInteger app)
|
||||||
Loading…
Reference in New Issue
Block a user