From 4827469dee30c3563f7a2d9a3e0b7031979116c6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 23 Oct 2014 11:26:56 -0700 Subject: [PATCH] core: recognize more complex content types for json [GH-4525] --- CHANGELOG.md | 2 + lib/vagrant/action/builtin/box_add.rb | 2 +- .../vagrant/action/builtin/box_add_test.rb | 51 ++++++++++++++++++- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 526252a6c..e59199eb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,8 @@ BUG FIXES: - core: Custom Vagrant Cloud server URL now respected in more cases. - core: On downloads, don't continue downloads if the remote server doesn't support byte ranges. [GH-4479] + - core: Box downloads recognize more complex content types that include + "application/json" [GH-4525] - commands/box: `--cert` flag works properly. [GH-4691] - command/docker-logs: Won't crash if container is removed. [GH-3990] - command/docker-run: Synced folders will be attached properly. [GH-3873] diff --git a/lib/vagrant/action/builtin/box_add.rb b/lib/vagrant/action/builtin/box_add.rb index 5b8f27488..554613b94 100644 --- a/lib/vagrant/action/builtin/box_add.rb +++ b/lib/vagrant/action/builtin/box_add.rb @@ -483,7 +483,7 @@ module Vagrant output = d.head match = output.scan(/^Content-Type: (.+?)$/i).last return false if !match - match.last.chomp == "application/json" + !!(match.last.chomp =~ /application\/json/) end def validate_checksum(checksum_type, checksum, path) diff --git a/test/unit/vagrant/action/builtin/box_add_test.rb b/test/unit/vagrant/action/builtin/box_add_test.rb index 03e064920..ad3a56dfc 100644 --- a/test/unit/vagrant/action/builtin/box_add_test.rb +++ b/test/unit/vagrant/action/builtin/box_add_test.rb @@ -34,12 +34,14 @@ describe Vagrant::Action::Builtin::BoxAdd do FileChecksum.new(path, Digest::SHA1).checksum end - def with_web_server(path) + def with_web_server(path, **opts) tf = Tempfile.new("vagrant") tf.close + opts[:json_type] ||= "application/json" + mime_types = WEBrick::HTTPUtils::DefaultMimeTypes - mime_types.store "json", "application/json" + mime_types.store "json", opts[:json_type] port = 3838 server = WEBrick::HTTPServer.new( @@ -258,6 +260,51 @@ describe Vagrant::Action::Builtin::BoxAdd do end end + it "adds from HTTP URL with complex JSON mime type" do + box_path = iso_env.box2_file(:virtualbox) + tf = Tempfile.new(["vagrant", ".json"]).tap do |f| + f.write(<<-RAW) + { + "name": "foo/bar", + "versions": [ + { + "version": "0.5" + }, + { + "version": "0.7", + "providers": [ + { + "name": "virtualbox", + "url": "#{box_path}" + } + ] + } + ] + } + RAW + f.close + end + + opts = { json_type: "application/json; charset=utf-8" } + + md_path = Pathname.new(tf.path) + with_web_server(md_path, **opts) do |port| + env[:box_url] = "http://127.0.0.1:#{port}/#{md_path.basename}" + + expect(box_collection).to receive(:add).with { |path, name, version, **opts| + expect(name).to eq("foo/bar") + expect(version).to eq("0.7") + expect(checksum(path)).to eq(checksum(box_path)) + expect(opts[:metadata_url]).to eq(env[:box_url]) + true + }.and_return(box) + + expect(app).to receive(:call).with(env) + + subject.call(env) + end + end + it "adds from shorthand path" do box_path = iso_env.box2_file(:virtualbox) td = Pathname.new(Dir.mktmpdir)