Files

36 lines
955 B
Go

package api
import (
"encoding/json"
"io"
"net/http/httptest"
"reflect"
"testing"
"github.com/stretchr/testify/require"
bunpaginate "github.com/hanzo-fi/go-libs/v5/pkg/storage/bun/paginate"
"github.com/hanzo-fi/go-libs/v5/pkg/transport/api"
)
func ReadErrorResponse(t *testing.T, r io.Reader) *api.ErrorResponse {
t.Helper()
ret := &api.ErrorResponse{}
require.NoError(t, json.NewDecoder(r).Decode(ret))
return ret
}
func ReadResponse[T any](t *testing.T, rec *httptest.ResponseRecorder, to T) {
t.Helper()
ret := &api.BaseResponse[T]{}
require.NoError(t, json.NewDecoder(rec.Body).Decode(ret))
reflect.ValueOf(to).Elem().Set(reflect.ValueOf(*ret.Data).Elem())
}
func ReadCursor[T any](t *testing.T, rec *httptest.ResponseRecorder, to *bunpaginate.Cursor[T]) {
t.Helper()
ret := &api.BaseResponse[T]{}
require.NoError(t, json.NewDecoder(rec.Body).Decode(ret))
reflect.ValueOf(to).Elem().Set(reflect.ValueOf(ret.Cursor).Elem())
}