error_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package rest
  2. import (
  3. "errors"
  4. "io"
  5. "net/http/httptest"
  6. "testing"
  7. )
  8. type ErrorTestCase struct {
  9. Input Error
  10. Code int
  11. Str string
  12. JSON string
  13. Err error
  14. }
  15. var errorTestCases = []ErrorTestCase{
  16. // Empty error
  17. {
  18. Input: Err,
  19. Code: 200,
  20. Str: "",
  21. JSON: `{"message":""}`,
  22. },
  23. // Standard errors
  24. {
  25. Input: ErrPermanentRedirect,
  26. Code: 308,
  27. Str: "Permanent Redirect",
  28. JSON: `{"message":"Permanent Redirect"}`,
  29. },
  30. {
  31. Input: ErrNotFound,
  32. Code: 404,
  33. Str: "Not Found",
  34. JSON: `{"message":"Not Found"}`,
  35. },
  36. {
  37. Input: ErrInternalServerError,
  38. Code: 500,
  39. Str: "Internal Server Error",
  40. JSON: `{"message":"Internal Server Error"}`,
  41. },
  42. // Error with changed message
  43. {
  44. Input: ErrBadRequest.WithMessage("Invalid Recipe"),
  45. Code: 400,
  46. Str: "Invalid Recipe",
  47. JSON: `{"message":"Invalid Recipe"}`,
  48. },
  49. // Error with data
  50. {
  51. Input: ErrGatewayTimeout.WithData(map[string]any{"service": "RecipeDatabase"}),
  52. Code: 504,
  53. Str: "Gateway Timeout",
  54. JSON: `{"message":"Gateway Timeout","data":{"service":"RecipeDatabase"}}`,
  55. },
  56. // Error with value
  57. {
  58. Input: ErrGatewayTimeout.WithValue("service", "RecipeDatabase"),
  59. Code: 504,
  60. Str: "Gateway Timeout",
  61. JSON: `{"message":"Gateway Timeout","data":{"service":"RecipeDatabase"}}`,
  62. },
  63. // Error with error
  64. {
  65. Input: ErrInternalServerError.WithError(errors.New("recipe is too delicious")),
  66. Code: 500,
  67. Str: "Internal Server Error",
  68. JSON: `{"message":"Internal Server Error","data":{"error":"recipe is too delicious"}}`,
  69. },
  70. }
  71. func TestErrorWrite(t *testing.T) {
  72. for i, tc := range errorTestCases {
  73. t.Logf("(%d) Testing %v", i, tc.Input)
  74. rec := httptest.NewRecorder()
  75. _, err := tc.Input.Write(rec)
  76. if err != tc.Err {
  77. t.Errorf("Expected error %v, got %v", tc.Err, err)
  78. }
  79. if err != nil {
  80. continue
  81. }
  82. res := rec.Result()
  83. if res.StatusCode != tc.Code {
  84. t.Errorf("Expected status code %d, got %d", tc.Code, res.StatusCode)
  85. }
  86. body, err := io.ReadAll(res.Body)
  87. if err != nil {
  88. t.Errorf("Unexpected error reading response body: %v", err)
  89. continue
  90. }
  91. if string(body) != tc.Str {
  92. t.Errorf("Expected body %q, got %q", tc.Str, string(body))
  93. }
  94. }
  95. }
  96. func TestErrorWriteJSON(t *testing.T) {
  97. for i, tc := range errorTestCases {
  98. t.Logf("(%d) Testing %v", i, tc.Input)
  99. rec := httptest.NewRecorder()
  100. err := tc.Input.WriteJSON(rec)
  101. if err != tc.Err {
  102. t.Errorf("Expected error %v, got %v", tc.Err, err)
  103. }
  104. if err != nil {
  105. continue
  106. }
  107. res := rec.Result()
  108. if res.StatusCode != tc.Code {
  109. t.Errorf("Expected status code %d, got %d", tc.Code, res.StatusCode)
  110. }
  111. body, err := io.ReadAll(res.Body)
  112. if err != nil {
  113. t.Errorf("Unexpected error reading response body: %v", err)
  114. continue
  115. }
  116. if string(body) != tc.JSON {
  117. t.Errorf("Expected body %q, got %q", tc.JSON, string(body))
  118. }
  119. }
  120. }