diff --git a/internal/common/errors.go b/internal/common/errors.go index b7bfdbb..f93997b 100644 --- a/internal/common/errors.go +++ b/internal/common/errors.go @@ -17,10 +17,15 @@ func Error(status int, title string, detail string) ProblemJSONError { func CustomErrorHandler(ctx fiber.Ctx, err error) error { var problemJSON *ProblemJSONError - // Retrieve the custom status code if it's a fiber.*Error - var e *fiber.Error - if errors.Is(err, e) { - problemJSON = &ProblemJSONError{Status: e.Code, Title: e.Message} + // Retrieve the custom status code if it's a fiber.Error. + fiberError, ok := errors.AsType[*fiber.Error](err) + if ok { + problemJSON = &ProblemJSONError{ + Code: "", + Title: fiberError.Message, + Detail: "", + Status: fiberError.Code, + } } if problemJSON == nil { diff --git a/main_test.go b/main_test.go index 4d049b0..f5dad04 100644 --- a/main_test.go +++ b/main_test.go @@ -30,6 +30,7 @@ type TestCase struct { expectedCode int expectedBody string expectedContentType string + expectedHeaders map[string]string validateFunc func(t *testing.T, response map[string]any) } @@ -107,6 +108,9 @@ func runTestCases(t *testing.T, tests []TestCase) { } assert.Equal(t, test.expectedContentType, res.Header.Get("Content-Type")) + for name, expectedValue := range test.expectedHeaders { + assert.Equal(t, expectedValue, res.Header.Get(name)) + } }) } } @@ -118,7 +122,7 @@ func TestApi(t *testing.T) { query: "GET /v1/i-dont-exist", expectedCode: 404, - expectedBody: `{"title":"Not Found","detail":"Not Found","status":404}`, + expectedBody: `{"title":"Not Found","status":404}`, expectedContentType: "application/problem+json", }, } @@ -131,6 +135,16 @@ func TestValidateEndpoint(t *testing.T) { invalidYml := loadTestdata(t, "invalid.publiccode.yml") tests := []TestCase{ + { + description: "validate: unsupported method", + query: "GET /v1/validate", + expectedCode: 405, + expectedBody: `{"title":"Method Not Allowed","status":405}`, + expectedContentType: "application/problem+json", + expectedHeaders: map[string]string{ + "Allow": "POST, QUERY", + }, + }, { description: "validate: empty body", query: "QUERY /v1/validate", diff --git a/publiccode-validator-api.oas.yaml b/publiccode-validator-api.oas.yaml index c4e3cb4..772d6de 100644 --- a/publiccode-validator-api.oas.yaml +++ b/publiccode-validator-api.oas.yaml @@ -105,6 +105,21 @@ paths: title: empty body detail: need a body to validate status: 400 + "405": + description: Method not allowed + headers: + Allow: + description: Supported HTTP methods for this endpoint + schema: + type: string + example: POST, QUERY + content: + application/problem+json: + schema: + $ref: "#/components/schemas/ProblemJSON" + example: + title: Method Not Allowed + status: 405 "404": description: Not found content: @@ -113,7 +128,6 @@ paths: $ref: "#/components/schemas/ProblemJSON" example: title: Not Found - detail: Not Found status: 404 components: