http_helpers_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package errdefs
  2. import (
  3. "fmt"
  4. "net/http"
  5. "testing"
  6. "gotest.tools/v3/assert"
  7. )
  8. func TestFromStatusCode(t *testing.T) {
  9. testErr := fmt.Errorf("some error occurred")
  10. testCases := []struct {
  11. err error
  12. status int
  13. check func(error) bool
  14. }{
  15. {
  16. err: testErr,
  17. status: http.StatusNotFound,
  18. check: IsNotFound,
  19. },
  20. {
  21. err: testErr,
  22. status: http.StatusBadRequest,
  23. check: IsInvalidParameter,
  24. },
  25. {
  26. err: testErr,
  27. status: http.StatusConflict,
  28. check: IsConflict,
  29. },
  30. {
  31. err: testErr,
  32. status: http.StatusUnauthorized,
  33. check: IsUnauthorized,
  34. },
  35. {
  36. err: testErr,
  37. status: http.StatusServiceUnavailable,
  38. check: IsUnavailable,
  39. },
  40. {
  41. err: testErr,
  42. status: http.StatusForbidden,
  43. check: IsForbidden,
  44. },
  45. {
  46. err: testErr,
  47. status: http.StatusNotModified,
  48. check: IsNotModified,
  49. },
  50. {
  51. err: testErr,
  52. status: http.StatusNotImplemented,
  53. check: IsNotImplemented,
  54. },
  55. {
  56. err: testErr,
  57. status: http.StatusInternalServerError,
  58. check: IsSystem,
  59. },
  60. {
  61. err: Unknown(testErr),
  62. status: http.StatusInternalServerError,
  63. check: IsUnknown,
  64. },
  65. {
  66. err: DataLoss(testErr),
  67. status: http.StatusInternalServerError,
  68. check: IsDataLoss,
  69. },
  70. {
  71. err: Deadline(testErr),
  72. status: http.StatusInternalServerError,
  73. check: IsDeadline,
  74. },
  75. {
  76. err: Cancelled(testErr),
  77. status: http.StatusInternalServerError,
  78. check: IsCancelled,
  79. },
  80. }
  81. for _, tc := range testCases {
  82. t.Run(http.StatusText(tc.status), func(t *testing.T) {
  83. err := FromStatusCode(tc.err, tc.status)
  84. assert.Check(t, tc.check(err), "unexpected error-type %T", err)
  85. })
  86. }
  87. }