With the following Scotty endpoint:
myendpoint :: Text -> ScottyM ()
myendpoint info =
post "/foo/bar/:var" $ do
var :: Text <- param "var"
query :: Query <- jsonData
result <- liftIO $ retrieveResult info var query
json $ result
runApi :: IO ()
runApi = scotty 4602 $ do
myendpoint
How to test myendpoint with different possible inputs, for instance
info1 = "foo"
query1 = Query { qParam1 = "foo", qParam2 = "bar" }
var1 = "bar"
expect1 = Result { foo = "foo", bar = "bar" }
res1 = decode $ test_myendpoint info1 query1 var1
res1 `shouldBe` expect1
info2 = "baz"
query2 = Query { qParam1 = "hello", qParam2 = "there" }
var2 = "boo"
expect2 = Result { foo = "biz", bar = "dev" }
res2 = decode $ test_myendpoint info2 query2 var2
res2 `shouldBe` expect2
I'm tempted to simply run the API in another thread, then query the endpoint for real with some JSON, but is there a cleaner way to do this? (although the benefit of the above would be real end-to-end testing)
Found the answer. Need to import
hspec-waiandhspec-wai-json.Then instead of running the api directly, we create an
apifunction that returns anIO Application, and then anotherrunApifunction that actually runs the thing.Now we're free to use
with apiin the tests: