Skip to content

Commit ecfa52b

Browse files
committed
Use correct content-type.
1 parent dd75477 commit ecfa52b

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

lib/grape/middleware/formatter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def after
8484
encode_txt(body)
8585
end
8686
end
87-
headers['Content-Type'] = 'application/json'
87+
headers['Content-Type'] = content_types[env['api.format']]
8888
Rack::Response.new(bodymap, status, headers).to_a
8989
end
9090

@@ -103,4 +103,4 @@ def encode_txt(object)
103103
end
104104
end
105105
end
106-
end
106+
end

spec/grape/middleware/formatter_spec.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,24 @@ def serializable_hash
8484
subject.env['api.format'].should == :json
8585
end
8686
end
87-
end
87+
88+
context 'Content-type' do
89+
it 'should be set for json' do
90+
_, headers, _ = subject.call({'PATH_INFO' => '/info.json'})
91+
headers['Content-type'].should == 'application/json'
92+
end
93+
it 'should be set for xml' do
94+
_, headers, _ = subject.call({'PATH_INFO' => '/info.xml'})
95+
headers['Content-type'].should == 'application/xml'
96+
end
97+
it 'should be set for txt' do
98+
_, headers, _ = subject.call({'PATH_INFO' => '/info.txt'})
99+
headers['Content-type'].should == 'text/plain'
100+
end
101+
it 'should be set for custom' do
102+
subject.options[:content_types][:custom] = 'application/x-custom'
103+
_, headers, _ = subject.call({'PATH_INFO' => '/info.custom'})
104+
headers['Content-type'].should == 'application/x-custom'
105+
end
106+
end
107+
end

0 commit comments

Comments
 (0)