-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic tests, add test support in CI
- Loading branch information
Showing
7 changed files
with
100 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ jobs: | |
ghc-version: ${{ matrix.ghc }} | ||
cabal-version: ${{ matrix.cabal }} | ||
- name: Configure main repo | ||
run: cabal new-configure --disable-optimization | ||
run: cabal new-configure --disable-optimization --enable-tests | ||
- name: Freeze | ||
run: cabal freeze | ||
- uses: actions/[email protected] | ||
|
@@ -46,3 +46,5 @@ jobs: | |
cabal new-build || { # macOS + TH = QQ | ||
cabal clean && cabal new-build | ||
} | ||
- name: Run tests | ||
run: cabal new-test --test-show-details=direct |
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,36 +1,7 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Main (main) where | ||
|
||
import Jsonifier (toByteString) | ||
import Shields (parseResponse, renderResponse) | ||
import Snap.Core | ||
( Snap, | ||
finishWith, | ||
getResponse, | ||
getsRequest, | ||
ifTop, | ||
modifyResponse, | ||
rqQueryParams, | ||
setContentType, | ||
setResponseStatus, | ||
writeBS, | ||
) | ||
import Shields.Api (api) | ||
import Snap.Http.Server (defaultConfig, httpServe) | ||
|
||
main :: IO () | ||
main = httpServe defaultConfig api | ||
|
||
-- Helpers | ||
|
||
api :: Snap () | ||
api = ifTop $ do | ||
queryParams <- getsRequest rqQueryParams | ||
case parseResponse queryParams of | ||
Nothing -> do | ||
modifyResponse . setResponseStatus 500 $ "Invalid badge request" | ||
getResponse >>= finishWith | ||
Just sr -> do | ||
writeBS . toByteString . renderResponse $ sr | ||
resp <- getResponse | ||
finishWith . setContentType "application/json" $ resp |
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,31 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Shields.Api (api) where | ||
|
||
import Jsonifier (toByteString) | ||
import Shields.Response (parseResponse, renderResponse) | ||
import Snap.Core | ||
( Snap, | ||
finishWith, | ||
getResponse, | ||
getsRequest, | ||
ifTop, | ||
modifyResponse, | ||
rqQueryParams, | ||
setContentType, | ||
setResponseStatus, | ||
writeBS, | ||
) | ||
|
||
-- | @since 1.0.0 | ||
api :: Snap () | ||
api = ifTop $ do | ||
queryParams <- getsRequest rqQueryParams | ||
case parseResponse queryParams of | ||
Nothing -> do | ||
modifyResponse . setResponseStatus 500 $ "Invalid badge request" | ||
getResponse >>= finishWith | ||
Just sr -> do | ||
writeBS . toByteString . renderResponse $ sr | ||
resp <- getResponse | ||
finishWith . setContentType "application/json" $ resp |
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,40 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Main (main) where | ||
|
||
import Data.CaseInsensitive (mk) | ||
import qualified Data.Map.Strict as M | ||
import Shields.Api (api) | ||
import Snap.Core (getHeader, rspStatus, rspStatusReason) | ||
import Snap.Test (RequestBuilder, get, runHandler) | ||
import Test.Tasty (TestTree, defaultMain, testGroup) | ||
import Test.Tasty.HUnit (assertEqual, testCase) | ||
|
||
main :: IO () | ||
main = defaultMain tests | ||
|
||
tests :: TestTree | ||
tests = | ||
testGroup | ||
"Responses" | ||
[ testCase "Get, /, no query params" $ do | ||
resp <- runHandler getTopLevelNoQP api | ||
assertEqual "Status code is 500" 500 . rspStatus $ resp | ||
assertEqual "Explains that badge request is invalid" "Invalid badge request" | ||
. rspStatusReason | ||
$ resp, | ||
testCase "Get, /, label only" $ do | ||
resp <- runHandler getTopLevelLabelOnly api | ||
assertEqual "Status code is 200" 200 . rspStatus $ resp | ||
assertEqual "Content type is JSON" (Just "application/json") | ||
. getHeader (mk "content-type") | ||
$ resp | ||
] | ||
|
||
-- Helpers | ||
|
||
getTopLevelNoQP :: RequestBuilder IO () | ||
getTopLevelNoQP = get "/" M.empty | ||
|
||
getTopLevelLabelOnly :: RequestBuilder IO () | ||
getTopLevelLabelOnly = get "/" . M.singleton "label" $ ["foo"] |