spec/inputs/imap_spec.rb in logstash-input-imap-3.0.7 vs spec/inputs/imap_spec.rb in logstash-input-imap-3.1.0
- old
+ new
@@ -1,10 +1,13 @@
# encoding: utf-8
require "logstash/devutils/rspec/spec_helper"
+require "insist"
+require "logstash/devutils/rspec/shared_examples"
require "logstash/inputs/imap"
require "mail"
require "net/imap"
+require "base64"
describe LogStash::Inputs::IMAP do
context "when interrupting the plugin" do
@@ -37,19 +40,23 @@
user = "logstash"
password = "secret"
msg_time = Time.new
msg_text = "foo\nbar\nbaz"
msg_html = "<p>a paragraph</p>\n\n"
+ msg_binary = "\x42\x43\x44"
+ msg_unencoded = "raw text 🐐"
subject do
Mail.new do
from "me@example.com"
to "you@example.com"
subject "logstash imap input test"
date msg_time
body msg_text
add_file :filename => "some.html", :content => msg_html
+ add_file :filename => "image.png", :content => msg_binary
+ add_file :filename => "unencoded.data", :content => msg_unencoded, :content_transfer_encoding => "7bit"
end
end
context "with both text and html parts" do
context "when no content-type selected" do
@@ -129,7 +136,38 @@
input = LogStash::Inputs::IMAP.new config
input.register
event = input.parse_mail(subject)
insist { event.get("message") } == msg_text
end
+ end
+
+ context "with attachments" do
+ it "should extract filenames" do
+ config = {"type" => "imap", "host" => "localhost",
+ "user" => "#{user}", "password" => "#{password}"}
+
+ input = LogStash::Inputs::IMAP.new config
+ input.register
+ event = input.parse_mail(subject)
+ insist { event.get("attachments") } == [
+ {"filename"=>"some.html"},
+ {"filename"=>"image.png"},
+ {"filename"=>"unencoded.data"}
+ ]
+ end
+
+ it "should extract the encoded content" do
+ config = {"type" => "imap", "host" => "localhost",
+ "user" => "#{user}", "password" => "#{password}",
+ "save_attachments" => true}
+
+ input = LogStash::Inputs::IMAP.new config
+ input.register
+ event = input.parse_mail(subject)
+ insist { event.get("attachments") } == [
+ {"data"=> Base64.encode64(msg_html).encode(crlf_newline: true), "filename"=>"some.html"},
+ {"data"=> Base64.encode64(msg_binary).encode(crlf_newline: true), "filename"=>"image.png"},
+ {"data"=> msg_unencoded, "filename"=>"unencoded.data"}
+ ]
+ end
end
end