|
63 | 63 | end |
64 | 64 | end |
65 | 65 |
|
| 66 | + context "when server returns 401 with Authorization header" do |
| 67 | + let(:source) { "http://example.org/vagrant.box" } |
| 68 | + let(:options) { {headers: ["Authorization: Bearer expired"]} } |
| 69 | + |
| 70 | + let(:subprocess_401) do |
| 71 | + double("subprocess_401").tap do |result| |
| 72 | + allow(result).to receive(:exit_code).and_return(22) |
| 73 | + allow(result).to receive(:stderr).and_return("curl: (22) The requested URL returned error: 401") |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + it "retries without the Authorization header and succeeds" do |
| 78 | + first_call = ["-q", "--fail", "--location", "--max-redirs", "10", |
| 79 | + "--verbose", "--user-agent", described_class::USER_AGENT, |
| 80 | + "-H", "Authorization: Bearer expired", |
| 81 | + "--output", destination, source, {}] |
| 82 | + second_call = ["-q", "--fail", "--location", "--max-redirs", "10", |
| 83 | + "--verbose", "--user-agent", described_class::USER_AGENT, |
| 84 | + "--output", destination, source, {}] |
| 85 | + |
| 86 | + expect(Vagrant::Util::Subprocess).to receive(:execute). |
| 87 | + with("curl", *first_call).ordered.and_return(subprocess_401) |
| 88 | + |
| 89 | + expect(Vagrant::Util::Subprocess).to receive(:execute). |
| 90 | + with("curl", *second_call).ordered.and_return(subprocess_result) |
| 91 | + |
| 92 | + expect(subject.download!).to be(true) |
| 93 | + end |
| 94 | + end |
| 95 | + |
66 | 96 | context "with UI" do |
67 | 97 | let(:ui) { Vagrant::UI::Silent.new } |
68 | 98 | let(:options) { {ui: ui} } |
|
320 | 350 |
|
321 | 351 | expect(subject.head).to eq("foo") |
322 | 352 | end |
| 353 | + |
| 354 | + context "when server returns 401 with Authorization header" do |
| 355 | + let(:source) { "http://example.org/metadata.json" } |
| 356 | + let(:options) { {headers: ["Authorization: Bearer expired"]} } |
| 357 | + |
| 358 | + let(:subprocess_401) do |
| 359 | + double("subprocess_401").tap do |result| |
| 360 | + allow(result).to receive(:exit_code).and_return(22) |
| 361 | + allow(result).to receive(:stderr).and_return("curl: (22) The requested URL returned error: 401") |
| 362 | + allow(result).to receive(:stdout).and_return("") |
| 363 | + end |
| 364 | + end |
| 365 | + |
| 366 | + let(:subprocess_ok) do |
| 367 | + double("subprocess_ok").tap do |result| |
| 368 | + allow(result).to receive(:exit_code).and_return(0) |
| 369 | + allow(result).to receive(:stderr).and_return("") |
| 370 | + allow(result).to receive(:stdout).and_return("HTTP/1.1 200 OK\nContent-Type: application/json") |
| 371 | + end |
| 372 | + end |
| 373 | + |
| 374 | + it "retries without the Authorization header and succeeds" do |
| 375 | + # First attempt should include Authorization header and fail with 401 |
| 376 | + first_call = ["-q", "-I", "--fail", "--location", "--max-redirs", "10", |
| 377 | + "--verbose", "--user-agent", described_class::USER_AGENT, |
| 378 | + "-H", "Authorization: Bearer expired", |
| 379 | + source, {}] |
| 380 | + expect(Vagrant::Util::Subprocess).to receive(:execute). |
| 381 | + with("curl", *first_call).ordered.and_return(subprocess_401) |
| 382 | + |
| 383 | + # Second attempt should exclude Authorization header and succeed |
| 384 | + second_call = ["-q", "-I", "--fail", "--location", "--max-redirs", "10", |
| 385 | + "--verbose", "--user-agent", described_class::USER_AGENT, |
| 386 | + source, {}] |
| 387 | + expect(Vagrant::Util::Subprocess).to receive(:execute). |
| 388 | + with("curl", *second_call).ordered.and_return(subprocess_ok) |
| 389 | + |
| 390 | + # Should not raise and should return the successful output |
| 391 | + expect(subject.head).to include("Content-Type: application/json") |
| 392 | + end |
| 393 | + end |
323 | 394 | end |
324 | 395 |
|
325 | 396 | describe "#options" do |
|
0 commit comments