Skip to content

Commit d19e3f9

Browse files
jnunemakerclaude
andcommitted
Gzip compress request bodies for Cloud migrate and push
Use Typecast.to_gzip to compress request bodies and set content-encoding: gzip header, matching the pattern used by telemetry submitter. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1cb3a42 commit d19e3f9

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

lib/flipper/cloud/migrate.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require "json"
22
require "flipper/adapters/http/client"
3+
require "flipper/typecast"
34

45
module Flipper
56
module Cloud
@@ -21,7 +22,7 @@ def self.migrate(flipper = Flipper, app_name: nil)
2122
}
2223

2324
client = build_client("/api")
24-
response = client.post("/migrate", JSON.generate(payload))
25+
response = client.post("/migrate", Typecast.to_gzip(JSON.generate(payload)))
2526
body = JSON.parse(response.body) rescue {}
2627

2728
MigrateResult.new(
@@ -43,7 +44,7 @@ def self.push(token, flipper = Flipper)
4344
client = build_client("/adapter", headers: {
4445
"flipper-cloud-token" => token,
4546
})
46-
response = client.post("/import", export.contents)
47+
response = client.post("/import", Typecast.to_gzip(export.contents))
4748
body = JSON.parse(response.body) rescue {}
4849

4950
MigrateResult.new(
@@ -59,7 +60,7 @@ def self.build_client(path, headers: {})
5960

6061
Flipper::Adapters::Http::Client.new(
6162
url: "#{base_url}#{path}",
62-
headers: headers,
63+
headers: {"content-encoding" => "gzip"}.merge(headers),
6364
open_timeout: 5,
6465
read_timeout: 30,
6566
write_timeout: 30,

spec/flipper/cloud/migrate_spec.rb

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "flipper/cloud/migrate"
2+
require "flipper/typecast"
23
require "webmock/rspec"
34

45
RSpec.describe Flipper::Cloud, ".migrate" do
@@ -18,6 +19,11 @@
1819
ENV["FLIPPER_CLOUD_URL"] = original
1920
end
2021

22+
def decompress_request_body
23+
raw = WebMock::RequestRegistry.instance.requested_signatures.hash.keys.last.body
24+
JSON.parse(Flipper::Typecast.from_gzip(raw))
25+
end
26+
2127
describe ".migrate" do
2228
it "returns a MigrateResult with code and url on success" do
2329
stub_request(:post, "http://localhost:5555/api/migrate")
@@ -37,12 +43,24 @@
3743
Flipper::Cloud.migrate(flipper, app_name: "MyApp")
3844

3945
expect(stub).to have_been_requested
40-
body = JSON.parse(WebMock::RequestRegistry.instance.requested_signatures.hash.keys.last.body)
46+
body = decompress_request_body
4147
expect(body["metadata"]["app_name"]).to eq("MyApp")
4248
expect(body["export"]["version"]).to eq(1)
4349
expect(body["export"]["features"]).to have_key("search")
4450
end
4551

52+
it "sends gzip-compressed request body" do
53+
stub = stub_request(:post, "http://localhost:5555/api/migrate")
54+
.with(headers: {"content-encoding" => "gzip"})
55+
.to_return(status: 200, body: '{"url":"http://localhost:5555/cloud/setup/abc"}')
56+
57+
Flipper::Cloud.migrate(flipper)
58+
59+
expect(stub).to have_been_requested
60+
body = decompress_request_body
61+
expect(body["export"]["features"]).to have_key("search")
62+
end
63+
4664
it "handles error responses" do
4765
stub_request(:post, "http://localhost:5555/api/migrate")
4866
.to_return(status: 500, body: '{"error":"Internal Server Error"}')
@@ -107,13 +125,15 @@
107125
expect(stub).to have_been_requested
108126
end
109127

110-
it "sends export contents as the body" do
128+
it "sends gzip-compressed export contents as the body" do
111129
stub = stub_request(:post, "http://localhost:5555/adapter/import")
130+
.with(headers: {"content-encoding" => "gzip"})
112131
.to_return(status: 204, body: "")
113132

114133
Flipper::Cloud.push("test-token", flipper)
115134

116-
body = JSON.parse(WebMock::RequestRegistry.instance.requested_signatures.hash.keys.last.body)
135+
expect(stub).to have_been_requested
136+
body = decompress_request_body
117137
expect(body["version"]).to eq(1)
118138
expect(body["features"]).to have_key("search")
119139
end

0 commit comments

Comments
 (0)