Skip to content

Commit d831b87

Browse files
authored
Merge pull request #32 from meydjer/master
Better snapshot output and JSON content-type verification
2 parents 39d5cd3 + d1dea39 commit d831b87

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

assert.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ func AssertHTTPResponse(t *testing.T, id string, w *http.Response) {
3939

4040
data := string(body)
4141

42+
contentType := w.Header.Get("Content-Type")
43+
4244
// If the response body is JSON, indent.
43-
if w.Header.Get("Content-Type") == "application/json" {
45+
if contentTypeIsJSON(contentType) {
4446
lines := strings.Split(data, "\n")
4547
jsonStr := lines[len(lines)-1]
4648

@@ -68,6 +70,22 @@ func AssertHTTPResponse(t *testing.T, id string, w *http.Response) {
6870
createOrUpdateSnapshot(t, id, data)
6971
}
7072

73+
func contentTypeIsJSON(contentType string) bool {
74+
contentTypeParts := strings.Split(contentType, ";")
75+
firstPart := contentTypeParts[0]
76+
77+
isPlainJSON := firstPart == "application/json"
78+
if isPlainJSON {
79+
return isPlainJSON
80+
}
81+
82+
isVendor := strings.HasPrefix(firstPart, "application/vnd.")
83+
84+
isJSON := strings.HasSuffix(firstPart, "+json")
85+
86+
return isVendor && isJSON
87+
}
88+
7189
// AssertReader asserts the value of an io.Reader.
7290
func AssertReader(t *testing.T, id string, r io.Reader) {
7391
data, err := ioutil.ReadAll(r)
@@ -84,7 +102,7 @@ func createOrUpdateSnapshot(t *testing.T, id, data string) {
84102
var err error
85103
if snapshot == nil {
86104
if !args.shouldUpdate {
87-
t.Error(newSnapshotMessage(data))
105+
t.Error(newSnapshotMessage(id, data))
88106
return
89107
}
90108

@@ -109,7 +127,7 @@ func createOrUpdateSnapshot(t *testing.T, id, data string) {
109127
return
110128
}
111129

112-
t.Error(didNotMatchMessage(diff))
130+
t.Error(didNotMatchMessage(id, diff))
113131
return
114132
}
115133
}
@@ -132,16 +150,18 @@ func compareResults(t *testing.T, existing, new string) string {
132150
return dmp.DiffPrettyText(allDiffs)
133151
}
134152

135-
func didNotMatchMessage(diff string) string {
136-
msg := "\n\nExisting snapshot does not match results...\n\n"
153+
func didNotMatchMessage(id, diff string) string {
154+
msg := "\n\n## Existing snapshot does not match results...\n"
155+
msg += "## \"" + id + "\"\n\n"
137156
msg += diff
138157
msg += "\n\n"
139158
msg += "If this change was intentional, run tests again, $ go test -v -- -u\n"
140159
return msg
141160
}
142161

143-
func newSnapshotMessage(body string) string {
144-
msg := "\n\nNew snapshot found...\n\n"
162+
func newSnapshotMessage(id, body string) string {
163+
msg := "\n\n## New snapshot found...\n"
164+
msg += "## \"" + id + "\"\n\n"
145165
msg += body
146166
msg += "\n\n"
147167
msg += "To save, run tests again, $ go test -v -- -u\n"

assert_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package abide
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestContentTypeIsJSON(test *testing.T) {
8+
contentTypeTestCases := map[string]bool{
9+
"application/json": true,
10+
"application/json; charset=utf-8": true,
11+
"application/vnd.foo.bar.v2+json": true,
12+
"application/application/json": false,
13+
"application/json/json": false,
14+
"application/jsoner; charset=utf-8": false,
15+
"application/jsoner": false,
16+
"application/vnd.foo.bar.v2+jsoner": false,
17+
"application/xml": false,
18+
"text/html": false,
19+
"": false,
20+
}
21+
22+
for input, expectedOutput := range contentTypeTestCases {
23+
result := contentTypeIsJSON(input)
24+
25+
if result != expectedOutput {
26+
test.Errorf("contentTypeIsJSON(\"%s\" unexpected result. Got=%t, Want=%t", input, result, expectedOutput)
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)