Simple helpers for testing http-handlers and other handlers
go get github.com/redagain/go-testrequest
- Test request builder
- No-op http.ResponseWriter
func TestHandler(t *testing.T) {
type args struct {
w http.ResponseWriter
r *http.Request
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Success",
args: args{
w: testrequest.NopResponseWriter(),
r: testrequest.Builder().SetJSONFromValue(
map[string]interface{}{
"title": "The Go Programming Language",
"isbn": "978-0134190440",
"authors": []string{"Alan A. A. Donovan", "Brian W. Kernighan"},
}).
SetAccept("application/json").
SetBearerAuth("4mwbMA6zq9Nxf3XzLk9n01MJX57jdjMAdfYCaJu44vEUJVfdVLF9").
Request(),
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- simpleapi - examples of using helpers to test a simple API
- clientcredentials - example of testing a function to extract client credentials from a http.Request