-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrades_test.go
138 lines (133 loc) · 3.13 KB
/
grades_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"regexp"
"testing"
)
func TestGradesHandler(t *testing.T) {
tests := []struct {
Desc string
Handler func(http.ResponseWriter, *http.Request)
Method string
Path string
Body map[string]interface{}
Status int
Match map[string]bool
}{
{
Desc: "Template: index.html",
Handler: index,
Method: "GET",
Path: "/",
// Body: map[string]interface{}{},
Status: http.StatusOK,
Match: map[string]bool{
"<!DOCTYPE html>": true,
},
}, {
Desc: "blank submission",
Handler: grades,
Method: "POST",
Path: "/grades",
// Body: map[string]interface{}{},
Status: http.StatusOK,
Match: map[string]bool{
"error": true,
"Fields missing from submission": true,
},
}, {
Desc: "incorrect submission: Affiliate is wrong",
Handler: grades,
Method: "POST",
Path: "/grades",
Body: map[string]interface{}{
"Affiliate": "987",
"LastName": "smith",
},
Status: http.StatusOK,
Match: map[string]bool{
"error": true,
"No match for ID and last name": true,
},
}, {
Desc: "incorrect submission: LastName is wrong",
Handler: grades,
Method: "POST",
Path: "/grades",
Body: map[string]interface{}{
"Affiliate": "123",
"LastName": "jones",
},
Status: http.StatusOK,
Match: map[string]bool{
"error": true,
"No match for ID and last name": true,
},
}, {
Desc: "incorrect submission: both Affiliate and LastName are wrong",
Handler: grades,
Method: "POST",
Path: "/grades",
Body: map[string]interface{}{
"Affiliate": "456",
"LastName": "wilson",
},
Status: http.StatusOK,
Match: map[string]bool{
"error": true,
"No match for ID and last name": true,
},
}, {
Desc: "correct submission for: smith",
Handler: grades,
Method: "POST",
Path: "/grades",
Body: map[string]interface{}{
"Affiliate": "123",
"LastName": "smith",
},
Status: http.StatusOK,
Match: map[string]bool{
"FirstName": true,
"CurrentGrade": true,
},
}, {
Desc: "odd capitalization for: sMiTh",
Handler: grades,
Method: "POST",
Path: "/grades",
Body: map[string]interface{}{
"Affiliate": "123",
"LastName": "sMiTh",
},
Status: http.StatusOK,
Match: map[string]bool{
"FirstName": true,
"CurrentGrade": true,
},
},
}
for _, test := range tests {
record := httptest.NewRecorder()
body, err := json.Marshal(test.Body)
if err != nil {
t.Fatal("Test json could not be Marshal'ed", err)
}
req, err := http.NewRequest(test.Method, test.Path, bytes.NewReader(body))
if err != nil {
t.Fatal("Test request could not be made", err)
}
test.Handler(record, req)
if got, want := record.Code, test.Status; got != want {
t.Errorf("%s: response code = %d, want %d", test.Desc, got, want)
}
for re, match := range test.Match {
if got := regexp.MustCompile(re).Match(record.Body.Bytes()); got != match {
t.Errorf("%s: %q ~ /%s/ = %v, want %v", test.Desc, record.Body, re, got, match)
}
}
}
}