Skip to content

Commit

Permalink
Simplify JSON assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 10, 2019
1 parent 289f149 commit 88c6b86
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
12 changes: 10 additions & 2 deletions mite/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (r *Recorder) RequestBody() []byte {
return r.reqBody
}

func (r *Recorder) RequestBodyCanonical() string {
return string(canonicalJSON(r.reqBody))
}

func (r *Recorder) ResponseContentType(contentType string) *Recorder {
r.resHeader.Add("Content-Type", contentType)
return r
Expand Down Expand Up @@ -106,9 +110,13 @@ func (r *Recorder) Handler() http.Handler {
})
}

func prettifyJson(b []byte, indent string) []byte {
func CanonicalString(s string) string {
return string(canonicalJSON([]byte(s)))
}

func canonicalJSON(b []byte) []byte {
buf := &bytes.Buffer{}
err := json.Indent(buf, b, "", indent)
err := json.Indent(buf, b, "", " ")
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions mite/time_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestApi_CreateTimeEntry(t *testing.T) {
assert.Equal(t, "application/json", rec.RequestContentType())
assert.Equal(t, testUserAgent, rec.RequestUserAgent())
assert.Equal(t, testApiKey, rec.RequestMiteKey())
assert.Equal(t, timeEntryRequest, string(prettifyJson(rec.RequestBody(), " ")))
assert.Equal(t, CanonicalString(timeEntryRequest), rec.RequestBodyCanonical())
}

func TestApi_EditTimeEntry(t *testing.T) {
Expand Down Expand Up @@ -219,7 +219,7 @@ func TestApi_EditTimeEntry(t *testing.T) {
assert.Equal(t, "application/json", rec.RequestContentType())
assert.Equal(t, testUserAgent, rec.RequestUserAgent())
assert.Equal(t, testApiKey, rec.RequestMiteKey())
assert.Equal(t, timeEntryRequest, string(prettifyJson(rec.RequestBody(), " ")))
assert.Equal(t, CanonicalString(timeEntryRequest), rec.RequestBodyCanonical())
}

func TestApi_DeleteTimeEntry(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions mite/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestApi_StartTracker(t *testing.T) {
assert.Equal(t, "application/json", rec.RequestContentType())
assert.Equal(t, testUserAgent, rec.RequestUserAgent())
assert.Equal(t, testApiKey, rec.RequestMiteKey())
assert.Equal(t, "{}", string(prettifyJson(rec.RequestBody(), "")))
assert.Equal(t, `{}`, rec.RequestBodyCanonical())
}

func TestApi_StartTracker_Running(t *testing.T) {
Expand Down Expand Up @@ -168,7 +168,7 @@ func TestApi_StartTracker_Running(t *testing.T) {
assert.Equal(t, "application/json", rec.RequestContentType())
assert.Equal(t, testUserAgent, rec.RequestUserAgent())
assert.Equal(t, testApiKey, rec.RequestMiteKey())
assert.Equal(t, "{}", string(prettifyJson(rec.RequestBody(), "")))
assert.Equal(t, `{}`, rec.RequestBodyCanonical())
}

func TestApi_StopTracker(t *testing.T) {
Expand Down

0 comments on commit 88c6b86

Please sign in to comment.