1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
# encoding: utf-8
# frozen_string_literal: true
require 'spec_helper'
def encode_base64(str)
Mail::Encodings::Base64.encode(str)
end
def check_decoded(actual, expected)
if RUBY_VERSION >= '1.9'
expect(actual.encoding).to eq Encoding::BINARY
expect(actual).to eq expected.dup.force_encoding(Encoding::BINARY)
else
expect(actual).to eq expected
end
end
describe "Attachments" do
before(:each) do
@mail = Mail.new
@test_png = File.open(fixture('attachments', 'test.png'), 'rb', &:read)
end
describe "from direct content" do
it "should work" do
@mail.attachments['test.png'] = @test_png
expect(@mail.attachments['test.png'].filename).to eq 'test.png'
check_decoded(@mail.attachments[0].decoded, @test_png)
end
it "should work out magically the mime_type" do
@mail.attachments['test.png'] = @test_png
expect(@mail.attachments[0].mime_type).to eq 'image/png'
end
it "should assign the filename" do
@mail.attachments['test.png'] = @test_png
expect(@mail.attachments[0].filename).to eq 'test.png'
end
it "should assign mime-encoded multibyte filename" do
@mail.attachments['てすと.txt'] = File.open(fixture('attachments', 'てすと.txt'), 'rb', &:read)
expect(Mail::Utilities.blank?(@mail.attachments)).not_to eq true
expect(Mail::Encodings.decode_encode(@mail.attachments[0].filename, :decode)).to eq 'てすと.txt'
end
end
describe "from a supplied Hash" do
it "should work" do
@mail.attachments['test.png'] = { :content => @test_png }
expect(@mail.attachments[0].filename).to eq 'test.png'
check_decoded(@mail.attachments[0].decoded, @test_png)
end
it "should allow you to override the content_type" do
@mail.attachments['test.png'] = { :content => @test_png,
:content_type => "application/x-gzip" }
expect(@mail.attachments[0].content_type).to eq 'application/x-gzip'
end
it "should allow you to override the mime_type" do
@mail.attachments['test.png'] = { :content => @test_png,
:mime_type => "application/x-gzip" }
expect(@mail.attachments[0].mime_type).to eq 'application/x-gzip'
end
it "should allow you to override the mime_type" do
@mail.attachments['invoice.jpg'] = { :data => "you smiling",
:mime_type => "image/x-jpg",
:transfer_encoding => "base64" }
expect(@mail.attachments[0].mime_type).to eq 'image/x-jpg'
end
end
describe "decoding and encoding" do
it "should set its content_transfer_encoding" do
@mail.attachments['test.png'] = { :content => @test_png }
@mail.ready_to_send!
expect(@mail.attachments[0].content_transfer_encoding).to eq 'base64'
end
it "should encode its body to base64" do
@mail.attachments['test.png'] = { :content => @test_png }
@mail.ready_to_send!
expect(@mail.attachments[0].encoded).to include(encode_base64(@test_png))
end
it "should allow you to pass in an encoded attachment with an encoding" do
encoded_data = encode_base64(@test_png)
@mail.attachments['test.png'] = { :content => encoded_data,
:encoding => 'base64' }
check_decoded(@mail.attachments[0].decoded, @test_png)
end
it "should allow you set a mime type and encoding without overriding the encoding" do
encoded = encode_base64('<foo/>')
@mail.attachments['test.png'] = { :mime_type => 'text/xml', :content => encoded, :encoding => 'base64' }
expect(@mail.attachments[0].content_transfer_encoding).to eq 'base64'
check_decoded(@mail.attachments[0].decoded, '<foo/>')
end
it "should not allow you to pass in an encoded attachment with an unknown encoding" do
base64_encoded_data = encode_base64(@test_png)
expect {@mail.attachments['test.png'] = { :content => base64_encoded_data,
:encoding => 'weird_encoding' }}.to raise_error(/Do not know how to handle Content Transfer Encoding weird_encoding/)
end
it "should be able to call read on the attachment to return the decoded data" do
@mail.attachments['test.png'] = { :content => @test_png }
if RUBY_VERSION >= '1.9'
expected = @mail.attachments[0].read.force_encoding(@test_png.encoding)
else
expected = @mail.attachments[0].read
end
expect(expected).to eq @test_png
end
it "should only add one newline between attachment body and boundary" do
contents = "I have\ntwo lines with trailing newlines\n\n"
@mail.attachments['text.txt'] = { :content => contents}
encoded = @mail.encoded
regex = /\r\n#{Regexp.escape(contents.gsub(/\n/, "\r\n"))}\r\n--#{@mail.boundary}--\r\n\Z/
expect(encoded).to match regex
end
end
describe "multiple attachments" do
it "should allow you to pass in more than one attachment" do
mail = Mail.new
mail.attachments['test.pdf'] = File.open(fixture('attachments', 'test.pdf'), 'rb', &:read)
mail.attachments['test.gif'] = File.open(fixture('attachments', 'test.gif'), 'rb', &:read)
mail.attachments['test.jpg'] = File.open(fixture('attachments', 'test.jpg'), 'rb', &:read)
mail.attachments['test.zip'] = File.open(fixture('attachments', 'test.zip'), 'rb', &:read)
expect(mail.attachments[0].filename).to eq 'test.pdf'
expect(mail.attachments[1].filename).to eq 'test.gif'
expect(mail.attachments[2].filename).to eq 'test.jpg'
expect(mail.attachments[3].filename).to eq 'test.zip'
end
end
describe "inline attachments" do
it "should set the content_disposition to inline or attachment as appropriate" do
mail = Mail.new
mail.attachments['test.pdf'] = File.open(fixture('attachments', 'test.pdf'), 'rb', &:read)
expect(mail.attachments['test.pdf'].content_disposition).to eq 'attachment; filename=test.pdf'
mail.attachments.inline['test.png'] = File.open(fixture('attachments', 'test.png'), 'rb', &:read)
expect(mail.attachments.inline['test.png'].content_disposition).to eq 'inline; filename=test.png'
end
it "should return a cid" do
mail = Mail.new
mail.attachments.inline['test.png'] = @test_png
expect(mail.attachments['test.png'].url).to eq "cid:#{mail.attachments['test.png'].cid}"
end
it "should respond true to inline?" do
mail = Mail.new
mail.attachments.inline['test.png'] = @test_png
expect(mail.attachments['test.png']).to be_inline
end
end
describe "getting the content ID from an attachment" do
before(:each) do
@mail = Mail.new
@mail.attachments['test.gif'] = File.open(fixture('attachments', 'test.gif'), 'rb', &:read)
@cid = @mail.attachments['test.gif'].content_id
end
it "should return a content-id for the attachment on creation if passed inline => true" do
expect(@cid).not_to be_nil
end
it "should return a valid content-id on inline attachments" do
expect(Mail::ContentIdField.new(@cid).errors).to be_empty
end
it "should provide a URL escaped content_id (without brackets) for use inside an email" do
@inline = @mail.attachments['test.gif'].cid
uri_parser = URI.const_defined?(:Parser) ? URI::Parser.new : URI
expect(@inline).to eq uri_parser.escape(@cid.gsub(/^</, '').gsub(/>$/, ''))
end
end
describe "setting the content type correctly" do
it "should set the content type to multipart/mixed if none given and you add an attachment" do
mail = Mail.new
mail.attachments['test.pdf'] = File.open(fixture('attachments', 'test.pdf'), 'rb', &:read)
mail.encoded
expect(mail.mime_type).to eq 'multipart/mixed'
end
it "allows you to set the attachment before the content type" do
mail = Mail.new
mail.attachments["test.png"] = File.open(fixture('attachments', 'test.png'), 'rb', &:read)
mail.body = "Lots of HTML"
mail.mime_version = '1.0'
mail.content_type = 'text/html; charset=UTF-8'
end
end
describe "should handle filenames with non-7bit characters correctly" do
it "should not raise an exception with a filename that contains a non-7bit-character" do
filename = "f\u00f6\u00f6.b\u00e4r"
if RUBY_VERSION >= '1.9'
expect(filename.encoding).to eq Encoding::UTF_8
end
mail = Mail.new
expect {
mail.attachments[filename] = File.open(fixture('attachments', 'test.pdf'), 'rb', &:read)
}.not_to raise_error
end
end
end
describe "reading emails with attachments" do
describe "test emails" do
it "should find the attachment using content location" do
mail = Mail.read(fixture(File.join('emails', 'attachment_emails', 'attachment_content_location.eml')))
expect(mail.attachments.length).to eq 1
end
it "should find an attachment defined with 'name' and Content-Disposition: attachment" do
mail = Mail.read(fixture(File.join('emails', 'attachment_emails', 'attachment_content_disposition.eml')))
expect(mail.attachments.length).to eq 1
end
it "should use the content-type filename or name over the content-disposition filename" do
mail = Mail.read(fixture(File.join('emails', 'attachment_emails', 'attachment_content_disposition.eml')))
expect(mail.attachments[0].filename).to eq 'hello.rb'
end
it "should decode an attachment" do
mail = Mail.read(fixture(File.join('emails', 'attachment_emails', 'attachment_pdf.eml')))
expect(mail.attachments[0].decoded.length).to eq 1026
end
it "should find an attachment that has an encoded name value" do
mail = Mail.read(fixture(File.join('emails', 'attachment_emails', 'attachment_with_encoded_name.eml')))
expect(mail.attachments.length).to eq 1
result = mail.attachments[0].filename
if RUBY_VERSION >= '1.9'
expected = "01 Quien Te Dij\212at. Pitbull.mp3".dup.force_encoding(result.encoding)
else
expected = "01 Quien Te Dij\212at. Pitbull.mp3"
end
expect(result).to eq expected
end
it "should find an attachment that has a name not surrounded by quotes" do
mail = Mail.read(fixture(File.join('emails', 'attachment_emails', "attachment_with_unquoted_name.eml")))
expect(mail.attachments.length).to eq 1
expect(mail.attachments.first.filename).to eq "This is a test.txt"
end
it "should find attachments inside parts with content-type message/rfc822" do
mail = Mail.read(fixture(File.join("emails",
"attachment_emails",
"attachment_message_rfc822.eml")))
expect(mail.attachments.length).to eq 1
expect(mail.attachments[0].decoded.length).to eq 1026
end
it "attach filename decoding (issue 83)" do
data = <<-limitMAIL
Subject: aaa
From: aaa@aaa.com
To: bbb@aaa.com
Content-Type: multipart/mixed; boundary=0016e64c0af257c3a7048b69e1ac
--0016e64c0af257c3a7048b69e1ac
Content-Type: multipart/alternative; boundary=0016e64c0af257c3a1048b69e1aa
--0016e64c0af257c3a1048b69e1aa
Content-Type: text/plain; charset=ISO-8859-1
aaa
--0016e64c0af257c3a1048b69e1aa
Content-Type: text/html; charset=ISO-8859-1
aaa<br>
--0016e64c0af257c3a1048b69e1aa--
--0016e64c0af257c3a7048b69e1ac
Content-Type: text/plain; charset=US-ASCII; name="=?utf-8?b?Rm90bzAwMDkuanBn?="
Content-Disposition: attachment; filename="=?utf-8?b?Rm90bzAwMDkuanBn?="
Content-Transfer-Encoding: base64
X-Attachment-Id: f_gbneqxxy0
YWFhCg==
--0016e64c0af257c3a7048b69e1ac--
limitMAIL
mail = Mail.new(data)
#~ puts Mail::Encodings.decode_encode(mail.attachments[0].filename, :decode)
expect(mail.attachments[0].filename).to eq "Foto0009.jpg"
end
end
end
describe "attachment order" do
it "should be preserved instead when content type exists" do
mail = Mail.new do
to "aaaa@aaaa.aaa"
from "aaaa2@aaaa.aaa"
subject "a subject"
date Time.now
text_part do
content_type 'text/plain; charset=UTF-8'
body "a \nsimplebody\n"
end
end
mail.attachments['test.zip'] = File.open(fixture('attachments', 'test.zip'), 'rb', &:read)
mail.attachments['test.pdf'] = File.open(fixture('attachments', 'test.pdf'), 'rb', &:read)
mail.attachments['test.gif'] = File.open(fixture('attachments', 'test.gif'), 'rb', &:read)
mail.attachments['test.jpg'] = File.open(fixture('attachments', 'test.jpg'), 'rb', &:read)
expect(mail.attachments[0].filename).to eq 'test.zip'
expect(mail.attachments[1].filename).to eq 'test.pdf'
expect(mail.attachments[2].filename).to eq 'test.gif'
expect(mail.attachments[3].filename).to eq 'test.jpg'
mail2 = Mail.new(mail.encoded)
expect(mail2.attachments[0].filename).to eq 'test.zip'
expect(mail2.attachments[1].filename).to eq 'test.pdf'
expect(mail2.attachments[2].filename).to eq 'test.gif'
expect(mail2.attachments[3].filename).to eq 'test.jpg'
end
end
|