Skip to content

Commit

Permalink
Merge pull request #71 from thomasvl/updates
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
thomasvl authored Dec 6, 2016
2 parents ade245d + f16f9c4 commit 90e200a
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 20 deletions.
17 changes: 0 additions & 17 deletions Source/GTMSessionFetcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,6 @@ extern "C" {
#ifndef GTM_USE_SESSION_FETCHER
#define GTM_USE_SESSION_FETCHER 1
#endif

#define GTMSESSION_DEPRECATE_OLD_ENUMS 1
#endif

#if !defined(GTMBridgeFetcher)
Expand Down Expand Up @@ -502,21 +500,6 @@ typedef NS_ENUM(NSInteger, GTMSessionFetcherStatus) {
GTMSessionFetcherStatusPreconditionFailed = 412
};

#if !GTMSESSION_DEPRECATE_OLD_ENUMS
#define kGTMSessionFetcherErrorDownloadFailed GTMSessionFetcherErrorDownloadFailed
#define kGTMSessionFetcherErrorUploadChunkUnavailable GTMSessionFetcherErrorUploadChunkUnavailable
#define kGTMSessionFetcherErrorBackgroundExpiration GTMSessionFetcherErrorBackgroundExpiration
#define kGTMSessionFetcherErrorBackgroundFetchFailed GTMSessionFetcherErrorBackgroundFetchFailed
#define kGTMSessionFetcherErrorInsecureRequest GTMSessionFetcherErrorInsecureRequest
#define kGTMSessionFetcherErrorTaskCreationFailed GTMSessionFetcherErrorTaskCreationFailed

#define kGTMSessionFetcherStatusNotModified GTMSessionFetcherStatusNotModified
#define kGTMSessionFetcherStatusBadRequest GTMSessionFetcherStatusBadRequest
#define kGTMSessionFetcherStatusUnauthorized GTMSessionFetcherStatusUnauthorized
#define kGTMSessionFetcherStatusForbidden GTMSessionFetcherStatusForbidden
#define kGTMSessionFetcherStatusPreconditionFailed GTMSessionFetcherStatusPreconditionFailed
#endif // !GTMSESSION_DEPRECATE_OLD_ENUMS

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
12 changes: 9 additions & 3 deletions Source/GTMSessionUploadFetcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,7 @@ - (GTMSessionFetcher *)uploadFetcherWithProperties:(NSDictionary *)properties
void (^finish)(BOOL) = ^(BOOL shouldRetry){
// We'll retry by sending an offset query.
if (shouldRetry) {
self.shouldInitiateOffsetQuery = YES;
self.shouldInitiateOffsetQuery = !isQueryFetch;

// We don't know what our actual offset is anymore, but the server will tell us.
self.currentOffset = 0;
Expand Down Expand Up @@ -1373,21 +1373,27 @@ - (void)chunkFetcher:(GTMSessionFetcher *)chunkFetcher
responseHeaders, self);
BOOL isUploadStatusStopped = (uploadStatus == kStatusFinal || uploadStatus == kStatusCancelled);

// Check if the fetcher was actually querying. If it failed, do not retry,
// as it would enter an infinite retry loop.
NSString *uploadCommand =
chunkFetcher.request.allHTTPHeaderFields[kGTMSessionHeaderXGoogUploadCommand];
BOOL isQueryFetch = [uploadCommand isEqual:@"query"];

int64_t previousContentLength =
[[chunkFetcher.request valueForHTTPHeaderField:@"Content-Length"] longLongValue];
// The Content-Length header may not be present if the chunk fetcher was recreated from
// a background session.
BOOL hasKnownChunkSize = (previousContentLength > 0);
BOOL needsQuery = (!hasKnownChunkSize && !isUploadStatusStopped);

if (error || needsQuery) {
if (error || (needsQuery && !isQueryFetch)) {
NSInteger status = error.code;

// Status 4xx indicates a bad offset in the Google upload protocol. However, do not retry status
// 404 per spec, nor if the upload size appears to have been zero (since the server will just
// keep asking us to retry.)
if (self.shouldInitiateOffsetQuery ||
needsQuery ||
(needsQuery && !isQueryFetch) ||
([error.domain isEqual:kGTMSessionFetcherStatusDomain] &&
status >= 400 && status <= 499 &&
status != 404 &&
Expand Down
4 changes: 4 additions & 0 deletions Source/UnitTests/GTMReadMonitorInputStreamTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ @implementation GTMReadMonitorInputStreamTest

- (void)setUp {
_monitoredData = [[NSMutableData alloc] init];

[super setUp];
}

- (void)tearDown {
_monitoredData = nil;

[super tearDown];
}

- (void)testGTMReadMonitorInputStream {
Expand Down
44 changes: 44 additions & 0 deletions Source/UnitTests/GTMSessionFetcherChunkedUploadTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ - (void)setUp {
[super setUp];
}

- (void)tearDown {
_service = nil;

[super tearDown];
}

#pragma mark - Chunked Upload Fetch Tests

- (void)testChunkedUploadTestBlock {
Expand Down Expand Up @@ -738,6 +744,44 @@ - (void)testBigFileURLQueryFinalUploadFetch {
[self removeTemporaryFileURL:bigFileURL];
}

- (void)testBigFileURLQueryUploadFetchWithServerError {
// Force a query that fails.
FetcherNotificationsCounter *fnctr = [[FetcherNotificationsCounter alloc] init];

NSURL *bigFileURL = [self bigFileToUploadURLWithBaseName:NSStringFromSelector(_cmd)];
NSString *filename = @"gettysburgaddress.txt.upload?queryStatus=error";
NSURL *uploadLocationURL = [_testServer localURLForFile:filename];

GTMSessionUploadFetcher *fetcher =
[GTMSessionUploadFetcher uploadFetcherWithLocation:uploadLocationURL
uploadMIMEType:@"text/plain"
chunkSize:5000
fetcherService:_service];
fetcher.uploadFileURL = bigFileURL;
fetcher.useBackgroundSession = NO;
fetcher.allowLocalhostRequest = YES;
fetcher.retryEnabled = YES;

XCTestExpectation *expectation = [self expectationWithDescription:@"fetched"];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
XCTAssertNil(data);
XCTAssertEqual(error.code, (NSInteger)502);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:_timeoutInterval handler:nil];
[self assertCallbacksReleasedForFetcher:fetcher];

XCTAssertEqual(fnctr.fetchStarted, 1);
XCTAssertEqual(fnctr.fetchStopped, 1);
XCTAssertEqual(fnctr.uploadChunkFetchStarted, 1);
XCTAssertEqual(fnctr.uploadChunkFetchStopped, 1);
XCTAssertEqual(fnctr.retryDelayStarted, 0);
XCTAssertEqual(fnctr.retryDelayStopped, 0);
XCTAssertEqual(fnctr.uploadLocationObtained, 0);

[self removeTemporaryFileURL:bigFileURL];
}

- (void)testBigFileURLQueryCanceledUploadFetch {
// Force a query that indicates the upload was abandoned (status cancelled.)
FetcherNotificationsCounter *fnctr = [[FetcherNotificationsCounter alloc] init];
Expand Down
8 changes: 8 additions & 0 deletions Source/UnitTests/GTMSessionFetcherFetchingTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ @implementation GTMSessionFetcherBaseTest
+ (void)setUp {
SubstituteUIApplication *app = [[SubstituteUIApplication alloc] init];
[GTMSessionFetcher setSubstituteUIApplication:app];

[super setUp];
}

+ (void)tearDown {
[GTMSessionFetcher setSubstituteUIApplication:nil];

[super tearDown];
}

#endif // GTM_BACKGROUND_TASK_FETCHING
Expand All @@ -59,6 +63,8 @@ - (void)setUp {
_isServerRunning = (_testServer != nil);
XCTAssertTrue(_isServerRunning,
@">>> http test server failed to launch; skipping fetcher tests\n");

[super setUp];
}

- (void)tearDown {
Expand All @@ -69,6 +75,8 @@ - (void)tearDown {
_fetcherService = nil;

[[GTMSessionFetcher staticCookieStorage] removeAllCookies];

[super tearDown];
}

#pragma mark -
Expand Down
4 changes: 4 additions & 0 deletions Source/UnitTests/GTMSessionFetcherServiceTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ - (void)setUp {

XCTAssertTrue(_isServerRunning,
@">>> http test server failed to launch; skipping service tests\n");

[super setUp];
}

- (void)tearDown {
_testServer = nil;
_isServerRunning = NO;

[super tearDown];
}

- (void)testFetcherService {
Expand Down
3 changes: 3 additions & 0 deletions Source/UnitTests/GTMSessionFetcherTestServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ - (GTMHTTPResponseMessage *)httpServer:(GTMHTTPServer *)server
// The upload server does not send Size-Received with the status "final" query response.
NSString *pathWithoutLoc = [requestPath stringByDeletingPathExtension];
queryResponseData = [self documentDataAtPath:pathWithoutLoc];
} else if ([uploadQueryStatus isEqual:@"error"]) {
// Pretend the query failed on server side.
return sendResponse(502, nil, nil);
} else if (uploadQueryStatus.length == 0) {
// Normally, we'll treat queries as being for uploads that are active.
responseHeaders[@"X-Goog-Upload-Size-Received"] = [@(_uploadBytesReceived) stringValue];
Expand Down

0 comments on commit 90e200a

Please sign in to comment.