react-native/RNTester/RNTesterUnitTests/RCTBlobManagerTests.m

105 lines
2.7 KiB
Mathematica
Raw Normal View History

Implement Blob support for XMLHttpRequest Summary: This PR is a followup to https://github.com/facebook/react-native/pull/11417 and should be merged after that one is merged. 1. Add support for creating blobs from strings, not just other blobs 1. Add the `File` constructor which is a superset of `Blob` 1. Add the `FileReader` API which can be used to read blobs as strings or data url (base64) 1. Add support for uploading and downloading blobs via `XMLHttpRequest` and `fetch` 1. Add ability to download local files on Android so you can do `fetch(uri).then(res => res.blob())` to get a blob for a local file (iOS already supported this) 1. Clone the repo https://github.com/expo/react-native-blob-test 1. Change the `package.json` and update `react-native` dependency to point to this branch, then run `npm install` 1. Run the `server.js` file with `node server.js` 1. Open the `index.common.js` file and replace `localhost` with your computer's IP address 1. Start the packager with `yarn start` and run the app on your device If everything went well, all tests should pass, and you should see a screen like this: ![screen shot 2017-06-08 at 7 53 08 pm](https://user-images.githubusercontent.com/1174278/26936407-435bbce2-4c8c-11e7-9ae3-eb104e46961e.png)! Pull to rerun all tests or tap on specific test to re-run it [GENERAL] [FEATURE] [Blob] - Implement blob support for XMLHttpRequest Closes https://github.com/facebook/react-native/pull/11573 Reviewed By: shergin Differential Revision: D6082054 Pulled By: hramos fbshipit-source-id: cc9c174fdefdfaf6e5d9fd7b300120a01a50e8c1
2018-01-26 17:06:14 +00:00
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* 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.
*
*/
#import <XCTest/XCTest.h>
#import <RCTBlob/RCTBlobManager.h>
@interface RCTBlobManagerTests : XCTestCase
@end
@implementation RCTBlobManagerTests
{
RCTBlobManager *_module;
NSMutableData *_data;
NSString *_blobId;
}
- (void)setUp
{
[super setUp];
_module = [RCTBlobManager new];
[_module setValue:nil forKey:@"bridge"];
NSInteger size = 120;
_data = [NSMutableData dataWithCapacity:size];
for (NSInteger i = 0; i < size / 4; i++) {
uint32_t randomBits = arc4random();
[_data appendBytes:(void *)&randomBits length:4];
}
_blobId = [NSUUID UUID].UUIDString;
[_module store:_data withId:_blobId];
}
- (void)testResolve
{
XCTAssertTrue([_data isEqualToData:[_module resolve:_blobId offset:0 size:_data.length]]);
NSData *rangeData = [_data subdataWithRange:NSMakeRange(30, _data.length - 30)];
XCTAssertTrue([rangeData isEqualToData:[_module resolve:_blobId offset:30 size:_data.length - 30]]);
}
- (void)testResolveMap
{
NSDictionary<NSString *, id> *map = @{
@"blobId": _blobId,
@"size": @(_data.length),
@"offset": @0,
};
XCTAssertTrue([_data isEqualToData:[_module resolve:map]]);
}
- (void)testResolveURL
{
NSURLComponents *components = [NSURLComponents new];
[components setPath:_blobId];
[components setQuery:[NSString stringWithFormat:@"offset=0&size=%lu", (unsigned long)_data.length]];
XCTAssertTrue([_data isEqualToData:[_module resolveURL:[components URL]]]);
}
- (void)testRemove
{
XCTAssertNotNil([_module resolve:_blobId offset:0 size:_data.length]);
[_module remove:_blobId];
XCTAssertNil([_module resolve:_blobId offset:0 size:_data.length]);
}
- (void)testCreateFromParts
{
NSDictionary<NSString *, id> *blobData = @{
@"blobId": _blobId,
@"offset": @0,
@"size": @(_data.length),
};
NSDictionary<NSString *, id> *blob = @{
@"data": blobData,
@"type": @"blob",
};
NSString *stringData = @"i \u2665 dogs";
NSDictionary<NSString *, id> *string = @{
@"data": stringData,
@"type": @"string",
};
NSString *resultId = [NSUUID UUID].UUIDString;
NSArray<id> *parts = @[blob, string];
[_module createFromParts:parts withId:resultId];
NSMutableData *expectedData = [NSMutableData new];
[expectedData appendData:_data];
[expectedData appendData:[stringData dataUsingEncoding:NSUTF8StringEncoding]];
NSData *result = [_module resolve:resultId offset:0 size:expectedData.length];
XCTAssertTrue([expectedData isEqualToData:result]);
}
@end