Skip to content

Commit

Permalink
Merge pull request #183 from buildkite/pie-1599/gzip-upload-data
Browse files Browse the repository at this point in the history
Gzip body of upload request
  • Loading branch information
niceking authored Apr 20, 2023
2 parents 2bcd94c + 723d021 commit b468395
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion buildkite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ steps:
- "bundle exec rake"
plugins:
- docker#v3.7.0:
image: ruby:latest
image: 445615400570.dkr.ecr.us-east-1.amazonaws.com/ecr-public/docker/library/ruby:3.1.4
11 changes: 10 additions & 1 deletion lib/buildkite/test_collector/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,25 @@ def post_json(data)
contact = Net::HTTP::Post.new(contact_uri.path, {
"Authorization" => authorization_header,
"Content-Type" => "application/json",
"Content-Encoding" => "gzip",
})

data_set = data.map(&:as_hash)

contact.body = {
body = {
run_env: Buildkite::TestCollector::CI.env,
format: "json",
data: data_set
}.to_json

compressed_body = StringIO.new

writer = Zlib::GzipWriter.new(compressed_body)
writer.write(body)
writer.close

contact.body = compressed_body.string

http.request(contact)
end

Expand Down
16 changes: 13 additions & 3 deletions spec/test_collector/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,24 @@
"failure_expanded": [],
"history": "pie lore"
}]
}
}.to_json
end

let(:compressed_body) do
str = StringIO.new

writer = Zlib::GzipWriter.new(str)
writer.write(request_body)
writer.close

str.string
end

before do
allow(Net::HTTP).to receive(:new).and_return(http_double)
allow(http_double).to receive(:use_ssl=)

allow(Net::HTTP::Post).to receive(:new).with("buildkite.localhost", {"Authorization"=>"Token token=\"my-cool-token\"", "Content-Type"=>"application/json"}).and_return(post_double)
allow(Net::HTTP::Post).to receive(:new).with("buildkite.localhost", {"Authorization"=>"Token token=\"my-cool-token\"", "Content-Encoding"=>"gzip", "Content-Type"=>"application/json"}).and_return(post_double)

allow(ENV).to receive(:[]).and_call_original
fake_env("BUILDKITE_ANALYTICS_KEY", "build-123")
Expand All @@ -62,7 +72,7 @@

describe "#post_json" do
it "sends the right data" do
expect(post_double).to receive(:body=).with(request_body.to_json)
expect(post_double).to receive(:body=).with(compressed_body)
expect(http_double).to receive(:request).with(post_double)
subject.post_json([trace])
end
Expand Down

0 comments on commit b468395

Please sign in to comment.