response.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package apitest
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "github.com/porter-dev/porter/api/types"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. // AssertResponseExpected asserts that the expected http response matches the actual response
  11. //
  12. // Note that arguments need to be passed as pointer values due to how testify/assert handles serialization
  13. func AssertResponseExpected(t *testing.T, rr *httptest.ResponseRecorder, expResponse interface{}, gotTarget interface{}) {
  14. err := json.NewDecoder(rr.Body).Decode(gotTarget)
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. assert.Equal(
  19. t,
  20. expResponse,
  21. gotTarget,
  22. "incorrect response data",
  23. )
  24. }
  25. func AssertResponseForbidden(t *testing.T, rr *httptest.ResponseRecorder) {
  26. reqErr := &types.ExternalError{}
  27. err := json.NewDecoder(rr.Result().Body).Decode(reqErr)
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. expReqErr := &types.ExternalError{
  32. Error: "Forbidden",
  33. }
  34. assert.Equal(t, http.StatusForbidden, rr.Result().StatusCode, "status code should be forbidden")
  35. assert.Equal(t, expReqErr, reqErr, "body should be forbidden error")
  36. }
  37. // AssertForbiddenError asserts that the response has status code of a forbidden error
  38. func AssertForbiddenError(t *testing.T, rr *httptest.ResponseRecorder) {
  39. reqErr := &types.ExternalError{}
  40. err := json.NewDecoder(rr.Result().Body).Decode(reqErr)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. assert.Equal(t, http.StatusForbidden, rr.Result().StatusCode, "status code should be forbidden")
  45. }
  46. func AssertResponseInternalServerError(t *testing.T, rr *httptest.ResponseRecorder) {
  47. reqErr := &types.ExternalError{}
  48. err := json.NewDecoder(rr.Result().Body).Decode(reqErr)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. expReqErr := &types.ExternalError{
  53. Error: "An internal error occurred.",
  54. }
  55. assert.Equal(t, http.StatusInternalServerError, rr.Result().StatusCode, "status code should be internal server error")
  56. assert.Equal(t, expReqErr, reqErr, "body should be internal server error")
  57. }
  58. // AssertInternalServerError asserts that the response has status code of an internal server error
  59. func AssertInternalServerError(t *testing.T, rr *httptest.ResponseRecorder) {
  60. reqErr := &types.ExternalError{}
  61. err := json.NewDecoder(rr.Result().Body).Decode(reqErr)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. assert.Equal(t, http.StatusInternalServerError, rr.Result().StatusCode, "status code should be internal server error")
  66. }
  67. func AssertResponseError(t *testing.T, rr *httptest.ResponseRecorder, statusCode int, expReqErr *types.ExternalError) {
  68. reqErr := &types.ExternalError{}
  69. err := json.NewDecoder(rr.Result().Body).Decode(reqErr)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. assert.Equal(t, statusCode, rr.Result().StatusCode, "status code should match")
  74. assert.Equal(t, expReqErr, reqErr, "body should be matching error")
  75. }