{-# LANGUAGE OverloadedStrings #-} module Hello where import Test.Tasty (TestTree, defaultMain, testGroup) import Test.Tasty.HUnit ((@?=), testCase) import Nero app1 :: Request -> Maybe Response app1 request = request ^? _GET . path . prefixed "/hello/" <&> \name -> ok $ "

Hello " <> name <> "

" -- Match a tuple of any text and an int. app2 :: Request -> Maybe Response app2 request = request ^? _GET . path . prefixed "/hello/" . split "/" . suffixed "/" . target <&> \(name,uid) -> ok $ "

Hello " <> name <> " " <> uid <> "

" -- | Named matching -- app4 :: Request -> Maybe Response -- app4 request = toMatchOf request $ _GET . path -- . prefixed "/hello/" . text "name" . "/" . text "surname" . "/" . int "uid" . suffixed "/" <&> do -- name <- ix "name" -- surname <- ix "surname" -- uid <- ix "uid" -- return . ok $ "

Hello " <> name -- <> " " -- <> surname -- <> " your id is: " -- <> show id -- <> "

" tests :: TestTree tests = testGroup "Hello" [ testCase "hello" $ run app1 "/hello/there" @?= Just (ok "

Hello there

") , testCase "hello with Int" $ run app2 "/hello/there/4" @?= Nothing , testCase "hello with Int" $ run app2 "/hello/there/4/" @?= Just (ok "

Hello there 4

") , testCase "bye" $ run app1 "/bye/there" @?= Nothing ] where run a p = a $ dummyRequest & path .~ p main :: IO () main = defaultMain tests