add followRedirect

This commit is contained in:
Mark Wotton 2016-03-21 12:13:23 -04:00
parent 89e39464a1
commit df6834a335
3 changed files with 49 additions and 3 deletions

View File

@ -50,6 +50,7 @@ module Yesod.Test
, get , get
, post , post
, postBody , postBody
, followRedirect
, request , request
, addRequestHeader , addRequestHeader
, setMethod , setMethod
@ -693,6 +694,25 @@ get url = request $ do
setMethod "GET" setMethod "GET"
setUrl url setUrl url
-- | Follow a redirect, if the last response was a redirect.
-- ==== __Examples__
-- > get HomeR
--
-- > followRedirect
followRedirect :: Yesod site
=> YesodExample site ()
followRedirect = do
mr <- getResponse
case mr of
Nothing -> failure "no response, so no redirect to follow"
Just r -> do
if not ((H.statusCode $ simpleStatus r) `elem` [301,303])
then failure "followRedirect called, but previous request was not a redirect"
else do
case lookup "Location" (simpleHeaders r) of
Nothing -> failure "No location header set"
Just h -> get (TE.decodeUtf8 h)
-- | Sets the HTTP method used by the request. -- | Sets the HTTP method used by the request.
-- --
-- ==== __Examples__ -- ==== __Examples__

View File

@ -20,11 +20,13 @@ import Data.Monoid ((<>))
import Control.Applicative import Control.Applicative
import Network.Wai (pathInfo, requestHeaders) import Network.Wai (pathInfo, requestHeaders)
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
import Data.Either (isLeft)
import Control.Monad.Catch (try)
import Data.ByteString.Lazy.Char8 () import Data.ByteString.Lazy.Char8 ()
import qualified Data.Map as Map import qualified Data.Map as Map
import qualified Text.HTML.DOM as HD import qualified Text.HTML.DOM as HD
import Network.HTTP.Types.Status (unsupportedMediaType415) import Network.HTTP.Types.Status (status301, status303, unsupportedMediaType415)
parseQuery_ = either error id . parseQuery parseQuery_ = either error id . parseQuery
findBySelector_ x = either error id . findBySelector x findBySelector_ x = either error id . findBySelector x
@ -213,8 +215,28 @@ main = hspec $ do
setMethod "POST" setMethod "POST"
setUrl ("/" :: Text) setUrl ("/" :: Text)
statusIs 403 statusIs 403
describe "test redirects" $ yesodSpec app $ do
yit "follows 303 redirects when requested" $ do
get ("/redirect303" :: Text)
statusIs 303
followRedirect
statusIs 200
bodyContains "we have been successfully redirected"
yit "follows 301 redirects when requested" $ do
get ("/redirect301" :: Text)
statusIs 301
followRedirect
statusIs 200
bodyContains "we have been successfully redirected"
yit "throws an exception when no redirect was returned" $ do
get ("/" :: Text)
statusIs 200
r <- followRedirect
statusIs 200
-- assertBool "expected exception" $ isLeft r
instance RenderMessage LiteApp FormMessage where instance RenderMessage LiteApp FormMessage where
renderMessage _ _ = defaultFormMessage renderMessage _ _ = defaultFormMessage
@ -235,6 +257,9 @@ app = liteApp $ do
case mfoo of case mfoo of
Nothing -> error "No foo" Nothing -> error "No foo"
Just foo -> return foo Just foo -> return foo
onStatic "redirect301" $ dispatchTo $ redirectWith status301 ("/redirectTarget" :: Text) >> return ()
onStatic "redirect303" $ dispatchTo $ redirectWith status303 ("/redirectTarget" :: Text) >> return ()
onStatic "redirectTarget" $ dispatchTo $ return ("we have been successfully redirected" :: Text)
onStatic "form" $ dispatchTo $ do onStatic "form" $ dispatchTo $ do
((mfoo, widget), _) <- runFormPost ((mfoo, widget), _) <- runFormPost
$ renderDivs $ renderDivs
@ -290,4 +315,4 @@ postHomeR = defaultLayout
[whamlet| [whamlet|
<p> <p>
Welcome to my test application. Welcome to my test application.
|] |]

View File

@ -4,7 +4,7 @@ license: MIT
license-file: LICENSE license-file: LICENSE
author: Nubis <nubis@woobiz.com.ar> author: Nubis <nubis@woobiz.com.ar>
maintainer: Michael Snoyman, Greg Weber, Nubis <nubis@woobiz.com.ar> maintainer: Michael Snoyman, Greg Weber, Nubis <nubis@woobiz.com.ar>
synopsis: integration testing for WAI/Yesod Applications synopsis: integration testing for WAI/Yesod Applications
category: Web, Yesod, Testing category: Web, Yesod, Testing
stability: Experimental stability: Experimental
cabal-version: >= 1.8 cabal-version: >= 1.8
@ -60,6 +60,7 @@ test-suite test
, yesod-form , yesod-form
, text , text
, wai , wai
, exceptions
, http-types , http-types
source-repository head source-repository head