Skip to content

Commit a0166e7

Browse files
committed
Return 405 even if version is using as header
Fixes #1355
1 parent 4b349ef commit a0166e7

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#### Fixes
1111

1212
* [#1357](https://github.com/ruby-grape/grape/pull/1357): Don't include fixed named captures as route params - [@namusyaka](https://github.com/namusyaka).
13+
* [#1359](https://github.com/ruby-grape/grape/pull/1359): Return 404 even if version is using as header - [@namusyaka](https://github.com/namusyaka).
1314

1415
0.16.1 (4/3/2016)
1516
=================

lib/grape/router.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ def greedy_match?(input)
124124
def method_not_allowed(env, methods, endpoint)
125125
current = endpoint.dup
126126
current.instance_eval do
127+
namespace_inheritable_to_nil(:version)
128+
@lazy_initialized = false
127129
run_filters befores, :before
128130
@method_not_allowed = true
129131
@block = proc do
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
require 'spec_helper'
2+
3+
describe Grape::API::Helpers do
4+
module PatchHelpersSpec
5+
class PatchPublic < Grape::API
6+
format :json
7+
version 'public-v1', using: :header, vendor: 'grape'
8+
9+
get do
10+
{ ok: 'public' }
11+
end
12+
end
13+
14+
module AuthMethods
15+
def authenticate!
16+
end
17+
end
18+
19+
class PatchPrivate < Grape::API
20+
format :json
21+
version 'private-v1', using: :header, vendor: 'grape'
22+
23+
helpers AuthMethods
24+
25+
before do
26+
authenticate!
27+
end
28+
29+
get do
30+
{ ok: 'private' }
31+
end
32+
end
33+
34+
class Main < Grape::API
35+
mount PatchPublic
36+
mount PatchPrivate
37+
end
38+
end
39+
40+
def app
41+
PatchHelpersSpec::Main
42+
end
43+
44+
context 'default' do
45+
it 'public' do
46+
get '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-public-v1+json'
47+
expect(last_response.status).to eq 200
48+
expect(last_response.body).to eq({ ok: 'public' }.to_json)
49+
end
50+
51+
it 'private' do
52+
get '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-private-v1+json'
53+
expect(last_response.status).to eq 200
54+
expect(last_response.body).to eq({ ok: 'private' }.to_json)
55+
end
56+
57+
it 'default' do
58+
get '/'
59+
expect(last_response.status).to eq 200
60+
expect(last_response.body).to eq({ ok: 'public' }.to_json)
61+
end
62+
end
63+
64+
context 'patch' do
65+
it 'public' do
66+
patch '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-public-v1+json'
67+
expect(last_response.status).to eq 405
68+
end
69+
70+
it 'private' do
71+
patch '/', {}, 'HTTP_ACCEPT' => 'application/vnd.grape-private-v1+json'
72+
expect(last_response.status).to eq 405
73+
end
74+
75+
it 'default' do
76+
patch '/'
77+
expect(last_response.status).to eq 405
78+
end
79+
end
80+
end

0 commit comments

Comments
 (0)