seborama / govcr Goto Github PK
View Code? Open in Web Editor NEWHTTP mock for Golang: record and replay HTTP/HTTPS interactions for offline testing
License: Apache License 2.0
HTTP mock for Golang: record and replay HTTP/HTTPS interactions for offline testing
License: Apache License 2.0
I'm using govcr but when I have logging turned on I see the following message:
2018/07/25 06:21:15 json: unsupported type: func(string, []uint8, int) ([]uint8, bool)
Also, after running the test I don't see any new files at the CassettePath
I have configured.
Hi Seb,
I was wondering if you've read through https://github.com/dnaeon/go-vcr and the reason why you chose to write your own ?
I'm currently choosing a solution, and I'd hugely appreciate your insight on the design of both.
thanks!
Hi @seborama!
I tried your lib, but had a problem with it that forced me to use https://github.com/dnaeon/go-vcr instead.
My use case: I inject your *http.Client
into gorequest.SuperAgent
and that into library that I maintain.
All requests are HTTPS. The problem is, the records contain the body encrypted.
Firstly, that's hard to maintain. Secondly, it doesn't match for request body, so I can't replay.
It seems that for no real reason, the full path is stored in a cassette:
{
"Name": "TestUnauth.gz",
"Path": "/home/klaus/gopath/src/github.com/app/testdata",
"Tracks": [ ....
This creates problems for me, when I run the cassette as part of a CI test, since the cassette is no longer at this path. Therefore I get an unexpected mkdir /home/klaus: permission denied
when it wants to add to the cassette.
I don't see any reason to have this information inside the cassette since it must be provided anyway for loading it, and it only causes problems. So I'd propose to remove it.
Hi,
Thanks for a great tool! I use it extensively for running test. one thing I've noticed is that whenever in captuing mode, saving http traffic, it appears to save the cassette to disk for each new track. It appears to entail both json marshaling and a disk write. Some of my tests are long, with thousands of requests, and this amount of writing cause the capturing phase to grind to a crawl when the cassette gets larger in size.
Would it be possible to defer the (json rendering and) writing to disk until an explicit .Save() call is made?
Thanks!
Typically you would expect an http.RoundTripper
to handle concurrent requests.
If we look at the exiting Roundtripper it will cause race conditions on concurrent access.
A "simple" solution could be to put a mu sync.Mutex
in the vcrTransport
and add t.mu.Lock() / defer t.mu.Unlock()
to the RoundTrip function. This could serialize roundtrips so only one can run at the time. It will not guarantee any order of recordings, but if you do concurrent requests you wouldn't expect to.
In an empty package, trying to load govcr with Go modules grabs some older versions:
$ cat go.mod
module example
go 1.12
$ go get gopkg.in/seborama/govcr.v2
$ go get gopkg.in/seborama/govcr.v3
$ go get gopkg.in/seborama/govcr.v4
go: finding gopkg.in/seborama/govcr.v4 latest
$ cat go.mod
module example
go 1.12
require (
gopkg.in/seborama/govcr.v2 v2.2.1 // indirect
gopkg.in/seborama/govcr.v3 v3.0.1 // indirect
gopkg.in/seborama/govcr.v4 v4.0.0-20190303132007-6b478b087ffb // indirect
)
The latest v2 tag seems to be 2.4.2, but it doesn't get found because it's missing the v
prefix (note: it's also missing from gopkg.in.) The latest v3 tag is v3.2 and the latest v4 tag is v4.4. However, modules seem to require semver which needs 3-component versions, i.e., quoting from the Modules wiki page:
Modules must be semantically versioned according to semver, usually in the form v(major).(minor).(patch), such as v0.1.0, v1.2.3, or v1.5.0-rc.1. The leading v is required. If using Git, tag released commits with their versions. Public and private module repositories and proxies are becoming available (see FAQ below).
So if these were re-tagged with a trailing .0
(e.g., v4.4.0
), I think this would work (though I'm not totally sure, Go modules are fancy new things.)
Sensitive headers (e.g., Set-Cookie
) are stored in the cassette currently. Is there a way to ensure they are not stored?
Currently, I am injecting a custom RoundTripper
:
func (tr *testScrubTransport) RoundTrip(req *http.Request) (*http.Response, error) {
resp, err := tr.Original.RoundTrip(req)
if err == nil {
// Scrub sensitive material here
resp.Header.Del("Set-Cookie")
}
return resp, err
}
But it would be nice if we could use govcr's ResponseFilter
machinery for this.
Hi @seborama!
Thank you for govcr, is a fantastic library.
I am planning to use this library for 2 purposes:
The second use is not the intended library use. Do you know a better library to store network requests in golang? If not, what will be the best strategy to turn govcr agnostic to persistance (i.e. .cassette files, databases, etc..)?
Thanks,
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.