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

Return 405 correctly even if version is using as header and wrong request method #1362

Merged
merged 3 commits into from
Apr 11, 2016
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#### Fixes

* [#1357](https://github.com/ruby-grape/grape/pull/1357): Don't include fixed named captures as route params - [@namusyaka](https://github.com/namusyaka).
* [#1359](https://github.com/ruby-grape/grape/pull/1359): Return 404 even if version is using as header - [@namusyaka](https://github.com/namusyaka).
* [#1359](https://github.com/ruby-grape/grape/pull/1359): Avoid evaluating the same route twice - [@namusyaka](https://github.com/namusyaka), [@dblock](https://github.com/dblock).
* [#1361](https://github.com/ruby-grape/grape/pull/1361): Return 405 correctly even if version is using as header and wrong request method - [@namusyaka](https://github.com/namusyaka), [@dblock](https://github.com/dblock).

0.16.1 (4/3/2016)
=================
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def run

run_filters before_validations, :before_validation

run_validators validations, request unless @method_not_allowed
run_validators validations, request unless env[Grape::Env::GRAPE_METHOD_NOT_ALLOWED]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see it now :) Better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to audit the use if @variable elsewhere, we have at least an @app_response in a formatter middleware, @named_params in a DSL helper and @versions in routing.


run_filters after_validations, :after_validation

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/versioner/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Header < Base
def before
strict_header_checks if strict?

if media_type
if media_type || env[Grape::Env::GRAPE_METHOD_NOT_ALLOWED]
media_type_header_handler
elsif headers_contain_wrong_vendor?
fail_with_invalid_accept_header!('API vendor not found.')
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ def greedy_match?(input)
end

def method_not_allowed(env, methods, endpoint)
env[Grape::Env::GRAPE_METHOD_NOT_ALLOWED] = true
current = endpoint.dup
current.instance_eval do
namespace_inheritable_to_nil(:version)
@lazy_initialized = false
lazy_initialize!
run_filters befores, :before
@method_not_allowed = true
@block = proc do
fail Grape::Exceptions::MethodNotAllowed, header.merge('Allow' => methods)
end
Expand Down
1 change: 1 addition & 0 deletions lib/grape/util/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ module Env
GRAPE_REQUEST_HEADERS = 'grape.request.headers'.freeze
GRAPE_REQUEST_PARAMS = 'grape.request.params'.freeze
GRAPE_ROUTING_ARGS = 'grape.routing_args'.freeze
GRAPE_METHOD_NOT_ALLOWED = 'grape.method_not_allowed'.freeze
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,40 @@ def app
PatchHelpersSpec::Main
end

context 'default' do
context 'patch' do
it 'public' do
get '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-public-v1+json'
expect(last_response.status).to eq 200
expect(last_response.body).to eq({ ok: 'public' }.to_json)
patch '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-public-v1+json'
expect(last_response.status).to eq 405
end

it 'private' do
get '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-private-v1+json'
expect(last_response.status).to eq 200
expect(last_response.body).to eq({ ok: 'private' }.to_json)
patch '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-private-v1+json'
expect(last_response.status).to eq 405
end

it 'default' do
get '/'
expect(last_response.status).to eq 200
expect(last_response.body).to eq({ ok: 'public' }.to_json)
patch '/'
expect(last_response.status).to eq 405
end
end

context 'patch' do
context 'default' do
it 'public' do
patch '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-public-v1+json'
expect(last_response.status).to eq 405
get '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-public-v1+json'
expect(last_response.status).to eq 200
expect(last_response.body).to eq({ ok: 'public' }.to_json)
end

it 'private' do
patch '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-private-v1+json'
expect(last_response.status).to eq 405
get '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-private-v1+json'
expect(last_response.status).to eq 200
expect(last_response.body).to eq({ ok: 'private' }.to_json)
end

it 'default' do
patch '/'
expect(last_response.status).to eq 405
get '/'
expect(last_response.status).to eq 200
expect(last_response.body).to eq({ ok: 'public' }.to_json)
end
end
end