1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Handle Content-Types that are not :json, :xml, or :url_encoded_form

In c546a2b this was changed to mimic how the browser behaves in a real
situation but left out types that were registered.

When this was changed it didn't take `text/plain` or `text/html` content
types into account. This is a problem if you're manipulating the
`Content-Type` headers in your controller tests, and expect a certain
result.

The reason I changed this to use `to_sym` is because if the
`Content-Type` is not registered then the symbol will not exist. If it's
one of the special types we handle that specifically (:json, :xml, or
:url_encoded_form). If it's any registered type we handle it by setting
the `path_parameters` and then the `request_parameters`. If the `to_sym`
returns nil an error will be thrown.

If the controller test sets a `Content-Type` on the request that `Content-Type`
should remain in the header and pass along the filename.

For example:
If a test sets a content type on a post
```
@request.headers['CONTENT_TYPE'] = 'text/plain'
post :create, params: { name: 'foo.txt' }
```

Then `foo.txt` should be in the `request_parameters` and params related
to the path should be in the `path_parameters` and the `Content-Type`
header should match the one set in the `@request`. When c546a2b was
committed `text/plain` and `text/html` types were throwing a "Unknown
Content-Type" error which is misleading and incorrect.

Note: this does not affect how this is handled in the browser, just how
the controller tests handle setting `Content-Type`.
This commit is contained in:
eileencodes 2015-09-08 11:43:10 -04:00
parent d73d1a26b3
commit 43d7a03aef
2 changed files with 30 additions and 2 deletions

View file

@ -78,7 +78,9 @@ module ActionController
# params parser middleware, and we should remove this roundtripping
# when we switch to caling `call` on the controller
case content_mime_type.ref
case content_mime_type.to_sym
when nil
raise "Unknown Content-Type: #{content_type}"
when :json
data = ActiveSupport::JSON.encode(non_path_parameters)
params = ActiveSupport::JSON.decode(data).with_indifferent_access
@ -90,7 +92,8 @@ module ActionController
when :url_encoded_form
data = non_path_parameters.to_query
else
raise "Unknown Content-Type: #{content_type}"
data = non_path_parameters.to_query
self.request_parameters = non_path_parameters
end
end

View file

@ -627,6 +627,31 @@ XML
assert_equal "application/json", parsed_env["CONTENT_TYPE"]
end
def test_mutating_content_type_headers_for_plain_text_files_sets_the_header
@request.headers['Content-Type'] = 'text/plain'
post :render_body, params: { name: 'foo.txt' }
assert_equal 'text/plain', @request.headers['Content-type']
assert_equal 'foo.txt', @request.request_parameters[:name]
assert_equal 'render_body', @request.path_parameters[:action]
end
def test_mutating_content_type_headers_for_html_files_sets_the_header
@request.headers['Content-Type'] = 'text/html'
post :render_body, params: { name: 'foo.html' }
assert_equal 'text/html', @request.headers['Content-type']
assert_equal 'foo.html', @request.request_parameters[:name]
assert_equal 'render_body', @request.path_parameters[:action]
end
def test_mutating_content_type_headers_for_non_registered_mime_type_raises_an_error
assert_raises(RuntimeError) do
@request.headers['Content-Type'] = 'type/fake'
post :render_body, params: { name: 'foo.fake' }
end
end
def test_id_converted_to_string
get :test_params, params: {
id: 20, foo: Object.new