File: field_spec.rb

package info (click to toggle)
ruby-mail 2.7.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,732 kB
  • sloc: ruby: 71,628; makefile: 3
file content (366 lines) | stat: -rw-r--r-- 15,519 bytes parent folder | download | duplicates (3)
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# encoding: utf-8
# frozen_string_literal: true
require 'spec_helper'

describe Mail::Field do

  describe 'parsing' do
    it "parses full header fields" do
      expect($stderr).to_not receive(:puts)
      field = Mail::Field.parse('To: Mikel')
      expect(field.name).to eq 'To'
      expect(field.value).to eq 'Mikel'
      if field.value.respond_to?(:encoding)
        expect(field.value.encoding).to eq Encoding::UTF_8
      end
      expect(field.field).to be_kind_of(Mail::ToField)
    end

    it "parses missing whitespace" do
      field = Mail::Field.parse('To:Bob')
      expect(field.name).to eq 'To'
      expect(field.value).to eq 'Bob'
    end

    it "parses added inapplicable whitespace" do
      field = Mail::Field.parse('To                  :                   Bob                      ')
      expect(field.name).to eq 'To'
      expect(field.value).to eq 'Bob'
    end
  end

  describe "initialization" do
    if Mail::VERSION.version >= '2.8'
      it "raises if instantiating by parsing a full header field" do
        expect {
          Mail::Field.new('To: Mikel')
        }.to raise_error(ArgumentError)
      end
    else
      it "deprecates instantiation by parsing a full header field" do
        expect(Kernel).to receive(:warn).with('Passing an unparsed header field to Mail::Field.new is deprecated and will be removed in Mail 2.8.0. Use Mail::Field.parse instead.')
        field = Mail::Field.new('To: Mikel')
        expect(field.name).to eq 'To'
        expect(field.value).to eq 'Mikel'
        expect(field.field).to be_kind_of(Mail::ToField)
      end
    end

    it "instantiates with name and value" do
      expect(Mail::Field.new('To', 'Mikel').field).to be_kind_of(Mail::ToField)
    end

    it "accepts arrays of values" do
      field = Mail::Field.new("To", ['test1@lindsaar.net', 'Mikel <test2@lindsaar.net>'])
      expect(field.addresses).to eq ["test1@lindsaar.net", "test2@lindsaar.net"]
    end

    it "accepts omitted values" do
      expect(Mail::Field.new('To').field).to be_kind_of(Mail::ToField)
    end

    it "should match up fields to class names" do
      structured_fields = %w[ Date From Sender Reply-To To Cc Bcc Message-ID In-Reply-To
                              References Keywords Resent-Date Resent-From Resent-Sender
                              Resent-To Resent-Cc Resent-Bcc Resent-Message-ID
                              Return-Path Received Subject Comments Mime-Version
                              Content-Transfer-Encoding Content-Description
                              Content-Disposition Content-Type ]
      structured_fields.each do |sf|
        words = sf.split("-").map { |a| a.capitalize }
        klass = "#{words.join}Field"
        expect(Mail::Field.new(sf).field).to be_kind_of(Mail.const_get(klass))
      end
    end

    it "should match up fields to class names regardless of case" do
      structured_fields = %w[ dATE fROM sENDER REPLY-TO TO CC BCC MESSAGE-ID IN-REPLY-TO
                              REFERENCES KEYWORDS resent-date resent-from rESENT-sENDER
                              rESENT-tO rESent-cc resent-bcc reSent-MESSAGE-iD
                              rEtURN-pAtH rEcEiVeD Subject Comments Mime-VeRSIOn
                              cOntenT-transfer-EnCoDiNg Content-Description
                              Content-Disposition cOnTENt-TyPe ]
      structured_fields.each do |sf|
        words = sf.split("-").map { |a| a.capitalize }
        klass = "#{words.join}Field"
        expect(Mail::Field.new(sf).field).to be_kind_of(Mail.const_get(klass))
      end
    end

    it "should say anything that is not a known field is an optional field" do
      unstructured_fields = %w[ Too Becc bccc Random X-Mail MySpecialField ]
      value = '😊'
      unstructured_fields.each do |sf|
        raw = "#{sf}: #{value}"
        raw = raw.dup.force_encoding(Encoding::BINARY) if raw.respond_to?(:force_encoding)
        field = Mail::Field.parse(raw)
        expect(field.field).to be_kind_of(Mail::OptionalField)
        expect(field.name).to eq(sf)
        expect(field.value).to eq(value)
      end
    end

    it "should return an unstuctured field if the structured field parsing raises an error" do
      expect(Mail::ToField).to receive(:new).and_raise(Mail::Field::ParseError.new(Mail::ToField, 'To: Bob, ,,, Frank, Smith', "Some reason"))
      field = Mail::Field.new('To', 'Bob, ,,, Frank, Smith')
      expect(field.field).to be_kind_of(Mail::UnstructuredField)
      expect(field.name).to eq 'To'
      expect(field.value).to eq 'Bob, ,,, Frank, Smith'
      if field.value.respond_to?(:encoding)
        expect(field.value.encoding).to eq Encoding::UTF_8
      end
    end

    it "delegates to_s to its field" do
      field = Mail::Field.new('To', 'Bob')
      expect(field.field).to receive(:to_s).once
      field.to_s
    end

    it "delegates missing methods to its field" do
      field = Mail::Field.new('To', 'Bob')
      expect(field.field).to receive(:anything_at_all).once
      field.anything_at_all
    end

    it "should respond_to? its own methods and the same methods as its instantiated field class" do
      field = Mail::Field.new('To', 'Bob')
      expect(field.respond_to?(:field)).to be_truthy
      expect(field.field).to receive(:"respond_to?").once
      field.respond_to?(:addresses)
    end

    it "should change its type if you change the name" do
      field = Mail::Field.new('To', 'mikel@me.com')
      expect(field.field).to be_kind_of(Mail::ToField)
      field.value = "bob@me.com"
      expect(field.field).to be_kind_of(Mail::ToField)
    end

    it "should create a field without trying to parse if given a symbol" do
      field = Mail::Field.new('Message-ID')
      expect(field.field).to be_kind_of(Mail::MessageIdField)
    end

    it "should inherit charset" do
      charset = 'iso-2022-jp'
      field = Mail::Field.new('Subject', 'こんにちは', charset)
      expect(field.charset).to eq charset
    end
  end

  describe "error handling" do
    it "should populate the errors array if it finds a field it can't deal with" do
      field = Mail::Field.new('Content-Transfer-Encoding', '8@bit')
      expect(field.field.errors.size).to eq 1
      expect(field.field.errors[0][0]).to eq 'Content-Transfer-Encoding'
      expect(field.field.errors[0][1]).to eq '8@bit'
      expect(field.field.errors[0][2].to_s).to match(/ContentTransferEncodingElement can not parse |17-bit|/)
    end
  end

  describe "helper methods" do
    it "should reply if it is responsible for a field name as a capitalized string - structured field" do
      field = Mail::Field.new('To', 'mikel@test.lindsaar.net')
      expect(field.responsible_for?("To")).to be_truthy
    end

    it "should reply if it is responsible for a field as a lower case string - structured field" do
      field = Mail::Field.new('To', 'mikel@test.lindsaar.net')
      expect(field.responsible_for?("to")).to be_truthy
    end

    it "should reply if it is responsible for a field as a symbol - structured field" do
      field = Mail::Field.new('To', 'mikel@test.lindsaar.net')
      expect(field.responsible_for?(:to)).to be_truthy
    end

    it "should say it is the \"same\" as another if their field types match" do
      expect(Mail::Field.new('To', 'mikel').same(Mail::Field.new('To', 'bob'))).to be_truthy
    end

    it "should say it is not the \"same\" as another if their field types don't match" do
      expect(Mail::Field.new('To', 'mikel').same(Mail::Field.new('From', 'mikel'))).to be_falsey
    end

    it "should say it is not the \"same\" as nil" do
      expect(Mail::Field.new('To', 'mikel').same(nil)).to be_falsey
    end

    it "should say it is == to another if their field and names match" do
      expect(Mail::Field.new('To', 'mikel')).to eq(Mail::Field.new('To', 'mikel'))
    end

    it "should say it is not == to another if their field names do not match" do
      expect(Mail::Field.new('From', 'mikel')).not_to eq(Mail::Field.new('To', 'bob'))
    end

    it "should say it is not == to another if their field names match, but not their values" do
      expect(Mail::Field.new('To', 'mikel')).not_to eq(Mail::Field.new('To', 'bob'))
    end

    it "should say it is not == to nil" do
      expect(Mail::Field.new('From', 'mikel')).not_to eq(nil)
    end

    it "should sort according to the field order" do
      list = [Mail::Field.new('To', 'mikel'), Mail::Field.new('Return-Path', 'bob')]
      expect(list.sort[0].name).to eq "Return-Path"
    end
  end

  describe 'user defined fields' do
    it "should say it is the \"same\" as another if their field names match" do
      expect(Mail::Field.new('X-Foo', 'mikel').same(Mail::Field.new('X-Foo', 'bob'))).to be_truthy
    end

    it "should say it is not == to another if their field names do not match" do
      expect(Mail::Field.new('X-Foo', 'mikel')).not_to eq(Mail::Field.new('X-Bar', 'bob'))
    end
  end

  describe "passing an encoding" do
    it "should allow you to send in unencoded strings to fields and encode them" do
      subject = Mail::SubjectField.new("This is あ string", 'utf-8')
      expect(subject.encoded).to eq "Subject: =?UTF-8?Q?This_is_=E3=81=82_string?=\r\n"
      expect(subject.decoded).to eq "This is あ string"
    end

    it "should allow you to send in unencoded strings to address fields and encode them" do
      to = Mail::ToField.new('"Mikel Lindsああr" <mikel@test.lindsaar.net>', 'utf-8')
      expect(to.encoded).to eq "To: =?UTF-8?B?TWlrZWwgTGluZHPjgYLjgYJy?= <mikel@test.lindsaar.net>\r\n"
    end

    it "should allow you to send in unencoded strings without quotes to address fields and encode them" do
      to = Mail::ToField.new('Mikel Lindsああr <mikel@test.lindsaar.net>', 'utf-8')
      expect(to.encoded).to eq "To: =?UTF-8?B?TWlrZWwgTGluZHPjgYLjgYJy?= <mikel@test.lindsaar.net>\r\n"
    end

    it "should allow you to send in unencoded strings to address fields and encode them" do
      to = Mail::ToField.new("あdあ <ada@test.lindsaar.net>", 'utf-8')
      expect(to.encoded).to eq "To: =?UTF-8?B?44GCZOOBgg==?= <ada@test.lindsaar.net>\r\n"
    end

    it "should allow you to send in multiple unencoded strings to address fields and encode them" do
      to = Mail::ToField.new(["Mikel Lindsああr <mikel@test.lindsaar.net>", "あdあ <ada@test.lindsaar.net>"], 'utf-8')
      expect(to.encoded).to eq "To: =?UTF-8?B?TWlrZWwgTGluZHPjgYLjgYJy?= <mikel@test.lindsaar.net>, \r\n\s=?UTF-8?B?44GCZOOBgg==?= <ada@test.lindsaar.net>\r\n"
    end

    it "should allow you to send in multiple unencoded strings to any address field" do
      mail = Mail.new
      mail.charset = 'utf-8'
      array = ["Mikel Lindsああr <mikel@test.lindsaar.net>", "あdあ <ada@test.lindsaar.net>"]
      field = Mail::ToField.new(array, 'utf-8')
      expect(field.encoded).to eq "#{Mail::ToField::CAPITALIZED_FIELD}: =?UTF-8?B?TWlrZWwgTGluZHPjgYLjgYJy?= <mikel@test.lindsaar.net>, \r\n\s=?UTF-8?B?44GCZOOBgg==?= <ada@test.lindsaar.net>\r\n"
      field = Mail::FromField.new(array, 'utf-8')
      expect(field.encoded).to eq "#{Mail::FromField::CAPITALIZED_FIELD}: =?UTF-8?B?TWlrZWwgTGluZHPjgYLjgYJy?= <mikel@test.lindsaar.net>, \r\n\s=?UTF-8?B?44GCZOOBgg==?= <ada@test.lindsaar.net>\r\n"
      field = Mail::CcField.new(array, 'utf-8')
      expect(field.encoded).to eq "#{Mail::CcField::CAPITALIZED_FIELD}: =?UTF-8?B?TWlrZWwgTGluZHPjgYLjgYJy?= <mikel@test.lindsaar.net>, \r\n\s=?UTF-8?B?44GCZOOBgg==?= <ada@test.lindsaar.net>\r\n"
      field = Mail::ReplyToField.new(array, 'utf-8')
      expect(field.encoded).to eq "#{Mail::ReplyToField::CAPITALIZED_FIELD}: =?UTF-8?B?TWlrZWwgTGluZHPjgYLjgYJy?= <mikel@test.lindsaar.net>, \r\n\s=?UTF-8?B?44GCZOOBgg==?= <ada@test.lindsaar.net>\r\n"
    end

    it "should allow an encoded value in the Subject field and decode it automatically (issue 44)" do
      skip if RUBY_VERSION < '1.9'
      subject = Mail::SubjectField.new("=?ISO-8859-1?Q?2_=FAlt?=", 'utf-8')
      expect(subject.decoded).to eq "2 últ"
    end

    it "should allow you to encoded text in the middle (issue 44)" do
      skip if RUBY_VERSION < '1.9'
      subject = Mail::SubjectField.new("ma=?ISO-8859-1?Q?=F1ana?=", 'utf-8')
      expect(subject.decoded).to eq "mañana"
    end

    it "more tolerable to encoding definitions, ISO (issue 120)" do
      skip if RUBY_VERSION < '1.9'
      subject = Mail::SubjectField.new("ma=?ISO88591?Q?=F1ana?=", 'utf-8')
      expect(subject.decoded).to eq "mañana"
    end

    it "more tolerable to encoding definitions, ISO-long (issue 120)" do
      # Rubies under 1.9 don't handle encoding conversions
      skip if RUBY_VERSION < '1.9'

      # TODO: JRuby 1.7.0 has an encoding issue https://jira.codehaus.org/browse/JRUBY-6999
      skip if defined?(JRUBY_VERSION) && JRUBY_VERSION >= '1.7.0'

      subject = Mail::SubjectField.new("=?iso2022jp?B?SEVBUlQbJEIkSiQ0TyJNbRsoQg?=", 'utf-8')
      expect(subject.decoded).to eq  "HEARTなご連絡"
    end

    it "more tolerable to encoding definitions, UTF (issue 120)" do
      to = Mail::ToField.new("=?utf-8?B?44GCZOOBgg==?= <ada@test.lindsaar.net>", 'utf-8')
      expect(to.decoded).to eq "\"あdあ\" <ada@test.lindsaar.net>"
      expect(to.encoded).to eq "To: =?utf-8?B?44GCZOOBgg==?= <ada@test.lindsaar.net>\r\n"
    end

    it "more tolerable to encoding definitions, ISO (issue 120)" do
      subject = Mail::SubjectField.new("=?UTF-8?B?UmU6IHRlc3QgZW52w61vIG1lbnNhamUgY29u?=", 'utf-8')
      expect(subject.decoded).to eq "Re: test envío mensaje con"
    end


    it "more tolerable to encoding definitions, Windows (issue 120)" do
      skip if RUBY_VERSION < '1.9'

      # TODO: JRuby 1.7.0 has an encoding issue https://jira.codehaus.org/browse/JRUBY-6999
      skip if defined?(JRUBY_VERSION) && JRUBY_VERSION >= '1.7.0'

      subject = Mail::SubjectField.new("=?Windows1252?Q?It=92s_a_test=3F?=", 'utf-8')
      expect(subject.decoded).to eq "It’s a test?"
    end

    it "should support ascii encoded utf-8 subjects" do
      s = "=?utf-8?Q?simp?= =?utf-8?Q?le_=E2=80=93_dash_=E2=80=93_?="

      subject = Mail::SubjectField.new(s, 'utf-8')
      expect(subject.decoded).to eq("simple – dash – ")
    end

    it "should support ascii encoded windows subjects" do
      skip if RUBY_VERSION < '1.9'

      # TODO: JRuby 1.7.0 has an encoding issue https://jira.codehaus.org/browse/JRUBY-6999
      skip if defined?(JRUBY_VERSION) && JRUBY_VERSION >= '1.7.0'

      s = "=?WINDOWS-1252?Q?simp?= =?WINDOWS-1252?Q?le_=96_dash_=96_?="

      subject = Mail::SubjectField.new(s, "UTF-8")
      expect(subject.decoded).to eq("simple – dash – ")
    end
  end

  describe "value" do
    let(:original) { { :template => "t1" } }
    subject { Mail::Field.new("name", original) }

    context "parsed" do
      it "returns parsed value" do
        expect(subject.value).to eq(original.to_s)
      end
    end

    context "unparsed" do
      it "returns origin unparsed value" do
        expect(subject.unparsed_value).to eq(original)
      end
    end
  end

  describe Mail::Field::ParseError do
    it "should be structured" do
      error = nil
      begin
        Mail::DateTimeElement.new("invalid")
      rescue Mail::Field::ParseError => e
        error = e
      end
      expect(error).not_to be_nil
      expect(error.element).to eq Mail::DateTimeElement
      expect(error.value).to eq "invalid"
      expect(error.reason).not_to be_nil
    end
  end

end