No description
Find a file
2019-02-04 21:00:28 +01:00
src Add a way to access the inner client headers 2019-02-04 21:00:16 +01:00
tests Close the client when done 2019-02-04 21:00:28 +01:00
cassette.nimble Initial import 2019-01-27 22:08:51 +01:00
README.md Initial import 2019-01-27 22:08:51 +01:00

Cassette

Record and replay your HTTP sessions!

Note: The project is in its early development stage, use at your own risk (and, of course, PR are welcome).

The documentation is available by doing nim doc src/cassette.nim, you'll find a file named cassette.html in the src folder.

Why?

  • Because you want your tests to be deterministic (and fast)!
  • Because you don't want to hammer the remote server during the early prototyping stages of your app/library!

How?

Here's a simple example that shows how to fetch Bing's image of the day in both json and xml format and store them in a cassette file named bing_api.json.

Once the cassette is ready the requested URLs will be fetched from that instead of hitting the network.

import cassette
import asyncdispatch, httpclient

const URL_FMT = "https://www.bing.com/HPImageArchive.aspx?format=$#" &
                "&idx=0&n=1&mkt=en-US"

proc main() {.async.} =
  let client = newAsyncHttpClient()
  var cass = newAsyncCassetteClient(client, "bing_api.json")
  let asJs = await cass.request(URL_FMT % ["js"], HttpGet)
  let asXml = await cass.request(URL_FMT % ["xml"], HttpGet)
  echo asJs.status
  echo asXml.status
  echo await asJs.body
  echo await asXml.body
  cass.dispose()

waitFor main()

Ideally CassetteClient (and AsyncCassetteClient) should behave as a plain old HttpClient (and AsyncHttpClient).

Acknowdlegments

The vcr project inspired me to turn a half-assed mocking harness into a pretentious half-assed library.