blob: 6a25ec50decea959ec55e60bed9f8c28f3b60681 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Minilang.ServerSpec where
import Data.Aeson (eitherDecode, FromJSON)
import Minilang.API (Score)
import Minilang.Server (serveAPI)
import Network.Wai (Application)
import Test.Hspec (Spec, it)
import Test.Hspec.Wai (get, matchBody, shouldRespondWith, with)
import Test.Hspec.Wai.Matcher (MatchBody (..))
import Data.Data (Proxy (..))
spec :: Spec
spec = with app $
it "GET /api/leaderboard returns empty list of scores" $ do
get "/api/leaderboard"
`shouldRespondWith` 200 {matchBody = arrayOfLengthOf (Proxy @Score) 2}
arrayOfLengthOf :: forall elem proxy . (FromJSON elem) => proxy elem -> Int -> MatchBody
arrayOfLengthOf _ len = MatchBody $ \_ actual ->
case eitherDecode actual of
Right (scores :: [elem]) ->
if length scores == len
then Nothing
else Just $ "Expected array of length " ++ show len ++ ", but got " ++ show (length scores)
Left err -> Just $ "Failed to decode JSON: " ++ err
app :: IO Application
app = pure serveAPI
|