diff --git a/yesod-test/Yesod/Test.hs b/yesod-test/Yesod/Test.hs index f886b399..cac92a6a 100644 --- a/yesod-test/Yesod/Test.hs +++ b/yesod-test/Yesod/Test.hs @@ -695,12 +695,15 @@ get url = request $ do setUrl url -- | Follow a redirect, if the last response was a redirect. +-- | Return Left with an error message if not a redirect +-- | Return Right with the redirected URL if it was. +-- -- ==== __Examples__ -- > get HomeR -- -- > followRedirect followRedirect :: Yesod site - => YesodExample site (Either T.Text ()) + => YesodExample site (Either T.Text T.Text) followRedirect = do mr <- getResponse case mr of @@ -711,7 +714,8 @@ followRedirect = do else do case lookup "Location" (simpleHeaders r) of Nothing -> return $ Left "followRedirect called, but no location header set" - Just h -> get (TE.decodeUtf8 h) >> return (Right ()) + Just h -> let url = TE.decodeUtf8 h in + get url >> return (Right url) -- | Sets the HTTP method used by the request. -- diff --git a/yesod-test/test/main.hs b/yesod-test/test/main.hs index 3cee06be..0ef4354c 100644 --- a/yesod-test/test/main.hs +++ b/yesod-test/test/main.hs @@ -20,7 +20,7 @@ import Data.Monoid ((<>)) import Control.Applicative import Network.Wai (pathInfo, requestHeaders) import Data.Maybe (fromMaybe) -import Data.Either (isLeft) +import Data.Either (isLeft, isRight) import Control.Exception.Lifted(try, SomeException) import Data.ByteString.Lazy.Char8 () @@ -219,14 +219,16 @@ main = hspec $ do yit "follows 303 redirects when requested" $ do get ("/redirect303" :: Text) statusIs 303 - followRedirect + r <- followRedirect + liftIO $ assertBool "expected a Right from a 303 redirect" $ isRight r statusIs 200 bodyContains "we have been successfully redirected" yit "follows 301 redirects when requested" $ do get ("/redirect301" :: Text) statusIs 301 - followRedirect + r <- followRedirect + liftIO $ assertBool "expected a Right from a 301 redirect" $ isRight r statusIs 200 bodyContains "we have been successfully redirected" @@ -235,7 +237,7 @@ main = hspec $ do get ("/" :: Text) statusIs 200 r <- followRedirect - liftIO $ assertBool "expected exception" $ isLeft r + liftIO $ assertBool "expected a Left when not a redirect" $ isLeft r instance RenderMessage LiteApp FormMessage where renderMessage _ _ = defaultFormMessage