Add more tests

This commit is contained in:
Rashad Gover 2022-07-27 09:41:27 +00:00
parent 37d6363e79
commit cfacbc6fef
3 changed files with 30 additions and 13 deletions

View File

@ -60,6 +60,8 @@ module Okapi
newEventSource,
sendEvent,
sendValue,
-- RESPONSE HELPERS
respond,
)
where
@ -280,3 +282,10 @@ bodyForm = do
-- TODO: bodyFile functions for file uploads to server?
bodyRaw :: forall m. MonadOkapi m => m LazyByteString.ByteString
bodyRaw = parseBody
respond :: forall m. MonadOkapi m => Response -> m Response
respond response = do
check1 <- methodParsed
check2 <- pathParsed
check3 <- queryParsed
if check1 && check2 && check3 then return response else skip

View File

@ -34,7 +34,10 @@ data TestRequest = TestRequest
testRequest :: TestRequest -> Wai.Test.Session Wai.Test.SResponse
testRequest TestRequest{..} =
let request = Wai.defaultRequest { Wai.requestMethod = testRequestMethod, Wai.requestHeaders = testRequestHeaders }
let request = Wai.defaultRequest
{ Wai.requestMethod = testRequestMethod
, Wai.requestHeaders = testRequestHeaders
}
simpleRequest = Wai.Test.setPath request testRequestRawPath
simpleRequestBody = testRequestBody
finalRequest = Wai.Test.SRequest simpleRequest simpleRequestBody

View File

@ -6,11 +6,13 @@ import Control.Monad.Identity
import Control.Monad.IO.Class
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import Data.Function
import Network.HTTP.Types
import Network.Wai.Test
import qualified Okapi
import Network.Wai
import Data.Text
import Data.Text.Encoding
import Okapi.Test
type Okapi = Okapi.OkapiT IO
@ -21,24 +23,26 @@ testServer = do
parser1 = do
Okapi.get
Okapi.pathSeg "todos"
return Okapi.ok
Okapi.respond Okapi.ok
parser2 = do
Okapi.get
Okapi.pathSeg "todos"
Okapi.pathSeg "completed"
return Okapi.ok
Okapi.respond Okapi.ok
parser3 = do
Okapi.get
Okapi.pathSeg "todos"
_ <- Okapi.queryParam @Text "status"
return Okapi.ok
status <- Okapi.queryParam @Text "status"
Okapi.ok
& Okapi.setResponseBody (Okapi.ResponseBodyRaw $ LBS.fromStrict $ encodeUtf8 status)
& Okapi.respond
parser4 = do
Okapi.get
Okapi.pathSeg "a"
return Okapi.ok
Okapi.respond Okapi.ok
choice
[ parser1
@ -49,22 +53,23 @@ testServer = do
testSession :: Session ()
testSession = do
(testRequest $ TestRequest methodGet [] "/todos" "")
testRequest (TestRequest methodGet [] "/todos" "")
>>= assertStatus 200
(testRequest $ TestRequest methodGet [] "/todos/completed" "")
testRequest (TestRequest methodGet [] "/todos/completed" "")
>>= assertStatus 200
(testRequest $ TestRequest methodGet [] "/todos?status=done" "")
>>= assertStatus 200
res3 <- testRequest $ TestRequest methodGet [] "/todos?status=done" ""
assertStatus 200 res3
assertBody "done" res3
(testRequest $ TestRequest methodGet [] "/todos?progress=finished" "")
testRequest (TestRequest methodGet [] "/todos?progress=finished" "")
>>= assertStatus 404
(testRequest $ TestRequest methodGet [] "/what" "")
testRequest (TestRequest methodGet [] "/what" "")
>>= assertStatus 404
(testRequest $ TestRequest methodGet [] "/a" "")
testRequest (TestRequest methodGet [] "/a" "")
>>= assertStatus 200
main :: IO ()