Skip to content

Commit e89dd3b

Browse files
committed
iframe logic support api.staging.onesignal.com
* When we switch to api.onesignal.com in the future for API calls the iframe will be opened the correct URL still.
1 parent 53872f5 commit e89dd3b

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

src/context/shared/utils/Utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ export class Utils {
137137
return Number(`${majorVersion}.${minorVersion}`);
138138
}
139139

140+
/**
141+
* Gives back the last x number of parts providing a string with a delimiter.
142+
* Example: lastParts("api.staging.onesignal.com", ".", 3) will return "staging.onesignal.com"
143+
*/
144+
public static lastParts(subject: string, delimiter: string, maxParts: number): string {
145+
const parts = subject.split(delimiter);
146+
const skipParts = Math.max(parts.length - maxParts, 0);
147+
return parts.slice(skipParts).join(delimiter);
148+
}
149+
140150
/**
141151
* Checks if a version is number is greater than or equal (AKA at least) to a specific compare
142152
* to version.

src/managers/AltOriginManager.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ export default class AltOriginManager {
100100
static getCanonicalSubscriptionUrls(config: AppConfig,
101101
buildEnv: EnvironmentKind = SdkEnvironment.getApiEnv()
102102
): Array<URL> {
103-
const apiUrl = SdkEnvironment.getOneSignalApiUrl(buildEnv);
104-
const legacyDomainUrl = new URL(`https://${config.subdomain}.${apiUrl.host}`);
103+
const subscriptionDomain = AltOriginManager.getWildcardLegacySubscriptionDomain(buildEnv);
104+
const legacyDomainUrl = new URL(`https://${config.subdomain}.${subscriptionDomain}`);
105105

106106
// Staging and Dev don't support going through the os.tc domain
107107
if (buildEnv !== EnvironmentKind.Production) {
@@ -118,6 +118,23 @@ export default class AltOriginManager {
118118
return urls;
119119
}
120120

121+
/**
122+
* Get the wildcard part of the legacy subscription domain.
123+
* Examples: onesignal.com, staging.onesignal.com, or localhost
124+
*/
125+
static getWildcardLegacySubscriptionDomain(buildEnv: EnvironmentKind): string {
126+
const apiUrl = SdkEnvironment.getOneSignalApiUrl(buildEnv);
127+
128+
// Prod and Dev support domains like *.onesignal.com and *.localhost
129+
let envSubdomainParts: number = 2;
130+
if (buildEnv === EnvironmentKind.Staging) {
131+
// Allow up to 3 parts so *.staging.onesignal.com works.
132+
envSubdomainParts = 3;
133+
}
134+
135+
return Utils.lastParts(apiUrl.host, ".", envSubdomainParts);
136+
}
137+
121138
/**
122139
* Returns the URL of the OneSignal proxy iFrame helper.
123140
*/

test/unit/managers/AltOriginManager.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ test(`should get correct canonical subscription URL for staging environment`, as
4343
t.is(stagingUrls[0].host, new URL(`https://test.${stagingDomain}`).host);
4444
});
4545

46+
47+
test(`should get correct canonical subscription URL when api.staging.onesignal.com is used`, async t => {
48+
const stagingDomain = "staging.onesignal.com";
49+
(<any>global).__API_ORIGIN__ = `api.${stagingDomain}`;
50+
const config = TestEnvironment.getFakeAppConfig();
51+
config.subdomain = 'test';
52+
config.httpUseOneSignalCom = true;
53+
54+
const browser = await TestEnvironment.stubDomEnvironment();
55+
browser.changeURL(window, `http://${stagingDomain}`);
56+
57+
const stagingUrlsOsTcDomain = AltOriginManager.getCanonicalSubscriptionUrls(config, EnvironmentKind.Staging);
58+
t.is(stagingUrlsOsTcDomain.length, 1);
59+
t.is(stagingUrlsOsTcDomain[0].host, new URL(`https://test.${stagingDomain}`).host);
60+
});
61+
4662
test(`should get correct canonical subscription URL for production environment`, async t => {
4763
const config = TestEnvironment.getFakeAppConfig();
4864
config.subdomain = 'test';
@@ -60,6 +76,24 @@ test(`should get correct canonical subscription URL for production environment`,
6076
t.is(prodUrls[0].host, new URL('https://test.os.tc').host);
6177
});
6278

79+
test(`should get correct canonical subscription URL for production environment with api. prefix`, async t => {
80+
(<any>global).__API_ORIGIN__ = `api.onesignal.com`;
81+
const config = TestEnvironment.getFakeAppConfig();
82+
config.subdomain = 'test';
83+
config.httpUseOneSignalCom = true;
84+
85+
const prodUrlsOsTcDomain = AltOriginManager.getCanonicalSubscriptionUrls(config, EnvironmentKind.Production);
86+
t.is(prodUrlsOsTcDomain.length, 2);
87+
t.is(prodUrlsOsTcDomain[0].host, new URL('https://test.os.tc').host);
88+
t.is(prodUrlsOsTcDomain[1].host, new URL('https://test.onesignal.com').host);
89+
90+
config.httpUseOneSignalCom = false;
91+
92+
const prodUrls = AltOriginManager.getCanonicalSubscriptionUrls(config, EnvironmentKind.Production);
93+
t.is(prodUrls.length, 1);
94+
t.is(prodUrls[0].host, new URL('https://test.os.tc').host);
95+
});
96+
6397
function setupDiscoverAltOriginTest(t: any) {
6498
const appConfig = TestEnvironment.getFakeAppConfig();
6599
appConfig.subdomain = 'test';

0 commit comments

Comments
 (0)