render_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package render
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "math"
  7. "net/http"
  8. "net/http/httptest"
  9. "strconv"
  10. "testing"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/smallstep/certificates/logging"
  13. )
  14. func TestJSON(t *testing.T) {
  15. rec := httptest.NewRecorder()
  16. rw := logging.NewResponseLogger(rec)
  17. JSON(rw, map[string]interface{}{"foo": "bar"})
  18. assert.Equal(t, http.StatusOK, rec.Result().StatusCode)
  19. assert.Equal(t, "application/json", rec.Header().Get("Content-Type"))
  20. assert.Equal(t, "{\"foo\":\"bar\"}\n", rec.Body.String())
  21. assert.Empty(t, rw.Fields())
  22. }
  23. func TestJSONPanicsOnUnsupportedType(t *testing.T) {
  24. jsonPanicTest[json.UnsupportedTypeError](t, make(chan struct{}))
  25. }
  26. func TestJSONPanicsOnUnsupportedValue(t *testing.T) {
  27. jsonPanicTest[json.UnsupportedValueError](t, math.NaN())
  28. }
  29. func TestJSONPanicsOnMarshalerError(t *testing.T) {
  30. var v erroneousJSONMarshaler
  31. jsonPanicTest[json.MarshalerError](t, v)
  32. }
  33. type erroneousJSONMarshaler struct{}
  34. func (erroneousJSONMarshaler) MarshalJSON() ([]byte, error) {
  35. return nil, assert.AnError
  36. }
  37. func jsonPanicTest[T json.UnsupportedTypeError | json.UnsupportedValueError | json.MarshalerError](t *testing.T, v any) {
  38. t.Helper()
  39. defer func() {
  40. var err error
  41. if r := recover(); r == nil {
  42. t.Fatal("expected panic")
  43. } else if e, ok := r.(error); !ok {
  44. t.Fatalf("did not panic with an error (%T)", r)
  45. } else {
  46. err = e
  47. }
  48. var e *T
  49. assert.ErrorAs(t, err, &e)
  50. }()
  51. JSON(httptest.NewRecorder(), v)
  52. }
  53. type renderableError struct {
  54. Code int `json:"-"`
  55. Message string `json:"message"`
  56. }
  57. func (err renderableError) Error() string {
  58. return err.Message
  59. }
  60. func (err renderableError) Render(w http.ResponseWriter) {
  61. w.Header().Set("Content-Type", "something/custom")
  62. JSONStatus(w, err, err.Code)
  63. }
  64. type statusedError struct {
  65. Contents string
  66. }
  67. func (err statusedError) Error() string { return err.Contents }
  68. func (statusedError) StatusCode() int { return 432 }
  69. func TestError(t *testing.T) {
  70. cases := []struct {
  71. err error
  72. code int
  73. body string
  74. header string
  75. }{
  76. 0: {
  77. err: renderableError{532, "some string"},
  78. code: 532,
  79. body: "{\"message\":\"some string\"}\n",
  80. header: "something/custom",
  81. },
  82. 1: {
  83. err: statusedError{"123"},
  84. code: 432,
  85. body: "{\"Contents\":\"123\"}\n",
  86. header: "application/json",
  87. },
  88. }
  89. for caseIndex := range cases {
  90. kase := cases[caseIndex]
  91. t.Run(strconv.Itoa(caseIndex), func(t *testing.T) {
  92. rec := httptest.NewRecorder()
  93. Error(rec, kase.err)
  94. assert.Equal(t, kase.code, rec.Result().StatusCode)
  95. assert.Equal(t, kase.body, rec.Body.String())
  96. assert.Equal(t, kase.header, rec.Header().Get("Content-Type"))
  97. })
  98. }
  99. }
  100. type causedError struct {
  101. cause error
  102. }
  103. func (err causedError) Error() string { return fmt.Sprintf("cause: %s", err.cause) }
  104. func (err causedError) Cause() error { return err.cause }
  105. func TestStatusCodeFromError(t *testing.T) {
  106. cases := []struct {
  107. err error
  108. exp int
  109. }{
  110. 0: {nil, http.StatusInternalServerError},
  111. 1: {io.EOF, http.StatusInternalServerError},
  112. 2: {statusedError{"123"}, 432},
  113. 3: {causedError{statusedError{"432"}}, 432},
  114. }
  115. for caseIndex, kase := range cases {
  116. assert.Equal(t, kase.exp, statusCodeFromError(kase.err), "case: %d", caseIndex)
  117. }
  118. }