Beginning of new test suite
This commit is contained in:
parent
7b7cbc950b
commit
3003c9b3cd
79
Test/CleanPath.hs
Normal file
79
Test/CleanPath.hs
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
{-# LANGUAGE QuasiQuotes, TypeFamilies, TemplateHaskell, MultiParamTypeClasses #-}
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
module Test.CleanPath (cleanPathTest) where
|
||||||
|
|
||||||
|
import Yesod.Core
|
||||||
|
import Yesod.Content
|
||||||
|
import Yesod.Dispatch
|
||||||
|
|
||||||
|
import Test.Framework (defaultMain, testGroup, Test)
|
||||||
|
import Test.Framework.Providers.HUnit
|
||||||
|
import Test.HUnit hiding (Test)
|
||||||
|
import Network.Wai
|
||||||
|
import Network.Wai.Test
|
||||||
|
|
||||||
|
data Y = Y
|
||||||
|
mkYesod "Y" [$parseRoutes|
|
||||||
|
/foo FooR GET
|
||||||
|
/bar BarR GET
|
||||||
|
|]
|
||||||
|
|
||||||
|
instance Yesod Y where
|
||||||
|
approot _ = "http://test"
|
||||||
|
cleanPath _ ["bar", ""] = Right ["bar"]
|
||||||
|
cleanPath _ ["bar"] = Left ["bar", ""]
|
||||||
|
cleanPath _ s =
|
||||||
|
if corrected == s
|
||||||
|
then Right s
|
||||||
|
else Left corrected
|
||||||
|
where
|
||||||
|
corrected = filter (not . null) s
|
||||||
|
|
||||||
|
getFooR = return $ RepPlain "foo"
|
||||||
|
getBarR = return $ RepPlain "bar"
|
||||||
|
|
||||||
|
cleanPathTest :: Test
|
||||||
|
cleanPathTest = testGroup "Test.CleanPath"
|
||||||
|
[ testCase "remove trailing slash" removeTrailingSlash
|
||||||
|
, testCase "noTrailingSlash" noTrailingSlash
|
||||||
|
, testCase "add trailing slash" addTrailingSlash
|
||||||
|
, testCase "has trailing slash" hasTrailingSlash
|
||||||
|
]
|
||||||
|
|
||||||
|
runner f = toWaiApp Y >>= runSession f
|
||||||
|
defaultRequest = Request
|
||||||
|
{ pathInfo = ""
|
||||||
|
, requestHeaders = []
|
||||||
|
, queryString = ""
|
||||||
|
, requestMethod = "GET"
|
||||||
|
}
|
||||||
|
|
||||||
|
removeTrailingSlash = runner $ do
|
||||||
|
res <- request defaultRequest
|
||||||
|
{ pathInfo = "/foo/"
|
||||||
|
}
|
||||||
|
assertStatus 301 res
|
||||||
|
assertHeader "Location" "http://test/foo" res
|
||||||
|
|
||||||
|
noTrailingSlash = runner $ do
|
||||||
|
res <- request defaultRequest
|
||||||
|
{ pathInfo = "/foo"
|
||||||
|
}
|
||||||
|
assertStatus 200 res
|
||||||
|
assertContentType "text/plain; charset=utf-8" res
|
||||||
|
assertBody "foo" res
|
||||||
|
|
||||||
|
addTrailingSlash = runner $ do
|
||||||
|
res <- request defaultRequest
|
||||||
|
{ pathInfo = "/bar"
|
||||||
|
}
|
||||||
|
assertStatus 301 res
|
||||||
|
assertHeader "Location" "http://test/bar/" res
|
||||||
|
|
||||||
|
hasTrailingSlash = runner $ do
|
||||||
|
res <- request defaultRequest
|
||||||
|
{ pathInfo = "/bar/"
|
||||||
|
}
|
||||||
|
assertStatus 200 res
|
||||||
|
assertContentType "text/plain; charset=utf-8" res
|
||||||
|
assertBody "bar" res
|
||||||
@ -22,9 +22,6 @@ module Yesod.Core
|
|||||||
-- * Misc
|
-- * Misc
|
||||||
, yesodVersion
|
, yesodVersion
|
||||||
, yesodRender
|
, yesodRender
|
||||||
#if TEST
|
|
||||||
, coreTestSuite
|
|
||||||
#endif
|
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Yesod.Content
|
import Yesod.Content
|
||||||
@ -59,15 +56,6 @@ import Web.Cookie (parseCookies)
|
|||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
import Data.Time
|
import Data.Time
|
||||||
|
|
||||||
#if TEST
|
|
||||||
import Test.Framework (testGroup, Test)
|
|
||||||
import Test.Framework.Providers.HUnit
|
|
||||||
import Test.Framework.Providers.QuickCheck2 (testProperty)
|
|
||||||
import Test.HUnit hiding (Test)
|
|
||||||
import qualified Data.Text
|
|
||||||
import qualified Data.Text.Encoding
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if GHC7
|
#if GHC7
|
||||||
#define HAMLET hamlet
|
#define HAMLET hamlet
|
||||||
#else
|
#else
|
||||||
@ -486,54 +474,3 @@ yesodRender y u qs =
|
|||||||
(urlRenderOverride y u)
|
(urlRenderOverride y u)
|
||||||
where
|
where
|
||||||
(ps, qs') = renderRoute u
|
(ps, qs') = renderRoute u
|
||||||
|
|
||||||
#if TEST
|
|
||||||
coreTestSuite :: Test
|
|
||||||
coreTestSuite = testGroup "Yesod.Yesod"
|
|
||||||
[ testProperty "join/split path" propJoinSplitPath
|
|
||||||
, testCase "join/split path [\".\"]" caseJoinSplitPathDquote
|
|
||||||
, testCase "utf8 split path" caseUtf8SplitPath
|
|
||||||
, testCase "utf8 join path" caseUtf8JoinPath
|
|
||||||
]
|
|
||||||
|
|
||||||
data TmpYesod = TmpYesod
|
|
||||||
data TmpRoute = TmpRoute deriving Eq
|
|
||||||
type instance Route TmpYesod = TmpRoute
|
|
||||||
instance Yesod TmpYesod where approot _ = ""
|
|
||||||
|
|
||||||
fromString :: String -> S8.ByteString
|
|
||||||
fromString = Data.Text.Encoding.encodeUtf8 . Data.Text.pack
|
|
||||||
|
|
||||||
propJoinSplitPath :: [String] -> Bool
|
|
||||||
propJoinSplitPath ss =
|
|
||||||
splitPath TmpYesod (fromString $ joinPath TmpYesod "" ss' [])
|
|
||||||
== Right ss'
|
|
||||||
where
|
|
||||||
ss' = filter (not . null) ss
|
|
||||||
|
|
||||||
caseJoinSplitPathDquote :: Assertion
|
|
||||||
caseJoinSplitPathDquote = do
|
|
||||||
splitPath TmpYesod (fromString "/x%2E/") @?= Right ["x."]
|
|
||||||
splitPath TmpYesod (fromString "/y./") @?= Right ["y."]
|
|
||||||
joinPath TmpYesod "" ["z."] [] @?= "/z./"
|
|
||||||
x @?= Right ss
|
|
||||||
where
|
|
||||||
x = splitPath TmpYesod (fromString $ joinPath TmpYesod "" ss' [])
|
|
||||||
ss' = filter (not . null) ss
|
|
||||||
ss = ["a."]
|
|
||||||
|
|
||||||
caseUtf8SplitPath :: Assertion
|
|
||||||
caseUtf8SplitPath = do
|
|
||||||
Right ["שלום"] @=?
|
|
||||||
splitPath TmpYesod (fromString "/שלום/")
|
|
||||||
Right ["page", "Fooé"] @=?
|
|
||||||
splitPath TmpYesod (fromString "/page/Fooé/")
|
|
||||||
Right ["\156"] @=?
|
|
||||||
splitPath TmpYesod (fromString "/\156/")
|
|
||||||
Right ["ð"] @=?
|
|
||||||
splitPath TmpYesod (fromString "/%C3%B0/")
|
|
||||||
|
|
||||||
caseUtf8JoinPath :: Assertion
|
|
||||||
caseUtf8JoinPath = do
|
|
||||||
"/%D7%A9%D7%9C%D7%95%D7%9D/" @=? joinPath TmpYesod "" ["שלום"] []
|
|
||||||
#endif
|
|
||||||
|
|||||||
@ -18,9 +18,6 @@ module Yesod.Dispatch
|
|||||||
-- * Convert to WAI
|
-- * Convert to WAI
|
||||||
, toWaiApp
|
, toWaiApp
|
||||||
, toWaiAppPlain
|
, toWaiAppPlain
|
||||||
#if TEST
|
|
||||||
, dispatchTestSuite
|
|
||||||
#endif
|
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Prelude hiding (exp)
|
import Prelude hiding (exp)
|
||||||
@ -46,13 +43,6 @@ import Data.Char (isUpper)
|
|||||||
|
|
||||||
import Web.Routes (decodePathInfo)
|
import Web.Routes (decodePathInfo)
|
||||||
|
|
||||||
#if TEST
|
|
||||||
import Test.Framework (testGroup, Test)
|
|
||||||
import Test.Framework.Providers.QuickCheck2 (testProperty)
|
|
||||||
import Test.QuickCheck
|
|
||||||
import System.IO.Unsafe
|
|
||||||
#endif
|
|
||||||
|
|
||||||
-- | Generates URL datatype and site function for the given 'Resource's. This
|
-- | Generates URL datatype and site function for the given 'Resource's. This
|
||||||
-- is used for creating sites, /not/ subsites. See 'mkYesodSub' for the latter.
|
-- is used for creating sites, /not/ subsites. See 'mkYesodSub' for the latter.
|
||||||
-- Use 'parseRoutes' to create the 'Resource's.
|
-- Use 'parseRoutes' to create the 'Resource's.
|
||||||
@ -181,10 +171,10 @@ toWaiApp' :: (Yesod y, YesodDispatch y y)
|
|||||||
-> Maybe Key
|
-> Maybe Key
|
||||||
-> W.Application
|
-> W.Application
|
||||||
toWaiApp' y key' env = do
|
toWaiApp' y key' env = do
|
||||||
let segments =
|
let dropSlash ('/':x) = x
|
||||||
case decodePathInfo $ B.unpack $ W.pathInfo env of
|
dropSlash x = x
|
||||||
"":x -> x
|
let segments = decodePathInfo $ dropSlash $ B.unpack $ W.pathInfo env
|
||||||
x -> x
|
-- FIXME cleanPath will not force redirect if yesodDispatch likes its arguments
|
||||||
case yesodDispatch y key' segments y id of
|
case yesodDispatch y key' segments y id of
|
||||||
Just app -> app env
|
Just app -> app env
|
||||||
Nothing ->
|
Nothing ->
|
||||||
@ -203,32 +193,3 @@ toWaiApp' y key' env = do
|
|||||||
[ ("Content-Type", "text/plain")
|
[ ("Content-Type", "text/plain")
|
||||||
, ("Location", B.pack $ dest')
|
, ("Location", B.pack $ dest')
|
||||||
] "Redirecting"
|
] "Redirecting"
|
||||||
|
|
||||||
#if TEST
|
|
||||||
|
|
||||||
dispatchTestSuite :: Test
|
|
||||||
dispatchTestSuite = testGroup "Yesod.Dispatch"
|
|
||||||
[ testProperty "encode/decode session" propEncDecSession
|
|
||||||
, testProperty "get/put time" propGetPutTime
|
|
||||||
]
|
|
||||||
|
|
||||||
propEncDecSession :: [(String, String)] -> Bool
|
|
||||||
propEncDecSession session' = unsafePerformIO $ do
|
|
||||||
key <- getDefaultKey
|
|
||||||
now <- getCurrentTime
|
|
||||||
let expire = addUTCTime 1 now
|
|
||||||
let rhost = B.pack "some host"
|
|
||||||
let val = encodeSession key expire rhost session'
|
|
||||||
return $ Just session' == decodeSession key now rhost val
|
|
||||||
|
|
||||||
propGetPutTime :: UTCTime -> Bool
|
|
||||||
propGetPutTime t = Right t == runGet getTime (runPut $ putTime t)
|
|
||||||
|
|
||||||
instance Arbitrary UTCTime where
|
|
||||||
arbitrary = do
|
|
||||||
a <- arbitrary
|
|
||||||
b <- arbitrary
|
|
||||||
return $ addUTCTime (fromRational b)
|
|
||||||
$ UTCTime (ModifiedJulianDay a) 0
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|||||||
@ -98,9 +98,6 @@ module Yesod.Handler
|
|||||||
, handlerToYAR
|
, handlerToYAR
|
||||||
, yarToResponse
|
, yarToResponse
|
||||||
, headerToPair
|
, headerToPair
|
||||||
#if TEST
|
|
||||||
, handlerTestSuite
|
|
||||||
#endif
|
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Prelude hiding (catch)
|
import Prelude hiding (catch)
|
||||||
@ -136,10 +133,6 @@ import Data.ByteString (ByteString)
|
|||||||
import Data.Enumerator (Iteratee (..))
|
import Data.Enumerator (Iteratee (..))
|
||||||
import Network.Wai.Parse (parseHttpAccept)
|
import Network.Wai.Parse (parseHttpAccept)
|
||||||
|
|
||||||
#if TEST
|
|
||||||
import Test.Framework (testGroup, Test)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
import Yesod.Content
|
import Yesod.Content
|
||||||
import Data.Maybe (fromMaybe)
|
import Data.Maybe (fromMaybe)
|
||||||
import Web.Cookie (SetCookie (..), renderSetCookie)
|
import Web.Cookie (SetCookie (..), renderSetCookie)
|
||||||
@ -650,15 +643,6 @@ lookupSession n = GHandler $ do
|
|||||||
getSession :: Monad mo => GGHandler s m mo SessionMap
|
getSession :: Monad mo => GGHandler s m mo SessionMap
|
||||||
getSession = liftM ghsSession $ GHandler $ lift $ lift $ lift get
|
getSession = liftM ghsSession $ GHandler $ lift $ lift $ lift get
|
||||||
|
|
||||||
#if TEST
|
|
||||||
|
|
||||||
handlerTestSuite :: Test
|
|
||||||
handlerTestSuite = testGroup "Yesod.Handler"
|
|
||||||
[
|
|
||||||
]
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
handlerToYAR :: (HasReps a, HasReps b)
|
handlerToYAR :: (HasReps a, HasReps b)
|
||||||
=> m -- ^ master site foundation
|
=> m -- ^ master site foundation
|
||||||
-> s -- ^ sub site foundation
|
-> s -- ^ sub site foundation
|
||||||
|
|||||||
@ -1,12 +1,7 @@
|
|||||||
import Test.Framework (defaultMain)
|
import Test.Framework (defaultMain)
|
||||||
|
import Test.CleanPath
|
||||||
import Yesod.Content
|
|
||||||
import Yesod.Dispatch
|
|
||||||
import Yesod.Handler
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = defaultMain
|
main = defaultMain
|
||||||
[ contentTestSuite
|
[ cleanPathTest
|
||||||
, dispatchTestSuite
|
|
||||||
, handlerTestSuite
|
|
||||||
]
|
]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user