Skip to content

Commit

Permalink
Merge pull request #332 from htwroclau/no-validate-response-of-304-st…
Browse files Browse the repository at this point in the history
…atus

Don't validate response for status 304(Not Modified)
  • Loading branch information
ota42y authored Sep 5, 2021
2 parents 50b0be3 + e9b116b commit 1d9b924
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
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

0 comments on commit 1d9b924

Please sign in to comment.