iothunk and fixed some tests
This commit is contained in:
parent
a0cd0b5008
commit
71b62e0363
@ -8,6 +8,7 @@ import Test.Framework (testGroup, Test)
|
|||||||
import Test.Framework.Providers.HUnit
|
import Test.Framework.Providers.HUnit
|
||||||
import Test.HUnit hiding (Test)
|
import Test.HUnit hiding (Test)
|
||||||
import Data.List
|
import Data.List
|
||||||
|
import Network.Wai (Method (..))
|
||||||
|
|
||||||
data MyYesod = MyYesod
|
data MyYesod = MyYesod
|
||||||
|
|
||||||
@ -19,7 +20,7 @@ addHead' x = (cs "", x)
|
|||||||
addHead :: Monad m => HtmlObject -> m (Html, HtmlObject)
|
addHead :: Monad m => HtmlObject -> m (Html, HtmlObject)
|
||||||
addHead = return . addHead'
|
addHead = return . addHead'
|
||||||
|
|
||||||
getStatic :: Verb -> [String] -> Handler MyYesod (Html, HtmlObject)
|
getStatic :: Method -> [String] -> Handler MyYesod (Html, HtmlObject)
|
||||||
getStatic v p = addHead $ toHtmlObject ["getStatic", show v, show p]
|
getStatic v p = addHead $ toHtmlObject ["getStatic", show v, show p]
|
||||||
pageIndex :: Handler MyYesod (Html, HtmlObject)
|
pageIndex :: Handler MyYesod (Html, HtmlObject)
|
||||||
pageIndex = addHead $ toHtmlObject ["pageIndex"]
|
pageIndex = addHead $ toHtmlObject ["pageIndex"]
|
||||||
@ -38,26 +39,26 @@ userVariable i s = addHead $ toHtmlObject ["userVariable", show i, s]
|
|||||||
userPage :: Integer -> [String] -> Handler MyYesod (Html, HtmlObject)
|
userPage :: Integer -> [String] -> Handler MyYesod (Html, HtmlObject)
|
||||||
userPage i p = addHead $ toHtmlObject ["userPage", show i, show p]
|
userPage i p = addHead $ toHtmlObject ["userPage", show i, show p]
|
||||||
|
|
||||||
instance Show (Verb -> Handler MyYesod ChooseRep) where
|
instance Show (Method -> Handler MyYesod ChooseRep) where
|
||||||
show _ = "verb -> handler"
|
show _ = "verb -> handler"
|
||||||
instance Show (Resource -> Verb -> Handler MyYesod ChooseRep) where
|
instance Show (Resource -> Method -> Handler MyYesod ChooseRep) where
|
||||||
show _ = "resource -> verb -> handler"
|
show _ = "resource -> verb -> handler"
|
||||||
handler :: Resource -> Verb -> Handler MyYesod ChooseRep
|
handler :: Resource -> Method -> Handler MyYesod ChooseRep
|
||||||
handler = [$mkResources|
|
handler = [$mkResources|
|
||||||
/static/*filepath/: getStatic
|
/static/*filepath/: getStatic
|
||||||
/page/:
|
/page/:
|
||||||
Get: pageIndex
|
GET: pageIndex
|
||||||
Put: pageAdd
|
PUT: pageAdd
|
||||||
/page/$page/:
|
/page/$page/:
|
||||||
Get: pageDetail
|
GET: pageDetail
|
||||||
Delete: pageDelete
|
DELETE: pageDelete
|
||||||
Post: pageUpdate
|
POST: pageUpdate
|
||||||
/user/#id/:
|
/user/#id/:
|
||||||
Get: userInfo
|
GET: userInfo
|
||||||
/user/#id/profile/$variable/:
|
/user/#id/profile/$variable/:
|
||||||
Get: userVariable
|
GET: userVariable
|
||||||
/user/#id/page/*page/:
|
/user/#id/page/*page/:
|
||||||
Get: userPage
|
GET: userPage
|
||||||
|]
|
|]
|
||||||
|
|
||||||
ph :: [String] -> Handler MyYesod ChooseRep -> Assertion
|
ph :: [String] -> Handler MyYesod ChooseRep -> Assertion
|
||||||
@ -71,7 +72,8 @@ ph ss h = do
|
|||||||
mapM_ (helper res') ss
|
mapM_ (helper res') ss
|
||||||
where
|
where
|
||||||
helper haystack needle =
|
helper haystack needle =
|
||||||
assertBool needle $ needle `isInfixOf` haystack
|
assertBool (show ("needle", needle, ss, haystack))
|
||||||
|
$ needle `isInfixOf` haystack
|
||||||
|
|
||||||
myShow :: Response -> IO String
|
myShow :: Response -> IO String
|
||||||
myShow (Response sc hs ct c) = runContent c >>= \c' -> return $ unlines
|
myShow (Response sc hs ct c) = runContent c >>= \c' -> return $ unlines
|
||||||
@ -83,13 +85,13 @@ myShow (Response sc hs ct c) = runContent c >>= \c' -> return $ unlines
|
|||||||
|
|
||||||
caseQuasi :: Assertion
|
caseQuasi :: Assertion
|
||||||
caseQuasi = do
|
caseQuasi = do
|
||||||
ph ["200", "foo"] $ handler ["static", "foo", "bar", "baz"] Get
|
ph ["200", "foo"] $ handler ["static", "foo", "bar", "baz"] GET
|
||||||
ph ["404"] $ handler ["foo", "bar", "baz"] Get
|
ph ["404"] $ handler ["foo", "bar", "baz"] GET
|
||||||
ph ["200", "pageIndex"] $ handler ["page"] Get
|
ph ["200", "pageIndex"] $ handler ["page"] GET
|
||||||
ph ["404"] $ handler ["user"] Get
|
ph ["404"] $ handler ["user"] GET
|
||||||
ph ["404"] $ handler ["user", "five"] Get
|
ph ["404"] $ handler ["user", "five"] GET
|
||||||
ph ["200", "userInfo", "5"] $ handler ["user", "5"] Get
|
ph ["200", "userInfo", "5"] $ handler ["user", "5"] GET
|
||||||
ph ["200", "userVar"] $ handler ["user", "5", "profile", "email"] Get
|
ph ["200", "userVar"] $ handler ["user", "5", "profile", "email"] GET
|
||||||
|
|
||||||
testSuite :: Test
|
testSuite :: Test
|
||||||
testSuite = testGroup "Test.QuasiResource"
|
testSuite = testGroup "Test.QuasiResource"
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
/static/*filepath/: getStatic
|
/static/*filepath/: getStatic
|
||||||
/page/:
|
/page/:
|
||||||
Get: pageIndex
|
GET: pageIndex
|
||||||
Put: pageAdd
|
PUT: pageAdd
|
||||||
/page/$page/:
|
/page/$page/:
|
||||||
Get: pageDetail
|
GET: pageDetail
|
||||||
Delete: pageDelete
|
DELETE: pageDelete
|
||||||
Post: pageUpdate
|
POST: pageUpdate
|
||||||
/user/#id/:
|
/user/#id/:
|
||||||
Get: userInfo
|
GET: userInfo
|
||||||
|
|||||||
@ -103,6 +103,19 @@ postParams rr = do
|
|||||||
(pp, _) <- liftIO $ rawRequestBody rr
|
(pp, _) <- liftIO $ rawRequestBody rr
|
||||||
return $ multiLookup pp
|
return $ multiLookup pp
|
||||||
|
|
||||||
|
-- | Produces a \"compute on demand\" value. The computation will be run once
|
||||||
|
-- it is requested, and then the result will be stored. This will happen only
|
||||||
|
-- once.
|
||||||
|
iothunk :: IO a -> IO (IO a)
|
||||||
|
iothunk = fmap go . newMVar . Left where
|
||||||
|
go :: MVar (Either (IO a) a) -> IO a
|
||||||
|
go mvar = modifyMVar mvar go'
|
||||||
|
go' :: Either (IO a) a -> IO (Either (IO a) a, a)
|
||||||
|
go' (Right val) = return (Right val, val)
|
||||||
|
go' (Left comp) = do
|
||||||
|
val <- comp
|
||||||
|
return (Right val, val)
|
||||||
|
|
||||||
-- | All cookies with the given name.
|
-- | All cookies with the given name.
|
||||||
cookies :: RawRequest -> ParamName -> [ParamValue]
|
cookies :: RawRequest -> ParamName -> [ParamValue]
|
||||||
cookies rr name = map snd . filter (fst `equals` name) . rawCookies $ rr
|
cookies rr name = map snd . filter (fst `equals` name) . rawCookies $ rr
|
||||||
@ -120,23 +133,20 @@ parseWaiRequest env session = do
|
|||||||
langs'' = case lookup langKey gets' of
|
langs'' = case lookup langKey gets' of
|
||||||
Nothing -> langs'
|
Nothing -> langs'
|
||||||
Just x -> x : langs'
|
Just x -> x : langs'
|
||||||
mrb <- newMVar $ Left env
|
rbthunk <- iothunk $ rbHelper env
|
||||||
return $ RawRequest gets' cookies' session (rbHelper mrb) env langs''
|
return $ RawRequest gets' cookies' session rbthunk env langs''
|
||||||
|
|
||||||
rbHelper :: MVar (Either W.Request RequestBodyContents)
|
rbHelper :: W.Request -> IO RequestBodyContents
|
||||||
-> IO RequestBodyContents
|
rbHelper env = do
|
||||||
rbHelper mvar = modifyMVar mvar helper where
|
inputLBS <- WE.toLBS $ W.requestBody env -- FIXME
|
||||||
helper (Right bc) = return (Right bc, bc)
|
let clength = maybe "0" cs $ lookup W.ReqContentLength
|
||||||
helper (Left env) = do
|
$ W.requestHeaders env
|
||||||
inputLBS <- WE.toLBS $ W.requestBody env -- FIXME
|
let ctype = maybe "" cs $ lookup W.ReqContentType $ W.requestHeaders env
|
||||||
let clength = maybe "0" cs $ lookup W.ReqContentLength
|
let convertFileInfo (FileInfo a b c) = FileInfo (cs a) (cs b) c
|
||||||
$ W.requestHeaders env
|
let ret = map (cs *** cs) ***
|
||||||
let ctype = maybe "" cs $ lookup W.ReqContentType $ W.requestHeaders env
|
map (cs *** convertFileInfo)
|
||||||
let convertFileInfo (FileInfo a b c) = FileInfo (cs a) (cs b) c
|
$ parsePost ctype clength inputLBS
|
||||||
let ret = map (cs *** cs) ***
|
return ret
|
||||||
map (cs *** convertFileInfo)
|
|
||||||
$ parsePost ctype clength inputLBS
|
|
||||||
return (Right ret, ret)
|
|
||||||
|
|
||||||
#if TEST
|
#if TEST
|
||||||
testSuite :: Test
|
testSuite :: Test
|
||||||
|
|||||||
@ -496,12 +496,12 @@ caseFromYaml = do
|
|||||||
rp4 <- readRP "user/#id"
|
rp4 <- readRP "user/#id"
|
||||||
let expected =
|
let expected =
|
||||||
[ RPNode rp1 $ AllMethods "getStatic"
|
[ RPNode rp1 $ AllMethods "getStatic"
|
||||||
, RPNode rp2 $ Methods [(Get, "pageIndex"), (Put, "pageAdd")]
|
, RPNode rp2 $ Methods [(GET, "pageIndex"), (PUT, "pageAdd")]
|
||||||
, RPNode rp3 $ Methods [ (Get, "pageDetail")
|
, RPNode rp3 $ Methods [ (GET, "pageDetail")
|
||||||
, (Delete, "pageDelete")
|
, (DELETE, "pageDelete")
|
||||||
, (Post, "pageUpdate")
|
, (POST, "pageUpdate")
|
||||||
]
|
]
|
||||||
, RPNode rp4 $ Methods [(Get, "userInfo")]
|
, RPNode rp4 $ Methods [(GET, "userInfo")]
|
||||||
]
|
]
|
||||||
contents' <- decodeFile "Test/resource-patterns.yaml"
|
contents' <- decodeFile "Test/resource-patterns.yaml"
|
||||||
contents <- convertAttemptWrap (contents' :: TextObject)
|
contents <- convertAttemptWrap (contents' :: TextObject)
|
||||||
@ -519,7 +519,7 @@ caseCheckRPNodes = do
|
|||||||
]
|
]
|
||||||
Nothing @=? checkRPNodes bad1
|
Nothing @=? checkRPNodes bad1
|
||||||
rp' <- readRP ""
|
rp' <- readRP ""
|
||||||
let bad2 = [RPNode rp' $ Methods [(Get, "foo"), (Get, "bar")]]
|
let bad2 = [RPNode rp' $ Methods [(GET, "foo"), (GET, "bar")]]
|
||||||
Nothing @=? checkRPNodes bad2
|
Nothing @=? checkRPNodes bad2
|
||||||
|
|
||||||
caseReadRP :: Assertion
|
caseReadRP :: Assertion
|
||||||
|
|||||||
@ -65,11 +65,12 @@ import qualified Data.Text.Lazy.Encoding as DTLE
|
|||||||
import Web.Encodings (formatW3)
|
import Web.Encodings (formatW3)
|
||||||
import qualified Network.Wai as W
|
import qualified Network.Wai as W
|
||||||
import qualified Network.Wai.Enumerator as WE
|
import qualified Network.Wai.Enumerator as WE
|
||||||
import Yesod.Request
|
|
||||||
|
|
||||||
#if TEST
|
#if TEST
|
||||||
|
import Yesod.Request hiding (testSuite)
|
||||||
import Data.Object.Html hiding (testSuite)
|
import Data.Object.Html hiding (testSuite)
|
||||||
#else
|
#else
|
||||||
|
import Yesod.Request
|
||||||
import Data.Object.Html
|
import Data.Object.Html
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user