Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't validate response for status 304(Not Modified) #332

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/committee/middleware/response_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ def handle(request)

class << self
def validate?(status, validate_success_only)
status != 204 && (!validate_success_only || (200...300).include?(status))
case status
when 204
false
when 200..299
true
when 304
false
else
!validate_success_only
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(link, options = {})
end

def call(status, headers, data)
unless status == 204 # 204 No Content
unless [204, 304].include?(status) # 204 No Content or 304 Not Modified
response = Rack::Response.new(data, status, headers)
check_content_type!(response)
end
Expand Down
6 changes: 6 additions & 0 deletions test/middleware/response_validation_open_api_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ def app
assert_equal 204, last_response.status
end

it "passes through a 304 (not modified) response" do
@app = new_response_rack("", {}, {schema: open_api_3_schema}, {status: 304})
post "/validate"
assert_equal 304, last_response.status
end

it "passes through a valid response with prefix" do
@app = new_response_rack(JSON.generate(CHARACTERS_RESPONSE), {}, schema: open_api_3_schema, prefix: "/v1")
get "/v1/characters"
Expand Down
6 changes: 6 additions & 0 deletions test/middleware/response_validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ def app
assert_equal 204, last_response.status
end

it "passes through a 304 (not modified) response" do
@app = new_rack_app("", {}, app_status: 304, schema: hyper_schema)
get "/apps"
assert_equal 304, last_response.status
end

it "skip validation when 4xx" do
@app = new_rack_app("[{x:y}]", {}, schema: hyper_schema, validate_success_only: true, app_status: 400)
get "/apps"
Expand Down
10 changes: 10 additions & 0 deletions test/schema_validator/hyper_schema/response_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
call
end

it "passes through a 304 Not Modified response" do
@status, @headers, @data = 304, {}, nil
call
end

it "passes through a valid list response for for rel instances links" do
@link = @list_link

Expand Down Expand Up @@ -91,6 +96,11 @@
call
end

it "allows no Content-Type for 304 Not Modified" do
@status, @headers = 304, {}
call
end

it "raises errors generated by json_schema" do
@data.merge!("name" => "%@!")
e = assert_raises(Committee::InvalidResponse) { call }
Expand Down
5 changes: 5 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 @@ -67,6 +67,11 @@
call_response_validator
end

it "passes through a 304 Not Modified response" do
@status, @headers, @data = 304, {}, nil
call_response_validator
end

private

def call_response_validator(strict = false)
Expand Down