Skip to content

Commit

Permalink
Support paths with + in List API
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Oct 8, 2020
1 parent 672256e commit 969ba5c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
6 changes: 6 additions & 0 deletions FirebaseStorage/Sources/FIRStorageUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ + (NSURLRequest *)defaultRequestForPath:(FIRStoragePath *)path
[queryItems addObject:[NSURLQueryItem queryItemWithName:key value:queryParams[key]]];
}
[components setQueryItems:queryItems];
// NSURLComponents does not encode "+" as "%2B". This is however required by our backend, as
// it treats "+" as a shorthand encoding for spaces. See also
// https://stackoverflow.com/questions/31577188/how-to-encode-into-2b-with-nsurlcomponents
[components setPercentEncodedQuery:[[components percentEncodedQuery]
stringByReplacingOccurrencesOfString:@"+"
withString:@"%2B"]];

NSString *encodedPath = [self encodedURLForPath:path];
[components setPercentEncodedPath:encodedPath];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ - (void)setUp {

NSArray<NSString *> *largeFiles = @[ @"ios/public/1mb" ];
NSArray<NSString *> *emptyFiles = @[
@"ios/public/empty", @"ios/public/list/a", @"ios/public/list/b", @"ios/public/list/prefix/c"
@"ios/public/empty", @"ios/public/list/+375331111113", @"ios/public/list/b",
@"ios/public/list/prefix/c"
];
setUpExpectation.expectedFulfillmentCount = largeFiles.count + emptyFiles.count;

Expand Down Expand Up @@ -774,7 +775,7 @@ - (void)testPagedListFiles {
- (void)testListAllFiles {
XCTestExpectation *expectation = [self expectationWithDescription:@"testListAllFiles"];

FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/list"];
FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/list/+foo"];

[ref listAllWithCompletion:^(FIRStorageListResult *_Nullable listResult,
NSError *_Nullable error) {
Expand Down
36 changes: 36 additions & 0 deletions FirebaseStorage/Tests/Unit/FIRStorageListTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,42 @@ - (void)testListWithPageSizeAndPageToken {
[FIRStorageTestHelpers waitForExpectation:self];
}

- (void)testPercentEncodesPlusToken {
XCTestExpectation *expectation = [self expectationWithDescription:@"testPercentEncodesPlusToken"];
NSURL *expectedURL = [NSURL URLWithString:@"https://firebasestorage.googleapis.com/v0/b/bucket/"
@"o?prefix=%2Bfoo/&delimiter=/"];

self.fetcherService.testBlock =
^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-retain-cycles"
XCTAssertEqualObjects(fetcher.request.URL, expectedURL); // Implicitly retains self
XCTAssertEqualObjects(fetcher.request.HTTPMethod, @"GET");
#pragma clang diagnostic pop
NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
statusCode:200
HTTPVersion:kHTTPVersion
headerFields:nil];
response(httpResponse, nil, nil);
};

FIRStoragePath *path =
[FIRStoragePath pathFromString:@"https://firebasestorage.googleapis.com/v0/b/bucket/0/+foo"];
FIRStorageReference *ref = [[FIRStorageReference alloc] initWithStorage:self.storage path:path];
FIRStorageListTask *task = [[FIRStorageListTask alloc]
initWithReference:ref
fetcherService:self.fetcherService
dispatchQueue:self.dispatchQueue
pageSize:nil
previousPageToken:nil
completion:^(FIRStorageListResult *result, NSError *error) {
[expectation fulfill];
}];
[task enqueue];

[FIRStorageTestHelpers waitForExpectation:self];
}

- (void)testListWithResponse {
XCTestExpectation *expectation = [self expectationWithDescription:@"testListWithErrorResponse"];

Expand Down

0 comments on commit 969ba5c

Please sign in to comment.