|
| 1 | +package problem |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "github.com/gin-gonic/gin" |
| 6 | + "github.com/labstack/echo/v4" |
| 7 | + custom_errors "github.com/meysamhadeli/problem-details/samples/custom-errors" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func Test_BadRequest_Err(t *testing.T) { |
| 15 | + badRequestErr := New(http.StatusBadRequest, "bad-request", "We have a bad request error") |
| 16 | + |
| 17 | + assert.Equal(t, "We have a bad request error", badRequestErr.GetDetails()) |
| 18 | + assert.Equal(t, "bad-request", badRequestErr.GetTitle()) |
| 19 | + assert.Equal(t, "https://httpstatuses.io/400", badRequestErr.GetType()) |
| 20 | + assert.Equal(t, http.StatusBadRequest, badRequestErr.GetStatus()) |
| 21 | +} |
| 22 | + |
| 23 | +func TestMap_CustomType_Echo(t *testing.T) { |
| 24 | + |
| 25 | + e := echo.New() |
| 26 | + req := httptest.NewRequest(http.MethodGet, "http://echo_endpoint1", nil) |
| 27 | + rec := httptest.NewRecorder() |
| 28 | + c := e.NewContext(req, rec) |
| 29 | + |
| 30 | + err := echo_endpoint1(c) |
| 31 | + |
| 32 | + var problemErr ProblemDetailErr |
| 33 | + |
| 34 | + Map[custom_errors.BadRequestError](func() ProblemDetailErr { |
| 35 | + problemErr = New(http.StatusBadRequest, "bad-request", err.Error()) |
| 36 | + return problemErr |
| 37 | + }) |
| 38 | + |
| 39 | + _ = ResolveProblemDetails(c.Response(), c.Request(), err) |
| 40 | + |
| 41 | + assert.Equal(t, c.Response().Status, http.StatusBadRequest) |
| 42 | + assert.Equal(t, err.Error(), problemErr.GetDetails()) |
| 43 | + assert.Equal(t, "bad-request", problemErr.GetTitle()) |
| 44 | + assert.Equal(t, "https://httpstatuses.io/400", problemErr.GetType()) |
| 45 | + assert.Equal(t, http.StatusBadRequest, problemErr.GetStatus()) |
| 46 | +} |
| 47 | + |
| 48 | +func TestMap_Status_Echo(t *testing.T) { |
| 49 | + |
| 50 | + e := echo.New() |
| 51 | + req := httptest.NewRequest(http.MethodGet, "http://echo_endpoint2", nil) |
| 52 | + rec := httptest.NewRecorder() |
| 53 | + c := e.NewContext(req, rec) |
| 54 | + |
| 55 | + err := echo_endpoint2(c) |
| 56 | + |
| 57 | + var problemErr ProblemDetailErr |
| 58 | + |
| 59 | + // map status code to problem details error |
| 60 | + MapStatus(http.StatusBadGateway, func() ProblemDetailErr { |
| 61 | + problemErr = New(http.StatusUnauthorized, "unauthorized", err.Error()) |
| 62 | + return problemErr |
| 63 | + }) |
| 64 | + |
| 65 | + _ = ResolveProblemDetails(c.Response(), c.Request(), err) |
| 66 | + |
| 67 | + assert.Equal(t, c.Response().Status, http.StatusUnauthorized) |
| 68 | + assert.Equal(t, err.(*echo.HTTPError).Message.(error).Error(), problemErr.GetDetails()) |
| 69 | + assert.Equal(t, "unauthorized", problemErr.GetTitle()) |
| 70 | + assert.Equal(t, "https://httpstatuses.io/401", problemErr.GetType()) |
| 71 | + assert.Equal(t, http.StatusUnauthorized, problemErr.GetStatus()) |
| 72 | +} |
| 73 | + |
| 74 | +func TestMap_CustomType_Gin(t *testing.T) { |
| 75 | + |
| 76 | + gin.SetMode(gin.TestMode) |
| 77 | + w := httptest.NewRecorder() |
| 78 | + c, _ := gin.CreateTestContext(w) |
| 79 | + r := gin.Default() |
| 80 | + |
| 81 | + r.GET("/gin_endpoint1", func(ctx *gin.Context) { |
| 82 | + err := errors.New("We have a custom type error in our endpoint") |
| 83 | + customBadRequestError := custom_errors.BadRequestError{InternalError: err} |
| 84 | + _ = c.Error(customBadRequestError) |
| 85 | + }) |
| 86 | + |
| 87 | + req, _ := http.NewRequest(http.MethodGet, "/gin_endpoint1", nil) |
| 88 | + r.ServeHTTP(w, req) |
| 89 | + |
| 90 | + for _, err := range c.Errors { |
| 91 | + |
| 92 | + var problemErr ProblemDetailErr |
| 93 | + |
| 94 | + Map[custom_errors.BadRequestError](func() ProblemDetailErr { |
| 95 | + problemErr = New(http.StatusBadRequest, "bad-request", err.Error()) |
| 96 | + return problemErr |
| 97 | + }) |
| 98 | + |
| 99 | + _ = ResolveProblemDetails(w, req, err) |
| 100 | + |
| 101 | + assert.Equal(t, http.StatusBadRequest, problemErr.GetStatus()) |
| 102 | + assert.Equal(t, err.Error(), problemErr.GetDetails()) |
| 103 | + assert.Equal(t, "bad-request", problemErr.GetTitle()) |
| 104 | + assert.Equal(t, "https://httpstatuses.io/400", problemErr.GetType()) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestMap_Status_Gin(t *testing.T) { |
| 109 | + |
| 110 | + gin.SetMode(gin.TestMode) |
| 111 | + w := httptest.NewRecorder() |
| 112 | + c, _ := gin.CreateTestContext(w) |
| 113 | + r := gin.Default() |
| 114 | + |
| 115 | + r.GET("/gin_endpoint2", func(ctx *gin.Context) { |
| 116 | + err := errors.New("We have a specific status code error in our endpoint") |
| 117 | + // change status code 'StatusBadGateway' to 'StatusUnauthorized' base on handler config |
| 118 | + _ = c.AbortWithError(http.StatusBadGateway, err) |
| 119 | + }) |
| 120 | + |
| 121 | + req, _ := http.NewRequest(http.MethodGet, "/gin_endpoint2", nil) |
| 122 | + r.ServeHTTP(w, req) |
| 123 | + |
| 124 | + for _, err := range c.Errors { |
| 125 | + |
| 126 | + var problemErr ProblemDetailErr |
| 127 | + |
| 128 | + // map status code to problem details error |
| 129 | + MapStatus(http.StatusBadGateway, func() ProblemDetailErr { |
| 130 | + problemErr = New(http.StatusUnauthorized, "unauthorized", err.Error()) |
| 131 | + return problemErr |
| 132 | + }) |
| 133 | + |
| 134 | + _ = ResolveProblemDetails(w, req, err) |
| 135 | + |
| 136 | + assert.Equal(t, http.StatusUnauthorized, problemErr.GetStatus()) |
| 137 | + assert.Equal(t, err.Error(), problemErr.GetDetails()) |
| 138 | + assert.Equal(t, "unauthorized", problemErr.GetTitle()) |
| 139 | + assert.Equal(t, "https://httpstatuses.io/401", problemErr.GetType()) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func echo_endpoint1(c echo.Context) error { |
| 144 | + err := errors.New("We have a custom type error in our endpoint") |
| 145 | + return custom_errors.BadRequestError{InternalError: err} |
| 146 | +} |
| 147 | + |
| 148 | +func echo_endpoint2(c echo.Context) error { |
| 149 | + err := errors.New("We have a specific status code error in our endpoint") |
| 150 | + // change status code 'StatusBadGateway' to 'StatusUnauthorized' base on handler config |
| 151 | + return echo.NewHTTPError(http.StatusBadGateway, err) |
| 152 | +} |
0 commit comments