Skip to content

Commit 05aa641

Browse files
committed
test: format request body in cassettes
1 parent d9c47c9 commit 05aa641

File tree

1 file changed

+5
-27
lines changed
  • cmd/osv-scanner/internal/testcmd

1 file changed

+5
-27
lines changed

cmd/osv-scanner/internal/testcmd/vcr.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package testcmd
33
import (
44
"bytes"
55
"cmp"
6-
"encoding/json"
76
"io"
87
"net/http"
98
"os"
@@ -103,11 +102,7 @@ func InsertCassette(t *testing.T) *http.Client {
103102

104103
delete(i.Request.Headers, "User-Agent")
105104

106-
body, err := normalizeBody([]byte(i.Request.Body))
107-
if err != nil {
108-
return err
109-
}
110-
i.Request.Body = string(body)
105+
i.Request.Body = string(pretty.Pretty([]byte(i.Request.Body)))
111106
i.Request.ContentLength = int64(len(i.Request.Body))
112107

113108
// use a static duration since we don't care about replicating latency
@@ -208,13 +203,10 @@ func matchBody(r *http.Request, i cassette.Request) bool {
208203

209204
r.Body = io.NopCloser(bytes.NewBuffer(buffer.Bytes()))
210205

211-
body2, err := normalizeBody(buffer.Bytes())
212-
if err != nil {
213-
// This file will only run in tests, and this should be very unlikely
214-
panic(err)
215-
}
216-
217-
if !bytes.Equal(body2, []byte(i.Body)) {
206+
if !bytes.Equal(
207+
pretty.PrettyOptions(buffer.Bytes(), &pretty.Options{SortKeys: true}),
208+
pretty.PrettyOptions([]byte(i.Body), &pretty.Options{SortKeys: true}),
209+
) {
218210
return false
219211
}
220212
} else if len(i.Body) != 0 {
@@ -223,17 +215,3 @@ func matchBody(r *http.Request, i cassette.Request) bool {
223215

224216
return true
225217
}
226-
227-
func normalizeBody(body []byte) ([]byte, error) {
228-
var intermediate any
229-
if err := json.Unmarshal(body, &intermediate); err != nil {
230-
return nil, err
231-
}
232-
233-
buffer2, err := json.Marshal(intermediate)
234-
if err != nil {
235-
return nil, err
236-
}
237-
238-
return buffer2, nil
239-
}

0 commit comments

Comments
 (0)