Skip to content

Commit

Permalink
Refactor "can gzip requests" spec to fix race condition (#577)
Browse files Browse the repository at this point in the history
This spec had a race condition caused by the date and time in the "local file header" of the zip file. See https://en.wikipedia.org/wiki/ZIP_(file_format)#File_headers

This could result in the zip file that we compute during the test to be different from the zip file that the transporter sends in the HTTP request. This in turn caused stub_request(:post, url) to not match the request made in the transporter.

By asserting that decompressing the transporter's request body results in the original JSON, we avoid this race condition and clarify the intention of the test assertion.

Fixes #569
  • Loading branch information
willkoehler committed Apr 17, 2023
1 parent 0809d9c commit 45027b5
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions spec/lib/snapshots_transporter_spec.rb
Expand Up @@ -138,22 +138,16 @@
it 'can gzip requests' do
snapshot = Rack::MiniProfiler::TimerStruct::Page.new({})
json = { snapshots: [snapshot] }.to_json
compressed = compress(json)
stub_request(:post, url)
.with(
body: compressed,
headers: {
'Mini-Profiler-Transport-Auth' => 'somepasswordhere',
'Content-Encoding' => 'gzip'
}
)
.to_return(status: 200, body: "", headers: {})
stub_request(:post, url).to_return(status: 200, body: "", headers: {})
expect(gzip_transporter.gzip_requests).to eq(true)
gzip_transporter.ship(snapshot)
gzip_transporter.flush_buffer
expect(gzip_transporter.buffer.size).to eq(0)
expect(compressed.bytes.size < json.bytes.size).to eq(true)
expect(decompress(compressed)).to eq(json)
assert_requested(:post, url,
headers: {
'Mini-Profiler-Transport-Auth' => 'somepasswordhere',
'Content-Encoding' => 'gzip'
}, times: 1) { |req| decompress(req.body) == json }
end

def compress(body)
Expand Down

0 comments on commit 45027b5

Please sign in to comment.