diff --git a/UnitTests/MPNetworkCommunication+Tests.h b/UnitTests/MPNetworkCommunication+Tests.h index 36500ecc..c3c870db 100644 --- a/UnitTests/MPNetworkCommunication+Tests.h +++ b/UnitTests/MPNetworkCommunication+Tests.h @@ -2,12 +2,19 @@ @class MPURL; +extern NSString * _Nonnull const kMPURLHostEventSubdomain; +extern NSString * _Nonnull const kMPURLHostIdentitySubdomain; + @interface MPNetworkCommunication(Tests) -- (MPURL *)configURL; -- (MPURL *)eventURL; -- (MPURL *)aliasURL; -- (MPURL *)modifyURL; -- (MPURL *)identifyURL; +- (nonnull NSString *)defaultHostWithSubdomain:(nonnull NSString *)subdomain apiKey:(nonnull NSString *)apiKey enableDirectRouting:(BOOL)enableDirectRouting; +- (nonnull NSString *)defaultEventHost; +- (nonnull NSString *)defaultIdentityHost; + +- (nonnull MPURL *)configURL; +- (nonnull MPURL *)eventURL; +- (nonnull MPURL *)aliasURL; +- (nonnull MPURL *)modifyURL; +- (nonnull MPURL *)identifyURL; @end diff --git a/UnitTests/MPNetworkCommunicationTests.m b/UnitTests/MPNetworkCommunicationTests.m index 4ad17708..f19dca81 100644 --- a/UnitTests/MPNetworkCommunicationTests.m +++ b/UnitTests/MPNetworkCommunicationTests.m @@ -838,4 +838,31 @@ - (void)testMaxAgeForCacheCapitalization { XCTAssertEqualObjects([networkCommunication maxAgeForCache:test5], @16); } +- (void)testPodURLRouting { + // NOTE: All keys are fake and randomly generated just for this test + NSArray *testKeys = @[ + @[@"4u8wmsug0pf5tbf58lgjiouma3qukrgbu", @"nativesdks.us1.mparticle.com", @"identity.us1.mparticle.com"], + @[@"us1-1vc4gbp24cdtx6e31s58icnymzy83f1uf", @"nativesdks.us1.mparticle.com", @"identity.us1.mparticle.com"], + @[@"us2-v2p8lr3w2g90vtpaumbq21zy05cl50qm3", @"nativesdks.us2.mparticle.com", @"identity.us2.mparticle.com"], + @[@"eu1-bkabfno0b8zpv5bwi2zm2mfa1kfml19al", @"nativesdks.eu1.mparticle.com", @"identity.eu1.mparticle.com"], + @[@"au1-iermuj83dbeoshm0n32f10feotclq6i4a", @"nativesdks.au1.mparticle.com", @"identity.au1.mparticle.com"], + @[@"st1-k77ivhkbbqf4ce0s3y12zpcthyn1ixfyu", @"nativesdks.st1.mparticle.com", @"identity.st1.mparticle.com"], + @[@"us3-w1y2y8yj8q58d5bx9u2dvtxzl4cpa7cuf", @"nativesdks.us3.mparticle.com", @"identity.us3.mparticle.com"] + ]; + NSString *oldEventHost = @"nativesdks.mparticle.com"; + NSString *oldIdentityHost = @"identity.mparticle.com"; + + MPNetworkCommunication *networkCommunication = [[MPNetworkCommunication alloc] init]; + for (NSArray *test in testKeys) { + NSString *key = test[0]; + NSString *eventHost = test[1]; + NSString *identityHost = test[2]; + + XCTAssertEqualObjects(eventHost, [networkCommunication defaultHostWithSubdomain:kMPURLHostEventSubdomain apiKey:key enableDirectRouting:YES]); + XCTAssertEqualObjects(identityHost, [networkCommunication defaultHostWithSubdomain:kMPURLHostIdentitySubdomain apiKey:key enableDirectRouting:YES]); + XCTAssertEqualObjects(oldEventHost, [networkCommunication defaultHostWithSubdomain:kMPURLHostEventSubdomain apiKey:key enableDirectRouting:NO]); + XCTAssertEqualObjects(oldIdentityHost, [networkCommunication defaultHostWithSubdomain:kMPURLHostIdentitySubdomain apiKey:key enableDirectRouting:NO]); + } +} + @end diff --git a/mParticle-Apple-SDK/Network/MPNetworkCommunication.h b/mParticle-Apple-SDK/Network/MPNetworkCommunication.h index 3b41ff26..08fb76e3 100644 --- a/mParticle-Apple-SDK/Network/MPNetworkCommunication.h +++ b/mParticle-Apple-SDK/Network/MPNetworkCommunication.h @@ -11,8 +11,6 @@ extern NSString * _Nonnull const kMPURLScheme; -extern NSString * _Nonnull const kMPURLHost; -extern NSString * _Nonnull const kMPURLHostConfig; typedef NS_ENUM(NSInteger, MPNetworkError) { MPNetworkErrorTimeout = 1, diff --git a/mParticle-Apple-SDK/Network/MPNetworkCommunication.m b/mParticle-Apple-SDK/Network/MPNetworkCommunication.m index 7079d12f..6b474129 100644 --- a/mParticle-Apple-SDK/Network/MPNetworkCommunication.m +++ b/mParticle-Apple-SDK/Network/MPNetworkCommunication.m @@ -49,9 +49,9 @@ NSString *const kMPIdentityKey = @"identity"; NSString *const kMPURLScheme = @"https"; -NSString *const kMPURLHost = @"nativesdks.mparticle.com"; NSString *const kMPURLHostConfig = @"config2.mparticle.com"; -NSString *const kMPURLHostIdentity = @"identity.mparticle.com"; +NSString *const kMPURLHostEventSubdomain = @"nativesdks"; +NSString *const kMPURLHostIdentitySubdomain = @"identity"; static NSObject *factory = nil; @@ -114,6 +114,31 @@ - (instancetype)init { } #pragma mark Private accessors + +- (NSString *)defaultHostWithSubdomain:(NSString *)subdomain apiKey:(NSString *)apiKey enableDirectRouting:(BOOL)enableDirectRouting { + if (enableDirectRouting) { + NSArray *splitKey = [apiKey componentsSeparatedByString:@"-"]; + if (splitKey.count <= 1) { + // Handle case with no prefix, default to US1 (old keys) + return [NSString stringWithFormat:@"%@.us1.mparticle.com", subdomain]; + } + return [NSString stringWithFormat:@"%@.%@.mparticle.com", subdomain, splitKey[0]]; + } + + // Handle feature flag disabled (old behavior) + return [NSString stringWithFormat:@"%@.mparticle.com", subdomain]; +} + +- (NSString *)defaultEventHost { + MPStateMachine *stateMachine = [MParticle sharedInstance].stateMachine; + return [self defaultHostWithSubdomain:kMPURLHostEventSubdomain apiKey:stateMachine.apiKey enableDirectRouting:stateMachine.enableDirectRouting]; +} + +- (NSString *)defaultIdentityHost { + MPStateMachine *stateMachine = [MParticle sharedInstance].stateMachine; + return [self defaultHostWithSubdomain:kMPURLHostIdentitySubdomain apiKey:stateMachine.apiKey enableDirectRouting:stateMachine.enableDirectRouting]; +} + - (MPURL *)configURL { if (_configURL) { return _configURL; @@ -165,8 +190,8 @@ - (MPURL *)eventURL { } MPStateMachine *stateMachine = [MParticle sharedInstance].stateMachine; - NSString *eventHost = [MParticle sharedInstance].networkOptions.eventsHost ?: kMPURLHost; - NSString *urlString = [NSString stringWithFormat:urlFormat, kMPURLScheme, kMPURLHost, kMPEventsVersion, stateMachine.apiKey, kMPEventsURL]; + NSString *eventHost = [MParticle sharedInstance].networkOptions.eventsHost ?: self.defaultEventHost; + NSString *urlString = [NSString stringWithFormat:urlFormat, kMPURLScheme, self.defaultEventHost, kMPEventsVersion, stateMachine.apiKey, kMPEventsURL]; NSURL *defaultURL = [NSURL URLWithString:urlString]; if ([MParticle sharedInstance].networkOptions.overridesEventsSubdirectory) { @@ -213,8 +238,8 @@ - (MPURL *)logoutURL { } - (MPURL *)identityURL:(NSString *)pathComponent { - NSString *identityHost = [MParticle sharedInstance].networkOptions.identityHost ?: kMPURLHostIdentity; - NSString *urlString = [NSString stringWithFormat:identityURLFormat, kMPURLScheme, kMPURLHostIdentity, kMPIdentityVersion, pathComponent]; + NSString *identityHost = [MParticle sharedInstance].networkOptions.identityHost ?: self.defaultIdentityHost; + NSString *urlString = [NSString stringWithFormat:identityURLFormat, kMPURLScheme, self.defaultIdentityHost, kMPIdentityVersion, pathComponent]; NSURL *defaultURL = [NSURL URLWithString:urlString]; if ([MParticle sharedInstance].networkOptions.overridesIdentitySubdirectory) { @@ -237,8 +262,8 @@ - (MPURL *)identityURL:(NSString *)pathComponent { - (MPURL *)modifyURL { NSString *pathComponent = @"modify"; - NSString *identityHost = [MParticle sharedInstance].networkOptions.identityHost ?: kMPURLHostIdentity; - NSString *urlString = [NSString stringWithFormat:modifyURLFormat, kMPURLScheme, kMPURLHostIdentity, kMPIdentityVersion, [MPPersistenceController mpId], pathComponent]; + NSString *identityHost = [MParticle sharedInstance].networkOptions.identityHost ?: self.defaultIdentityHost; + NSString *urlString = [NSString stringWithFormat:modifyURLFormat, kMPURLScheme, self.defaultIdentityHost, kMPIdentityVersion, [MPPersistenceController mpId], pathComponent]; NSURL *defaultURL = [NSURL URLWithString:urlString]; if ([MParticle sharedInstance].networkOptions.overridesIdentitySubdirectory) { @@ -267,13 +292,13 @@ - (MPURL *)aliasURL { NSString *pathComponent = @"alias"; MPStateMachine *stateMachine = [MParticle sharedInstance].stateMachine; - NSString *eventHost = [MParticle sharedInstance].networkOptions.aliasHost ?: kMPURLHost; - NSString *urlString = [NSString stringWithFormat:aliasURLFormat, kMPURLScheme, kMPURLHost, kMPIdentityVersion, kMPIdentityKey, stateMachine.apiKey, pathComponent]; + NSString *eventHost = [MParticle sharedInstance].networkOptions.aliasHost ?: self.defaultEventHost; + NSString *urlString = [NSString stringWithFormat:aliasURLFormat, kMPURLScheme, self.defaultEventHost, kMPIdentityVersion, kMPIdentityKey, stateMachine.apiKey, pathComponent]; NSURL *defaultURL = [NSURL URLWithString:urlString]; BOOL overrides = [MParticle sharedInstance].networkOptions.overridesAliasSubdirectory; if (![MParticle sharedInstance].networkOptions.eventsOnly && ![MParticle sharedInstance].networkOptions.aliasHost) { - eventHost = [MParticle sharedInstance].networkOptions.eventsHost ?: kMPURLHost; + eventHost = [MParticle sharedInstance].networkOptions.eventsHost ?: self.defaultEventHost; overrides = [MParticle sharedInstance].networkOptions.overridesEventsSubdirectory; } diff --git a/mParticle-Apple-SDK/Utils/MPStateMachine.h b/mParticle-Apple-SDK/Utils/MPStateMachine.h index 803192b5..63b319e8 100644 --- a/mParticle-Apple-SDK/Utils/MPStateMachine.h +++ b/mParticle-Apple-SDK/Utils/MPStateMachine.h @@ -57,6 +57,7 @@ @property (nonatomic) BOOL automaticSessionTracking; @property (nonatomic) BOOL allowASR; @property (nonatomic, nullable) MPDataPlanOptions *dataPlanOptions; +@property (nonatomic) BOOL enableDirectRouting; + (MPEnvironment)environment; + (void)setEnvironment:(MPEnvironment)environment;