http-test 0.2.0.3 → 0.2.1.0
raw patch · 2 files changed
+26/−6 lines, 2 filesdep +time
Dependencies added: time
Files
- http-test.cabal +2/−1
- src/Test/HTTP.hs +24/−5
http-test.cabal view
@@ -1,5 +1,5 @@ Name: http-test-Version: 0.2.0.3+Version: 0.2.1.0 synopsis: Test framework for HTTP APIs Description: A simple framework for making assertions about the responses of HTTP servers.@@ -43,6 +43,7 @@ , bytestring , lens , http-client+ , time executable test-http-test-bayeshive main-is: test.hs
src/Test/HTTP.hs view
@@ -1,4 +1,4 @@-module Test.HTTP (httpTestCase, get, getJSON, withJSON, post, postJSON, postForm, assert, assertEq, assertParse, debug, Session, Url, Tasty.defaultMain) where+module Test.HTTP (httpTestCase, get, getJSON, withJSON, post, postJSON, postForm, assert, assertEq, assertParse, debug, Session, Url, Tasty.defaultMain, tic, toc) where import Control.Monad import Control.Monad.Error@@ -22,6 +22,7 @@ import qualified Network.Wreq.Types as WreqT import Control.Lens import Data.Aeson.Lens+import Data.Time import qualified Network.HTTP.Client as HT @@ -30,7 +31,8 @@ type Session = S.StateT HttpTest IO data HttpTest = HttpTest { baseUrl :: String,- cookieJar :: HT.CookieJar }+ cookieJar :: HT.CookieJar,+ timer :: UTCTime } type Url = String @@ -40,7 +42,8 @@ -> Session () -- ^ the actions and assertions that define the session -> TestTree httpTestCase sessionName sessBaseURL m = HUnit.testCase sessionName $ do- S.evalStateT m $ HttpTest sessBaseURL (HT.createCookieJar [])+ tm <- getCurrentTime+ S.evalStateT m $ HttpTest sessBaseURL (HT.createCookieJar []) tm withHT :: (HttpTest -> IO (HT.CookieJar, a)) -> Session a@@ -62,7 +65,7 @@ getRaw :: Url -> Session (Int, String) -getRaw url = withHT $ \(HttpTest base cj) -> do+getRaw url = withHT $ \(HttpTest base cj _) -> do r <- Wreq.getWith (Wreq.defaults & Wreq.cookies .~ cj) (base ++ url) return (r ^. Wreq.responseCookieJar, (r ^. Wreq.responseStatus . Wreq.statusCode, @@ -95,7 +98,7 @@ -- | Post a string body postRaw :: WreqT.Postable a => Url -> a -> Session String-postRaw url body = withHT $ \(HttpTest base cj) -> do+postRaw url body = withHT $ \(HttpTest base cj _) -> do r <- Wreq.postWith (Wreq.defaults & Wreq.cookies .~ cj) (base ++ url) body let code = r ^. Wreq.responseStatus . Wreq.statusCode@@ -168,3 +171,19 @@ else return () -} +-- | Re-start the timer++tic :: Session ()+tic = do+ now <- liftIO $ getCurrentTime+ S.modify $ \s -> s { timer = now }+++-- | Print the number of seconds elapsed, with a prefix++toc :: String -> Session ()+toc s = do+ last <- fmap timer $ S.get+ now <- liftIO $ getCurrentTime+ let df = diffUTCTime now last+ liftIO $ putStrLn $ s ++ " "++show df