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
|
# encoding: utf-8
# frozen_string_literal: true
require 'spec_helper'
describe Mail::CommonField do
describe "multi-charset support" do
before(:each) do
@original = $KCODE if RUBY_VERSION < '1.9'
end
after(:each) do
$KCODE = @original if RUBY_VERSION < '1.9'
end
it "should return '' on to_s if there is no value" do
expect(Mail::SubjectField.new(nil).to_s).to eq ''
end
it "should leave ascii alone" do
field = Mail::SubjectField.new("This is a test")
expect(field.encoded).to eq "Subject: This is a test\r\n"
expect(field.decoded).to eq "This is a test"
end
it "should encode a utf-8 string as utf-8 quoted printable" do
value = "かきくけこ"
if RUBY_VERSION < '1.9'
$KCODE = 'u'
result = "Subject: =?UTF-8?Q?=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n"
else
value = value.dup.force_encoding('UTF-8')
result = "Subject: =?UTF-8?Q?=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n"
end
field = Mail::SubjectField.new(value)
expect(field.encoded).to eq result
expect(field.decoded).to eq value
expect(field.value).to eq value
end
it "should wrap an encoded at 60 characters" do
value = "かきくけこ かきくけこ かきくけこ かきくけこ かきくけこ かきくけこ かきくけこ かきくけこ かきくけこ"
if RUBY_VERSION < '1.9'
$KCODE = 'u'
result = "Subject: =?UTF-8?Q?=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n"
else
value = value.dup.force_encoding('UTF-8')
result = "Subject: =?UTF-8?Q?=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n\s=?UTF-8?Q?_=E3=81=8B=E3=81=8D=E3=81=8F=E3=81=91=E3=81=93?=\r\n"
end
field = Mail::SubjectField.new(value)
expect(field.encoded).to eq result
expect(field.decoded).to eq value
expect(field.value).to eq value
end
it "should handle charsets in assigned addresses" do
value = '"かきくけこ" <mikel@test.lindsaar.net>'
if RUBY_VERSION < '1.9'
$KCODE = 'u'
result = "From: =?UTF-8?B?44GL44GN44GP44GR44GT?= <mikel@test.lindsaar.net>\r\n"
else
value = value.dup.force_encoding('UTF-8')
result = "From: =?UTF-8?B?44GL44GN44GP44GR44GT?= <mikel@test.lindsaar.net>\r\n"
end
field = Mail::FromField.new(value)
expect(field.encoded).to eq result
expect(field.decoded).to eq value
end
end
context "when including the field name" do
it "does not strip out content that looks identitcal to the field name" do
field = Mail::SubjectField.new("Subject: Subject: for your approval")
expect(field.decoded).to eq("Subject: for your approval")
end
end
context "when not including the field name" do
it "does not strip out content that looks identitcal to the field name" do
field = Mail::SubjectField.new("This is an important subject: here")
expect(field.decoded).to eq("This is an important subject: here")
end
end
end
|