2016-10-31 20:06:13 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
2016-10-31 20:06:13 +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.
|
2016-10-31 20:06:13 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
|
2016-11-24 17:44:51 +00:00
|
|
|
#import <React/RCTNetworking.h>
|
2016-10-31 20:06:13 +00:00
|
|
|
|
2016-11-24 17:44:51 +00:00
|
|
|
static NSString *const niqqudStringB64 = @"15HWsNa816jWtdeQ16nWtNeB15nXqiwg15HWuNa816jWuNeQINeQ1rHXnNa515TWtNeZ150sINeQ1rXXqiDXlNa316nWuNa814HXnta315nWtNedLCDXldaw15DWtdeqINeU1rjXkNa416jWttelLg==";
|
2016-10-31 20:06:13 +00:00
|
|
|
|
|
|
|
@interface RCTNetworking ()
|
|
|
|
|
2016-11-24 17:44:51 +00:00
|
|
|
+ (NSString *)decodeTextData:(NSData *)data fromResponse:(NSURLResponse *)response withCarryData:(NSMutableData *)inputCarryData;
|
2016-10-31 20:06:13 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface RCTUnicodeDecodeTests : XCTestCase
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTUnicodeDecodeTests
|
|
|
|
|
2016-11-24 17:44:51 +00:00
|
|
|
- (void)runTestForString:(NSString *)unicodeString usingEncoding:(NSString *)encodingName cutAt:(NSUInteger)cutPoint
|
2016-10-31 20:06:13 +00:00
|
|
|
{
|
|
|
|
CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)encodingName);
|
|
|
|
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
|
2016-11-24 17:44:51 +00:00
|
|
|
|
|
|
|
NSData *unicodeBytes = [unicodeString dataUsingEncoding:encoding];
|
|
|
|
|
|
|
|
NSURLResponse *fakeResponse = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"testurl://"]
|
2016-10-31 20:06:13 +00:00
|
|
|
statusCode:200
|
|
|
|
HTTPVersion:@"1.1"
|
|
|
|
headerFields:@{@"content-type": [NSString stringWithFormat:@"text/plain; charset=%@", encodingName]}];
|
|
|
|
XCTAssert([fakeResponse.textEncodingName isEqualToString:encodingName]);
|
2016-11-24 17:44:51 +00:00
|
|
|
|
|
|
|
NSMutableData *carryStorage = [NSMutableData new];
|
|
|
|
NSMutableString *parsedString = [NSMutableString new];
|
|
|
|
|
2016-10-31 20:06:13 +00:00
|
|
|
[parsedString appendString:[RCTNetworking decodeTextData:[unicodeBytes subdataWithRange:NSMakeRange(0, cutPoint)]
|
|
|
|
fromResponse:fakeResponse
|
|
|
|
withCarryData:carryStorage] ?: @""];
|
2016-11-24 17:44:51 +00:00
|
|
|
|
2016-10-31 20:06:13 +00:00
|
|
|
[parsedString appendString:[RCTNetworking decodeTextData:[unicodeBytes subdataWithRange:NSMakeRange(cutPoint, unicodeBytes.length - cutPoint)]
|
|
|
|
fromResponse:fakeResponse
|
|
|
|
withCarryData:carryStorage] ?: @""];
|
2016-11-24 17:44:51 +00:00
|
|
|
|
2016-10-31 20:06:13 +00:00
|
|
|
XCTAssert(carryStorage.length == 0);
|
|
|
|
XCTAssert([parsedString isEqualToString:unicodeString]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testNiqqud
|
|
|
|
{
|
2016-11-24 17:44:51 +00:00
|
|
|
NSString *unicodeString = [[NSString alloc] initWithData:[[NSData alloc] initWithBase64EncodedString:niqqudStringB64
|
2016-10-31 20:06:13 +00:00
|
|
|
options:(NSDataBase64DecodingOptions)0]
|
|
|
|
encoding:NSUTF8StringEncoding];
|
2016-11-24 17:44:51 +00:00
|
|
|
|
2016-10-31 20:06:13 +00:00
|
|
|
[self runTestForString:unicodeString usingEncoding:@"utf-8" cutAt:25];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)testEmojis
|
|
|
|
{
|
2016-11-24 17:44:51 +00:00
|
|
|
NSString *unicodeString = @"\U0001F602\U0001F602";
|
|
|
|
|
2016-10-31 20:06:13 +00:00
|
|
|
[self runTestForString:unicodeString usingEncoding:@"utf-8" cutAt:7];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|