Skip to content

Instantly share code, notes, and snippets.

@gabrielfalcao
Last active June 3, 2020 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielfalcao/43e4af9755d3c6fa380d5dbf99ea33e5 to your computer and use it in GitHub Desktop.
Save gabrielfalcao/43e4af9755d3c6fa380d5dbf99ea33e5 to your computer and use it in GitHub Desktop.
Go-VCR Example

Usage

go test ./...
module github.com/gabrielfalcao/hello-vcr
go 1.14
require (
github.com/dnaeon/go-vcr v1.0.1
github.com/stretchr/testify v1.6.0
gopkg.in/yaml.v2 v2.3.0 // indirect
)
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dnaeon/go-vcr v1.0.1 h1:r8L/HqC0Hje5AXMu1ooW8oyQyOFv4GxqpL0nRP7SLLY=
github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgho=
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
package hello
func Hello() string {
return "Hello, world."
}
package hello
import (
"github.com/dnaeon/go-vcr/recorder"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
func TestHello(t *testing.T) {
r, err := recorder.New("httpbin-ip-address-response")
client := &http.Client{
Transport: r,
}
defer r.Stop() // Make sure recorder is stopped once done with it
client.Get("https://httpbin.org/ip")
assert.Equal(t, err, nil)
}
---
version: 1
interactions:
- request:
body: ""
form: {}
headers: {}
url: https://httpbin.org/ip
method: GET
response:
body: |
{
"origin": "89.14.65.175"
}
headers:
Access-Control-Allow-Credentials:
- "true"
Access-Control-Allow-Origin:
- '*'
Content-Length:
- "31"
Content-Type:
- application/json
Date:
- Tue, 02 Jun 2020 20:49:15 GMT
Server:
- gunicorn/19.9.0
status: 200 OK
code: 200
duration: ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment