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.
gopkg.in/appleboy/gofight.v1
API Handler Testing for Golang framework.
Downladn this package.
$ go get gopkg.in/appleboy/gofight.v1
To import this package, add the following line to your code:
import "gopkg.in/appleboy/gofight.v1"
The following is basic testing example.
Main Program:
package main
import (
"io"
"net/http"
)
func BasicHelloHandler(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello World")
}
func BasicEngine() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/", BasicHelloHandler)
return mux
}
Testing:
package main
import (
"github.com/appleboy/gofight"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
func TestBasicHelloWorld(t *testing.T) {
r := gofight.New()
r.GET("/").
// turn on the debug mode.
SetDebug(true).
Run(BasicEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, "Hello World", r.Body.String())
assert.Equal(t, http.StatusOK, r.Code)
})
}
You can add custom header via SetHeader
func.
func TestBasicHelloWorld(t *testing.T) {
r := gofight.New()
version := "0.0.1"
r.GET("/").
// turn on the debug mode.
SetDebug(true).
SetHeader(gofight.H{
"X-Version": version,
}).
Run(BasicEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, version, rq.Header.Get("X-Version"))
assert.Equal(t, "Hello World", r.Body.String())
assert.Equal(t, http.StatusOK, r.Code)
})
}
Using SetFORM
to generate form data.
func TestPostFormData(t *testing.T) {
r := gofight.New()
r.POST("/form").
SetFORM(gofight.H{
"a": "1",
"b": "2",
}).
Run(BasicEngine(), func(r HTTPResponse, rq HTTPRequest) {
data := []byte(r.Body.String())
a, _ := jsonparser.GetString(data, "a")
b, _ := jsonparser.GetString(data, "b")
assert.Equal(t, "1", a)
assert.Equal(t, "2", b)
assert.Equal(t, http.StatusOK, r.Code)
})
}
Using SetJSON
to generate json data.
func TestPostJSONData(t *testing.T) {
r := gofight.New()
r.POST("/json").
SetJSON(gofight.D{
"a": 1,
"b": 2,
}).
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
data := []byte(r.Body.String())
a, _ := jsonparser.GetInt(data, "a")
b, _ := jsonparser.GetInt(data, "b")
assert.Equal(t, 1, int(a))
assert.Equal(t, 2, int(b))
assert.Equal(t, http.StatusOK, r.Code)
})
}
Using SetBody
to generate raw data.
func TestPostRawData(t *testing.T) {
r := gofight.New()
r.POST("/raw").
SetBody("a=1&b=1").
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
data := []byte(r.Body.String())
a, _ := jsonparser.GetString(data, "a")
b, _ := jsonparser.GetString(data, "b")
assert.Equal(t, "1", a)
assert.Equal(t, "2", b)
assert.Equal(t, http.StatusOK, r.Code)
})
}
Using SetQuery
to generate raw data.
func TestQueryString(t *testing.T) {
r := gofight.New()
r.GET("/hello").
SetQuery(gofight.H{
"a": "1",
"b": "2",
}).
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
or append exist query parameter.
func TestQueryString(t *testing.T) {
r := gofight.New()
r.GET("/hello?foo=bar").
SetQuery(gofight.H{
"a": "1",
"b": "2",
}).
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
Using SetCookie
to generate raw data.
func TestQueryString(t *testing.T) {
r := gofight.New()
r.GET("/hello").
SetQuery(gofight.H{
"foo": "bar",
}).
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
assert.Equal(t, "foo=bar", rq.Header.Get("cookie"))
})
}
$ make
Copyright 2016 Bo-Yi Wu @appleboy.
Licensed under the MIT License.
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.