-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
40 lines (38 loc) · 1000 Bytes
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func Test_myHandler(t *testing.T) {
type args struct {
url string
expectedName string
status int
}
tests := []struct {
name string
args args
}{
{"t1", args{"/json/test", "test", http.StatusOK}},
{"t2", args{"/x/y", "", http.StatusNotImplemented}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
url := tt.args.url
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
t.Errorf("could't create request: %v", tt.args.url)
}
recorder := httptest.NewRecorder()
myHandler(recorder, req)
if recorder.Result().StatusCode != tt.args.status {
t.Errorf("Wrong status code %v, expected %v", recorder.Result().StatusCode, tt.args.status)
}
if !strings.Contains(recorder.Body.String(), tt.args.expectedName) {
t.Errorf("The response does't contain '%v': '%v'", tt.args.expectedName, recorder.Body.String())
}
})
}
}