This repository has been archived on 2023-04-18. You can view files and clone it, but cannot push or open issues or pull requests.
notificationarr/internal/rest/rest_test.go

34 lines
855 B
Go

package rest_test
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/go-chi/chi/v5"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
)
func doRequest(mux chi.Router, method, target string, body io.Reader) *http.Response {
rr := httptest.NewRecorder()
req := httptest.NewRequest(method, target, body)
mux.ServeHTTP(rr, req)
return rr.Result()
}
func assertJSONResponse(tb testing.TB, resp *http.Response, expectedStatus int, expected any, target any) {
tb.Helper()
assert.Equal(tb, expectedStatus, resp.StatusCode)
assert.NoError(tb, json.NewDecoder(resp.Body).Decode(target))
opts := cmp.Options{
cmpopts.IgnoreUnexported(time.Time{}),
}
assert.True(tb, cmp.Equal(expected, target, opts...), cmp.Diff(expected, target, opts...))
}