Skip to content

Commit 7158494

Browse files
committed
Add unit tests for compressed vs uncompressed
1 parent 3c7cfd1 commit 7158494

File tree

1 file changed

+124
-59
lines changed

1 file changed

+124
-59
lines changed

spec/ios_send_app_size_metrics_spec.rb

Lines changed: 124 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def test_app_size_action(fake_ipa_size:, expected_json:, **other_action_args)
2222
expected_headers = {
2323
Authorization: "Bearer #{mocked_token}",
2424
'Content-Type': 'application/json',
25-
'Content-Encoding': 'gzip'
26-
}
25+
'Content-Encoding': ('gzip' unless other_action_args[:use_gzip_content_encoding] === false)
26+
}.compact
2727
last_received_body = nil
2828
stub = stub_request(:post, mocked_endpoint).with(headers: expected_headers) do |req|
2929
last_received_body = req.body
@@ -41,82 +41,147 @@ def test_app_size_action(fake_ipa_size:, expected_json:, **other_action_args)
4141
expect(stub).to have_been_made.once
4242
expect(code).to eq(200)
4343
last_received_body_uncompressed = nil
44-
expect do
45-
last_received_body_uncompressed = Zlib.gunzip(last_received_body)
46-
end.not_to raise_error, 'Payload was not valid GZipped data'
44+
puts other_action_args.inspect
45+
if other_action_args[:use_gzip_content_encoding] === false
46+
last_received_body_uncompressed = last_received_body
47+
else
48+
expect do
49+
last_received_body_uncompressed = Zlib.gunzip(last_received_body)
50+
end.not_to raise_error, 'Payload was not valid GZipped data'
51+
end
4752
# Compare the payloads as pretty-formatted JSON, to make the diff in test failures more readable if one happen
4853
expect(pretty_json(last_received_body_uncompressed)).to eq(pretty_json(expected_json)), 'Decompressed JSON payload was not as expected'
4954
# Compare the payloads as raw uncompressed data as a final check
5055
expect(last_received_body_uncompressed).to eq(expected_json)
5156
end
5257
end
5358

54-
it 'sends the right payload from just an ipa file' do
55-
expected_json = {
56-
meta: [
57-
{ name: 'Platform', value: 'iOS' },
58-
{ name: 'App Name', value: 'my-app' },
59-
{ name: 'App Version', value: '1.2.3' },
60-
{ name: 'Build Type', value: 'beta' },
61-
{ name: 'Source', value: 'unit-test' },
62-
],
63-
metrics: [
64-
{ name: 'File Size', value: 123_456 },
65-
]
66-
}.to_json
67-
68-
test_app_size_action(
69-
fake_ipa_size: 123_456,
70-
expected_json: expected_json,
71-
app_name: 'my-app',
72-
build_type: 'beta',
73-
app_version: '1.2.3',
74-
source: 'unit-test'
75-
)
76-
end
59+
context 'when using using a non-file:// url' do
60+
it 'sends the right payload from just an ipa file' do
61+
expected_json = {
62+
meta: [
63+
{ name: 'Platform', value: 'iOS' },
64+
{ name: 'App Name', value: 'my-app' },
65+
{ name: 'App Version', value: '1.2.3' },
66+
{ name: 'Build Type', value: 'beta' },
67+
{ name: 'Source', value: 'unit-test' },
68+
],
69+
metrics: [
70+
{ name: 'File Size', value: 123_456 },
71+
]
72+
}.to_json
7773

78-
it 'sends the right payload from an ipa file and an app-thinning.plist' do
79-
app_thinning_plist_path = File.join(test_data_dir, 'app-thinning.plist')
80-
expected_fixture = File.join(test_data_dir, 'ios-metrics-payload.json')
81-
# Parse as Hash then reconvert to JSON, in order to get the 'minified' JSON version
82-
expected_json = JSON.parse(File.read(expected_fixture)).to_json
83-
84-
test_app_size_action(
85-
fake_ipa_size: fake_ipa_size,
86-
expected_json: expected_json,
87-
app_thinning_plist_path: app_thinning_plist_path,
88-
app_name: 'wordpress',
89-
build_type: 'internal',
90-
app_version: '19.8.0.2'
91-
)
92-
end
74+
test_app_size_action(
75+
fake_ipa_size: 123_456,
76+
expected_json: expected_json,
77+
app_name: 'my-app',
78+
build_type: 'beta',
79+
app_version: '1.2.3',
80+
source: 'unit-test'
81+
)
82+
end
9383

94-
it 'writes the payload to a file if provided a file:// base URL' do
95-
in_tmp_dir do |tmp_dir|
96-
# Arrange: Generate a fake `.ipa` file with the expected size
97-
ipa_path = File.join(tmp_dir, 'fake.ipa')
98-
File.write(ipa_path, SecureRandom.random_bytes(fake_ipa_size))
99-
output_path = File.join(tmp_dir, 'payload.json')
84+
it 'sends the uncompressed payload from just an ipa file if disabling gzip' do
85+
expected_json = {
86+
meta: [
87+
{ name: 'Platform', value: 'iOS' },
88+
{ name: 'App Name', value: 'my-app' },
89+
{ name: 'App Version', value: '1.2.3' },
90+
{ name: 'Build Type', value: 'beta' },
91+
{ name: 'Source', value: 'unit-test' },
92+
],
93+
metrics: [
94+
{ name: 'File Size', value: 123_456 },
95+
]
96+
}.to_json
97+
98+
test_app_size_action(
99+
fake_ipa_size: 123_456,
100+
expected_json: expected_json,
101+
app_name: 'my-app',
102+
build_type: 'beta',
103+
app_version: '1.2.3',
104+
source: 'unit-test',
105+
use_gzip_content_encoding: false
106+
)
107+
end
100108

109+
it 'sends the right payload from an ipa file and an app-thinning.plist' do
101110
app_thinning_plist_path = File.join(test_data_dir, 'app-thinning.plist')
102111
expected_fixture = File.join(test_data_dir, 'ios-metrics-payload.json')
103112
# Parse as Hash then reconvert to JSON, in order to get the 'minified' JSON version
104113
expected_json = JSON.parse(File.read(expected_fixture)).to_json
105114

106-
# Act
107-
code = run_described_fastlane_action(
108-
api_base_url: File.join('file://localhost/', output_path),
115+
test_app_size_action(
116+
fake_ipa_size: fake_ipa_size,
117+
expected_json: expected_json,
118+
app_thinning_plist_path: app_thinning_plist_path,
109119
app_name: 'wordpress',
110120
build_type: 'internal',
111-
app_version: '19.8.0.2',
112-
ipa_path: ipa_path,
113-
app_thinning_plist_path: app_thinning_plist_path
121+
app_version: '19.8.0.2'
114122
)
123+
end
124+
end
115125

116-
# Assert
117-
expect(code).to eq(200)
118-
expect(File).to exist(output_path)
119-
expect(JSON.parse(File.read(output_path))).to eq(JSON.parse(expected_json))
126+
context 'when using a file:// URL' do
127+
it 'writes the payload uncompressed to a file' do
128+
in_tmp_dir do |tmp_dir|
129+
# Arrange: Generate a fake `.ipa` file with the expected size
130+
ipa_path = File.join(tmp_dir, 'fake.ipa')
131+
File.write(ipa_path, SecureRandom.random_bytes(fake_ipa_size))
132+
output_path = File.join(tmp_dir, 'payload.json.gz')
133+
134+
app_thinning_plist_path = File.join(test_data_dir, 'app-thinning.plist')
135+
expected_fixture = File.join(test_data_dir, 'ios-metrics-payload.json')
136+
# Parse as Hash then reconvert to JSON, in order to get the 'minified' JSON version
137+
expected_json = JSON.parse(File.read(expected_fixture)).to_json
138+
139+
# Act
140+
code = run_described_fastlane_action(
141+
api_base_url: File.join('file://localhost/', output_path),
142+
app_name: 'wordpress',
143+
build_type: 'internal',
144+
app_version: '19.8.0.2',
145+
ipa_path: ipa_path,
146+
app_thinning_plist_path: app_thinning_plist_path,
147+
use_gzip_content_encoding: true,
148+
)
149+
150+
# Assert
151+
expect(code).to eq(200)
152+
expect(File).to exist(output_path)
153+
expect(JSON.parse(Zlib.gunzip(File.read(output_path)))).to eq(JSON.parse(expected_json))
154+
end
155+
end
156+
157+
it 'writes the payload uncompressed to a file when disabling gzip' do
158+
in_tmp_dir do |tmp_dir|
159+
# Arrange: Generate a fake `.ipa` file with the expected size
160+
ipa_path = File.join(tmp_dir, 'fake.ipa')
161+
File.write(ipa_path, SecureRandom.random_bytes(fake_ipa_size))
162+
output_path = File.join(tmp_dir, 'payload.json')
163+
164+
app_thinning_plist_path = File.join(test_data_dir, 'app-thinning.plist')
165+
expected_fixture = File.join(test_data_dir, 'ios-metrics-payload.json')
166+
# Parse as Hash then reconvert to JSON, in order to get the 'minified' JSON version
167+
expected_json = JSON.parse(File.read(expected_fixture)).to_json
168+
169+
# Act
170+
code = run_described_fastlane_action(
171+
api_base_url: File.join('file://localhost/', output_path),
172+
app_name: 'wordpress',
173+
build_type: 'internal',
174+
app_version: '19.8.0.2',
175+
ipa_path: ipa_path,
176+
app_thinning_plist_path: app_thinning_plist_path,
177+
use_gzip_content_encoding: false,
178+
)
179+
180+
# Assert
181+
expect(code).to eq(200)
182+
expect(File).to exist(output_path)
183+
expect(JSON.parse(File.read(output_path))).to eq(JSON.parse(expected_json))
184+
end
120185
end
121186
end
122187
end

0 commit comments

Comments
 (0)