-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
99 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import Test.Api qualified as Api | ||
import Test.Server.Hello qualified as Server.Hello | ||
|
||
import Test.Hspec | ||
|
||
main :: IO () | ||
main = | ||
hspec $ Api.spec | ||
hspec $ do | ||
Api.spec | ||
Server.Hello.spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
module Test.Server.Hello (spec) where | ||
|
||
import Data.Aeson qualified as Json | ||
import Data.Map.Strict qualified as Map | ||
import Data.Text (Text) | ||
import Mig.Core | ||
import Mig.Core qualified as Request (Request (..)) | ||
import Network.HTTP.Types.Method (methodGet, methodPost) | ||
import Network.HTTP.Types.Status (ok200) | ||
import Test.Hspec | ||
|
||
-- hello world server | ||
|
||
server :: Server IO | ||
server = | ||
"api/v1" | ||
/. [ "hello" /. handleHello | ||
, "bye" /. handleBye | ||
] | ||
|
||
handleHello :: Get IO (Resp Json Text) | ||
handleHello = pure $ ok "hello" | ||
|
||
handleBye :: Get IO (Resp Json Text) | ||
handleBye = pure $ ok "bye" | ||
|
||
-- tests | ||
|
||
-- we use low-level representation of server as a function: Request -> m (Maybe Response) | ||
-- to check server properties without wpawning a full server environment | ||
spec :: Spec | ||
spec = describe "hello world server" $ do | ||
describe "plain route finder" $ specBy plainApiStrategy | ||
describe "tree route finder" $ specBy treeApiStrategy | ||
|
||
specBy :: FindRoute nf IO -> Spec | ||
specBy finder = do | ||
checkPositiveRoutes | ||
checkNegativeRoutes | ||
where | ||
serverFun :: ServerFun IO | ||
serverFun = fromServer finder server | ||
|
||
checkPositiveRoutes = do | ||
it "call routes (positive case)" $ do | ||
serverFun helloReq `shouldReturn` helloResp | ||
serverFun byeReq `shouldReturn` byeResp | ||
|
||
checkNegativeRoutes = do | ||
describe "negative cases" $ do | ||
it "wrong path" $ do | ||
serverFun emptyReq `shouldReturn` Nothing | ||
serverFun wrongPathReq `shouldReturn` Nothing | ||
it "wrong method" $ do | ||
serverFun (helloReq{Request.method = methodPost}) `shouldReturn` Nothing | ||
it "wrong output media type" $ do | ||
serverFun (helloReq{Request.headers = Map.fromList [("Accept", "text/html")]}) `shouldReturn` Nothing | ||
|
||
helloReq = emptyReq{path = ["api", "v1", "hello"]} | ||
helloResp = Just $ jsonResp @Text "hello" | ||
|
||
byeReq = emptyReq{path = ["api", "v1", "bye"]} | ||
byeResp = Just $ jsonResp @Text "bye" | ||
|
||
wrongPathReq = emptyReq{path = ["api", "v2", "hello"]} | ||
|
||
emptyReq :: Request | ||
emptyReq = | ||
Request | ||
{ path = [] | ||
, query = mempty | ||
, capture = mempty | ||
, headers = Map.fromList [("Accept", "application/json")] | ||
, method = methodGet | ||
, readBody = pure (Right "") | ||
, isSecure = False | ||
} | ||
|
||
jsonResp :: (Json.ToJSON a) => a -> Response | ||
jsonResp a = | ||
Response | ||
{ status = ok200 | ||
, headers = [("Content-Type", "application/json")] | ||
, body = RawResp "application/json" (Json.encode a) | ||
} |