summaryrefslogtreecommitdiff
path: root/spec/bundler/bundler/compact_index_client/updater_spec.rb
blob: 6eed88ca9e58369468f9bd6b69c1cb94cce5cae6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# frozen_string_literal: true

require "bundler/vendored_net_http"
require "bundler/compact_index_client"
require "bundler/compact_index_client/updater"
require "tmpdir"

RSpec.describe Bundler::CompactIndexClient::Updater do
  subject(:updater) { described_class.new(fetcher) }

  let(:fetcher) { double(:fetcher) }
  let(:local_path) { Pathname.new(Dir.mktmpdir("localpath")).join("versions") }
  let(:etag_path) { Pathname.new(Dir.mktmpdir("localpath-etags")).join("versions.etag") }
  let(:remote_path) { double(:remote_path) }

  let(:full_body) { "abc123" }
  let(:response) { double(:response, body: full_body, is_a?: false) }
  let(:digest) { Digest::SHA256.base64digest(full_body) }

  context "when the local path does not exist" do
    before do
      allow(response).to receive(:[]).with("Repr-Digest") { nil }
      allow(response).to receive(:[]).with("Digest") { nil }
      allow(response).to receive(:[]).with("ETag") { '"thisisanetag"' }
    end

    it "downloads the file without attempting append" do
      expect(fetcher).to receive(:call).once.with(remote_path, {}) { response }

      updater.update(remote_path, local_path, etag_path)

      expect(local_path.read).to eq(full_body)
      expect(etag_path.read).to eq("thisisanetag")
    end

    it "fails immediately on bad checksum" do
      expect(fetcher).to receive(:call).once.with(remote_path, {}) { response }
      allow(response).to receive(:[]).with("Repr-Digest") { "sha-256=:baddigest:" }

      expect do
        updater.update(remote_path, local_path, etag_path)
      end.to raise_error(Bundler::CompactIndexClient::Updater::MismatchedChecksumError)
    end
  end

  context "when the local path exists" do
    let(:local_body) { "abc" }

    before do
      local_path.open("w") {|f| f.write(local_body) }
    end

    context "with an etag" do
      before do
        etag_path.open("w") {|f| f.write("LocalEtag") }
      end

      let(:headers) do
        {
          "If-None-Match" => '"LocalEtag"',
          "Range" => "bytes=2-",
        }
      end

      it "does nothing if etags match" do
        expect(fetcher).to receive(:call).once.with(remote_path, headers).and_return(response)
        allow(response).to receive(:is_a?).with(Gem::Net::HTTPPartialContent) { false }
        allow(response).to receive(:is_a?).with(Gem::Net::HTTPNotModified) { true }

        updater.update(remote_path, local_path, etag_path)

        expect(local_path.read).to eq("abc")
        expect(etag_path.read).to eq("LocalEtag")
      end

      it "appends the file if etags do not match" do
        expect(fetcher).to receive(:call).once.with(remote_path, headers).and_return(response)
        allow(response).to receive(:[]).with("Repr-Digest") { "sha-256=:#{digest}:" }
        allow(response).to receive(:[]).with("ETag") { '"NewEtag"' }
        allow(response).to receive(:is_a?).with(Gem::Net::HTTPPartialContent) { true }
        allow(response).to receive(:is_a?).with(Gem::Net::HTTPNotModified) { false }
        allow(response).to receive(:body) { "c123" }

        updater.update(remote_path, local_path, etag_path)

        expect(local_path.read).to eq(full_body)
        expect(etag_path.read).to eq("NewEtag")
      end

      it "replaces the file if response ignores range" do
        expect(fetcher).to receive(:call).once.with(remote_path, headers).and_return(response)
        allow(response).to receive(:[]).with("Repr-Digest") { "sha-256=:#{digest}:" }
        allow(response).to receive(:[]).with("ETag") { '"NewEtag"' }
        allow(response).to receive(:body) { full_body }

        updater.update(remote_path, local_path, etag_path)

        expect(local_path.read).to eq(full_body)
        expect(etag_path.read).to eq("NewEtag")
      end

      it "tries the request again if the partial response fails digest check" do
        allow(response).to receive(:[]).with("Repr-Digest") { "sha-256=:baddigest:" }
        allow(response).to receive(:body) { "the beginning of the file changed" }
        allow(response).to receive(:is_a?).with(Gem::Net::HTTPPartialContent) { true }
        expect(fetcher).to receive(:call).once.with(remote_path, headers).and_return(response)

        full_response = double(:full_response, body: full_body, is_a?: false)
        allow(full_response).to receive(:[]).with("Repr-Digest") { "sha-256=:#{digest}:" }
        allow(full_response).to receive(:[]).with("ETag") { '"NewEtag"' }
        expect(fetcher).to receive(:call).once.with(remote_path, { "If-None-Match" => '"LocalEtag"' }).and_return(full_response)

        updater.update(remote_path, local_path, etag_path)

        expect(local_path.read).to eq(full_body)
        expect(etag_path.read).to eq("NewEtag")
      end
    end

    context "without an etag file" do
      let(:headers) do
        {
          "Range" => "bytes=2-",
          # This MD5 feature should be deleted after sufficient time has passed since release.
          # From then on, requests that still don't have a saved etag will be made without this header.
          "If-None-Match" => %("#{Digest::MD5.hexdigest(local_body)}"),
        }
      end

      it "saves only the etag_path if generated etag matches" do
        expect(fetcher).to receive(:call).once.with(remote_path, headers).and_return(response)
        allow(response).to receive(:is_a?).with(Gem::Net::HTTPPartialContent) { false }
        allow(response).to receive(:is_a?).with(Gem::Net::HTTPNotModified) { true }

        updater.update(remote_path, local_path, etag_path)

        expect(local_path.read).to eq("abc")
        expect(%("#{etag_path.read}")).to eq(headers["If-None-Match"])
      end

      it "appends the file" do
        expect(fetcher).to receive(:call).once.with(remote_path, headers).and_return(response)
        allow(response).to receive(:[]).with("Repr-Digest") { "sha-256=:#{digest}:" }
        allow(response).to receive(:[]).with("ETag") { '"OpaqueEtag"' }
        allow(response).to receive(:is_a?).with(Gem::Net::HTTPPartialContent) { true }
        allow(response).to receive(:is_a?).with(Gem::Net::HTTPNotModified) { false }
        allow(response).to receive(:body) { "c123" }

        updater.update(remote_path, local_path, etag_path)

        expect(local_path.read).to eq(full_body)
        expect(etag_path.read).to eq("OpaqueEtag")
      end

      it "replaces the file on full file response that ignores range request" do
        expect(fetcher).to receive(:call).once.with(remote_path, headers).and_return(response)
        allow(response).to receive(:[]).with("Repr-Digest") { nil }
        allow(response).to receive(:[]).with("Digest") { nil }
        allow(response).to receive(:[]).with("ETag") { '"OpaqueEtag"' }
        allow(response).to receive(:is_a?).with(Gem::Net::HTTPPartialContent) { false }
        allow(response).to receive(:is_a?).with(Gem::Net::HTTPNotModified) { false }
        allow(response).to receive(:body) { full_body }

        updater.update(remote_path, local_path, etag_path)

        expect(local_path.read).to eq(full_body)
        expect(etag_path.read).to eq("OpaqueEtag")
      end

      it "tries the request again if the partial response fails digest check" do
        allow(response).to receive(:[]).with("Repr-Digest") { "sha-256=:baddigest:" }
        allow(response).to receive(:body) { "the beginning of the file changed" }
        allow(response).to receive(:is_a?).with(Gem::Net::HTTPPartialContent) { true }
        expect(fetcher).to receive(:call).once.with(remote_path, headers) do
          # During the failed first request, we simulate another process writing the etag.
          # This ensures the second request doesn't generate the md5 etag again but just uses whatever is written.
          etag_path.open("w") {|f| f.write("LocalEtag") }
          response
        end

        full_response = double(:full_response, body: full_body, is_a?: false)
        allow(full_response).to receive(:[]).with("Repr-Digest") { "sha-256=:#{digest}:" }
        allow(full_response).to receive(:[]).with("ETag") { '"NewEtag"' }
        expect(fetcher).to receive(:call).once.with(remote_path, { "If-None-Match" => '"LocalEtag"' }).and_return(full_response)

        updater.update(remote_path, local_path, etag_path)

        expect(local_path.read).to eq(full_body)
        expect(etag_path.read).to eq("NewEtag")
      end
    end
  end

  context "when the ETag header is missing" do
    # Regression test for https://github.com/rubygems/bundler/issues/5463
    let(:response) { double(:response, body: full_body) }

    it "treats the response as an update" do
      allow(response).to receive(:[]).with("Repr-Digest") { nil }
      allow(response).to receive(:[]).with("Digest") { nil }
      allow(response).to receive(:[]).with("ETag") { nil }
      expect(fetcher).to receive(:call) { response }

      updater.update(remote_path, local_path, etag_path)
    end
  end

  context "when the download is corrupt" do
    let(:response) { double(:response, body: "") }

    it "raises HTTPError" do
      expect(fetcher).to receive(:call).and_raise(Zlib::GzipFile::Error)

      expect do
        updater.update(remote_path, local_path, etag_path)
      end.to raise_error(Bundler::HTTPError)
    end
  end

  context "when receiving non UTF-8 data and default internal encoding set to ASCII" do
    let(:response) { double(:response, body: "\x8B".b) }

    it "works just fine" do
      old_verbose = $VERBOSE
      previous_internal_encoding = Encoding.default_internal

      begin
        $VERBOSE = false
        Encoding.default_internal = "ASCII"
        allow(response).to receive(:[]).with("Repr-Digest") { nil }
        allow(response).to receive(:[]).with("Digest") { nil }
        allow(response).to receive(:[]).with("ETag") { nil }
        expect(fetcher).to receive(:call) { response }

        updater.update(remote_path, local_path, etag_path)
      ensure
        Encoding.default_internal = previous_internal_encoding
        $VERBOSE = old_verbose
      end
    end
  end
end