From 25bc191dd53efd9aefb8413b2e8f1e1688b8207c Mon Sep 17 00:00:00 2001 From: Jens Alfke Date: Wed, 15 Jun 2016 13:20:57 -0700 Subject: [PATCH] Fixed local-to-local replication Regression from the conversion to NSURLSession. The CBL_URLProtocol has to be explicitly registered with the session. This restores the ability of the replicator to reach URLs served by the router. Fixes #1292 --- Source/CBLRemoteSession.m | 6 +++ Unit-Tests/ReplicatorInternal_Tests.m | 57 ++++++++++++++------------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/Source/CBLRemoteSession.m b/Source/CBLRemoteSession.m index 471423fcc..e8a7a07f2 100644 --- a/Source/CBLRemoteSession.m +++ b/Source/CBLRemoteSession.m @@ -36,6 +36,12 @@ + (NSURLSessionConfiguration*) defaultConfiguration { config.HTTPShouldSetCookies = NO; config.URLCache = nil; config.HTTPAdditionalHeaders = @{@"User-Agent": [CBL_ReplicatorSettings userAgentHeader]}; + + // Register the router's NSURLProtocol. This allows the replicator to access local databases + // via their internalURLs. + Class cblURLProtocol = NSClassFromString(@"CBL_URLProtocol"); + if (cblURLProtocol) + config.protocolClasses = @[cblURLProtocol]; return config; } diff --git a/Unit-Tests/ReplicatorInternal_Tests.m b/Unit-Tests/ReplicatorInternal_Tests.m index a358a9caa..b3620a2df 100644 --- a/Unit-Tests/ReplicatorInternal_Tests.m +++ b/Unit-Tests/ReplicatorInternal_Tests.m @@ -542,35 +542,36 @@ - (void) test_15_ParseReplicatorProperties { } }]; - if (NSClassFromString(@"CBLURLProtocol")) { - // Local-to-local replication: - props = $dict({@"source", @"foo"}, - {@"target", @"bar"}); - AssertEq([dbmgr parseReplicatorProperties: props - toDatabase: &parsedDB - remote: &remote - isPush: &isPush - createTarget: &createTarget - headers: &headers - authorizer: NULL], - 404); - props = $dict({@"source", @"foo"}, - {@"target", @"bar"}, {@"create_target", $true}); - AssertEq([dbmgr parseReplicatorProperties: props - toDatabase: &parsedDB - remote: &remote - isPush: &isPush - createTarget: &createTarget - headers: &headers - authorizer: NULL], - 200); - AssertEq(parsedDB, db); - AssertEqual(remote, $url(@"http://lite.couchbase./bar/")); - AssertEq(isPush, YES); - AssertEq(createTarget, YES); - AssertEqual(headers, nil); - } + // Local-to-local replication: + Assert(dbmgr.internalURL); + props = $dict({@"source", db.name}, + {@"target", @"bar"}); + AssertEq([dbmgr parseReplicatorProperties: props + toDatabase: &parsedDB + remote: &remote + isPush: &isPush + createTarget: &createTarget + headers: &headers + authorizer: NULL], + 404); + + CBLDatabase* barDB = [dbmgr databaseNamed: @"bar" error: NULL]; + Assert(barDB); + + AssertEq([dbmgr parseReplicatorProperties: props + toDatabase: &parsedDB + remote: &remote + isPush: &isPush + createTarget: &createTarget + headers: &headers + authorizer: NULL], + 200); + AssertEq(parsedDB, db); + AssertEqual(remote, barDB.internalURL); + AssertEq(isPush, YES); + AssertEqual(headers, nil); + // OAuth: NSDictionary* oauthDict = $dict({@"consumer_secret", @"consumer_secret"}, {@"consumer_key", @"consumer_key"}, {@"token_secret", @"token_secret"},