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
|
# frozen_string_literal: true
require 'spec_helper'
describe Mail::ContentTransferEncodingField do
# Content-Transfer-Encoding Header Field
#
# Many media types which could be usefully transported via email are
# represented, in their "natural" format, as 8bit character or binary
# data. Such data cannot be transmitted over some transfer protocols.
# For example, RFC 821 (SMTP) restricts mail messages to 7bit US-ASCII
# data with lines no longer than 1000 characters including any trailing
# CRLF line separator.
#
# It is necessary, therefore, to define a standard mechanism for
# encoding such data into a 7bit short line format. Proper labelling
# of unencoded material in less restrictive formats for direct use over
# less restrictive transports is also desireable. This document
# specifies that such encodings will be indicated by a new "Content-
# Transfer-Encoding" header field. This field has not been defined by
# any previous standard.
#
# 6.1. Content-Transfer-Encoding Syntax
#
# The Content-Transfer-Encoding field's value is a single token
# specifying the type of encoding, as enumerated below. Formally:
#
# encoding := "Content-Transfer-Encoding" ":" mechanism
#
# mechanism := "7bit" / "8bit" / "binary" /
# "quoted-printable" / "base64" /
# ietf-token / x-token
#
# These values are not case sensitive -- Base64 and BASE64 and bAsE64
# are all equivalent. An encoding type of 7BIT requires that the body
#
# is already in a 7bit mail-ready representation. This is the default
# value -- that is, "Content-Transfer-Encoding: 7BIT" is assumed if the
# Content-Transfer-Encoding header field is not present.
#
describe "initialization" do
it "should initialize" do
expect { Mail::ContentTransferEncodingField.new("Content-Transfer-Encoding: 7bit") }.not_to raise_error
end
it "should accept a string with the field name" do
t = Mail::ContentTransferEncodingField.new('Content-Transfer-Encoding: 7bit')
expect(t.name).to eq 'Content-Transfer-Encoding'
expect(t.value).to eq '7bit'
end
it "should accept a string without the field name" do
t = Mail::ContentTransferEncodingField.new('7bit')
expect(t.name).to eq 'Content-Transfer-Encoding'
expect(t.value).to eq '7bit'
end
it "should render an encoded field" do
t = Mail::ContentTransferEncodingField.new('7bit')
expect(t.encoded).to eq "Content-Transfer-Encoding: 7bit\r\n"
end
it "should render a decoded field" do
t = Mail::ContentTransferEncodingField.new('7bit')
expect(t.decoded).to eq '7bit'
end
end
describe "parsing the value" do
it "should return an encoding string" do
["7bit", "8bit", "binary", 'quoted-printable', "base64"].each do |encoding|
t = Mail::ContentTransferEncodingField.new(encoding)
expect(t.encoding).to eq encoding
end
end
it "should treat 7bits/7-bit and 8bits/8-bit as 7bit and 8bit" do
%w(7bits 7-bit).each do |mechanism|
expect(Mail::ContentTransferEncodingField.new(mechanism).encoding).to eq '7bit'
end
%w(8bits 8-bit).each do |mechanism|
expect(Mail::ContentTransferEncodingField.new(mechanism).encoding).to eq '8bit'
end
end
it "should handle any valid 'x-token' value" do
t = Mail::ContentTransferEncodingField.new('X-This-is_MY-encoding')
expect(t.encoding).to eq 'x-this-is_my-encoding'
end
it "should handle an x-encoding" do
t = Mail::ContentTransferEncodingField.new("x-uuencode")
expect(t.encoding).to eq "x-uuencode"
end
it "should handle an ietf encoding (practically, any token)" do
t = Mail::ContentTransferEncodingField.new("ietf-token")
expect(t.encoding).to eq "ietf-token"
end
it "should replace the existing value" do
t = Mail::ContentTransferEncodingField.new("7bit")
t.parse("quoted-printable")
expect(t.encoding).to eq 'quoted-printable'
end
it "should raise an error on bogus values" do
expect { Mail::ContentTransferEncodingField.new("broken@foo") }.to raise_error(Mail::Field::ParseError)
end
it "should handle an empty content transfer encoding" do
t = Mail::ContentTransferEncodingField.new("")
expect(t.encoding).to eq ""
end
it "should handle a hyphen" do
t = Mail::ContentTransferEncodingField.new('7-bit')
expect(t.decoded).to eq '7bit'
t = Mail::ContentTransferEncodingField.new('8-bit')
expect(t.decoded).to eq '8bit'
end
end
end
|