Navigation Menu

Skip to content

Commit

Permalink
Convert test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
blackxored committed Feb 22, 2014
1 parent 525d0dd commit d162b47
Show file tree
Hide file tree
Showing 80 changed files with 2,460 additions and 2,460 deletions.
106 changes: 53 additions & 53 deletions spec/mail/attachments_list_spec.rb
Expand Up @@ -7,10 +7,10 @@ def encode_base64(str)

def check_decoded(actual, expected)
if RUBY_VERSION >= '1.9'
actual.encoding.should eq Encoding::BINARY
actual.should eq expected.force_encoding(Encoding::BINARY)
expect(actual.encoding).to eq Encoding::BINARY
expect(actual).to eq expected.force_encoding(Encoding::BINARY)
else
actual.should eq expected
expect(actual).to eq expected
end
end

Expand All @@ -24,51 +24,51 @@ def check_decoded(actual, expected)
describe "from direct content" do
it "should work" do
@mail.attachments['test.png'] = @test_png
@mail.attachments['test.png'].filename.should eq '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
@mail.attachments[0].mime_type.should eq 'image/png'
expect(@mail.attachments[0].mime_type).to eq 'image/png'
end

it "should assign the filename" do
@mail.attachments['test.png'] = @test_png
@mail.attachments[0].filename.should eq '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)
@mail.attachments.should_not be_blank
Mail::Encodings.decode_encode(@mail.attachments[0].filename, :decode).should eq 'てすと.txt'
expect(@mail.attachments).not_to be_blank
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 }
@mail.attachments[0].filename.should eq '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" }
@mail.attachments[0].content_type.should eq '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" }
@mail.attachments[0].mime_type.should eq '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" }
@mail.attachments[0].mime_type.should eq 'image/x-jpg'
expect(@mail.attachments[0].mime_type).to eq 'image/x-jpg'
end

end
Expand All @@ -78,13 +78,13 @@ def check_decoded(actual, expected)
it "should set its content_transfer_encoding" do
@mail.attachments['test.png'] = { :content => @test_png }
@mail.ready_to_send!
@mail.attachments[0].content_transfer_encoding.should eq 'base64'
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!
@mail.attachments[0].encoded.should include(encode_base64(@test_png))
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
Expand All @@ -97,14 +97,14 @@ def check_decoded(actual, expected)
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' }
@mail.attachments[0].content_transfer_encoding.should eq '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)
doing {@mail.attachments['test.png'] = { :content => base64_encoded_data,
:encoding => 'weird_encoding' }}.should raise_error
expect(doing {@mail.attachments['test.png'] = { :content => base64_encoded_data,
:encoding => 'weird_encoding' }}).to raise_error
end

it "should be able to call read on the attachment to return the decoded data" do
Expand All @@ -114,15 +114,15 @@ def check_decoded(actual, expected)
else
expected = @mail.attachments[0].read
end
expected.should eq @test_png
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/
encoded.should match regex
expect(encoded).to match regex
end

end
Expand All @@ -135,10 +135,10 @@ def check_decoded(actual, expected)
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)
mail.attachments[0].filename.should eq 'test.pdf'
mail.attachments[1].filename.should eq 'test.gif'
mail.attachments[2].filename.should eq 'test.jpg'
mail.attachments[3].filename.should eq 'test.zip'
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
Expand All @@ -148,21 +148,21 @@ def check_decoded(actual, expected)
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)
mail.attachments['test.pdf'].content_disposition.should eq 'attachment; filename=test.pdf'
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)
mail.attachments.inline['test.png'].content_disposition.should eq 'inline; filename=test.png'
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
mail.attachments['test.png'].url.should eq "cid:#{mail.attachments['test.png'].cid}"
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
mail.attachments['test.png'].should be_inline
expect(mail.attachments['test.png']).to be_inline
end
end

Expand All @@ -174,17 +174,17 @@ def check_decoded(actual, expected)
end

it "should return a content-id for the attachment on creation if passed inline => true" do
@cid.should_not be_nil
expect(@cid).not_to be_nil
end

it "should return a valid content-id on inline attachments" do
Mail::ContentIdField.new(@cid).errors.should be_empty
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
@inline.should eq uri_parser.escape(@cid.gsub(/^</, '').gsub(/>$/, ''))
expect(@inline).to eq uri_parser.escape(@cid.gsub(/^</, '').gsub(/>$/, ''))
end
end

Expand All @@ -193,7 +193,7 @@ def check_decoded(actual, expected)
mail = Mail.new
mail.attachments['test.pdf'] = File.open(fixture('attachments', 'test.pdf'), 'rb', &:read)
mail.encoded
mail.mime_type.should eq 'multipart/mixed'
expect(mail.mime_type).to eq 'multipart/mixed'
end

it "allows you to set the attachment before the content type" do
Expand All @@ -210,12 +210,12 @@ def check_decoded(actual, expected)
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'
filename.encoding.should eq Encoding::UTF_8
expect(filename.encoding).to eq Encoding::UTF_8
end
mail = Mail.new
doing {
expect(doing {
mail.attachments[filename] = File.open(fixture('attachments', 'test.pdf'), 'rb', &:read)
}.should_not raise_error
}).not_to raise_error
end
end

Expand All @@ -226,48 +226,48 @@ def check_decoded(actual, expected)

it "should find the attachment using content location" do
mail = Mail.read(fixture(File.join('emails', 'attachment_emails', 'attachment_content_location.eml')))
mail.attachments.length.should eq 1
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')))
mail.attachments.length.should eq 1
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')))
mail.attachments[0].filename.should eq 'hello.rb'
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')))
mail.attachments[0].decoded.length.should eq 1026
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')))
mail.attachments.length.should eq 1
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".force_encoding(result.encoding)
else
expected = "01 Quien Te Dij\212at. Pitbull.mp3"
end
result.should eq expected
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")))
mail.attachments.length.should eq 1
mail.attachments.first.filename.should eq "This is a test.txt"
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")))
mail.attachments.length.should eq 1
mail.attachments[0].decoded.length.should eq 1026
expect(mail.attachments.length).to eq 1
expect(mail.attachments[0].decoded.length).to eq 1026
end

it "attach filename decoding (issue 83)" do
Expand Down Expand Up @@ -302,7 +302,7 @@ def check_decoded(actual, expected)
limitMAIL
mail = Mail.new(data)
#~ puts Mail::Encodings.decode_encode(mail.attachments[0].filename, :decode)
mail.attachments[0].filename.should eq "Foto0009.jpg"
expect(mail.attachments[0].filename).to eq "Foto0009.jpg"
end

end
Expand All @@ -325,16 +325,16 @@ def check_decoded(actual, expected)
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[0].filename.should eq 'test.zip'
mail.attachments[1].filename.should eq 'test.pdf'
mail.attachments[2].filename.should eq 'test.gif'
mail.attachments[3].filename.should eq 'test.jpg'
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)
mail2.attachments[0].filename.should eq 'test.zip'
mail2.attachments[1].filename.should eq 'test.pdf'
mail2.attachments[2].filename.should eq 'test.gif'
mail2.attachments[3].filename.should eq 'test.jpg'
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

0 comments on commit d162b47

Please sign in to comment.