vcr

package module
v0.0.0-...-faa03fd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2018 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

VCR

Circle CI

But for golang.

Features

  • Easy to use
  • No flags (records when no cassete file is present, otherwise replays)
  • Filter sensitive data (such as API keys)
  • Modify requests before they are sent (e.g. to add row limits)
  • Ignores request headers (so a timestamp or crypto signature does not break your cassette files)
  • No dependencies

Examples

Simple:

package examples

import (
  "github.com/ad2games/vcr-go"
  "net/http"
  "testing"
)

func TestSomeFeature(t *testing.T) {
  // This will automatically create a cassette file at ./fixtures/vcr/my_feature.json
  vcr.Start("my_feature", nil)
  defer vcr.Stop()

  resp, _ := http.Get("http://google.com/")
  if resp.StatusCode != 200 {
    t.Fatalf("Status code is not 200!")
  }
}

Advanced:

func modifier(req *http.Request) {
  req.Header["Content-Type"] = []string{"text/xml"}

  vcr.ModifyHTTPRequestBody(req, func(body string) string {
    return body + "<row_limit>1</row_limit>"
  })
}

func TestSomeOtherFeature(t *testing.T) {
  vcr.Start("my_other_feature", modifier)
  vcr.FilterData(os.Getenv("SOME_API_KEY"), "some-api-key")
  defer vcr.Stop()

  http.Get("http://example.com/")
}

License

MIT, see LICENSE.txt

Contributing

  1. Fork it (https://github.com/ad2games/vcr-go/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FilterData

func FilterData(original string, replacement string)

FilterData allows replacement of sensitive data with a dummy-string

func ModifyHTTPRequestBody

func ModifyHTTPRequestBody(request *http.Request, modifyFunc ModifyStringFunc)

ModifyHTTPRequestBody changes the body of an HTTP request using a callback function

func Start

func Start(cassetteName string, modFunc RequestModifierFunc)

Start starts a VCR session with the given cassette name. Records episodes if the cassette file does not exists. Otherwise plays back recorded episodes.

func Stop

func Stop()

Stop stops the VCR session and writes the cassette file (when recording)

Types

type ModifyStringFunc

type ModifyStringFunc func(input string) string

ModifyStringFunc is a function that modifies a given string and returns it

type RequestModifierFunc

type RequestModifierFunc func(request *http.Request)

RequestModifierFunc is a function that can be used to manipulate HTTP requests before they are sent to the server. Useful for adding row-limits in integration tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL