http_helpers_test.go 1.6 KB

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