1
1
require_relative './spec_helper'
2
2
3
- # A test double to mock the Gzip compression and keep the 'fake-compressed' result readable in test failures
4
- class GzipWriterDouble < StringIO
5
- GZIP_PREFIX = '$GZIP$:' . freeze
6
- def close
7
- super
8
- # `GzipWriter#close` returns `self` after closing (allowing use to use method chaining), unlike `StringIO#close`
9
- self
10
- end
11
-
12
- def string
13
- "#{ GZIP_PREFIX } #{ super } "
14
- end
15
- end
16
-
17
3
describe Fastlane ::Actions ::IosSendAppSizeMetricsAction do
18
4
let ( :test_data_dir ) { File . join ( File . dirname ( __FILE__ ) , 'test-data' , 'app_size_metrics' ) }
19
5
let ( :mocked_endpoint ) { 'https://localhost/api/metrics' }
@@ -22,7 +8,7 @@ def string
22
8
# Utility method to turn a minified JSON into a pretty-formatted JSON (without the GZIP fake prefix if any)
23
9
# To make any test failure and diff easier to debug
24
10
def pretty_json ( json_string )
25
- JSON . pretty_generate ( JSON . parse ( json_string . delete_prefix ( GzipWriterDouble :: GZIP_PREFIX ) ) )
11
+ JSON . pretty_generate ( JSON . parse ( json_string ) )
26
12
end
27
13
28
14
def test_app_size_action ( fake_ipa_size :, expected_json :, **other_action_args )
@@ -31,9 +17,6 @@ def test_app_size_action(fake_ipa_size:, expected_json:, **other_action_args)
31
17
ipa_path = File . join ( tmp_dir , 'fake.ipa' )
32
18
File . write ( ipa_path , SecureRandom . random_bytes ( fake_ipa_size ) )
33
19
34
- # Arrange: Mock GzipWriter with a mock for easier debugging in case of test failures
35
- allow ( Zlib ::GzipWriter ) . to receive ( :new ) . and_return ( GzipWriterDouble . new )
36
-
37
20
# Arrange: Stub any request to the mocked_endpoint, and memorize the received body
38
21
expected_headers = {
39
22
Authorization : "Bearer #{ mocked_token } " ,
@@ -56,11 +39,14 @@ def test_app_size_action(fake_ipa_size:, expected_json:, **other_action_args)
56
39
# Asserts
57
40
expect ( stub ) . to have_been_made . once
58
41
expect ( code ) . to eq ( 200 )
59
- expect ( last_received_body ) . to start_with ( GzipWriterDouble ::GZIP_PREFIX ) , 'Payload is expected to be GZipped, but was not'
42
+ last_received_body_uncompressed = nil
43
+ expect do
44
+ last_received_body_uncompressed = Zlib . gunzip ( last_received_body )
45
+ end . not_to raise_error , 'Payload was not valid GZipped data'
60
46
# Compare the payloads as pretty-formatted JSON, to make the diff in test failures more readable if one happen
61
- expect ( pretty_json ( last_received_body ) ) . to eq ( pretty_json ( expected_json ) ) , 'Decompressed JSON payload was not as expected'
62
- # Compare the payloads as raw strings as a final check
63
- expect ( last_received_body ) . to eq ( GzipWriterDouble . new ( expected_json ) . string )
47
+ expect ( pretty_json ( last_received_body_uncompressed ) ) . to eq ( pretty_json ( expected_json ) ) , 'Decompressed JSON payload was not as expected'
48
+ # Compare the payloads as raw uncompressed data as a final check
49
+ expect ( last_received_body_uncompressed ) . to eq ( expected_json )
64
50
end
65
51
end
66
52
0 commit comments