Skip to content

Commit

Permalink
Check the media type instead of content type
Browse files Browse the repository at this point in the history
content_type return the entire header in Rails 6 and we only want to
check the media type
  • Loading branch information
rafaelfranca committed Jun 21, 2019
1 parent 0937821 commit eabbd11
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions test/action_controller/respond_with_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,19 @@ def test_respond_with_shouldnt_modify_original_hash
def test_using_resource
@request.accept = "application/xml"
get :using_resource
assert_equal "application/xml", @response.content_type
assert_equal "application/xml", @response.media_type
assert_equal "<name>david</name>", @response.body

@request.accept = "application/json"
get :using_resource
assert_equal "application/json", @response.content_type
assert_equal "application/json", @response.media_type
assert_equal "{\"name\":\"david\",\"id\":13}", @response.body
end

def test_using_resource_with_js_simply_tries_to_render_the_template
@request.accept = "text/javascript"
get :using_resource
assert_equal "text/javascript", @response.content_type
assert_equal "text/javascript", @response.media_type
assert_equal "alert(\"Hi\");", @response.body
end

Expand All @@ -179,12 +179,12 @@ def test_using_hash_resource_with_js_raises_an_error_if_template_cant_be_found
def test_using_hash_resource
@request.accept = "application/xml"
get :using_hash_resource
assert_equal "application/xml", @response.content_type
assert_equal "application/xml", @response.media_type
assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n <name>david</name>\n</hash>\n", @response.body

@request.accept = "application/json"
get :using_hash_resource
assert_equal "application/json", @response.content_type
assert_equal "application/json", @response.media_type
assert @response.body.include?("result")
assert @response.body.include?('"name":"david"')
assert @response.body.include?('"id":13')
Expand All @@ -200,23 +200,23 @@ def test_using_hash_resource_with_post
def test_using_resource_with_block
@request.accept = "*/*"
get :using_resource_with_block
assert_equal "text/html", @response.content_type
assert_equal "text/html", @response.media_type
assert_equal 'Hello world!', @response.body

@request.accept = "text/csv"
get :using_resource_with_block
assert_equal "text/csv", @response.content_type
assert_equal "text/csv", @response.media_type
assert_equal "CSV", @response.body

@request.accept = "application/xml"
get :using_resource
assert_equal "application/xml", @response.content_type
assert_equal "application/xml", @response.media_type
assert_equal "<name>david</name>", @response.body
end

def test_using_resource_with_overwrite_block
get :using_resource_with_overwrite_block
assert_equal "text/html", @response.content_type
assert_equal "text/html", @response.media_type
assert_equal "HTML", @response.body
end

Expand All @@ -235,7 +235,7 @@ def test_not_acceptable
def test_using_resource_for_post_with_html_redirects_on_success
with_test_route_set do
post :using_resource
assert_equal "text/html", @response.content_type
assert_equal "text/html", @response.media_type
assert_equal 302, @response.status
assert_equal "http://www.example.com/customers/13", @response.location
assert @response.redirect?
Expand All @@ -247,7 +247,7 @@ def test_using_resource_for_post_with_html_rerender_on_failure
errors = { :name => :invalid }
Customer.any_instance.stubs(:errors).returns(errors)
post :using_resource
assert_equal "text/html", @response.content_type
assert_equal "text/html", @response.media_type
assert_equal 200, @response.status
assert_equal "New world!\n", @response.body
assert_nil @response.location
Expand All @@ -258,7 +258,7 @@ def test_using_resource_for_post_with_xml_yields_created_on_success
with_test_route_set do
@request.accept = "application/xml"
post :using_resource
assert_equal "application/xml", @response.content_type
assert_equal "application/xml", @response.media_type
assert_equal 201, @response.status
assert_equal "<name>david</name>", @response.body
assert_equal "http://www.example.com/customers/13", @response.location
Expand All @@ -271,7 +271,7 @@ def test_using_resource_for_post_with_xml_yields_unprocessable_entity_on_failure
errors = { :name => :invalid }
Customer.any_instance.stubs(:errors).returns(errors)
post :using_resource
assert_equal "application/xml", @response.content_type
assert_equal "application/xml", @response.media_type
assert_equal 422, @response.status
assert_equal errors.to_xml, @response.body
assert_nil @response.location
Expand All @@ -284,7 +284,7 @@ def test_using_resource_for_post_with_json_yields_unprocessable_entity_on_failur
errors = { :name => :invalid }
Customer.any_instance.stubs(:errors).returns(errors)
post :using_resource
assert_equal "application/json", @response.content_type
assert_equal "application/json", @response.media_type
assert_equal 422, @response.status
errors = {:errors => errors}
assert_equal errors.to_json, @response.body
Expand All @@ -295,7 +295,7 @@ def test_using_resource_for_post_with_json_yields_unprocessable_entity_on_failur
def test_using_resource_for_patch_with_html_redirects_on_success
with_test_route_set do
patch :using_resource
assert_equal "text/html", @response.content_type
assert_equal "text/html", @response.media_type
assert_equal 302, @response.status
assert_equal "http://www.example.com/customers/13", @response.location
assert @response.redirect?
Expand All @@ -307,7 +307,7 @@ def test_using_resource_for_patch_with_html_rerender_on_failure
errors = { :name => :invalid }
Customer.any_instance.stubs(:errors).returns(errors)
patch :using_resource
assert_equal "text/html", @response.content_type
assert_equal "text/html", @response.media_type
assert_equal 200, @response.status
assert_equal "Edit world!\n", @response.body
assert_nil @response.location
Expand All @@ -320,7 +320,7 @@ def test_using_resource_for_patch_with_html_rerender_on_failure_even_on_method_o
Customer.any_instance.stubs(:errors).returns(errors)
@request.env["rack.methodoverride.original_method"] = "POST"
patch :using_resource
assert_equal "text/html", @response.content_type
assert_equal "text/html", @response.media_type
assert_equal 200, @response.status
assert_equal "Edit world!\n", @response.body
assert_nil @response.location
Expand All @@ -330,7 +330,7 @@ def test_using_resource_for_patch_with_html_rerender_on_failure_even_on_method_o
def test_using_resource_for_put_with_html_redirects_on_success
with_test_route_set do
put :using_resource
assert_equal "text/html", @response.content_type
assert_equal "text/html", @response.media_type
assert_equal 302, @response.status
assert_equal "http://www.example.com/customers/13", @response.location
assert @response.redirect?
Expand All @@ -343,7 +343,7 @@ def test_using_resource_for_put_with_html_rerender_on_failure
Customer.any_instance.stubs(:errors).returns(errors)
put :using_resource

assert_equal "text/html", @response.content_type
assert_equal "text/html", @response.media_type
assert_equal 200, @response.status
assert_equal "Edit world!\n", @response.body
assert_nil @response.location
Expand All @@ -356,7 +356,7 @@ def test_using_resource_for_put_with_html_rerender_on_failure_even_on_method_ove
Customer.any_instance.stubs(:errors).returns(errors)
@request.env["rack.methodoverride.original_method"] = "POST"
put :using_resource
assert_equal "text/html", @response.content_type
assert_equal "text/html", @response.media_type
assert_equal 200, @response.status
assert_equal "Edit world!\n", @response.body
assert_nil @response.location
Expand All @@ -382,7 +382,7 @@ def test_using_resource_for_put_with_xml_yields_unprocessable_entity_on_failure
errors = { :name => :invalid }
Customer.any_instance.stubs(:errors).returns(errors)
put :using_resource
assert_equal "application/xml", @response.content_type
assert_equal "application/xml", @response.media_type
assert_equal 422, @response.status
assert_equal errors.to_xml, @response.body
assert_nil @response.location
Expand All @@ -393,7 +393,7 @@ def test_using_resource_for_put_with_json_yields_unprocessable_entity_on_failure
errors = { :name => :invalid }
Customer.any_instance.stubs(:errors).returns(errors)
put :using_resource
assert_equal "application/json", @response.content_type
assert_equal "application/json", @response.media_type
assert_equal 422, @response.status
errors = {:errors => errors}
assert_equal errors.to_json, @response.body
Expand All @@ -404,7 +404,7 @@ def test_using_resource_for_delete_with_html_redirects_on_success
with_test_route_set do
Customer.any_instance.stubs(:destroyed?).returns(true)
delete :using_resource
assert_equal "text/html", @response.content_type
assert_equal "text/html", @response.media_type
assert_equal 302, @response.status
assert_equal "http://www.example.com/customers", @response.location
end
Expand Down Expand Up @@ -432,7 +432,7 @@ def test_using_resource_for_delete_with_html_redirects_on_failure
Customer.any_instance.stubs(:errors).returns(errors)
Customer.any_instance.stubs(:destroyed?).returns(false)
delete :using_resource
assert_equal "text/html", @response.content_type
assert_equal "text/html", @response.media_type
assert_equal 302, @response.status
assert_equal "http://www.example.com/customers", @response.location
end
Expand All @@ -441,7 +441,7 @@ def test_using_resource_for_delete_with_html_redirects_on_failure
def test_using_resource_with_parent_for_get
@request.accept = "application/xml"
get :using_resource_with_parent
assert_equal "application/xml", @response.content_type
assert_equal "application/xml", @response.media_type
assert_equal 200, @response.status
assert_equal "<name>david</name>", @response.body
end
Expand All @@ -451,15 +451,15 @@ def test_using_resource_with_parent_for_post
@request.accept = "application/xml"

post :using_resource_with_parent
assert_equal "application/xml", @response.content_type
assert_equal "application/xml", @response.media_type
assert_equal 201, @response.status
assert_equal "<name>david</name>", @response.body
assert_equal "http://www.example.com/quiz_stores/11/customers/13", @response.location

errors = { :name => :invalid }
Customer.any_instance.stubs(:errors).returns(errors)
post :using_resource
assert_equal "application/xml", @response.content_type
assert_equal "application/xml", @response.media_type
assert_equal 422, @response.status
assert_equal errors.to_xml, @response.body
assert_nil @response.location
Expand All @@ -469,7 +469,7 @@ def test_using_resource_with_parent_for_post
def test_using_resource_with_collection
@request.accept = "application/xml"
get :using_resource_with_collection
assert_equal "application/xml", @response.content_type
assert_equal "application/xml", @response.media_type
assert_equal 200, @response.status
assert_match(/<name>david<\/name>/, @response.body)
assert_match(/<name>jamis<\/name>/, @response.body)
Expand Down Expand Up @@ -520,7 +520,7 @@ def test_first_in_respond_to_has_higher_priority
@controller = InheritedRespondWithController.new
@request.accept = "*/*"
get :index
assert_equal "application/xml", @response.content_type
assert_equal "application/xml", @response.media_type
assert_equal "<name>david</name>", @response.body
end

Expand Down

0 comments on commit eabbd11

Please # to comment.