Mocked network testing for Haskell
Haskell
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Data/Yaml
Network/HAVCR
test
.gitignore
LICENSE
README.md
Setup.hs
havcr.cabal

README.md

HAVCR

HAVCR is a testing tool for recording and playing back HTTP network transactions. It is inspired by VCR library from Ruby and its records are compatible with VCR "cassettes".

Abstract

Your networking code should be arranged such as to use Control.Proxy (see also "Refactoring HTTP Calls" below). When tests are running, HTTP requests are checked against prerecorded cassettes. If the recorded request exists, its corresponding recorded response is sent back to the application, without hitting the real network. If the recorded request is not found, it is sent to the real network and, together with the response that is received, recorded for the future reuse.

Refactoring HTTP Calls

(Copied from Network.HTTP To Control.Proxy Who Am The Only One).

Provided that you use simpleHTTP function from Network.HTTP library, here’s what you need to do to make it “testable”:

Your original function:

getResponse :: HStream ty => Request ty -> IO (Result (Response ty))
getResponse req = simpleHTTP req

should be modified to send requests via a Proxy:

getResponse :: Proxy p => FilePath -> () -> Session p IO (Result (Response String))
getResponse req = mockedServer "cassette.yml" >-> httpClient req

httpClient :: forall ty p r. (HStream ty, IsString ty, Proxy p) =>
              Request String -> () -> Client p (Request String) (Result (Response ty)) IO (Result (Response ty))
httpClient req () = runIdentityP $ do request req

Don’t forget to import Network.HAVCR which provides mockedServer.

Authors and Contributors

Slava Kravchenko

Thanks

Gabriel Gonzalez