Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
github.com/sebdah/goldie
goldie
is a golden file test utility for Go projects. It's typically used for
testing responses with larger data bodies.
The concept is straight forward. Valid response data is stored in a "golden file". The actual response data will be byte compared with the golden file and the test will fail if there is a difference.
Updating the golden file can be done by running go test -update ./...
.
See the GoDoc for API reference and configuration options.
The below example fetches data from a REST API. The last line in the test is the
actual usage of goldie
. It takes the HTTP response body and asserts that it's
what is present in the golden test file.
func TestExample(t *testing.T) {
recorder := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/example", nil)
assert.Nil(t, err)
handler := http.HandlerFunc(ExampleHandler)
handler.ServeHTTP()
goldie.Assert(t, "example", recorder.Body.Bytes())
}
If some values in the golden file can change depending on the test, you can use golang
template in the golden file and pass the data to goldie.AssertWithTemplate
.
This is a {{ .Type }} file.
func TestTemplateExample(t *testing.T) {
recorder := httptest.NewRecorder()
req, err := http.NewRequest("POST", "/example/Golden", nil)
assert.Nil(t, err)
handler := http.HandlerFunc(ExampleHandler)
handler.ServeHTTP()
data := struct {
Type string
}{
Type: "Golden",
}
goldie.AssertWithTemplate(t, "example", data, recorder.Body.Bytes())
}
Then run your test with the -update
flag the first time to store the result.
go test -update ./...
For any consecutive runs where you actually want to compare the data, simply
drop the -update
flag.
go test ./...
Yes, please! Pull requests are most welcome. On the wish list:
goldie
?The name comes from the fact that it's for Go and handles golden file testing. But yes, it may not be the best name in the world.
This is based on the great Advanced Go testing talk by @mitchellh.
MIT
Copyright 2016 Sebastian Dahlgren sebastian.dahlgren@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.