| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package apitest
- import (
- "encoding/json"
- "net/http"
- "net/http/httptest"
- "testing"
- "github.com/porter-dev/porter/api/types"
- "github.com/stretchr/testify/assert"
- )
- // AssertResponseExpected asserts that the expected http response matches the actual response
- //
- // Note that arguments need to be passed as pointer values due to how testify/assert handles serialization
- func AssertResponseExpected(t *testing.T, rr *httptest.ResponseRecorder, expResponse interface{}, gotTarget interface{}) {
- err := json.NewDecoder(rr.Body).Decode(gotTarget)
- if err != nil {
- t.Fatal(err)
- }
- assert.Equal(
- t,
- expResponse,
- gotTarget,
- "incorrect response data",
- )
- }
- func AssertResponseForbidden(t *testing.T, rr *httptest.ResponseRecorder) {
- reqErr := &types.ExternalError{}
- err := json.NewDecoder(rr.Result().Body).Decode(reqErr)
- if err != nil {
- t.Fatal(err)
- }
- expReqErr := &types.ExternalError{
- Error: "Forbidden",
- }
- assert.Equal(t, http.StatusForbidden, rr.Result().StatusCode, "status code should be forbidden")
- assert.Equal(t, expReqErr, reqErr, "body should be forbidden error")
- }
- func AssertResponseInternalServerError(t *testing.T, rr *httptest.ResponseRecorder) {
- reqErr := &types.ExternalError{}
- err := json.NewDecoder(rr.Result().Body).Decode(reqErr)
- if err != nil {
- t.Fatal(err)
- }
- expReqErr := &types.ExternalError{
- Error: "An internal error occurred.",
- }
- assert.Equal(t, http.StatusInternalServerError, rr.Result().StatusCode, "status code should be internal server error")
- assert.Equal(t, expReqErr, reqErr, "body should be internal server error")
- }
- func AssertResponseError(t *testing.T, rr *httptest.ResponseRecorder, statusCode int, expReqErr *types.ExternalError) {
- reqErr := &types.ExternalError{}
- err := json.NewDecoder(rr.Result().Body).Decode(reqErr)
- if err != nil {
- t.Fatal(err)
- }
- assert.Equal(t, statusCode, rr.Result().StatusCode, "status code should match")
- assert.Equal(t, expReqErr, reqErr, "body should be matching error")
- }
|