errors_test.go 1.8 KB

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