generated from jidicula/template-go
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
81 lines (71 loc) · 2 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestCheckHandler(t *testing.T) {
tests := map[string]struct {
issURL string
wantCode int
wantResponse string
}{
"trueIssuer": {
issURL: "https://myvaccinerecord.cdph.ca.gov/creds",
wantCode: http.StatusOK,
wantResponse: `{"message": true}`,
},
"falseIssuer": {
issURL: "https://mallory.me/creds",
wantCode: http.StatusOK,
wantResponse: `{"message": false}`,
},
"emptyIssuer": {
issURL: "",
wantCode: http.StatusBadRequest,
wantResponse: `{"message": "No issuer URL provided"}` + "\n",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
query := fmt.Sprintf("/?iss=%s", tt.issURL)
req, err := http.NewRequest("GET", query, nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(checkHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != tt.wantCode {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
got := rr.Body.String()
if got != tt.wantResponse {
t.Errorf("%s returned wrong response: got %s, want %s", name, got, tt.wantResponse)
}
})
}
name := "incorrect GET"
t.Run(name, func(t *testing.T) {
wantCode := http.StatusMethodNotAllowed
wantResponse := `{"message": "expect method GET at /?iss=<url>"}` + "\n"
reader := strings.NewReader(`{"foo": "bar"}`)
req, err := http.NewRequest("POST", "/", reader)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(checkHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != wantCode {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
got := rr.Body.String()
if got != wantResponse {
t.Errorf("%s returned wrong response: got %s, want %s", name, got, wantResponse)
}
})
}