Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat as: :html tests request params as :url_encoded_form #50390

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions actionpack/lib/action_dispatch/testing/request_encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# :markup: markdown

require "nokogiri"
require "action_dispatch/http/mime_type"

module ActionDispatch
class RequestEncoder # :nodoc:
Expand All @@ -15,9 +16,9 @@ def response_parser; -> body { body }; end

@encoders = { identity: IdentityEncoder.new }

attr_reader :response_parser
attr_reader :response_parser, :content_type

def initialize(mime_name, param_encoder, response_parser)
def initialize(mime_name, param_encoder, response_parser, content_type)
@mime = Mime[mime_name]

unless @mime
Expand All @@ -27,10 +28,7 @@ def initialize(mime_name, param_encoder, response_parser)

@response_parser = response_parser || -> body { body }
@param_encoder = param_encoder || :"to_#{@mime.symbol}".to_proc
end

def content_type
@mime.to_s
@content_type = content_type || @mime.to_s
end

def accept_header
Expand All @@ -50,11 +48,13 @@ def self.encoder(name)
@encoders[name] || @encoders[:identity]
end

def self.register_encoder(mime_name, param_encoder: nil, response_parser: nil)
@encoders[mime_name] = new(mime_name, param_encoder, response_parser)
def self.register_encoder(mime_name, param_encoder: nil, response_parser: nil, content_type: nil)
@encoders[mime_name] = new(mime_name, param_encoder, response_parser, content_type)
end

register_encoder :html, response_parser: -> body { Rails::Dom::Testing.html_document.parse(body) }
register_encoder :html, response_parser: -> body { Rails::Dom::Testing.html_document.parse(body) },
param_encoder: -> param { param },
content_type: Mime[:url_encoded_form].to_s
register_encoder :json, response_parser: -> body { JSON.parse(body, object_class: ActiveSupport::HashWithIndifferentAccess) }
end
end
17 changes: 17 additions & 0 deletions actionpack/test/controller/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,12 @@ def foos
render plain: "ok"
end

def foos_html
render inline: <<~ERB
<code><%= params.permit(:foo) %></code>
ERB
end

def foos_json
render json: params.permit(:foo)
end
Expand Down Expand Up @@ -1127,6 +1133,17 @@ def test_standard_json_encoding_works
end
end

def test_encoding_as_html
post_to_foos as: :html do
assert_response :success
assert_equal "application/x-www-form-urlencoded", request.media_type
assert_equal "text/html", request.accepts.first.to_s
assert_equal :html, request.format.ref
assert_equal({ "foo" => "fighters" }, request.request_parameters)
assert_equal({ "foo" => "fighters" }.to_s, response.parsed_body.at("code").text)
end
end

def test_encoding_as_json
post_to_foos as: :json do
assert_response :success
Expand Down