2015-07-17 11:50:42 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
2015-07-17 11:50:42 +00:00
|
|
|
*
|
2017-05-06 03:50:47 +00:00
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-07-17 11:50:42 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <XCTest/XCTest.h>
|
2016-11-24 17:44:51 +00:00
|
|
|
|
2017-04-21 15:16:17 +00:00
|
|
|
#import <RCTTest/RCTTestRunner.h>
|
2016-11-24 17:44:51 +00:00
|
|
|
#import <React/RCTNetworking.h>
|
|
|
|
#import <React/RCTUtils.h>
|
2015-07-17 11:50:42 +00:00
|
|
|
|
|
|
|
extern BOOL RCTIsGzippedData(NSData *data);
|
|
|
|
|
|
|
|
@interface RCTNetworking (Private)
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
- (void)buildRequest:(NSDictionary<NSString *, id> *)query
|
2015-07-17 11:50:42 +00:00
|
|
|
completionBlock:(void (^)(NSURLRequest *request))block;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface RCTGzipTests : XCTestCase
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTGzipTests
|
|
|
|
|
|
|
|
- (void)testGzip
|
|
|
|
{
|
|
|
|
//set up data
|
|
|
|
NSString *inputString = @"Hello World!";
|
|
|
|
NSData *inputData = [inputString dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
//compress
|
|
|
|
NSData *outputData = RCTGzipData(inputData, -1);
|
|
|
|
XCTAssertTrue(RCTIsGzippedData(outputData));
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testDontRezipZippedData
|
|
|
|
{
|
|
|
|
//set up data
|
|
|
|
NSString *inputString = @"Hello World!";
|
|
|
|
NSData *inputData = [inputString dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
//compress
|
|
|
|
NSData *compressedData = RCTGzipData(inputData, -1);
|
|
|
|
inputString = [[NSString alloc] initWithData:compressedData encoding:NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
//compress again
|
|
|
|
NSData *outputData = RCTGzipData(inputData, -1);
|
|
|
|
NSString *outputString = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];
|
|
|
|
XCTAssertEqualObjects(outputString, inputString);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testRequestBodyEncoding
|
|
|
|
{
|
|
|
|
NSDictionary *query = @{
|
2015-11-25 11:09:00 +00:00
|
|
|
@"url": @"http://example.com",
|
|
|
|
@"method": @"POST",
|
|
|
|
@"data": @{@"string": @"Hello World"},
|
|
|
|
@"headers": @{@"Content-Encoding": @"gzip"},
|
|
|
|
};
|
2015-07-17 11:50:42 +00:00
|
|
|
|
2015-08-17 14:35:34 +00:00
|
|
|
RCTNetworking *networker = [RCTNetworking new];
|
2015-11-25 11:09:00 +00:00
|
|
|
[networker setValue:dispatch_get_main_queue() forKey:@"methodQueue"];
|
2015-07-17 11:50:42 +00:00
|
|
|
__block NSURLRequest *request = nil;
|
|
|
|
[networker buildRequest:query completionBlock:^(NSURLRequest *_request) {
|
|
|
|
request = _request;
|
|
|
|
}];
|
|
|
|
|
2017-04-21 15:16:17 +00:00
|
|
|
RCT_RUN_RUNLOOP_WHILE(request == nil);
|
2015-11-25 11:09:00 +00:00
|
|
|
|
2015-07-17 11:50:42 +00:00
|
|
|
XCTAssertNotNil(request);
|
|
|
|
XCTAssertNotNil(request.HTTPBody);
|
|
|
|
XCTAssertTrue(RCTIsGzippedData(request.HTTPBody));
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|