Skip to content

Commit 7c85286

Browse files
committed
Update URL path to match routing rules
1 parent 31a4593 commit 7c85286

2 files changed

Lines changed: 46 additions & 20 deletions

File tree

UnitTests/ObjCTests/MPNetworkCommunicationTests.m

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ - (void)testConfigURLWithCustomBaseURL {
342342
MPNetworkCommunication_PRIVATE *networkCommunication = [[MPNetworkCommunication_PRIVATE alloc] init];
343343
NSURL *configURL = [networkCommunication configURL].url;
344344
[self deswizzle];
345-
XCTAssert([configURL.absoluteString rangeOfString:@"rkt.example.com/v4/"].location != NSNotFound);
345+
XCTAssert([configURL.absoluteString rangeOfString:@"rkt.example.com/config/v4/"].location != NSNotFound);
346346
XCTAssert([configURL.absoluteString rangeOfString:@"config2.mparticle.com"].location == NSNotFound);
347347
}
348348

@@ -355,7 +355,7 @@ - (void)testConfigURLCustomBaseURLOverridesConfigHost {
355355
MPNetworkCommunication_PRIVATE *networkCommunication = [[MPNetworkCommunication_PRIVATE alloc] init];
356356
NSURL *configURL = [networkCommunication configURL].url;
357357
[self deswizzle];
358-
XCTAssert([configURL.absoluteString rangeOfString:@"rkt.example.com/v4/"].location != NSNotFound);
358+
XCTAssert([configURL.absoluteString rangeOfString:@"rkt.example.com/config/v4/"].location != NSNotFound);
359359
XCTAssert([configURL.absoluteString rangeOfString:@"config.mpproxy.example.com"].location == NSNotFound);
360360
}
361361

@@ -367,7 +367,7 @@ - (void)testModifyURLWithCustomBaseURL {
367367
MPNetworkCommunication_PRIVATE *networkCommunication = [[MPNetworkCommunication_PRIVATE alloc] init];
368368
NSURL *modifyURL = [networkCommunication modifyURL].url;
369369
[self deswizzle];
370-
XCTAssert([modifyURL.absoluteString rangeOfString:@"https://rkt.example.com/v1/"].location != NSNotFound);
370+
XCTAssert([modifyURL.absoluteString rangeOfString:@"https://rkt.example.com/identity/v1/"].location != NSNotFound);
371371
XCTAssert([modifyURL.absoluteString rangeOfString:@"identity.us1.mparticle.com"].location == NSNotFound);
372372
XCTAssert([modifyURL.accessibilityHint isEqualToString:@"identity"]);
373373
}
@@ -384,7 +384,7 @@ - (void)testModifyURLCustomBaseURLOverridesIdentityTrackingHost {
384384
NSURL *modifyURL = [networkCommunication modifyURL].url;
385385
stateMachine.attAuthorizationStatus = nil;
386386
[self deswizzle];
387-
XCTAssert([modifyURL.absoluteString rangeOfString:@"https://rkt.example.com/v1/"].location != NSNotFound);
387+
XCTAssert([modifyURL.absoluteString rangeOfString:@"https://rkt.example.com/identity/v1/"].location != NSNotFound);
388388
XCTAssert([modifyURL.absoluteString rangeOfString:@"identity-tracking.mpproxy.example.com"].location == NSNotFound);
389389
}
390390

@@ -397,7 +397,7 @@ - (void)testEventURLWithCustomBaseURL {
397397
MPUpload *upload = [[MPUpload alloc] initWithSessionId:nil uploadDictionary:@{} dataPlanId:nil dataPlanVersion:nil uploadSettings:[MPUploadSettings currentUploadSettingsWithStateMachine:[MParticle sharedInstance].stateMachine networkOptions:[MParticle sharedInstance].networkOptions]];
398398
NSURL *eventURL = [networkCommunication eventURLForUpload:upload].url;
399399
[self deswizzle];
400-
XCTAssert([eventURL.absoluteString rangeOfString:@"rkt.example.com/v2/"].location != NSNotFound);
400+
XCTAssert([eventURL.absoluteString rangeOfString:@"rkt.example.com/nativeevents/v2/"].location != NSNotFound);
401401
XCTAssert([eventURL.absoluteString rangeOfString:@"nativesdks.us1.mparticle.com"].location == NSNotFound);
402402
}
403403

@@ -413,7 +413,7 @@ - (void)testEventURLCustomBaseURLAppliesToTrackingHost {
413413
NSURL *eventURL = [networkCommunication eventURLForUpload:upload].url;
414414
stateMachine.attAuthorizationStatus = nil;
415415
[self deswizzle];
416-
XCTAssert([eventURL.absoluteString rangeOfString:@"rkt.example.com/v2/"].location != NSNotFound);
416+
XCTAssert([eventURL.absoluteString rangeOfString:@"rkt.example.com/nativeevents/v2/"].location != NSNotFound);
417417
XCTAssert([eventURL.absoluteString rangeOfString:@"tracking-nativesdks"].location == NSNotFound);
418418
}
419419

@@ -426,7 +426,7 @@ - (void)testAliasURLWithCustomBaseURL {
426426
MPUpload *upload = [[MPUpload alloc] initWithSessionId:nil uploadDictionary:@{} dataPlanId:nil dataPlanVersion:nil uploadSettings:[MPUploadSettings currentUploadSettingsWithStateMachine:[MParticle sharedInstance].stateMachine networkOptions:[MParticle sharedInstance].networkOptions]];
427427
NSURL *aliasURL = [networkCommunication aliasURLForUpload:upload].url;
428428
[self deswizzle];
429-
XCTAssert([aliasURL.absoluteString rangeOfString:@"https://rkt.example.com/v1/identity/"].location != NSNotFound);
429+
XCTAssert([aliasURL.absoluteString rangeOfString:@"https://rkt.example.com/nativeevents/v1/identity/"].location != NSNotFound);
430430
XCTAssert([aliasURL.absoluteString rangeOfString:@"nativesdks.us1.mparticle.com"].location == NSNotFound);
431431
XCTAssert([aliasURL.accessibilityHint isEqualToString:@"identity"]);
432432
}
@@ -455,7 +455,7 @@ - (void)testAliasURLWithCustomBaseURLAndATTAuthorized {
455455
NSURL *aliasURL = [networkCommunication aliasURLForUpload:upload].url;
456456
stateMachine.attAuthorizationStatus = nil;
457457
[self deswizzle];
458-
XCTAssert([aliasURL.absoluteString rangeOfString:@"https://rkt.example.com/v1/identity/"].location != NSNotFound);
458+
XCTAssert([aliasURL.absoluteString rangeOfString:@"https://rkt.example.com/nativeevents/v1/identity/"].location != NSNotFound);
459459
XCTAssert([aliasURL.absoluteString rangeOfString:@"nativesdks.us1.mparticle.com"].location == NSNotFound);
460460
XCTAssert([aliasURL.accessibilityHint isEqualToString:@"identity"]);
461461
}

mParticle-Apple-SDK/Network/MPNetworkCommunication.m

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,13 @@ - (MPURL *)configURL {
179179
NSString *urlString = [NSString stringWithFormat:configURLFormat, kMPURLScheme, kMPURLHostConfig, kMPConfigVersion, stateMachine.apiKey, kMPConfigURL, [application.version percentEscape], kMParticleSDKVersion];
180180
NSURL *defaultURL = [NSURL URLWithString:urlString];
181181

182-
urlString = [NSString stringWithFormat:configURLFormat, kMPURLScheme, configHost, kMPConfigVersion, stateMachine.apiKey, kMPConfigURL, [application.version percentEscape], kMParticleSDKVersion];
182+
if (customHost && networkOptions.overridesConfigSubdirectory) {
183+
MPILogWarning(@"MPNetworkOptions: customBaseURL with overridesConfigSubdirectory is unsupported for CDN routing; overridesConfigSubdirectory will be ignored.");
184+
}
185+
NSString *configVersion = customHost ? @"config/v4" : kMPConfigVersion;
186+
urlString = [NSString stringWithFormat:configURLFormat, kMPURLScheme, configHost, configVersion, stateMachine.apiKey, kMPConfigURL, [application.version percentEscape], kMParticleSDKVersion];
183187

184-
if (networkOptions.overridesConfigSubdirectory) {
188+
if (!customHost && networkOptions.overridesConfigSubdirectory) {
185189
NSString *configURLFormat = [urlFormatOverride stringByAppendingString:@"?av=%@&sv=%@"];
186190
urlString = [NSString stringWithFormat:configURLFormat, kMPURLScheme, configHost, stateMachine.apiKey, kMPConfigURL, [application.version percentEscape], kMParticleSDKVersion];
187191
}
@@ -207,10 +211,15 @@ - (MPURL *)eventURLForUpload:(MPUpload *)mpUpload {
207211
NSString *urlString = [NSString stringWithFormat:urlFormat, kMPURLScheme, self.defaultEventHost, kMPEventsVersion, mpUpload.uploadSettings.apiKey, kMPEventsURL];
208212
NSURL *defaultURL = [NSURL URLWithString:urlString];
209213

210-
if (mpUpload.uploadSettings.overridesEventsSubdirectory) {
214+
BOOL usingCustomBaseURL = [MParticle sharedInstance].networkOptions.customBaseURL != nil;
215+
if (usingCustomBaseURL && mpUpload.uploadSettings.overridesEventsSubdirectory) {
216+
MPILogWarning(@"MPNetworkOptions: customBaseURL with overridesEventsSubdirectory is unsupported for CDN routing; overridesEventsSubdirectory will be ignored.");
217+
}
218+
NSString *eventsVersion = usingCustomBaseURL ? @"nativeevents/v2" : kMPEventsVersion;
219+
if (!usingCustomBaseURL && mpUpload.uploadSettings.overridesEventsSubdirectory) {
211220
urlString = [NSString stringWithFormat:urlFormatOverride, kMPURLScheme, eventHost, mpUpload.uploadSettings.apiKey, kMPEventsURL];
212221
} else {
213-
urlString = [NSString stringWithFormat:urlFormat, kMPURLScheme, eventHost, kMPEventsVersion, mpUpload.uploadSettings.apiKey, kMPEventsURL];
222+
urlString = [NSString stringWithFormat:urlFormat, kMPURLScheme, eventHost, eventsVersion, mpUpload.uploadSettings.apiKey, kMPEventsURL];
214223
}
215224

216225
NSURL *modifiedURL = [NSURL URLWithString:urlString];
@@ -234,12 +243,16 @@ - (MPURL *)audienceURL {
234243
NSString *urlString = [NSString stringWithFormat:audienceURLFormat, kMPURLScheme, self.defaultEventHost, kMPAudienceVersion, stateMachine.apiKey, kMPAudienceURL, [MPPersistenceController_PRIVATE mpId]];
235244
NSURL *defaultURL = [NSURL URLWithString:urlString];
236245

237-
if (networkOptions.overridesEventsSubdirectory) {
246+
if (customHost && networkOptions.overridesEventsSubdirectory) {
247+
MPILogWarning(@"MPNetworkOptions: customBaseURL with overridesEventsSubdirectory is unsupported for CDN routing; overridesEventsSubdirectory will be ignored.");
248+
}
249+
NSString *audienceVersion = customHost ? @"nativeevents/v1" : kMPAudienceVersion;
250+
if (!customHost && networkOptions.overridesEventsSubdirectory) {
238251
audienceURLFormat = [urlFormatOverride stringByAppendingString:@"?mpid=%@"];
239252
urlString = [NSString stringWithFormat:audienceURLFormat, kMPURLScheme, eventHost, kMPAudienceVersion, stateMachine.apiKey, kMPAudienceURL, [MPPersistenceController_PRIVATE mpId]];
240253
} else {
241254
audienceURLFormat = [urlFormat stringByAppendingString:@"?mpid=%@"];
242-
urlString = [NSString stringWithFormat:audienceURLFormat, kMPURLScheme, eventHost, kMPAudienceVersion, stateMachine.apiKey, kMPAudienceURL, [MPPersistenceController_PRIVATE mpId]];
255+
urlString = [NSString stringWithFormat:audienceURLFormat, kMPURLScheme, eventHost, audienceVersion, stateMachine.apiKey, kMPAudienceURL, [MPPersistenceController_PRIVATE mpId]];
243256
}
244257

245258
NSURL *modifiedURL = [NSURL URLWithString:urlString];
@@ -303,10 +316,14 @@ - (MPURL *)identityURL:(NSString *)pathComponent {
303316
NSString *urlString = [NSString stringWithFormat:identityURLFormat, kMPURLScheme, self.defaultIdentityHost, kMPIdentityVersion, pathComponent];
304317
NSURL *defaultURL = [NSURL URLWithString:urlString];
305318

306-
if (identityNetworkOptions.overridesIdentitySubdirectory) {
319+
if (identityCustomHost && identityNetworkOptions.overridesIdentitySubdirectory) {
320+
MPILogWarning(@"MPNetworkOptions: customBaseURL with overridesIdentitySubdirectory is unsupported for CDN routing; overridesIdentitySubdirectory will be ignored.");
321+
}
322+
NSString *identityVersion = identityCustomHost ? @"identity/v1" : kMPIdentityVersion;
323+
if (!identityCustomHost && identityNetworkOptions.overridesIdentitySubdirectory) {
307324
urlString = [NSString stringWithFormat:identityURLFormatOverride, kMPURLScheme, identityHost, pathComponent];
308325
} else {
309-
urlString = [NSString stringWithFormat:identityURLFormat, kMPURLScheme, identityHost, kMPIdentityVersion, pathComponent];
326+
urlString = [NSString stringWithFormat:identityURLFormat, kMPURLScheme, identityHost, identityVersion, pathComponent];
310327
}
311328

312329
NSURL *modifiedURL = [NSURL URLWithString:urlString];
@@ -341,10 +358,14 @@ - (MPURL *)modifyURL {
341358
NSString *urlString = [NSString stringWithFormat:modifyURLFormat, kMPURLScheme, self.defaultIdentityHost, kMPIdentityVersion, [MPPersistenceController_PRIVATE mpId], pathComponent];
342359
NSURL *defaultURL = [NSURL URLWithString:urlString];
343360

344-
if (modifyNetworkOptions.overridesIdentitySubdirectory) {
361+
if (modifyCustomHost && modifyNetworkOptions.overridesIdentitySubdirectory) {
362+
MPILogWarning(@"MPNetworkOptions: customBaseURL with overridesIdentitySubdirectory is unsupported for CDN routing; overridesIdentitySubdirectory will be ignored.");
363+
}
364+
NSString *modifyVersion = modifyCustomHost ? @"identity/v1" : kMPIdentityVersion;
365+
if (!modifyCustomHost && modifyNetworkOptions.overridesIdentitySubdirectory) {
345366
urlString = [NSString stringWithFormat:modifyURLFormatOverride, kMPURLScheme, identityHost, [MPPersistenceController_PRIVATE mpId], pathComponent];
346367
} else {
347-
urlString = [NSString stringWithFormat:modifyURLFormat, kMPURLScheme, identityHost, kMPIdentityVersion, [MPPersistenceController_PRIVATE mpId], pathComponent];
368+
urlString = [NSString stringWithFormat:modifyURLFormat, kMPURLScheme, identityHost, modifyVersion, [MPPersistenceController_PRIVATE mpId], pathComponent];
348369
}
349370

350371
NSURL *modifiedURL = [NSURL URLWithString:urlString];
@@ -371,16 +392,21 @@ - (MPURL *)aliasURLForUpload:(MPUpload *)mpUpload {
371392
NSString *urlString = [NSString stringWithFormat:aliasURLFormat, kMPURLScheme, self.defaultEventHost, kMPIdentityVersion, kMPIdentityKey, mpUpload.uploadSettings.apiKey, pathComponent];
372393
NSURL *defaultURL = [NSURL URLWithString:urlString];
373394

395+
BOOL usingCustomBaseURLAlias = [MParticle sharedInstance].networkOptions.customBaseURL != nil;
374396
BOOL overrides = mpUpload.uploadSettings.overridesAliasSubdirectory;
375397
if (!mpUpload.uploadSettings.eventsOnly && !mpUpload.uploadSettings.aliasHost) {
376398
eventHost = mpUpload.uploadSettings.eventsHost ?: self.defaultEventHost;
377399
overrides = mpUpload.uploadSettings.overridesEventsSubdirectory;
378400
}
379401

380-
if (overrides) {
402+
if (usingCustomBaseURLAlias && overrides) {
403+
MPILogWarning(@"MPNetworkOptions: customBaseURL with overridesAliasSubdirectory/overridesEventsSubdirectory is unsupported for CDN routing; subdirectory override will be ignored.");
404+
}
405+
NSString *aliasVersion = usingCustomBaseURLAlias ? @"nativeevents/v1" : kMPIdentityVersion;
406+
if (!usingCustomBaseURLAlias && overrides) {
381407
urlString = [NSString stringWithFormat:aliasURLFormatOverride, kMPURLScheme, eventHost, mpUpload.uploadSettings.apiKey, pathComponent];
382408
} else {
383-
urlString = [NSString stringWithFormat:aliasURLFormat, kMPURLScheme, eventHost, kMPIdentityVersion, kMPIdentityKey, mpUpload.uploadSettings.apiKey, pathComponent];
409+
urlString = [NSString stringWithFormat:aliasURLFormat, kMPURLScheme, eventHost, aliasVersion, kMPIdentityKey, mpUpload.uploadSettings.apiKey, pathComponent];
384410
}
385411

386412
NSURL *modifiedURL = [NSURL URLWithString:urlString];

0 commit comments

Comments
 (0)