Allow entity to support content disposition

This commit is contained in:
sophia 2020-10-28 15:55:57 -05:00
parent 799ebb32ed
commit 8a8eff130a
3 changed files with 23 additions and 12 deletions

View File

@ -60,6 +60,9 @@ module Vagrant
# @return [String] type of the entity content
attr_reader :content_type
# @return [String] content disposition
attr_accessor :disposition
# @param [String] entity content
# @param [String] type of the entity content
def initialize(content, content_type)
@ -76,8 +79,11 @@ module Vagrant
# @return [String] mime data
def to_s
output_string = "Content-ID: <#{@content_id}>\n"
output_string += "Content-Type: #{@content_type}\n\n"
output_string += content
output_string += "Content-Type: #{@content_type}\n"
if disposition
output_string += "Content-Disposition: #{@disposition}\n"
end
output_string += "\n#{content}"
output_string
end
end

View File

@ -93,7 +93,7 @@ describe Vagrant::Action::Builtin::CloudInitSetup do
it "takes a text cfg inline string with content_disposition_filename and saves it as a MIME text message" do
mime_text_part = double("mime_text_part")
expect(mime_text_part).to receive(:disposition=).with("attachment; filename=\"test.ps1\"")
expect(MIME::Text).to receive(:new).with("#ps1_sysnative\n", "x-shellscript").and_return(mime_text_part)
expect(Vagrant::Util::Mime::Entity).to receive(:new).with("#ps1_sysnative\n", "text/x-shellscript").and_return(mime_text_part)
subject.read_text_cfg(machine, cfg_with_content_disposition_filename_inline)
end
end

View File

@ -5,8 +5,7 @@ require 'mime/types'
describe Vagrant::Util::Mime::Multipart do
subject { described_class }
let(:mime) { subject }
let(:time) { 603907018 }
let(:secure_random) { "123qwe" }
@ -16,7 +15,6 @@ describe Vagrant::Util::Mime::Multipart do
end
it "can add headers" do
mime = subject.new()
mime.headers["Mime-Version"] = "1.0"
expected_string = "Content-ID: <#{time}@#{secure_random}.local>
Content-Type: multipart/mixed; boundary=Boundary_#{secure_random}
@ -28,7 +26,6 @@ Mime-Version: 1.0
end
it "can add content" do
mime = subject.new()
mime.add("something")
expected_string = "Content-ID: <#{time}@#{secure_random}.local>
Content-Type: multipart/mixed; boundary=Boundary_#{secure_random}
@ -41,7 +38,6 @@ something
end
it "can add Vagrant::Util::Mime::Entity content" do
mime = subject.new()
mime.add(Vagrant::Util::Mime::Entity.new("something", "text/cloud-config"))
expected_string = "Content-ID: <#{time}@#{secure_random}.local>
Content-Type: multipart/mixed; boundary=Boundary_#{secure_random}
@ -59,8 +55,6 @@ end
describe Vagrant::Util::Mime::Entity do
subject { described_class }
let(:time) { 603907018 }
let(:secure_random) { "123qwe" }
@ -70,15 +64,26 @@ describe Vagrant::Util::Mime::Entity do
end
it "registers the content type" do
subject.new("something", "text/cloud-config")
described_class.new("something", "text/cloud-config")
expect(MIME::Types).to include("text/cloud-config")
end
it "outputs as a string" do
entity = subject.new("something", "text/cloud-config")
entity = described_class.new("something", "text/cloud-config")
expected_string = "Content-ID: <#{time}@#{secure_random}.local>
Content-Type: text/cloud-config
something"
expect(entity.to_s).to eq(expected_string)
end
it "can set disposition" do
entity = described_class.new("something", "text/cloud-config")
entity.disposition = "attachment; filename='path.sh'"
expected_string = "Content-ID: <#{time}@#{secure_random}.local>
Content-Type: text/cloud-config
Content-Disposition: attachment; filename='path.sh'
something"
expect(entity.to_s).to eq(expected_string)
end