Skip to content

Commit 30e878d

Browse files
committed
Implement new specs for app_size_metrics_payload
1 parent b97b8f3 commit 30e878d

File tree

1 file changed

+96
-4
lines changed

1 file changed

+96
-4
lines changed

spec/app_size_metrics_payload_spec.rb

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,106 @@
5151
end
5252

5353
describe '#send_metrics' do
54+
let(:payload) do
55+
payload = described_class.new({
56+
'Group Metadata 1': 'Group Value 1',
57+
'Group Metadata 2': 'Group Value 2'
58+
})
59+
payload.add_metric(name: 'Metric 1', value: 12_345, meta: { m1a: 'Metric 1 Metadata A' })
60+
payload.add_metric(name: 'Metric 2', value: 67_890)
61+
payload.add_metric(name: 'Metric 3', value: 13_579, meta: { m3a: 'Metric 3 Metadata A', m3b: 'Metric 3 Metadata B' })
62+
payload
63+
end
64+
let(:expected_data) do
65+
{
66+
meta: [
67+
{ name: 'Group Metadata 1', value: 'Group Value 1' },
68+
{ name: 'Group Metadata 2', value: 'Group Value 2' },
69+
],
70+
metrics: [
71+
{ name: 'Metric 1', value: 12_345, meta: [{ name: 'm1a', value: 'Metric 1 Metadata A' }] },
72+
{ name: 'Metric 2', value: 67_890 },
73+
{ name: 'Metric 3', value: 13_579, meta: [{ name: 'm3a', value: 'Metric 3 Metadata A' }, { name: 'm3b', value: 'Metric 3 Metadata B' }] },
74+
]
75+
}.to_json
76+
end
77+
5478
context 'when using file:// scheme for the URL' do
55-
it 'writes the payload uncompressed to a file when disabling gzip'
56-
it 'writes the payload compressed to a file when enabling gzip'
79+
it 'writes the payload uncompressed to a file when disabling gzip' do
80+
in_tmp_dir do |tmp_dir|
81+
output_file = File.join(tmp_dir, 'payload.json')
82+
base_url = File.join('file://localhost/', output_file)
83+
84+
code = payload.send_metrics(base_url: base_url, api_token: nil, use_gzip: false)
85+
86+
expect(code).to eq(201)
87+
expect(File).to exist(output_file)
88+
uncompressed_data = File.read(output_file)
89+
expect(uncompressed_data).to eq(expected_data)
90+
end
91+
end
92+
93+
it 'writes the payload compressed to a file when enabling gzip' do
94+
in_tmp_dir do |tmp_dir|
95+
output_file = File.join(tmp_dir, 'payload.json.gz')
96+
base_url = File.join('file://localhost/', output_file)
97+
98+
code = payload.send_metrics(base_url: base_url, api_token: nil, use_gzip: true)
99+
100+
expect(code).to eq(201)
101+
expect(File).to exist(output_file)
102+
uncompressed_data = nil
103+
expect do
104+
uncompressed_data = Zlib.gunzip(File.read(output_file))
105+
end.not_to raise_error
106+
expect(uncompressed_data).to eq(expected_data)
107+
end
108+
end
57109
end
58110

59111
context 'when using non-file:// scheme for the URL' do
60-
it 'sends the payload uncompressed to the server and with the right headers when disabling gzip'
61-
it 'writes the payload compressed to the server and with the right headers when enabling gzip'
112+
let(:base_url) { 'https://fake-metrics-server/api/grouped-metrics' }
113+
let(:token) { 'fake#tokn' }
114+
115+
it 'sends the payload uncompressed to the server and with the right headers when disabling gzip' do
116+
expected_headers = {
117+
Authorization: "Bearer #{token}",
118+
Accept: 'application/json',
119+
'Content-Type': 'application/json'
120+
}
121+
last_received_body = nil
122+
stub = stub_request(:post, base_url).with(headers: expected_headers) do |req|
123+
last_received_body = req.body
124+
end.to_return(status: 201)
125+
126+
code = payload.send_metrics(base_url: base_url, api_token: token, use_gzip: false)
127+
128+
expect(code).to eq(201)
129+
expect(stub).to have_been_made.once
130+
expect(last_received_body).to eq(expected_data)
131+
end
132+
133+
it 'writes the payload compressed to the server and with the right headers when enabling gzip' do
134+
expected_headers = {
135+
Authorization: "Bearer #{token}",
136+
Accept: 'application/json',
137+
'Content-Type': 'application/json',
138+
'Content-Encoding': 'gzip'
139+
}
140+
last_received_body = nil
141+
stub = stub_request(:post, base_url).with(headers: expected_headers) do |req|
142+
last_received_body = req.body
143+
end.to_return(status: 201)
144+
145+
code = payload.send_metrics(base_url: base_url, api_token: token, use_gzip: true)
146+
147+
expect(code).to eq(201)
148+
expect(stub).to have_been_made.once
149+
expect do
150+
last_received_body = Zlib.gunzip(last_received_body)
151+
end.not_to raise_error
152+
expect(last_received_body).to eq(expected_data)
153+
end
62154
end
63155
end
64156
end

0 commit comments

Comments
 (0)