packages feed

servant-static-th-1.0.0.1: test/Spec/ServerSpec.hs

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}

module Spec.ServerSpec where

import Data.Proxy (Proxy(Proxy))
import Network.Wai (Application)
import Servant.Server (serve)
import Test.Hspec (it)
import Test.Hspec.Wai
       (ResponseMatcher(matchHeaders), (<:>), get, shouldRespondWith,
        with)
import Test.Tasty (TestTree)
import Test.Tasty.Hspec (testSpec)

import Servant.Static.TH (createApiAndServerDecs)

import Spec.TestDirLocation (testDir)

$(createApiAndServerDecs "FrontEndApi" "testDirServer" testDir)

app :: Application
app = serve (Proxy @FrontEndApi) testDirServer

serverTestsIO :: IO TestTree
serverTestsIO =
  testSpec "server" $
    with (pure app) $ do
      it "hello.html responds correctly and is html" $
        let expectedResp =
              "Hello World\n"
                { matchHeaders = ["Content-Type" <:> "text/html;charset=utf-8"]
                }
        in get "hello.html" `shouldRespondWith` expectedResp
      it "index.html responds correctly and is html" $
        let expectedResp =
              "Index\n"
                { matchHeaders = ["Content-Type" <:> "text/html;charset=utf-8"]
                }
        in get "index.html" `shouldRespondWith` expectedResp
      it "root responds with index.html contents" $
        let expectedResp =
              "Index\n"
                { matchHeaders = ["Content-Type" <:> "text/html;charset=utf-8"]
                }
        in get "" `shouldRespondWith` expectedResp
      it "dir/inner-file.html responds correctly" $
        get "dir/inner-file.html" `shouldRespondWith` "Inner File\n"
      it "non existing file gives 404" $
        get "somefilethatdoesntexist.html" `shouldRespondWith` 404