Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: SQDSDKS-5914 - Pod redirection #447

Closed
wants to merge 11 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ class MParticleOptionsTest : BaseAbstractTest() {
Assert.assertTrue(
com.mparticle.networking.AccessUtils.equals(
options.networkOptions,
com.mparticle.networking.AccessUtils.defaultNetworkOptions
com.mparticle.networking.AccessUtils.getDefaultNetworkOptions()
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.mparticle.networking

object AccessUtils {
val defaultNetworkOptions: NetworkOptions
get() = NetworkOptionsManager.defaultNetworkOptions()
private var defaultNetworkOptions: NetworkOptions? = null

fun getDefaultNetworkOptions(): NetworkOptions {
defaultNetworkOptions =
NetworkOptionsManager.defaultNetworkOptions()
return defaultNetworkOptions!!
}

fun equals(networkOptions1: NetworkOptions, networkOptions2: NetworkOptions): Boolean {
if (networkOptions1 === networkOptions2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package com.mparticle.networking

import com.mparticle.MParticle
import com.mparticle.MParticleOptions
import com.mparticle.NetworkUtilities
import com.mparticle.internal.AccessUtils
import com.mparticle.testutils.BaseCleanInstallEachTest
import junit.framework.TestCase
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import java.net.MalformedURLException
import kotlin.test.assertEquals

class MParticleBaseClientImplTest : BaseCleanInstallEachTest() {
var defaultUrls = HashMap<MParticleBaseClientImpl.Endpoint, MPUrl>()
Expand All @@ -20,11 +22,72 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() {
startMParticle(MParticleOptions.builder(mContext).credentials(apiKey, "secret"))
val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl
for (endpoint in MParticleBaseClientImpl.Endpoint.values()) {
defaultUrls[endpoint] = baseClientImpl.getUrl(endpoint, endpoint.name)
defaultUrls[endpoint] = baseClientImpl.getUrl(endpoint, endpoint.name, false)
}
MParticle.setInstance(null)
}

@Test
markvdouw marked this conversation as resolved.
Show resolved Hide resolved
fun testUrlPrefixWithPodRedirection() {
val prefix = "eu1"
val url =
NetworkUtilities.getUrlWithPrefix(NetworkOptionsManager.MP_URL_PREFIX, prefix, true)
Assert.assertEquals("${NetworkOptionsManager.MP_URL_PREFIX}.$prefix.mparticle.com", url)
}

@Test
fun testUrlPrefixWithoutPodRedirection() {
val prefix = "eu1"
val url =
NetworkUtilities.getUrlWithPrefix(NetworkOptionsManager.MP_URL_PREFIX, prefix, false)
Assert.assertEquals("${NetworkOptionsManager.MP_URL_PREFIX}.mparticle.com", url)
}

@Test
fun testAllPrefixes() {
val map = mapOf<String, String>(
Pair("us1-1vc4gbp24cdtx6e31s58icnymzy83f1uf", "us1"),
Pair("us2-v2p8lr3w2g90vtpaumbq21zy05cl50qm3", "us2"),
Pair("eu1-bkabfno0b8zpv5bwi2zm2mfa1kfml19al", "eu1"),
Pair("au1-iermuj83dbeoshm0n32f10feotclq6i4a", "au1"),
Pair("st1-k77ivhkbbqf4ce0s3y12zpcthyn1ixfyu", "st1"),
Pair("us3-w1y2y8yj8q58d5bx9u2dvtxzl4cpa7cuf", "us3")
)
map.forEach { key, value ->
val prefix = NetworkUtilities.getPodPrefix(key) ?: ""
assertEquals(value, prefix)
assertEquals(
"${NetworkOptionsManager.MP_URL_PREFIX}.$prefix.mparticle.com",
NetworkUtilities.getUrlWithPrefix(NetworkOptionsManager.MP_URL_PREFIX, prefix, true)
)
assertEquals(
"${NetworkOptionsManager.MP_IDENTITY_URL_PREFIX}.$prefix.mparticle.com",
NetworkUtilities.getUrlWithPrefix(
NetworkOptionsManager.MP_IDENTITY_URL_PREFIX,
prefix,
true
)
)

assertEquals(
"${NetworkOptionsManager.MP_URL_PREFIX}.mparticle.com",
NetworkUtilities.getUrlWithPrefix(
NetworkOptionsManager.MP_URL_PREFIX,
prefix,
false
)
)
assertEquals(
"${NetworkOptionsManager.MP_IDENTITY_URL_PREFIX}.mparticle.com",
NetworkUtilities.getUrlWithPrefix(
NetworkOptionsManager.MP_IDENTITY_URL_PREFIX,
prefix,
false
)
)
}
}

@Test
@Throws(MalformedURLException::class)
fun testGetUrlForceDefaultOption() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mparticle.networking

import com.mparticle.MParticle
import com.mparticle.MParticleOptions
import com.mparticle.NetworkUtilities
import com.mparticle.internal.AccessUtils
import com.mparticle.testutils.BaseCleanInstallEachTest
import org.junit.After
Expand Down Expand Up @@ -38,19 +39,19 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
MParticle.start(MParticleOptions.builder(mContext).credentials(apiKey, "s").build())
setClients()
Assert.assertEquals(
NetworkOptionsManager.MP_URL,
NetworkUtilities.getUrlWithPrefix(url = NetworkOptionsManager.MP_URL_PREFIX, "", false),
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority
)
Assert.assertEquals(
NetworkOptionsManager.MP_CONFIG_URL,
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority
)
Assert.assertEquals(
NetworkOptionsManager.MP_URL,
NetworkUtilities.getUrlWithPrefix(url = NetworkOptionsManager.MP_URL_PREFIX, "", false),
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority
)
Assert.assertEquals(
NetworkOptionsManager.MP_IDENTITY_URL,
NetworkUtilities.getUrlWithPrefix(url = NetworkOptionsManager.MP_IDENTITY_URL_PREFIX, "", false),
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority
)
var randIdentityPath = mRandomUtils.getAlphaString(10)
Expand All @@ -71,7 +72,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
identityClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority
)
Assert.assertEquals(
NetworkOptionsManager.MP_IDENTITY_URL,
NetworkOptionsManager.MP_IDENTITY_URL_PREFIX.addSuffix(),
identityClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority
)
randIdentityPath = mRandomUtils.getAlphaString(10)
Expand All @@ -84,10 +85,12 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
)
}

private fun String.addSuffix(suffix: String = ".mparticle.com") = "$this$suffix"

@Test
@Throws(MalformedURLException::class)
fun testRandomEndpoint() {
val identityUrl = mRandomUtils.getAlphaString(20)
val identityUrl = "${mRandomUtils.getAlphaString(20)}"
val configUrl = mRandomUtils.getAlphaString(20)
val audienceUrl = mRandomUtils.getAlphaString(20)
val eventsUrl = mRandomUtils.getAlphaString(20)
Expand All @@ -114,19 +117,19 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
MParticle.start(options)
setClients()
Assert.assertEquals(
audienceUrl,
audienceUrl.addSuffix(),
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority
)
Assert.assertEquals(
configUrl,
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority
)
Assert.assertEquals(
eventsUrl,
eventsUrl.addSuffix(),
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority
)
Assert.assertEquals(
identityUrl,
identityUrl.addSuffix(),
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority
)
var randIdentityPath = mRandomUtils.getAlphaString(10)
Expand All @@ -135,19 +138,19 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY, randIdentityPath).path
)
Assert.assertEquals(
audienceUrl,
audienceUrl.addSuffix(),
identityClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority
)
Assert.assertEquals(
configUrl,
identityClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority
)
Assert.assertEquals(
eventsUrl,
eventsUrl.addSuffix(),
identityClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority
)
Assert.assertEquals(
identityUrl,
identityUrl.addSuffix(),
identityClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority
)
randIdentityPath = mRandomUtils.getAlphaString(10)
Expand Down Expand Up @@ -211,19 +214,19 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
MParticle.start(options)
setClients()
Assert.assertEquals(
audienceUrl,
audienceUrl.addSuffix(),
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.AUDIENCE).authority
)
Assert.assertEquals(
configUrl,
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.CONFIG).authority
)
Assert.assertEquals(
eventsUrl,
eventsUrl.addSuffix(),
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority
)
Assert.assertEquals(
identityUrl,
identityUrl.addSuffix(),
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.IDENTITY).authority
)
val randIdentityPath = mRandomUtils.getAlphaString(10)
Expand Down Expand Up @@ -285,11 +288,11 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
MParticle.start(options)
setClients()
Assert.assertEquals(
eventsUrl,
eventsUrl.addSuffix(),
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority
)
Assert.assertEquals(
eventsUrl,
eventsUrl.addSuffix(),
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.ALIAS).authority
)
}
Expand All @@ -311,11 +314,11 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
MParticle.start(options)
setClients()
Assert.assertEquals(
eventsUrl,
eventsUrl.addSuffix(),
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority
)
Assert.assertEquals(
aliasUrl,
aliasUrl.addSuffix(),
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.ALIAS).authority
)
}
Expand All @@ -335,7 +338,7 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
MParticle.start(options)
setClients()
Assert.assertEquals(
eventsUrl,
eventsUrl.addSuffix(),
mpClient.getUrl(MParticleBaseClientImpl.Endpoint.EVENTS).authority
)
Assert.assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class ConfigManager {
static final String KEY_OPT_OUT = "oo";
public static final String KEY_UNHANDLED_EXCEPTIONS = "cue";
public static final String KEY_PUSH_MESSAGES = "pmk";
public static final String DIRECT_URL_ROUTING = "dur";
markvdouw marked this conversation as resolved.
Show resolved Hide resolved
public static final String KEY_EMBEDDED_KITS = "eks";
static final String KEY_UPLOAD_INTERVAL = "uitl";
static final String KEY_SESSION_TIMEOUT = "stl";
Expand Down Expand Up @@ -83,6 +84,7 @@ public class ConfigManager {
static SharedPreferences sPreferences;

private static JSONArray sPushKeys;
private boolean directUrlRouting = false;
private UserStorage mUserStorage;
private String mLogUnhandledExceptions = VALUE_APP_DEFINED;

Expand Down Expand Up @@ -405,6 +407,8 @@ private synchronized void updateCoreConfig(JSONObject responseJSON, boolean newC
sPushKeys = responseJSON.getJSONArray(KEY_PUSH_MESSAGES);
editor.putString(KEY_PUSH_MESSAGES, sPushKeys.toString());
}
//TODO Read from feature flag
editor.putBoolean(DIRECT_URL_ROUTING, directUrlRouting);

mRampValue = responseJSON.optInt(KEY_RAMP, -1);

Expand Down Expand Up @@ -1276,6 +1280,20 @@ public ConsentState getConsentState(long mpid) {
return ConsentState.withConsentState(serializedConsent).build();
}

public boolean podRedirectionEnabled() {
return directUrlRouting;
}

public String getPodPrefix() {
String prefix = "us1";
try {
markvdouw marked this conversation as resolved.
Show resolved Hide resolved
prefix = getApiKey().split("-")[0];
} catch (Exception e) {
prefix = "us1";
}
return prefix;
}

public void setConsentState(ConsentState state, long mpid) {
String serializedConsent = null;
if (state != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import androidx.annotation.Nullable;

import com.mparticle.BuildConfig;
import com.mparticle.NetworkUtilities;
import com.mparticle.internal.ConfigManager;
import com.mparticle.internal.Constants;
import com.mparticle.internal.Logger;
Expand Down Expand Up @@ -110,6 +111,8 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean
url = domainMappingUrl;
}
Uri uri;
if(endpoint!=Endpoint.CONFIG){
markvdouw marked this conversation as resolved.
Show resolved Hide resolved
url = NetworkUtilities.INSTANCE.getUrlWithPrefix(url, mConfigManager.getPodPrefix(), mConfigManager.podRedirectionEnabled());}
MPUrl defaultUrl = !isDefaultUrl ? getUrl(endpoint, identityPath, true) : null;
String subdirectory;
boolean overridesSubdirectory = domainMapping.isOverridesSubdirectory() && !forceDefaultUrl;
Expand All @@ -133,8 +136,7 @@ protected MPUrl getUrl(Endpoint endpoint, @Nullable String identityPath, boolean
}
}
}
uri = builder.build();
return MPUrl.getUrl(uri.toString(), defaultUrl);
return MPUrl.getUrl(builder.build().toString(), defaultUrl);
case EVENTS:
subdirectory = overridesSubdirectory ? "" : SERVICE_VERSION_2 + "/";
uri = new Uri.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public static Builder builder() {
return new Builder();
}

public static Builder builder(String podPrefix, boolean enable) {
return new Builder(podPrefix, enable);
}

@Nullable
public static NetworkOptions withNetworkOptions(@Nullable String jsonString) {
if (MPUtility.isEmpty(jsonString)) {
Expand Down Expand Up @@ -122,10 +126,17 @@ private JSONObject toJson() {
public static class Builder {
private Map<Endpoint, DomainMapping> domainMappings = new HashMap<Endpoint, DomainMapping>();
private Boolean pinningDisabledInDevelopment;
private String podPrefix = "us1";
private boolean enablePodRedirection = true;

private Builder() {
}

private Builder(String podPrefix, boolean enable){
this.podPrefix = podPrefix;
this.enablePodRedirection = enable;
}

@NonNull
public Builder addDomainMapping(@Nullable DomainMapping domain) {
if (domainMappings == null) {
Expand Down
Loading
Loading