Skip to content

Commit

Permalink
Rename :only to :accept_request_filter
Browse files Browse the repository at this point in the history
  • Loading branch information
maurobellati committed Nov 11, 2019
1 parent f5b79fe commit deb6df0
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Non-boolean options:
|prefix| String | supported | supported | Mounts the middleware to respond at a configured prefix. (e.g. prefix is '/v1' and request path is '/v1/test' use '/test' definition). |
|schema_path| String | supported | supported | Defines the location of the schema file to use for validation. |
|error_handler| Proc Object | supported | supported | A proc which will be called when error occurs. Take an Error instance as first argument, and request.env as second argument. (e.g. `-> (ex, env) { Raven.capture_exception(ex, extra: { rack_env: env }) }`) |
|only | Proc Object | supported | supported | A proc that accepts a Request and returns a boolean. It indicates whether to validate the current request, or not. (e.g. `-> (request) { request.path.start_with?('/something') }`) |
|accept_request_filter | Proc Object | supported | supported | A proc that accepts a Request and returns a boolean. It indicates whether to validate the current request, or not. (e.g. `-> (request) { request.path.start_with?('/something') }`) |

Note that Hyper-Schema and OpenAPI 2 get the same defaults for options.

Expand Down
8 changes: 2 additions & 6 deletions lib/committee/middleware/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ def initialize(app, options={})
@schema = self.class.get_schema(options)

@router = @schema.build_router(options)
@only = options[:only] || -> (_) { true }
@accept_request_filter = options[:accept_request_filter] || -> (_) { true }
end

def call(env)
request = Rack::Request.new(env)

if @router.includes_request?(request) && should_handle?(request)
if @router.includes_request?(request) && @accept_request_filter.call(request)
handle(request)
else
@app.call(request.env)
Expand Down Expand Up @@ -50,10 +50,6 @@ def get_schema(options)
def build_schema_validator(request)
@router.build_schema_validator(request)
end

def should_handle?(request)
@only.call(request)
end
end
end
end
10 changes: 5 additions & 5 deletions test/middleware/request_validation_open_api_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,14 @@ def app
end
end

describe ':only' do
describe ':accept_request_filter' do
[
{ description: 'when not specified, includes everything', only: nil, expected: { status: 400 } },
{ description: 'when predicate matches, performs validation', only: -> (request) { request.path.start_with?('/v1/c') }, expected: { status: 400 } },
{ description: 'when predicate does not match, skips validation', only: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
{ description: 'when not specified, includes everything', accept_request_filter: nil, expected: { status: 400 } },
{ description: 'when predicate matches, performs validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/c') }, expected: { status: 400 } },
{ description: 'when predicate does not match, skips validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
].each do |description:, only:, expected:|
it description do
@app = new_rack_app(prefix: '/v1', schema: open_api_3_schema, only: only)
@app = new_rack_app(prefix: '/v1', schema: open_api_3_schema, accept_request_filter: only)

post 'v1/characters', JSON.generate(string_post_1: 1)

Expand Down
10 changes: 5 additions & 5 deletions test/middleware/request_validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,14 +480,14 @@ def app
assert_equal 200, last_response.status
end

describe ':only' do
describe ':accept_request_filter' do
[
{ description: 'when not specified, includes everything', only: nil, expected: { status: 400 } },
{ description: 'when predicate matches, performs validation', only: -> (request) { request.path.start_with?('/v1/a') }, expected: { status: 400 } },
{ description: 'when predicate does not match, skips validation', only: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
{ description: 'when not specified, includes everything', accept_request_filter: nil, expected: { status: 400 } },
{ description: 'when predicate matches, performs validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/a') }, expected: { status: 400 } },
{ description: 'when predicate does not match, skips validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
].each do |description:, only:, expected:|
it description do
@app = new_rack_app(prefix: '/v1', schema: hyper_schema, only: only)
@app = new_rack_app(prefix: '/v1', schema: hyper_schema, accept_request_filter: only)

post '/v1/apps', '{x:y}'

Expand Down
10 changes: 5 additions & 5 deletions test/middleware/response_validation_open_api_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ def app
end
end

describe ':only' do
describe ':accept_request_filter' do
[
{ description: 'when not specified, includes everything', only: nil, expected: { status: 500 } },
{ description: 'when predicate matches, performs validation', only: -> (request) { request.path.start_with?('/v1/c') }, expected: { status: 500 } },
{ description: 'when predicate does not match, skips validation', only: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
{ description: 'when not specified, includes everything', accept_request_filter: nil, expected: { status: 500 } },
{ description: 'when predicate matches, performs validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/c') }, expected: { status: 500 } },
{ description: 'when predicate does not match, skips validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
].each do |description:, only:, expected:|
it description do
@app = new_response_rack('not_json', {}, schema: open_api_3_schema, prefix: '/v1', only: only)
@app = new_response_rack('not_json', {}, schema: open_api_3_schema, prefix: '/v1', accept_request_filter: only)

get 'v1/characters'

Expand Down
10 changes: 5 additions & 5 deletions test/middleware/response_validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ def app
assert_match(/valid JSON/i, last_response.body)
end

describe ':only' do
describe ':accept_request_filter' do
[
{ description: 'when not specified, includes everything', only: nil, expected: { status: 500 } },
{ description: 'when predicate matches, performs validation', only: -> (request) { request.path.start_with?('/v1/a') }, expected: { status: 500 } },
{ description: 'when predicate does not match, skips validation', only: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
{ description: 'when not specified, includes everything', accept_request_filter: nil, expected: { status: 500 } },
{ description: 'when predicate matches, performs validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/a') }, expected: { status: 500 } },
{ description: 'when predicate does not match, skips validation', accept_request_filter: -> (request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
].each do |description:, only:, expected:|
it description do
@app = new_rack_app('not_json', {}, schema: hyper_schema, prefix: '/v1', only: only)
@app = new_rack_app('not_json', {}, schema: hyper_schema, prefix: '/v1', accept_request_filter: only)

get '/v1/apps'

Expand Down

0 comments on commit deb6df0

Please sign in to comment.