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
|
require "spec"
require "mime/multipart"
describe MIME::Multipart::Builder do
it "generates valid multipart messages" do
io = IO::Memory.new
builder = MIME::Multipart::Builder.new(io, "fixed-boundary")
headers = HTTP::Headers{"X-Foo" => "bar"}
builder.body_part headers, "body part 1"
headers = HTTP::Headers{"X-Type" => "Empty-Body", "X-Foo" => "Bar"}
builder.body_part headers
builder.finish
expected_message = <<-MULTIPART
--fixed-boundary
X-Foo: bar
body part 1
--fixed-boundary
X-Type: Empty-Body
X-Foo: Bar
--fixed-boundary--
MULTIPART
io.to_s.should eq(expected_message.gsub('\n', "\r\n"))
end
it "generates valid multipart messages with preamble and epilogue" do
io = IO::Memory.new
builder = MIME::Multipart::Builder.new(io, "fixed-boundary")
builder.preamble "Here is a preamble to explain why multipart/mixed "
builder.preamble "exists and why your mail client should support it"
headers = HTTP::Headers{"X-Foo" => "bar"}
builder.body_part headers, "body part 1"
headers = HTTP::Headers{"X-Type" => "Empty-Body", "X-Foo" => "Bar"}
builder.body_part headers
builder.epilogue "Irrelevant text"
builder.epilogue "Much more irrelevant text"
builder.finish
expected_message = <<-MULTIPART
Here is a preamble to explain why multipart/mixed exists and why your mail client should support it
--fixed-boundary
X-Foo: bar
body part 1
--fixed-boundary
X-Type: Empty-Body
X-Foo: Bar
--fixed-boundary--
Irrelevant textMuch more irrelevant text
MULTIPART
io.to_s.should eq(expected_message.gsub('\n', "\r\n"))
end
describe "#content_type" do
it "calculates the content type" do
builder = MIME::Multipart::Builder.new(IO::Memory.new, "a delimiter string with a quote in \"")
builder.content_type("alternative").should eq(%q(multipart/alternative; boundary="a delimiter string with a quote in \""))
end
end
describe ".preamble" do
it "accepts different data types" do
io = IO::Memory.new
builder = MIME::Multipart::Builder.new(io, "boundary")
builder.preamble "string\r\n"
builder.preamble "slice\r\n".to_slice
preamble_io = IO::Memory.new "io\r\n"
builder.preamble preamble_io
builder.preamble do |io|
io.print "io"
io << ' '
io.print "block"
io << "\r\n"
end
builder.body_part(HTTP::Headers.new)
builder.finish
generated_multipart = io.to_s
expected_multipart = <<-MULTIPART
string
slice
io
io block
--boundary
--boundary--
MULTIPART
generated_multipart.should eq(expected_multipart.gsub('\n', "\r\n"))
end
it "raises when called after starting the body" do
builder = MIME::Multipart::Builder.new(IO::Memory.new)
builder.body_part HTTP::Headers.new, "test"
expect_raises(MIME::Multipart::Error, "Cannot generate preamble: body already started") do
builder.preamble "test"
end
end
end
describe ".body_part" do
it "accepts different data types" do
io = IO::Memory.new
builder = MIME::Multipart::Builder.new(io, "boundary")
headers = HTTP::Headers{"X-Foo" => "Bar"}
builder.body_part headers, "string\r\n"
builder.body_part headers, "slice".to_slice
body_part_io = IO::Memory.new "io"
builder.body_part headers, body_part_io
builder.body_part(headers) do |io|
io.print "io"
io << ' '
io.print "block"
end
builder.body_part(headers)
builder.finish
generated_multipart = io.to_s
expected_multipart = <<-MULTIPART
--boundary
X-Foo: Bar
string
--boundary
X-Foo: Bar
slice
--boundary
X-Foo: Bar
io
--boundary
X-Foo: Bar
io block
--boundary
X-Foo: Bar
--boundary--
MULTIPART
generated_multipart.should eq(expected_multipart.gsub('\n', "\r\n"))
end
it "raises when called after finishing" do
builder = MIME::Multipart::Builder.new(IO::Memory.new)
builder.body_part HTTP::Headers.new, "test"
builder.finish
expect_raises(MIME::Multipart::Error, "Cannot generate body part: already finished") do
builder.body_part HTTP::Headers.new, "test"
end
end
it "raises when called after epilogue" do
builder = MIME::Multipart::Builder.new(IO::Memory.new)
builder.body_part HTTP::Headers.new, "test"
builder.epilogue "test"
expect_raises(MIME::Multipart::Error, "Cannot generate body part: after epilogue") do
builder.body_part HTTP::Headers.new, "test"
end
end
end
describe ".epilogue" do
it "accepts different data types" do
io = IO::Memory.new
builder = MIME::Multipart::Builder.new(io, "boundary")
builder.body_part(HTTP::Headers.new)
builder.epilogue "string\r\n"
builder.epilogue "slice\r\n".to_slice
epilogue_io = IO::Memory.new "io\r\n"
builder.epilogue epilogue_io
builder.epilogue do |io|
io.print "io"
io << ' '
io.print "block"
io << "\r\n"
end
builder.finish
generated_multipart = io.to_s
expected_multipart = <<-MULTIPART
--boundary
--boundary--
string
slice
io
io block
MULTIPART
generated_multipart.should eq(expected_multipart.gsub('\n', "\r\n"))
end
it "raises when called after finishing" do
builder = MIME::Multipart::Builder.new(IO::Memory.new)
builder.body_part HTTP::Headers.new, "test"
builder.finish
expect_raises(MIME::Multipart::Error, "Cannot generate epilogue: already finished") do
builder.epilogue "test"
end
end
it "raises when called with no body parts" do
builder = MIME::Multipart::Builder.new(IO::Memory.new)
expect_raises(MIME::Multipart::Error, "Cannot generate epilogue: no body parts") do
builder.epilogue "test"
end
builder.preamble "test"
expect_raises(MIME::Multipart::Error, "Cannot generate epilogue: no body parts") do
builder.epilogue "test"
end
end
end
describe ".finish" do
it "raises if no body exists" do
builder = MIME::Multipart::Builder.new(IO::Memory.new)
expect_raises(MIME::Multipart::Error, "Cannot finish multipart: no body parts") do
builder.finish
end
builder.preamble "test"
expect_raises(MIME::Multipart::Error, "Cannot finish multipart: no body parts") do
builder.finish
end
end
it "raises if already finished" do
builder = MIME::Multipart::Builder.new(IO::Memory.new)
builder.body_part HTTP::Headers.new, "test"
builder.finish
expect_raises(MIME::Multipart::Error, "Cannot finish multipart: already finished") do
builder.finish
end
end
end
end
|