From 8a8eff130a8a5f958e575e104589521a0cd77361 Mon Sep 17 00:00:00 2001 From: sophia Date: Wed, 28 Oct 2020 15:55:57 -0500 Subject: [PATCH] Allow entity to support content disposition --- lib/vagrant/util/mime.rb | 10 ++++++-- .../action/builtin/cloud_init_setup_test.rb | 2 +- test/unit/vagrant/util/mime_test.rb | 23 +++++++++++-------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/vagrant/util/mime.rb b/lib/vagrant/util/mime.rb index 667402747..afec8e25b 100644 --- a/lib/vagrant/util/mime.rb +++ b/lib/vagrant/util/mime.rb @@ -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 diff --git a/test/unit/vagrant/action/builtin/cloud_init_setup_test.rb b/test/unit/vagrant/action/builtin/cloud_init_setup_test.rb index 8da0cac3e..8cd7703df 100644 --- a/test/unit/vagrant/action/builtin/cloud_init_setup_test.rb +++ b/test/unit/vagrant/action/builtin/cloud_init_setup_test.rb @@ -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 diff --git a/test/unit/vagrant/util/mime_test.rb b/test/unit/vagrant/util/mime_test.rb index a2dfdfb87..675ebf189 100644 --- a/test/unit/vagrant/util/mime_test.rb +++ b/test/unit/vagrant/util/mime_test.rb @@ -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