Skip to content

Commit

Permalink
Add response code verification into integration DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Stachyra committed Mar 21, 2019
1 parent 7b96ef6 commit f290b69
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
1 change: 1 addition & 0 deletions cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ test-suite integration
, generic-lens
, hspec
, hspec-core
, hspec-expectations-lifted
, http-client
, http-api-data
, http-types
Expand Down
50 changes: 41 additions & 9 deletions test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import Control.Concurrent.MVar

import Data.Aeson
( Value )
import Data.ByteString.Lazy
( ByteString )
import Data.Text
( Text )
import Network.HTTP.Client
( Manager, defaultManagerSettings, newManager )
( Manager, Request, Response, defaultManagerSettings, newManager )
import Network.HTTP.Types.Status
import Prelude
import System.Process
( proc, withCreateProcess )
Expand All @@ -20,14 +23,6 @@ import Test.Hspec
import qualified Data.Text as T

import Test.Integration.Framework.DSL
( Context (..)
, Scenarios
, expectError
, request
, request_
, scenario
, verify
)
import Test.Integration.Framework.Request
( RequestException (..) )

Expand All @@ -41,6 +36,9 @@ main = do
beforeAll (withWallet (newMVar . Context ())) $ do
describe "Integration test framework" dummySpec

beforeAll (dummySetup (newMVar . Context ())) $ do
describe "Test response codes" respCodesSpec

-- Runs the wallet server only. The API is not implemented yet, so this is
-- basically a placeholder until then.
withWallet :: ((Text, Manager) -> IO a) -> IO a
Expand All @@ -63,3 +61,37 @@ dummySpec = do

scenario "request_ function is always successful" $ do
request_ ("GET", "api/xyzzy") Nothing

-- Temporary test setup for testing response codes
dummySetup :: ((Text, Manager) -> IO a) -> IO a
dummySetup action = do
let baseURL = T.pack ("http://httpbin.org")
manager <- newManager defaultManagerSettings
action (baseURL, manager)

-- Exercise response codes
respCodesSpec :: Scenarios Context
respCodesSpec = do
scenario "GET; Response code 200" $ do
response <- request' ("GET", "/get") Nothing
verify (response :: Either RequestException (Request, Response ByteString))
[ expectResponseCode status200
]

scenario "GET; Response code 404" $ do
response <- request' ("GET", "/get/nothing") Nothing
verify (response :: Either RequestException (Request, Response ByteString))
[ expectResponseCode status404
]

scenario "POST; Response code 200" $ do
response <- request' ("POST", "/post") Nothing
verify (response :: Either RequestException (Request, Response ByteString))
[ expectResponseCode status200
]

scenario "POST; Response code 405" $ do
response <- request' ("POST", "/get") Nothing
verify (response :: Either RequestException (Request, Response ByteString))
[ expectResponseCode status405
]
23 changes: 21 additions & 2 deletions test/integration/Test/Integration/Framework/DSL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ module Test.Integration.Framework.DSL

-- * Steps
, request
, request'
, request_
, successfulRequest
, verify

-- * Expectations
, expectSuccess
, expectError
, expectResponseCode
, RequestException(..)

-- * Helpers
Expand All @@ -39,6 +41,8 @@ import Control.Monad.IO.Class
( MonadIO, liftIO )
import Data.Aeson.QQ
( aesonQQ )
import Data.ByteString.Lazy
( ByteString )
import Data.Function
( (&) )
import Data.List
Expand All @@ -50,14 +54,21 @@ import GHC.Generics
import Language.Haskell.TH.Quote
( QuasiQuoter )
import Network.HTTP.Client
( Manager )
( Manager
, Request
, Response
, responseStatus
)
import Network.HTTP.Types.Status

import Test.Hspec.Core.Spec
( SpecM, it, xit )
import Test.Hspec.Expectations.Lifted

import qualified Test.Hspec.Core.Spec as H

import Test.Integration.Framework.Request
( RequestException (..), request, request_, successfulRequest, ($-) )
( RequestException (..), request, request', request_, successfulRequest, ($-) )
import Test.Integration.Framework.Scenario
( Scenario )
import Web.HttpApiData
Expand Down Expand Up @@ -120,6 +131,14 @@ expectError = \case
Left _ -> return ()
Right a -> wantedErrorButSuccess a

expectResponseCode
:: (MonadIO m, MonadFail m)
=> Status
-> Either RequestException (Request, Response ByteString)
-> m ()
expectResponseCode c = \case
Left e -> wantedSuccessButError e
Right a -> (responseStatus (snd a)) `shouldBe` c

-- | Expect a successful response, without any further assumptions
expectSuccess
Expand Down
1 change: 1 addition & 0 deletions test/integration/Test/Integration/Framework/Request.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

module Test.Integration.Framework.Request
( request
, request'
, request_
, successfulRequest
, RequestException(..)
Expand Down

0 comments on commit f290b69

Please sign in to comment.