internal/helpers/test_helper.go   A
last analyzed

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
dl 0
loc 27
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A helpers.MakeTestServer 0 6 3
A helpers.AssertObjectEqualsJSON 0 4 1
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