Skip to content

Commit

Permalink
Merge pull request #32 from meydjer/master
Browse files Browse the repository at this point in the history
Better snapshot output and JSON content-type verification
  • Loading branch information
sjkaliski authored Mar 14, 2018
2 parents 39d5cd3 + d1dea39 commit d831b87
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
34 changes: 27 additions & 7 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ func AssertHTTPResponse(t *testing.T, id string, w *http.Response) {

data := string(body)

contentType := w.Header.Get("Content-Type")

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

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

func contentTypeIsJSON(contentType string) bool {
contentTypeParts := strings.Split(contentType, ";")
firstPart := contentTypeParts[0]

isPlainJSON := firstPart == "application/json"
if isPlainJSON {
return isPlainJSON
}

isVendor := strings.HasPrefix(firstPart, "application/vnd.")

isJSON := strings.HasSuffix(firstPart, "+json")

return isVendor && isJSON
}

// AssertReader asserts the value of an io.Reader.
func AssertReader(t *testing.T, id string, r io.Reader) {
data, err := ioutil.ReadAll(r)
Expand All @@ -84,7 +102,7 @@ func createOrUpdateSnapshot(t *testing.T, id, data string) {
var err error
if snapshot == nil {
if !args.shouldUpdate {
t.Error(newSnapshotMessage(data))
t.Error(newSnapshotMessage(id, data))
return
}

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

t.Error(didNotMatchMessage(diff))
t.Error(didNotMatchMessage(id, diff))
return
}
}
Expand All @@ -132,16 +150,18 @@ func compareResults(t *testing.T, existing, new string) string {
return dmp.DiffPrettyText(allDiffs)
}

func didNotMatchMessage(diff string) string {
msg := "\n\nExisting snapshot does not match results...\n\n"
func didNotMatchMessage(id, diff string) string {
msg := "\n\n## Existing snapshot does not match results...\n"
msg += "## \"" + id + "\"\n\n"
msg += diff
msg += "\n\n"
msg += "If this change was intentional, run tests again, $ go test -v -- -u\n"
return msg
}

func newSnapshotMessage(body string) string {
msg := "\n\nNew snapshot found...\n\n"
func newSnapshotMessage(id, body string) string {
msg := "\n\n## New snapshot found...\n"
msg += "## \"" + id + "\"\n\n"
msg += body
msg += "\n\n"
msg += "To save, run tests again, $ go test -v -- -u\n"
Expand Down
29 changes: 29 additions & 0 deletions assert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package abide

import (
"testing"
)

func TestContentTypeIsJSON(test *testing.T) {
contentTypeTestCases := map[string]bool{
"application/json": true,
"application/json; charset=utf-8": true,
"application/vnd.foo.bar.v2+json": true,
"application/application/json": false,
"application/json/json": false,
"application/jsoner; charset=utf-8": false,
"application/jsoner": false,
"application/vnd.foo.bar.v2+jsoner": false,
"application/xml": false,
"text/html": false,
"": false,
}

for input, expectedOutput := range contentTypeTestCases {
result := contentTypeIsJSON(input)

if result != expectedOutput {
test.Errorf("contentTypeIsJSON(\"%s\" unexpected result. Got=%t, Want=%t", input, result, expectedOutput)
}
}
}

0 comments on commit d831b87

Please sign in to comment.