File: body_spec.rb

package info (click to toggle)
ruby-mail 2.6.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,092 kB
  • ctags: 1,281
  • sloc: ruby: 43,919; makefile: 2
file content (423 lines) | stat: -rw-r--r-- 18,661 bytes parent folder | download
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# encoding: utf-8
require 'spec_helper'

describe Mail::Body do

  # 3.5 Overall message syntax
  #
  # A message consists of header fields, optionally followed by a message
  # body.  Lines in a message MUST be a maximum of 998 characters
  # excluding the CRLF, but it is RECOMMENDED that lines be limited to 78
  # characters excluding the CRLF.  (See section 2.1.1 for explanation.)
  # In a message body, though all of the characters listed in the text
  # rule MAY be used, the use of US-ASCII control characters (values 1
  # through 8, 11, 12, and 14 through 31) is discouraged since their
  # interpretation by receivers for display is not guaranteed.
  #
  #  message         =       (fields / obs-fields)
  #                          [CRLF body]
  #
  #  body            =       *(*998text CRLF) *998text
  #
  # The header fields carry most of the semantic information and are
  # defined in section 3.6.  The body is simply a series of lines of text
  # which are uninterpreted for the purposes of this standard.
  #
  describe "initialization" do

    it "should be instantiated" do
      doing { Mail::Body.new }.should_not raise_error
    end

    it "should initialize on a nil value" do
      doing { Mail::Body.new(nil) }.should_not raise_error
    end

    it "should accept text as raw source data" do
      body = Mail::Body.new('this is some text')
      body.to_s.should eq 'this is some text'
    end

    it "should accept nil as a value and return an empty body" do
      body = Mail::Body.new
      body.to_s.should eq ''
    end

    it "should accept an array as the body and join it" do
      doing { Mail::Body.new(["line one\n", "line two\n"]) }.should_not raise_error
    end

    it "should accept an array as the body and join it" do
      body = Mail::Body.new(["line one\n", "line two\n"])
      body.encoded.should eq "line one\r\nline two\r\n"
    end

  end

  describe "encoding" do

    it "should accept text as raw source data" do
      body = Mail::Body.new('this is some text')
      body.encoded.should eq 'this is some text'
    end

    it "should set its own encoding to us_ascii if it is ascii only body" do
      body = Mail::Body.new('This is some text')
      body.charset.should eq 'US-ASCII'
    end

    it "should allow you to set its encoding" do
      body = Mail::Body.new('')
      body.charset = 'UTF-8'
      body.charset.should eq 'UTF-8'
    end

    it "should allow you to specify an encoding" do
      body = Mail::Body.new('')
      body.encoding = 'base64'
      body.encoding.should eq 'base64'
    end

    it "should convert all new lines to crlf" do
      body = Mail::Body.new("This has \n various \r new \r\n lines")
      body.encoded.should eq "This has \r\n various \r\n new \r\n lines"
    end

  end

  describe "decoding" do

    it "should convert all new lines to crlf" do
      body = Mail::Body.new("This has \n various \r new \r\n lines")
      body.decoded.should eq "This has \n various \n new \n lines"
    end

    it "should not change a body on decode if not given an encoding type to decode" do
      body = Mail::Body.new("The=3Dbody")
      body.decoded.should eq "The=3Dbody"
    end

    it "should change return the raw text if it does not recognise the encoding" do
      body = Mail::Body.new("The=3Dbody")
      body.encoding = '7bit'
      body.decoded.should eq "The=3Dbody"
    end

    it "should change a body on decode if given an encoding type to decode" do
      body = Mail::Body.new("The=3Dbody")
      body.encoding = 'quoted-printable'
      body.decoded.should eq "The=body"
    end

    it "should change a body on decode if given an encoding type to decode" do
      body = Mail::Body.new("VGhlIGJvZHk=\n")
      body.encoding = 'base64'
      body.decoded.should eq "The body"
    end

  end

  describe "splitting up a multipart document" do
    it "should store the boundary passed in" do
      multipart_body = "this is some text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/plain; charset=ISO-8859-1\r\n\r\nThis is a plain text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/html\r\n\r\n<p>This is HTML</p>\r\n------=_Part_2192_32400445--\r\n"
      body = Mail::Body.new(multipart_body)
      body.split!('----=_Part_2192_32400445')
      body.boundary.should eq '----=_Part_2192_32400445'
    end

    it "should split at the boundry string given returning two message bodies" do
      multipart_body = "this is some text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/plain; charset=ISO-8859-1\r\n\r\nThis is a plain text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/html\r\n\r\n<p>This is HTML</p>\r\n------=_Part_2192_32400445--\r\n"
      body = Mail::Body.new(multipart_body)
      body.split!('----=_Part_2192_32400445').parts.length.should eq 2
    end

    it "should keep the preamble text as its own preamble" do
      multipart_body = "this is some text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/plain; charset=ISO-8859-1\r\n\r\nThis is a plain text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/html\r\n\r\n<p>This is HTML</p>\r\n------=_Part_2192_32400445--\r\n"
      body = Mail::Body.new(multipart_body)
      body.split!('----=_Part_2192_32400445')
      body.preamble.should eq "this is some text"
    end

    it "should return the parts as their own messages" do
      multipart_body = "this is some text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/plain; charset=ISO-8859-1\r\n\r\nThis is a plain text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/html\r\n\r\n<p>This is HTML</p>\r\n------=_Part_2192_32400445--\r\n"
      body = Mail::Body.new(multipart_body)
      body.split!('----=_Part_2192_32400445')
      body.parts[0].class.should eq Mail::Part
      body.parts[1].class.should eq Mail::Part
    end

    it "should return the first part as its own message" do
      multipart_body = "this is some text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/plain; charset=ISO-8859-1\r\n\r\nThis is a plain text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/html\r\n\r\n<p>This is HTML</p>\r\n------=_Part_2192_32400445--\r\n"
      body = Mail::Body.new(multipart_body)
      body.split!('----=_Part_2192_32400445')
      body.parts[0].content_type.should eq "text/plain; charset=ISO-8859-1"
    end

    it "should return the first part as its own message" do
      multipart_body = "this is some text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/plain; charset=ISO-8859-1\r\n\r\nThis is a plain text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/html\r\n\r\n<p>This is HTML</p>\r\n------=_Part_2192_32400445--\r\n"
      body = Mail::Body.new(multipart_body)
      body.split!('----=_Part_2192_32400445')
      body.parts[1].content_type.should eq "text/html"
    end

    it "should separate out its parts" do
      multipart_body = "this is some text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/plain; charset=ISO-8859-1\r\n\r\nThis is a plain text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/html\r\n\r\n<p>This is HTML</p>\r\n------=_Part_2192_32400445--\r\n"
      body = Mail::Body.new(multipart_body)
      body.split!('----=_Part_2192_32400445')
      body.should be_multipart
    end

    it "should keep track of its parts" do
      multipart_body = "this is some text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/plain; charset=ISO-8859-1\r\n\r\nThis is a plain text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/html\r\n\r\n<p>This is HTML</p>\r\n------=_Part_2192_32400445--\r\n"
      body = Mail::Body.new(multipart_body)
      body.split!('----=_Part_2192_32400445')
      body.parts.length.should eq 2
    end

    it "should round trip its parts" do
      multipart_body = "this is some text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/plain; charset=ISO-8859-1\r\n\r\nThis is a plain text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/html\r\n\r\n<p>This is HTML</p>\r\n------=_Part_2192_32400445--\r\n"
      body = Mail::Body.new(multipart_body)
      body.split!('----=_Part_2192_32400445')
      body.preamble = "Really! this is some text"
      body.epilogue = "And this is the end"
      new_body = Mail::Body.new(body.encoded)
      new_body.split!('----=_Part_2192_32400445')
      new_body.parts.length.should eq 2
      new_body.parts[0].decoded.should eq "This is a plain text\n"
      new_body.parts[1].decoded.should eq "<p>This is HTML</p>"
      new_body.preamble.should eq "Really! this is some text"
      new_body.epilogue.should eq "And this is the end"
    end

    it "should allow you to change the boundary" do
      multipart_body = "this is some text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/plain; charset=ISO-8859-1\r\n\r\nThis is a plain text\r\n\r\n------=_Part_2192_32400445\r\nContent-Type: text/html\r\n\r\n<p>This is HTML</p>\r\n------=_Part_2192_32400445--\r\n"
      body = Mail::Body.new(multipart_body)
      body.split!('----=_Part_2192_32400445')
      body.boundary = '------=_MIMEPART'
      new_body = Mail::Body.new(body.encoded)
      new_body.split!('------=_MIMEPART')
      new_body.parts.length.should eq 2
      new_body.preamble.should eq "this is some text"
    end

    it "should split if boundary is not set" do
      multipart_body = "\n\n--\nDate: Thu, 01 Aug 2013 15:14:20 +0100\nMime-Version: 1.0\nContent-Type: text/plain\nContent-Transfer-Encoding: 7bit\nContent-Disposition: attachment;\n filename=\"\"\nContent-ID: <51fa6d3cac796_d84e3fe5a58349e025683@local.mail>\n\n\n\n----"
      body = Mail::Body.new(multipart_body)
      doing { body.split!(nil) }.should_not raise_error
    end

  end

  describe "detecting non ascii" do
    it "should say an empty string is all ascii" do
      body = Mail::Body.new
      body.should be_only_us_ascii
    end

    it "should say if a body is ascii" do
      body = Mail::Body.new('This is ASCII')
      body.should be_only_us_ascii
    end

    it "should say if a body is not ascii" do
      body = Mail::Body.new("This is NOT plain text ASCII − かきくけこ")
      body.should_not be_only_us_ascii
    end
  end

  describe "adding parts" do
    it "should allow you to add a part" do
      body = Mail::Body.new('')
      body << Mail::Part.new('')
      body.parts.length.should eq 1
      body.should be_multipart
    end

    it "should allow you to sort the parts" do
      body = Mail::Body.new('')
      body << Mail::Part.new("content-type: text/html\r\nsubject: HTML")
      body << Mail::Part.new("content-type: text/plain\r\nsubject: Plain Text")
      body << Mail::Part.new("content-type: text/enriched\r\nsubject: Enriched")
      body.parts.length.should eq 3
      body.should be_multipart
      body.sort_parts!
      body.parts[0].content_type.should eq "text/plain"
      body.parts[1].content_type.should eq "text/enriched"
      body.parts[2].content_type.should eq "text/html"
    end

    it "should allow you to sort the parts with an arbitrary sort order" do
      body = Mail::Body.new('')
      body.set_sort_order([ "text/plain", "text/html", "text/enriched" ])
      body << Mail::Part.new("content-type: text/html\r\nsubject: HTML")
      body << Mail::Part.new("content-type: text/plain\r\nsubject: Plain Text")
      body << Mail::Part.new("content-type: text/enriched\r\nsubject: Enriched")
      body.parts.length.should eq 3
      body.should be_multipart
      body.sort_parts!
      body.parts[0].content_type.should eq "text/plain"
      body.parts[1].content_type.should eq "text/html"
      body.parts[2].content_type.should eq "text/enriched"
    end

    it "should allow you to sort the parts with an arbitrary sort order" do
      body = Mail::Body.new('')
      body.set_sort_order(["application/x-yaml", "text/plain"])
      body << Mail::Part.new("content-type: text/plain\r\nsubject: HTML")
      body << Mail::Part.new("content-type: text/html\r\nsubject: Plain Text")
      body << Mail::Part.new("content-type: application/x-yaml\r\nsubject: Enriched")
      body.parts.length.should eq 3
      body.should be_multipart
      body.sort_parts!
      body.parts[0].content_type.should eq "application/x-yaml"
      body.parts[1].content_type.should eq "text/plain"
      body.parts[2].content_type.should eq "text/html"
    end

    it "should sort the parts on encode" do
      body = Mail::Body.new('')
      body << Mail::Part.new("content-type: text/html\r\nsubject: HTML")
      body << Mail::Part.new("content-type: text/plain\r\nsubject: Plain Text")
      body << Mail::Part.new("content-type: text/enriched\r\nsubject: Enriched")
      body.parts.length.should eq 3
      body.should be_multipart
      body.encoded
      body.parts[0].content_type.should eq "text/plain"
      body.parts[1].content_type.should eq "text/enriched"
      body.parts[2].content_type.should eq "text/html"
    end

    it "should put the part types it doesn't know about at the end" do
      body = Mail::Body.new('')
      body << Mail::Part.new("content-type: text/html\r\nsubject: HTML")
      body << Mail::Part.new("content-type: text/plain\r\nsubject: Plain Text")
      body << Mail::Part.new("content-type: image/jpeg\r\n")
      body.parts.length.should eq 3
      body.should be_multipart
      body.encoded
      body.parts[0].content_type.should eq "text/plain"
      body.parts[1].content_type.should eq "text/html"
      body.parts[2].content_type.should eq "image/jpeg"
    end

    it "should allow you to sort the parts recursively" do
      part = Mail::Part.new('Content-Type: multipart/alternate')
      part.add_part(Mail::Part.new("content-type: text/plain\r\nsubject: Plain Text"))
      part.add_part(Mail::Part.new("content-type: text/html\r\nsubject: HTML"))
      part.add_part(Mail::Part.new("content-type: text/enriched\r\nsubject: Enriched"))
      body = Mail::Body.new('')
      body << part
      body << Mail::Part.new("content-type: image/jpeg\r\nsubject: JPGEG\r\n\r\nsdkjskjdksjdkjsd")
      body.parts.length.should eq 2
      body.should be_multipart
      body.sort_parts!
      body.parts[0].content_type.should match %r{\Amultipart/alternate(;|\Z)}
      body.parts[1].content_type.should eq "image/jpeg"
      body.parts[0].parts[0].content_type.should eq "text/plain"
      body.parts[0].parts[1].content_type.should eq "text/enriched"
      body.parts[0].parts[2].content_type.should eq "text/html"
    end

    it "should allow you to sort the parts recursively" do
      part = Mail::Part.new('Content-Type: multipart/alternate')
      part.add_part(Mail::Part.new("content-type: text/enriched\r\nsubject: Enriched"))
      part.add_part(Mail::Part.new("content-type: text/plain\r\nsubject: Plain Text"))
      part.add_part(Mail::Part.new("content-type: text/html\r\nsubject: HTML"))
      body = Mail::Body.new('')
      body << part
      body << Mail::Part.new("content-type: image/jpeg\r\nsubject: JPGEG\r\n\r\nsdkjskjdksjdkjsd")
      body.parts.length.should eq 2
      body.should be_multipart
      body.sort_parts!
      body.parts[0].content_type.should match %r{\Amultipart/alternate(;|\Z)}
      body.parts[1].content_type.should eq "image/jpeg"
      body.parts[0].parts[0].content_type.should eq "text/plain"
      body.parts[0].parts[1].content_type.should eq "text/enriched"
      body.parts[0].parts[2].content_type.should eq "text/html"
    end

    it "should maintain the relative order of the parts with the same content-type as they are added" do
      body = Mail::Body.new('');
      body << Mail::Part.new("content-type: text/html\r\nsubject: HTML_1")
      body << Mail::Part.new("content-type: image/jpeg\r\nsubject: JPGEG_1\r\n\r\nsdkjskjdksjdkjsd")
      body << Mail::Part.new("content-type: text/plain\r\nsubject: Plain Text_1")
      body << Mail::Part.new("content-type: text/enriched\r\nsubject: Enriched_1")
      body << Mail::Part.new("content-type: text/html\r\nsubject: HTML_2")
      body << Mail::Part.new("content-type: text/plain\r\nsubject: Plain Text_2")
      body << Mail::Part.new("content-type: image/jpeg\r\nsubject: JPGEG_2\r\n\r\nsdkjskjdksjdkjsd")
      body << Mail::Part.new("content-type: text/enriched\r\nsubject: Enriched_2")
      body.parts.length.should eq 8
      body.should be_multipart
      body.sort_parts!
      body.parts[0].subject.should eq "Plain Text_1"
      body.parts[1].subject.should eq "Plain Text_2"
      body.parts[2].subject.should eq "Enriched_1"
      body.parts[3].subject.should eq "Enriched_2"
      body.parts[4].subject.should eq "HTML_1"
      body.parts[5].subject.should eq "HTML_2"
      body.parts[6].subject.should eq "JPGEG_1"
      body.parts[7].subject.should eq "JPGEG_2"
    end
  end

  describe "matching" do

    it "should still equal itself" do
      body = Mail::Body.new('The body')
      body.should eq body
    end

    it "should match on the body part decoded if given a string to ==" do
      body = Mail::Body.new('The body')
      (body == 'The body').should be_true
    end

    it "should match on the body part decoded if given a string to ==" do
      body = Mail::Body.new("VGhlIGJvZHk=\n")
      body.encoding = 'base64'
      (body == "The body").should be_true
    end

    it "should match on the body part decoded if given a string to =~" do
      body = Mail::Body.new('The body')
      (body =~ /The/).should eq 0
    end

    it "should match on the body part decoded if given a string to ==" do
      body = Mail::Body.new("VGhlIGJvZHk=\n")
      body.encoding = 'base64'
      (body =~ /The/).should eq 0
    end

    it "should match on the body part decoded if given a string to match" do
      body = Mail::Body.new('The body')
      (body.match(/The/))[0].should eq 'The'
    end

    it "should match on the body part decoded if given a string to match" do
      body = Mail::Body.new("VGhlIGJvZHk=\n")
      body.encoding = 'base64'
      (body.match(/The/))[0].should eq 'The'
    end

    it "should match on the body part decoded if given a string to include?" do
      body = Mail::Body.new('The Body')
      body.should include('The')
    end

    it "should match on the body part decoded if given a string to include?" do
      body = Mail::Body.new("VGhlIGJvZHk=\n")
      body.encoding = 'base64'
      body.should include('The')
    end
  end

  describe "non US-ASCII charset" do
    it "should encoded" do
      body = Mail::Body.new("あいうえお\n")
      body.charset = 'iso-2022-jp'
      expect = (RUBY_VERSION < '1.9') ? "あいうえお\r\n" : "\e$B$\"$$$&$($*\e(B\r\n"
      body.encoded.should eq expect
    end
  end
end