Skip to content

Commit

Permalink
Fix MultipartResponse to allow for binary data to be sent
Browse files Browse the repository at this point in the history
Summary: Changelog: [Internal]

Reviewed By: motiz88

Differential Revision: D21995098

fbshipit-source-id: b387bd45b44fe93743d34d2a3435c165d15f054f
  • Loading branch information
cpojer authored and facebook-github-bot committed Jun 11, 2020
1 parent f53267f commit 1e61fa0
Showing 1 changed file with 68 additions and 50 deletions.
118 changes: 68 additions & 50 deletions RNTester/RNTesterUnitTests/RCTMultipartStreamReaderTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,84 +15,102 @@ @interface RCTMultipartStreamReaderTests : XCTestCase

@implementation RCTMultipartStreamReaderTests

- (void)testSimpleCase {
- (void)testSimpleCase
{
NSString *response =
@"preable, should be ignored\r\n"
@"--sample_boundary\r\n"
@"Content-Type: application/json; charset=utf-8\r\n"
@"Content-Length: 2\r\n\r\n"
@"{}\r\n"
@"--sample_boundary--\r\n"
@"epilogue, should be ignored";
@"preamble, should be ignored\r\n"
@"--sample_boundary\r\n"
@"Content-Type: application/json; charset=utf-8\r\n"
@"Content-Length: 2\r\n\r\n"
@"{}\r\n"
@"--sample_boundary--\r\n"
@"epilogue, should be ignored";

NSInputStream *inputStream = [NSInputStream inputStreamWithData:[response dataUsingEncoding:NSUTF8StringEncoding]];
RCTMultipartStreamReader *reader = [[RCTMultipartStreamReader alloc] initWithInputStream:inputStream boundary:@"sample_boundary"];
RCTMultipartStreamReader *reader = [[RCTMultipartStreamReader alloc] initWithInputStream:inputStream
boundary:@"sample_boundary"];
__block NSInteger count = 0;
BOOL success = [reader readAllPartsWithCompletionCallback:^(NSDictionary *headers, NSData *content, BOOL done) {
XCTAssertTrue(done);
XCTAssertEqualObjects(headers[@"Content-Type"], @"application/json; charset=utf-8");
XCTAssertEqualObjects([[NSString alloc] initWithData:content encoding:NSUTF8StringEncoding], @"{}");
count++;
} progressCallback: nil];
BOOL success = [reader
readAllPartsWithCompletionCallback:^(NSDictionary *headers, NSData *content, BOOL done) {
XCTAssertTrue(done);
XCTAssertEqualObjects(headers[@"Content-Type"], @"application/json; charset=utf-8");
XCTAssertEqualObjects([[NSString alloc] initWithData:content encoding:NSUTF8StringEncoding], @"{}");
count++;
}
progressCallback:nil];
XCTAssertTrue(success);
XCTAssertEqual(count, 1);
}

- (void)testMultipleParts {
- (void)testMultipleParts
{
NSString *response =
@"preable, should be ignored\r\n"
@"--sample_boundary\r\n"
@"1\r\n"
@"--sample_boundary\r\n"
@"2\r\n"
@"--sample_boundary\r\n"
@"3\r\n"
@"--sample_boundary--\r\n"
@"epilogue, should be ignored";
@"preamble, should be ignored\r\n"
@"--sample_boundary\r\n"
@"1\r\n"
@"--sample_boundary\r\n"
@"2\r\n"
@"--sample_boundary\r\n"
@"3\r\n"
@"--sample_boundary--\r\n"
@"epilogue, should be ignored";

NSInputStream *inputStream = [NSInputStream inputStreamWithData:[response dataUsingEncoding:NSUTF8StringEncoding]];
RCTMultipartStreamReader *reader = [[RCTMultipartStreamReader alloc] initWithInputStream:inputStream boundary:@"sample_boundary"];
RCTMultipartStreamReader *reader = [[RCTMultipartStreamReader alloc] initWithInputStream:inputStream
boundary:@"sample_boundary"];
__block NSInteger count = 0;
BOOL success = [reader readAllPartsWithCompletionCallback:^(__unused NSDictionary *headers, NSData *content, BOOL done) {
count++;
XCTAssertEqual(done, count == 3);
NSString *expectedBody = [NSString stringWithFormat:@"%ld", (long)count];
NSString *actualBody = [[NSString alloc] initWithData:content encoding:NSUTF8StringEncoding];
XCTAssertEqualObjects(actualBody, expectedBody);
} progressCallback:nil];
BOOL success = [reader
readAllPartsWithCompletionCallback:^(__unused NSDictionary *headers, NSData *content, BOOL done) {
count++;
XCTAssertEqual(done, count == 3);
NSString *expectedBody = [NSString stringWithFormat:@"%ld", (long)count];
NSString *actualBody = [[NSString alloc] initWithData:content encoding:NSUTF8StringEncoding];
XCTAssertEqualObjects(actualBody, expectedBody);
}
progressCallback:nil];
XCTAssertTrue(success);
XCTAssertEqual(count, 3);
}

- (void)testNoDelimiter {
- (void)testNoDelimiter
{
NSString *response = @"Yolo";

NSInputStream *inputStream = [NSInputStream inputStreamWithData:[response dataUsingEncoding:NSUTF8StringEncoding]];
RCTMultipartStreamReader *reader = [[RCTMultipartStreamReader alloc] initWithInputStream:inputStream boundary:@"sample_boundary"];
RCTMultipartStreamReader *reader = [[RCTMultipartStreamReader alloc] initWithInputStream:inputStream
boundary:@"sample_boundary"];
__block NSInteger count = 0;
BOOL success = [reader readAllPartsWithCompletionCallback:^(__unused NSDictionary *headers, __unused NSData *content, __unused BOOL done) {
count++;
} progressCallback:nil];
BOOL success = [reader
readAllPartsWithCompletionCallback:^(
__unused NSDictionary *headers, __unused NSData *content, __unused BOOL done) {
count++;
}
progressCallback:nil];
XCTAssertFalse(success);
XCTAssertEqual(count, 0);
}

- (void)testNoCloseDelimiter {
- (void)testNoCloseDelimiter
{
NSString *response =
@"preable, should be ignored\r\n"
@"--sample_boundary\r\n"
@"Content-Type: application/json; charset=utf-8\r\n"
@"Content-Length: 2\r\n\r\n"
@"{}\r\n"
@"--sample_boundary\r\n"
@"incomplete message...";
@"preamble, should be ignored\r\n"
@"--sample_boundary\r\n"
@"Content-Type: application/json; charset=utf-8\r\n"
@"Content-Length: 2\r\n\r\n"
@"{}\r\n"
@"--sample_boundary\r\n"
@"incomplete message...";

NSInputStream *inputStream = [NSInputStream inputStreamWithData:[response dataUsingEncoding:NSUTF8StringEncoding]];
RCTMultipartStreamReader *reader = [[RCTMultipartStreamReader alloc] initWithInputStream:inputStream boundary:@"sample_boundary"];
RCTMultipartStreamReader *reader = [[RCTMultipartStreamReader alloc] initWithInputStream:inputStream
boundary:@"sample_boundary"];
__block NSInteger count = 0;
BOOL success = [reader readAllPartsWithCompletionCallback:^(__unused NSDictionary *headers, __unused NSData *content, __unused BOOL done) {
count++;
} progressCallback:nil];
BOOL success = [reader
readAllPartsWithCompletionCallback:^(
__unused NSDictionary *headers, __unused NSData *content, __unused BOOL done) {
count++;
}
progressCallback:nil];
XCTAssertFalse(success);
XCTAssertEqual(count, 1);
}
Expand Down

0 comments on commit 1e61fa0

Please sign in to comment.