Total Lines | 27 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package helpers |
||
2 | |||
3 | import ( |
||
4 | "encoding/json" |
||
5 | "net/http" |
||
6 | "net/http/httptest" |
||
7 | "testing" |
||
8 | |||
9 | "github.com/stretchr/testify/assert" |
||
10 | ) |
||
11 | |||
12 | // MakeTestServer creates an api server for testing |
||
13 | func MakeTestServer(responseCode int, body []byte) *httptest.Server { |
||
14 | return httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { |
||
15 | res.WriteHeader(responseCode) |
||
16 | _, err := res.Write(body) |
||
17 | if err != nil { |
||
18 | panic(err) |
||
19 | } |
||
20 | })) |
||
21 | } |
||
22 | |||
23 | // AssertObjectEqualsJSON checks if the JSON representation of an object matches the expected value |
||
24 | func AssertObjectEqualsJSON(t *testing.T, expectedJSON []byte, actual any) { |
||
25 | actualJSON, err := json.Marshal(actual) |
||
26 | assert.Nil(t, err) |
||
27 | assert.JSONEq(t, string(expectedJSON), string(actualJSON)) |
||
28 | } |
||
29 |