Skip to content

Commit bc98c3e

Browse files
author
Michael Bleigh
committed
Merge pull request ruby-grape#96 from dblock/frontier-broken-format
frontier: bug: .format is broken, can't serve .txt or respond with params[:format]
2 parents dfae958 + f81b329 commit bc98c3e

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

lib/grape/endpoint.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def mount_in(route_set)
3333
options[:method].each do |method|
3434
options[:path].each do |path|
3535
prepared_path = prepare_path(path)
36-
path = compile_path(path, !options[:app])
36+
path = compile_path(prepared_path, !options[:app])
3737
regex = Rack::Mount::RegexpWithNamedGroups.new(path)
3838
path_params = regex.named_captures.map { |nc| nc[0] } - [ 'version', 'format' ]
3939
path_params |= (options[:route_options][:params] || [])
@@ -71,11 +71,10 @@ def namespace
7171
Rack::Mount::Utils.normalize_path(settings.stack.map{|s| s[:namespace]}.join('/'))
7272
end
7373

74-
def compile_path(path, anchor = true)
74+
def compile_path(prepared_path, anchor = true)
7575
endpoint_options = {}
7676
endpoint_options[:version] = /#{settings[:version].join('|')}/ if settings[:version]
77-
78-
Rack::Mount::Strexp.compile(prepare_path(path), endpoint_options, %w( / . ? ), anchor)
77+
Rack::Mount::Strexp.compile(prepared_path, endpoint_options, %w( / . ? ), anchor)
7978
end
8079

8180
def call(env)

spec/grape/api_spec.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,24 @@ def app; subject end
175175
last_response.body.should eql 'foo'
176176
end
177177

178-
it 'should allow for format' do
179-
subject.get("/abc") do
180-
"json"
178+
context "format" do
179+
before(:each) do
180+
subject.get("/abc") do
181+
params[:format]
182+
end
183+
end
184+
185+
it "should allow .json" do
186+
get '/abc.json'
187+
last_response.status.should == 200
188+
last_response.body.should eql '"json"' # json-encoded symbol
181189
end
182190

183-
get '/abc.json'
184-
last_response.body.should eql '"json"'
191+
it "should allow .txt" do
192+
get '/abc.txt'
193+
last_response.status.should == 200
194+
last_response.body.should eql "txt" # raw text
195+
end
185196
end
186197

187198
it 'should allow for format without corrupting a param' do

0 commit comments

Comments
 (0)