Dawid Wysokiński
79cc49d98e
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #5
34 lines
855 B
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...))
|
|
}
|