Skip to content

Commit 88c6b86

Browse files
author
Ulrich Lissé
committed
Simplify JSON assertions
1 parent 289f149 commit 88c6b86

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

mite/api_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ func (r *Recorder) RequestBody() []byte {
5555
return r.reqBody
5656
}
5757

58+
func (r *Recorder) RequestBodyCanonical() string {
59+
return string(canonicalJSON(r.reqBody))
60+
}
61+
5862
func (r *Recorder) ResponseContentType(contentType string) *Recorder {
5963
r.resHeader.Add("Content-Type", contentType)
6064
return r
@@ -106,9 +110,13 @@ func (r *Recorder) Handler() http.Handler {
106110
})
107111
}
108112

109-
func prettifyJson(b []byte, indent string) []byte {
113+
func CanonicalString(s string) string {
114+
return string(canonicalJSON([]byte(s)))
115+
}
116+
117+
func canonicalJSON(b []byte) []byte {
110118
buf := &bytes.Buffer{}
111-
err := json.Indent(buf, b, "", indent)
119+
err := json.Indent(buf, b, "", " ")
112120
if err != nil {
113121
panic(err)
114122
}

mite/time_entry_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func TestApi_CreateTimeEntry(t *testing.T) {
188188
assert.Equal(t, "application/json", rec.RequestContentType())
189189
assert.Equal(t, testUserAgent, rec.RequestUserAgent())
190190
assert.Equal(t, testApiKey, rec.RequestMiteKey())
191-
assert.Equal(t, timeEntryRequest, string(prettifyJson(rec.RequestBody(), " ")))
191+
assert.Equal(t, CanonicalString(timeEntryRequest), rec.RequestBodyCanonical())
192192
}
193193

194194
func TestApi_EditTimeEntry(t *testing.T) {
@@ -219,7 +219,7 @@ func TestApi_EditTimeEntry(t *testing.T) {
219219
assert.Equal(t, "application/json", rec.RequestContentType())
220220
assert.Equal(t, testUserAgent, rec.RequestUserAgent())
221221
assert.Equal(t, testApiKey, rec.RequestMiteKey())
222-
assert.Equal(t, timeEntryRequest, string(prettifyJson(rec.RequestBody(), " ")))
222+
assert.Equal(t, CanonicalString(timeEntryRequest), rec.RequestBodyCanonical())
223223
}
224224

225225
func TestApi_DeleteTimeEntry(t *testing.T) {

mite/tracker_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func TestApi_StartTracker(t *testing.T) {
139139
assert.Equal(t, "application/json", rec.RequestContentType())
140140
assert.Equal(t, testUserAgent, rec.RequestUserAgent())
141141
assert.Equal(t, testApiKey, rec.RequestMiteKey())
142-
assert.Equal(t, "{}", string(prettifyJson(rec.RequestBody(), "")))
142+
assert.Equal(t, `{}`, rec.RequestBodyCanonical())
143143
}
144144

145145
func TestApi_StartTracker_Running(t *testing.T) {
@@ -168,7 +168,7 @@ func TestApi_StartTracker_Running(t *testing.T) {
168168
assert.Equal(t, "application/json", rec.RequestContentType())
169169
assert.Equal(t, testUserAgent, rec.RequestUserAgent())
170170
assert.Equal(t, testApiKey, rec.RequestMiteKey())
171-
assert.Equal(t, "{}", string(prettifyJson(rec.RequestBody(), "")))
171+
assert.Equal(t, `{}`, rec.RequestBodyCanonical())
172172
}
173173

174174
func TestApi_StopTracker(t *testing.T) {

0 commit comments

Comments
 (0)