11require_relative './spec_helper'
22
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-
173describe Fastlane ::Actions ::IosSendAppSizeMetricsAction do
184 let ( :test_data_dir ) { File . join ( File . dirname ( __FILE__ ) , 'test-data' , 'app_size_metrics' ) }
195 let ( :mocked_endpoint ) { 'https://localhost/api/metrics' }
@@ -22,7 +8,7 @@ def string
228 # Utility method to turn a minified JSON into a pretty-formatted JSON (without the GZIP fake prefix if any)
239 # To make any test failure and diff easier to debug
2410 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 ) )
2612 end
2713
2814 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)
3117 ipa_path = File . join ( tmp_dir , 'fake.ipa' )
3218 File . write ( ipa_path , SecureRandom . random_bytes ( fake_ipa_size ) )
3319
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-
3720 # Arrange: Stub any request to the mocked_endpoint, and memorize the received body
3821 expected_headers = {
3922 Authorization : "Bearer #{ mocked_token } " ,
@@ -56,11 +39,14 @@ def test_app_size_action(fake_ipa_size:, expected_json:, **other_action_args)
5639 # Asserts
5740 expect ( stub ) . to have_been_made . once
5841 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'
6046 # 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 )
6450 end
6551 end
6652
0 commit comments