Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
github.com/agiammar/abide
A testing utility for http response snapshots. Inspired by Jest.
import "github.com/beme/abide"
func TestFunction(t *testing.T) {
req := httptest.NewRequest("GET", "http://example.com/", nil)
w := httptest.NewRecorder()
exampleHandler(w, req)
res := w.Result()
abide.AssertHTTPResponse(t, "example route", res)
}
$ go test -v
-u
flag.$ go test -v -- -u
Any snapshots created/updated will be located in package/__snapshots__
.
To ensure only the snapshots in-use are included, add the following to TestMain
. If your application does not have one yet, you can read about TestMain
usage here.
func TestMain(m *testing.M) {
exit := m.Run()
abide.Cleanup()
os.Exit(exit)
}
Once included, if the update -u
flag is used when running tests, any snapshot that is no longer in use will be removed. Note: if a single test is run, pruning will not occur.
A snapshot is essentially a lock file for an http response. Instead of having to manually compare every aspect of an http response to it's expected value, it can be automatically generated and used for matching in subsequent testing.
Here's an example snapshot:
/* snapshot: example route */
HTTP/1.1 200 OK
Connection: close
Content-Type: application/json
{
"key": "value"
}
When working with snapshots in a git repository, you could face some end line replacements that can cause comparison issues (warning: CRLF will be replaced by LF in ...
). To solve that just configure the snapshots as binary files in .gitattributes
of your project root:
*.snapshot binary
abide
also supports testing outside of http responses, by providing an Assert(*testing.T, string, Assertable)
method which will create snapshots for any type that implements String() string
.
See /example
for the usage of abide
in a basic web server. To run tests, simply $ go test -v
In some cases, attributes in a JSON response can by dynamic (e.g unique id's, dates, etc.), which can disrupt snapshot testing. To resolve this, an abide.json
file config can be included to override values with defaults. Consider the config in the supplied example project:
{
"defaults": {
"Etag": "default-etag-value",
"updated_at": 0,
"foo": "foobar"
}
}
When used with AssertHTTPResponse
, for any response with Content-Type: application/json
, the key-value pairs in defaults
will be used to override the JSON response, allowing for consistent snapshot testing. Any HTTP headers will also be override for key matches in defaults
.
__snapshot__
directoryTo write snapshots to a directory other than the default __snapshot__
, adjust abide.SnapshotDir
before any call to an Assert function. See example/models
package for a working example
func init() {
abide.SnapshotDir = "testdata"
}
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.