-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathtest_helpers_test.go
More file actions
50 lines (39 loc) · 894 Bytes
/
test_helpers_test.go
File metadata and controls
50 lines (39 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package buildkite
import (
"encoding/json"
"io"
"net/http"
"testing"
"github.com/google/go-cmp/cmp"
)
func must[T any](val T, err error) T {
if err != nil {
panic(err)
}
return val
}
func assertJSONEqual(t *testing.T, got, want string) {
t.Helper()
var gotJSON any
if err := json.Unmarshal([]byte(got), &gotJSON); err != nil {
t.Errorf("got invalid JSON %q: %v", got, err)
return
}
var wantJSON any
if err := json.Unmarshal([]byte(want), &wantJSON); err != nil {
t.Errorf("want invalid JSON %q: %v", want, err)
return
}
if diff := cmp.Diff(wantJSON, gotJSON); diff != "" {
t.Errorf("JSON mismatch (-want +got):\n%s", diff)
}
}
func assertRequestJSON(t *testing.T, r *http.Request, want string) {
t.Helper()
got, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("reading request body: %v", err)
return
}
assertJSONEqual(t, string(got), want)
}