Skip to content

Commit

Permalink
core: don't do HEAD request for box on non-HTTP [GH-5477]
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Nov 19, 2015
1 parent 1a7937e commit 891c47c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/vagrant/action/builtin/box_add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,9 @@ def metadata_url?(url, env)
end
end

# If this isn't HTTP, then don't do the HEAD request
return false if !uri.scheme.downcase.start_with?("http")

output = d.head
match = output.scan(/^Content-Type: (.+?)$/i).last
return false if !match
Expand Down
37 changes: 37 additions & 0 deletions test/unit/vagrant/action/builtin/box_add_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require "tmpdir"
require "webrick"

require "fake_ftp"

require File.expand_path("../../../../base", __FILE__)

require "vagrant/util/file_checksum"
Expand Down Expand Up @@ -34,6 +36,21 @@ def checksum(path)
FileChecksum.new(path, Digest::SHA1).checksum
end

def with_ftp_server(path, **opts)
path = Pathname.new(path)

tf = Tempfile.new("vagrant")
tf.close

port = 21212
server = FakeFtp::Server.new(port, port+1)
server.add_file(path.basename, path.read)
server.start
yield port
ensure
server.stop rescue nil
end

def with_web_server(path, **opts)
tf = Tempfile.new("vagrant")
tf.close
Expand Down Expand Up @@ -123,6 +140,26 @@ def with_web_server(path, **opts)
end
end

it "adds from FTP URL" do
box_path = iso_env.box2_file(:virtualbox)
with_ftp_server(box_path) do |port|
env[:box_name] = "foo"
env[:box_url] = "ftp://127.0.0.1:#{port}/#{box_path.basename}"

expect(box_collection).to receive(:add).with { |path, name, version, **opts|
expect(checksum(path)).to eq(checksum(box_path))
expect(name).to eq("foo")
expect(version).to eq("0")
expect(opts[:metadata_url]).to be_nil
true
}.and_return(box)

expect(app).to receive(:call).with(env)

subject.call(env)
end
end

it "raises an error if no name is given" do
box_path = iso_env.box2_file(:virtualbox)

Expand Down

0 comments on commit 891c47c

Please sign in to comment.