Skip to content

Commit

Permalink
Fixed pusher stopped when getting forbidden error
Browse files Browse the repository at this point in the history
Not treating 403 or forbidden error as an error when uploading multipart or non-multipart revision. This is inline with uploading bulk docs.

#1649
  • Loading branch information
pasin committed Mar 9, 2017
1 parent 05977be commit 7cf40e3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Source/CBLRestPusher.m
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,10 @@ - (BOOL) uploadMultipartRevision: (CBL_Revision*)rev {
_dontSendMultipart = YES;
[self uploadJSONRevision: rev];
} else {
self.error = error;
// 403/Forbidden means validation failed; don't treat it as an error
// because I did my job in sending the revision.
if (error.code != kCBLStatusForbidden)
self.error = error;
[self revisionFailed];
}
} else {
Expand Down Expand Up @@ -603,7 +606,10 @@ - (void) uploadJSONRevision: (CBL_Revision*)originalRev {
body: rev.properties
onCompletion: ^(id response, NSError *error) {
if (error) {
self.error = error;
// 403/Forbidden means validation failed; don't treat it as an error
// because I did my job in sending the revision.
if (error.code != kCBLStatusForbidden)
self.error = error;
[self revisionFailed];
} else {
LogVerbose(Sync, @"%@: Sent %@ (JSON), response=%@", self, rev, response);
Expand Down
41 changes: 41 additions & 0 deletions Unit-Tests/Replication_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1821,4 +1821,45 @@ - (NSURL*) loginToOIDCTestProvider: (NSURL*)remoteDbURL {
}


- (void) test30_PushForbiddenDocs {
// Readonly remote db:
NSURL* remoteDbURL = [self remoteTestDBURL: kAttachTestDBName];
if (!remoteDbURL)
return;
[self eraseRemoteDB: remoteDbURL];

// First push:
[db inTransaction:^BOOL{
for (int i = 1; i <= 10; i++) {
@autoreleasepool {
CBLDocument* doc = db[ $sprintf(@"doc-%d", i) ];
NSError* error;
[doc putProperties: @{@"index": @(i), @"bar": $false} error: &error];
AssertNil(error);
}
}
return YES;
}];

// Attach a big attachment (> 2M) to doc-1:
int size = 3 * 1024;
unsigned char attachbytes[3 * 1024];
for(int i=0; i<size; i++) {
attachbytes[i] = 1;
}
NSData* attach1 = [NSData dataWithBytes: attachbytes length: size];
CBLUnsavedRevision *rev2 = [db[@"dco-1"] newRevision];
[rev2 setAttachmentNamed: @"attach" withContentType: @"text/plain" content:attach1];
NSError* error;
Assert([rev2 save: &error], @"Error saving an attachment: %@", error);

// Avoid warning raised from uploadBulkDocs:changes: when there is an error occurred:
[CBLManager setWarningsRaiseExceptions: NO];
CBLReplication* repl = [db createPushReplication: remoteDbURL];
[self runReplication: repl expectedChangesCount: 0];
AssertNil(repl.lastError);
[CBLManager setWarningsRaiseExceptions: YES];
}


@end

0 comments on commit 7cf40e3

Please sign in to comment.