Don't mutate raw_source in mailer preview interceptor

The raw_source method is documented as returning the exact value that
was used to create the body; mutating it breaks that contract.

Additionally, if the value used to create the body is blank, raw_source
returns a frozen string which causes the interceptor to raise an error.
This commit is contained in:
Eugene Kenny 2017-01-31 03:36:23 +00:00 committed by Jeremy Daer
parent d1e0654649
commit ebededb372
No known key found for this signature in database
GPG Key ID: AB8F6399D5C60664
2 changed files with 35 additions and 5 deletions

View File

@ -26,7 +26,7 @@ module ActionMailer
def transform! #:nodoc: def transform! #:nodoc:
return message if html_part.blank? return message if html_part.blank?
html_source.gsub!(PATTERN) do |match| html_part.body = html_part.decoded.gsub(PATTERN) do |match|
if part = find_part(match[9..-2]) if part = find_part(match[9..-2])
%[src="#{data_url(part)}"] %[src="#{data_url(part)}"]
else else
@ -46,10 +46,6 @@ module ActionMailer
@html_part ||= message.html_part @html_part ||= message.html_part
end end
def html_source
html_part.body.raw_source
end
def data_url(part) def data_url(part)
"data:#{part.mime_type};base64,#{strict_encode64(part.body.raw_source)}" "data:#{part.mime_type};base64,#{strict_encode64(part.body.raw_source)}"
end end

View File

@ -671,6 +671,40 @@ module ApplicationTests
assert_match %r[<p>Hello, World!</p>], last_response.body assert_match %r[<p>Hello, World!</p>], last_response.body
end end
test "multipart mailer preview with empty parts" do
mailer "notifier", <<-RUBY
class Notifier < ActionMailer::Base
default from: "from@example.com"
def foo
mail to: "to@example.org"
end
end
RUBY
text_template "notifier/foo", <<-RUBY
RUBY
html_template "notifier/foo", <<-RUBY
RUBY
mailer_preview "notifier", <<-RUBY
class NotifierPreview < ActionMailer::Preview
def foo
Notifier.foo
end
end
RUBY
app("development")
get "/rails/mailers/notifier/foo?part=text/plain"
assert_equal 200, last_response.status
get "/rails/mailers/notifier/foo?part=text/html"
assert_equal 200, last_response.status
end
private private
def build_app def build_app
super super