Skip to content

Instantly share code, notes, and snippets.

@borrrden
Created July 21, 2015 01:22
Show Gist options
  • Save borrrden/c581d3ee45c75ffcfe8f to your computer and use it in GitHub Desktop.
Save borrrden/c581d3ee45c75ffcfe8f to your computer and use it in GitHub Desktop.
iOS implementation of .NET Issue 449 unit test
- (void)test18_pushAttachmentToCouchDB {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost:5984/issue449"]];
request.HTTPMethod = @"DELETE";
NSHTTPURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
Assert(response.statusCode == 200 || response.statusCode == 404);
request.HTTPMethod = @"PUT";
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
Assert(response.statusCode == 201);
CBLReplication *push = [db createPushReplication:request.URL];
[self createDocuments:2];
CBLDocument *attachDoc = [db createDocument];
[attachDoc update:^BOOL(CBLUnsavedRevision * __nonnull rev) {
NSMutableDictionary *props = [rev.userProperties mutableCopy];
props[@"foo"] = @"bar";
[rev setUserProperties:props];
NSUInteger size = 50 * 1024;
unsigned char attachbytes[size];
for (NSUInteger i = 0; i < size; i++) {
attachbytes[i] = 1;
}
NSData* attachment = [NSData dataWithBytes: attachbytes length: size];
[rev setAttachmentNamed: @"myattachment"
withContentType: @"text/plain; charset=utf-8"
content: attachment];
return YES;
} error:nil];
[self runReplication:push expectedChangesCount:3];
attachDoc = [db existingDocumentWithID:attachDoc.documentID];
[attachDoc update:^BOOL(CBLUnsavedRevision * __nonnull rev) {
NSMutableDictionary *props = [rev.userProperties mutableCopy];
props[@"extraminutes"] = @"5";
[rev setUserProperties:props];
return YES;
} error:nil];
push = [db createPushReplication:push.remoteURL];
[self runReplication:push expectedChangesCount:1];
[db close:nil];
[self eraseTestDB];
CBLReplication *pull = [db createPullReplication:push.remoteURL];
[self runReplication:pull expectedChangesCount:3];
AssertEq(db.documentCount, 3UL);
attachDoc = [db existingDocumentWithID:attachDoc.documentID];
Assert(attachDoc != nil);
Assert(attachDoc.currentRevision.attachments != nil);
[attachDoc update:^BOOL(CBLUnsavedRevision * __nonnull rev) {
NSMutableDictionary *props = [rev.userProperties mutableCopy];
props[@"extraminutes"] = @"10";
[rev setUserProperties:props];
return YES;
} error:nil];
push = [db createPushReplication:pull.remoteURL];
[self runReplication:push expectedChangesCount:1];
AssertNil(push.lastError);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment