192 lines
5.8 KiB
Ruby
Raw Normal View History

# typed: false
# frozen_string_literal: true
require "download_strategy"
describe CurlDownloadStrategy do
subject(:strategy) { described_class.new(url, name, version, **specs) }
let(:name) { "foo" }
let(:url) { "https://example.com/foo.tar.gz" }
let(:version) { "1.2.3" }
let(:specs) { { user: "download:123456" } }
it "parses the opts and sets the corresponding args" do
expect(strategy.send(:_curl_args)).to eq(["--user", "download:123456"])
end
describe "#cached_location" do
subject(:location) { described_class.new(url, name, version, **specs).cached_location }
context "when URL ends with file" do
it {
expect(location).to eq(
HOMEBREW_CACHE/"downloads/3d1c0ae7da22be9d83fb1eb774df96b7c4da71d3cf07e1cb28555cf9a5e5af70--foo.tar.gz",
)
}
end
context "when URL file is in middle" do
let(:url) { "https://example.com/foo.tar.gz/from/this/mirror" }
it {
expect(location).to eq(
HOMEBREW_CACHE/"downloads/1ab61269ba52c83994510b1e28dd04167a2f2e8393a35a9c50c1f7d33fd8f619--foo.tar.gz",
)
}
end
end
describe "#fetch" do
before do
strategy.temporary_path.dirname.mkpath
FileUtils.touch strategy.temporary_path
end
it "calls curl with default arguments" do
expect(strategy).to receive(:curl).with(
"--location",
"--remote-time",
"--continue-at", "0",
"--output", an_instance_of(Pathname),
url,
an_instance_of(Hash)
)
strategy.fetch
end
context "with an explicit user agent" do
let(:specs) { { user_agent: "Mozilla/25.0.1" } }
it "adds the appropriate curl args" do
expect(strategy).to receive(:system_command).with(
/curl/,
hash_including(args: array_including_cons("--user-agent", "Mozilla/25.0.1")),
)
.at_least(:once)
.and_return(instance_double(SystemCommand::Result, success?: true, stdout: "", assert_success!: nil))
strategy.fetch
end
end
context "with a generalized fake user agent" do
alias_matcher :a_string_matching, :match
let(:specs) { { user_agent: :fake } }
it "adds the appropriate curl args" do
expect(strategy).to receive(:system_command).with(
/curl/,
hash_including(args: array_including_cons(
"--user-agent",
a_string_matching(/Mozilla.*Mac OS X 10.*AppleWebKit/),
)),
)
.at_least(:once)
.and_return(instance_double(SystemCommand::Result, success?: true, stdout: "", assert_success!: nil))
strategy.fetch
end
end
context "with cookies set" do
let(:specs) {
{
cookies: {
coo: "k/e",
mon: "ster",
},
}
}
it "adds the appropriate curl args and does not URL-encode the cookies" do
expect(strategy).to receive(:system_command).with(
/curl/,
hash_including(args: array_including_cons("-b", "coo=k/e;mon=ster")),
)
.at_least(:once)
.and_return(instance_double(SystemCommand::Result, success?: true, stdout: "", assert_success!: nil))
strategy.fetch
end
end
context "with referer set" do
let(:specs) { { referer: "https://somehost/also" } }
it "adds the appropriate curl args" do
expect(strategy).to receive(:system_command).with(
/curl/,
hash_including(args: array_including_cons("-e", "https://somehost/also")),
)
.at_least(:once)
.and_return(instance_double(SystemCommand::Result, success?: true, stdout: "", assert_success!: nil))
strategy.fetch
end
end
context "with headers set" do
alias_matcher :a_string_matching, :match
let(:specs) { { headers: ["foo", "bar"] } }
it "adds the appropriate curl args" do
expect(strategy).to receive(:system_command).with(
/curl/,
hash_including(args: array_including_cons("--header", "foo").and(array_including_cons("--header", "bar"))),
)
.at_least(:once)
.and_return(instance_double(SystemCommand::Result, success?: true, stdout: "", assert_success!: nil))
strategy.fetch
end
end
end
describe "#cached_location" do
context "with a file name trailing the URL path" do
let(:url) { "https://example.com/cask.dmg" }
its("cached_location.extname") { is_expected.to eq(".dmg") }
end
context "with a file name trailing the first query parameter" do
let(:url) { "https://example.com/download?file=cask.zip&a=1" }
its("cached_location.extname") { is_expected.to eq(".zip") }
end
context "with a file name trailing the second query parameter" do
let(:url) { "https://example.com/dl?a=1&file=cask.zip&b=2" }
its("cached_location.extname") { is_expected.to eq(".zip") }
end
context "with an unusually long query string" do
let(:url) do
[
"https://node49152.ssl.fancycdn.example.com",
"/fancycdn/node/49152/file/upload/download",
"?cask_class=zf920df",
"&cask_group=2348779087242312",
"&cask_archive_file_name=cask.zip",
"&signature=CGmDulxL8pmutKTlCleNTUY%2FyO9Xyl5u9yVZUE0",
"uWrjadjuz67Jp7zx3H7NEOhSyOhu8nzicEHRBjr3uSoOJzwkLC8L",
"BLKnz%2B2X%2Biq5m6IdwSVFcLp2Q1Hr2kR7ETn3rF1DIq5o0lHC",
"yzMmyNe5giEKJNW8WF0KXriULhzLTWLSA3ZTLCIofAdRiiGje1kN",
"YY3C0SBqymQB8CG3ONn5kj7CIGbxrDOq5xI2ZSJdIyPysSX7SLvE",
"DBw2KdR24q9t1wfjS9LUzelf5TWk6ojj8p9%2FHjl%2Fi%2FVCXN",
"N4o1mW%2FMayy2tTY1qcC%2FTmqI1ulZS8SNuaSgr9Iys9oDF1%2",
"BPK%2B4Sg==",
].join
end
its("cached_location.extname") { is_expected.to eq(".zip") }
its("cached_location.to_path.length") { is_expected.to be_between(0, 255) }
end
end
end