Skip to content

Commit

Permalink
strict response validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ota42y committed May 15, 2021
1 parent f009322 commit 3a11378
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/committee/middleware/response_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class ResponseValidation < Base

def initialize(app, options = {})
super
# TODO: show error message for old version
@strict = options[:strict]
@validate_success_only = @schema.validator_option.validate_success_only
end

Expand All @@ -15,7 +17,7 @@ def handle(request)

begin
v = build_schema_validator(request)
v.response_validate(status, headers, response) if v.link_exist? && self.class.validate?(status, validate_success_only)
v.response_validate(status, headers, response, @strict) if v.link_exist? && self.class.validate?(status, validate_success_only)

rescue Committee::InvalidResponse
handle_exception($!, request.env)
Expand Down
1 change: 1 addition & 0 deletions lib/committee/schema_validator/open_api_3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def response_validate(status, headers, response, test_method = false)
full_body
end

# TODO: refactoring name
strict = test_method
Committee::SchemaValidator::OpenAPI3::ResponseValidator.
new(@operation_object, validator_option).
Expand Down
14 changes: 14 additions & 0 deletions test/middleware/response_validation_open_api_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,20 @@ def app
end
end

it "strict and invalid status" do
@app = new_response_rack(JSON.generate(CHARACTERS_RESPONSE), {}, {schema: open_api_3_schema, strict: true}, {status: 201})
get "/characters"
assert_equal 500, last_response.status
end

it "strict and invalid status with raise" do
@app = new_response_rack(JSON.generate(CHARACTERS_RESPONSE), {}, {schema: open_api_3_schema, strict: true, raise: true}, {status: 201})

assert_raises(Committee::InvalidResponse) do
get "/characters"
end
end

private

def new_response_rack(response, headers = {}, options = {}, rack_options = {})
Expand Down
9 changes: 9 additions & 0 deletions test/schema_validator/open_api_3/response_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
assert_kind_of(OpenAPIParser::OpenAPIError, e.original_error)
end

it "raises InvalidResponse when a invalid status code with strict option" do
@status = 201
e = assert_raises(Committee::InvalidResponse) {
call_response_validator(true)
}

assert_kind_of(OpenAPIParser::OpenAPIError, e.original_error)
end

it "passes through a valid response with no Content-Type" do
@headers = {}
call_response_validator
Expand Down

0 comments on commit 3a11378

Please sign in to comment.