Skip to content

Commit

Permalink
Remove protocol from NetworkController and NetworkConnection (close #432
Browse files Browse the repository at this point in the history
)
  • Loading branch information
AlexBenny committed Apr 1, 2021
1 parent 8bf62b9 commit a413474
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 79 deletions.
Expand Up @@ -217,7 +217,6 @@ private void setupAndTrackDemoEvents() {
NetworkController n = Snowplow.getDefaultTracker().getNetwork();
if (!e.isSending()) {
n.setEndpoint(uri);
n.setProtocol(security);
n.setMethod(method);
}

Expand Down
Expand Up @@ -77,7 +77,7 @@ public void basicInitialization() {

String protocol = networkConfiguration.getProtocol() == Protocol.HTTP ? "http" : "https";

assertEquals(networkConfiguration.getEndpoint(), host);
assertEquals(networkConfiguration.getEndpoint(), scheme + "://" + host);
assertEquals(protocol, scheme);
assertEquals(trackerConfiguration.appId, tracker.getAppId());
assertEquals("namespace", tracker.getNamespace());
Expand Down
Expand Up @@ -189,8 +189,8 @@ public void testUpdatingEmitterSettings() throws InterruptedException {
assertEquals(Single, emitter.getBufferOption());
assertEquals("http://" + uri + "/com.snowplowanalytics.snowplow/tp2", emitter.getEmitterUri());
emitter.setHttpMethod(GET);
String ur = emitter.getEmitterUri();
assertEquals("http://" + uri + "/i", emitter.getEmitterUri());
emitter.setEmitterUri(uri);
emitter.setRequestSecurity(Protocol.HTTPS);
assertEquals("https://" + uri + "/i", emitter.getEmitterUri());
emitter.setEmitterUri("com.acme");
Expand All @@ -205,8 +205,6 @@ public void testUpdatingEmitterSettings() throws InterruptedException {
assertTrue(emitter.getEmitterStatus());
emitter.setHttpMethod(POST);
assertEquals("https://com.acme/i", emitter.getEmitterUri());
emitter.setRequestSecurity(HTTP);
assertEquals("https://com.acme/i", emitter.getEmitterUri());
emitter.setEmitterUri("com/foo");
assertEquals("https://com.acme/i", emitter.getEmitterUri());
emitter.setBufferOption(DefaultGroup);
Expand Down
Expand Up @@ -16,6 +16,8 @@
import android.annotation.SuppressLint;
import android.test.AndroidTestCase;

import androidx.annotation.NonNull;

import com.snowplowanalytics.snowplow.network.OkHttpNetworkConnection;
import com.snowplowanalytics.snowplow.network.RequestResult;
import com.snowplowanalytics.snowplow.internal.emitter.TLSVersion;
Expand Down Expand Up @@ -50,7 +52,6 @@ public void testGetRequestWithSuccess() throws IOException, InterruptedException
MockWebServer mockServer = getMockServer(200);
OkHttpNetworkConnection connection =
new OkHttpNetworkConnection.OkHttpNetworkConnectionBuilder(getMockServerURI(mockServer))
.security(HTTP)
.method(GET)
.tls(EnumSet.of(TLSVersion.TLSv1_1, TLSVersion.TLSv1_2))
.emitTimeout(10)
Expand Down Expand Up @@ -78,7 +79,6 @@ public void testGetRequestWithNoSuccess() throws IOException, InterruptedExcepti
MockWebServer mockServer = getMockServer(404);
OkHttpNetworkConnection connection =
new OkHttpNetworkConnection.OkHttpNetworkConnectionBuilder(getMockServerURI(mockServer))
.security(HTTP)
.method(GET)
.tls(EnumSet.of(TLSVersion.TLSv1_1, TLSVersion.TLSv1_2))
.emitTimeout(10)
Expand All @@ -104,7 +104,6 @@ public void testPostRequestWithSuccess() throws IOException, InterruptedExceptio
MockWebServer mockServer = getMockServer(200);
OkHttpNetworkConnection connection =
new OkHttpNetworkConnection.OkHttpNetworkConnectionBuilder(getMockServerURI(mockServer))
.security(HTTP)
.method(POST)
.tls(EnumSet.of(TLSVersion.TLSv1_1, TLSVersion.TLSv1_2))
.emitTimeout(10)
Expand Down Expand Up @@ -136,7 +135,6 @@ public void testPostRequestWithNoSuccess() throws IOException, InterruptedExcept
MockWebServer mockServer = getMockServer(404);
OkHttpNetworkConnection connection =
new OkHttpNetworkConnection.OkHttpNetworkConnectionBuilder(getMockServerURI(mockServer))
.security(HTTP)
.method(POST)
.tls(EnumSet.of(TLSVersion.TLSv1_1, TLSVersion.TLSv1_2))
.emitTimeout(10)
Expand All @@ -163,6 +161,7 @@ public void testCustomClientIsUsed() throws IOException, InterruptedException {
AtomicBoolean hasClientBeenUsed = new AtomicBoolean(false);
OkHttpClient client = new OkHttpClient.Builder()
.addNetworkInterceptor(new Interceptor() {
@NonNull
@Override
public Response intercept(Chain chain) throws IOException {
hasClientBeenUsed.set(true);
Expand All @@ -174,7 +173,6 @@ public Response intercept(Chain chain) throws IOException {
MockWebServer mockServer = getMockServer(200);
OkHttpNetworkConnection connection =
new OkHttpNetworkConnection.OkHttpNetworkConnectionBuilder(getMockServerURI(mockServer))
.security(HTTP)
.method(GET)
.tls(EnumSet.of(TLSVersion.TLSv1_1, TLSVersion.TLSv1_2))
.emitTimeout(10)
Expand All @@ -194,7 +192,32 @@ public Response intercept(Chain chain) throws IOException {

mockServer.shutdown();
}


public void testFreeEndpoint_GetHttpsUrl() {
OkHttpNetworkConnection connection =
new OkHttpNetworkConnection.OkHttpNetworkConnectionBuilder("acme.test.url.com")
.method(POST)
.build();
assertTrue(connection.getUri().toString().startsWith("https://acme.test.url.com"));
}

public void testHttpsEndpoint_GetHttpsUrl() {
OkHttpNetworkConnection connection =
new OkHttpNetworkConnection.OkHttpNetworkConnectionBuilder("https://acme.test.url.com")
.method(POST)
.build();
assertTrue(connection.getUri().toString().startsWith("https://acme.test.url.com"));
}

public void testHttpEndpoint_GetHttpUrl() {
OkHttpNetworkConnection connection =
new OkHttpNetworkConnection.OkHttpNetworkConnectionBuilder("http://acme.test.url.com")
.method(POST)
.build();
assertTrue(connection.getUri().toString().startsWith("http://acme.test.url.com"));
}


// Service methods

private void assertGETRequest(RecordedRequest req) {
Expand Down Expand Up @@ -237,7 +260,7 @@ public MockWebServer getMockServer(int responseCode) throws IOException {
@SuppressLint("DefaultLocale")
public String getMockServerURI(MockWebServer mockServer) {
if (mockServer != null) {
return String.format("%s:%d", mockServer.getHostName(), mockServer.getPort());
return String.format("http://%s:%d", mockServer.getHostName(), mockServer.getPort());
}
return null;
}
Expand Down
Expand Up @@ -77,21 +77,21 @@ public NetworkConfiguration(@NonNull String endpoint, @NonNull HttpMethod method
String scheme = uri.getScheme();
if (scheme == null) {
protocol = Protocol.HTTPS;
this.endpoint = endpoint;
this.endpoint = "https://" + endpoint;
return;
}
switch (scheme) {
case "https":
protocol = Protocol.HTTPS;
this.endpoint = endpoint.substring(8);
this.endpoint = endpoint;
break;
case "http":
protocol = Protocol.HTTP;
this.endpoint = endpoint.substring(7);
this.endpoint = endpoint;
break;
default:
protocol = Protocol.HTTPS;
this.endpoint = endpoint;
this.endpoint = "https://" + endpoint;
}
}

Expand Down
Expand Up @@ -9,12 +9,12 @@
public interface NetworkController {

/**
* URL (without schema/protocol) used to send events to the collector.
* URL used to send events to the collector.
*/
void setEndpoint(@NonNull String endpoint);

/**
* URL (without schema/protocol) used to send events to the collector.
* URL used to send events to the collector.
*/
@NonNull
String getEndpoint();
Expand All @@ -30,17 +30,6 @@ public interface NetworkController {
@NonNull
HttpMethod getMethod();

/**
* Protocol used to send events to the collector.
*/
void setProtocol(@NonNull Protocol protocol);

/**
* Protocol used to send events to the collector.
*/
@NonNull
Protocol getProtocol();

/**
* A custom path which will be added to the endpoint URL to specify the
* complete URL of the collector when paired with the POST method.
Expand Down
Expand Up @@ -14,6 +14,7 @@
package com.snowplowanalytics.snowplow.internal.emitter;

import android.content.Context;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -35,6 +36,7 @@
import com.snowplowanalytics.snowplow.network.RequestResult;
import com.snowplowanalytics.snowplow.internal.utils.Util;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.EnumSet;
Expand Down Expand Up @@ -362,8 +364,14 @@ private Emitter(@NonNull EmitterBuilder builder) {

if (builder.networkConnection == null) {
isCustomNetworkConnection = false;
this.networkConnection = new OkHttpNetworkConnection.OkHttpNetworkConnectionBuilder(builder.uri)
.security(builder.requestSecurity)
String endpoint = builder.uri;
Uri uri = Uri.parse(endpoint);
if (!endpoint.startsWith("http")) {
String protocol = builder.requestSecurity == Protocol.HTTPS ? "https://" : "http://";
endpoint = protocol + endpoint;
}
this.uri = endpoint;
this.networkConnection = new OkHttpNetworkConnection.OkHttpNetworkConnectionBuilder(endpoint)
.method(builder.httpMethod)
.tls(builder.tlsVersions)
.emitTimeout(builder.emitTimeout)
Expand Down Expand Up @@ -703,7 +711,6 @@ public void setHttpMethod(@NonNull HttpMethod method) {
if (!isCustomNetworkConnection && !isRunning.get()) {
this.httpMethod = method;
this.networkConnection = new OkHttpNetworkConnection.OkHttpNetworkConnectionBuilder(uri)
.security(requestSecurity)
.method(httpMethod)
.tls(tlsVersions)
.emitTimeout(emitTimeout)
Expand All @@ -722,7 +729,6 @@ public void setRequestSecurity(@NonNull Protocol security) {
if (!isCustomNetworkConnection && !isRunning.get()) {
this.requestSecurity = security;
this.networkConnection = new OkHttpNetworkConnection.OkHttpNetworkConnectionBuilder(uri)
.security(requestSecurity)
.method(httpMethod)
.tls(tlsVersions)
.emitTimeout(emitTimeout)
Expand All @@ -741,7 +747,6 @@ public void setEmitterUri(@NonNull String uri) {
if (!isCustomNetworkConnection && !isRunning.get()) {
this.uri = uri;
this.networkConnection = new OkHttpNetworkConnection.OkHttpNetworkConnectionBuilder(uri)
.security(requestSecurity)
.method(httpMethod)
.tls(tlsVersions)
.emitTimeout(emitTimeout)
Expand All @@ -760,7 +765,6 @@ public void setCustomPostPath(@Nullable String customPostPath) {
if (!isCustomNetworkConnection && !isRunning.get()) {
this.customPostPath = customPostPath;
this.networkConnection = new OkHttpNetworkConnection.OkHttpNetworkConnectionBuilder(uri)
.security(requestSecurity)
.method(httpMethod)
.tls(tlsVersions)
.emitTimeout(emitTimeout)
Expand All @@ -779,7 +783,6 @@ public void setEmitTimeout(int emitTimeout) {
if (!isCustomNetworkConnection && !isRunning.get()) {
this.emitTimeout = emitTimeout;
this.networkConnection = new OkHttpNetworkConnection.OkHttpNetworkConnectionBuilder(uri)
.security(requestSecurity)
.method(httpMethod)
.tls(tlsVersions)
.emitTimeout(emitTimeout)
Expand Down
Expand Up @@ -44,17 +44,6 @@ public HttpMethod getMethod() {
return emitter.getHttpMethod();
}

@Override
public void setProtocol(@NonNull Protocol protocol) {
emitter.setRequestSecurity(protocol);
}

@NonNull
@Override
public Protocol getProtocol() {
return emitter.getRequestSecurity();
}

@Override
public void setCustomPostPath(@Nullable String customPostPath) {
emitter.setCustomPostPath(customPostPath);
Expand Down
Expand Up @@ -30,7 +30,6 @@

import static com.snowplowanalytics.snowplow.network.HttpMethod.GET;
import static com.snowplowanalytics.snowplow.network.HttpMethod.POST;
import static com.snowplowanalytics.snowplow.network.Protocol.HTTP;

/**
* Components in charge to send events to the collector.
Expand All @@ -57,7 +56,6 @@ public class OkHttpNetworkConnection implements NetworkConnection {
public static class OkHttpNetworkConnectionBuilder {
final String uri; // Required
HttpMethod httpMethod = POST; // Optional
Protocol protocol = Protocol.HTTP; // Optional
EnumSet<TLSVersion> tlsVersions = EnumSet.of(TLSVersion.TLSv1_2); // Optional
private int emitTimeout = 5; // Optional
OkHttpClient client = null; //Optional
Expand All @@ -80,16 +78,6 @@ public OkHttpNetworkConnectionBuilder method(@NonNull HttpMethod httpMethod) {
return this;
}

/**
* @param protocol the security chosen for requests
* @return itself
*/
@NonNull
public OkHttpNetworkConnectionBuilder security(@NonNull Protocol protocol) {
this.protocol = protocol;
return this;
}

/**
* @param version the TLS version allowed for requests
* @return itself
Expand Down Expand Up @@ -155,15 +143,45 @@ public OkHttpNetworkConnection build() {
}

private OkHttpNetworkConnection(OkHttpNetworkConnectionBuilder builder) {
this.uri = builder.uri;
this.protocol = builder.protocol;
this.httpMethod = builder.httpMethod;
this.emitTimeout = builder.emitTimeout;
this.customPostPath = builder.customPostPath;
// Decode uri to extract protocol
String tempUri = builder.uri;
Uri url = Uri.parse(builder.uri);
Protocol tempProtocol = Protocol.HTTPS;
if (url.getScheme() == null) {
tempUri = "https://" + builder.uri;
} else {
switch (url.getScheme()) {
case "https":
break;
case "http":
tempProtocol = Protocol.HTTP;
break;
default:
tempUri = "https://" + builder.uri;
}
}

// Configure
uri = tempUri;
protocol = tempProtocol;
httpMethod = builder.httpMethod;
emitTimeout = builder.emitTimeout;
customPostPath = builder.customPostPath;

TLSArguments tlsArguments = new TLSArguments(builder.tlsVersions);
buildUri();
String protocolString = protocol == Protocol.HTTP ? "http://" : "https://";
uriBuilder = Uri.parse(uri).buildUpon();

if (httpMethod == GET) {
uriBuilder.appendPath("i");
} else if (this.customPostPath == null) {
uriBuilder.appendEncodedPath(TrackerConstants.PROTOCOL_VENDOR + "/" +
TrackerConstants.PROTOCOL_VERSION);
} else {
uriBuilder.appendEncodedPath(this.customPostPath);
}

// Configure with external OkHttpClient
if (builder.client == null) {
client = new OkHttpClient.Builder()
.sslSocketFactory(tlsArguments.getSslSocketFactory(), tlsArguments.getTrustManager())
Expand Down Expand Up @@ -233,20 +251,6 @@ public List<RequestResult> sendRequests(@NonNull List<Request> requests) {
return results;
}

private void buildUri() {
String protocolString = protocol == HTTP ? "http://" : "https://";
uriBuilder = Uri.parse(protocolString + uri).buildUpon();

if (httpMethod == GET) {
uriBuilder.appendPath("i");
} else if (this.customPostPath == null) {
uriBuilder.appendEncodedPath(TrackerConstants.PROTOCOL_VENDOR + "/" +
TrackerConstants.PROTOCOL_VERSION);
} else {
uriBuilder.appendEncodedPath(this.customPostPath);
}
}

/**
* Builds an OkHttp GET request which is ready
* to be executed.
Expand Down

0 comments on commit a413474

Please sign in to comment.