diff --git a/analytics/src/main/java/com/segment/analytics/ConnectionFactory.java b/analytics/src/main/java/com/segment/analytics/ConnectionFactory.java index 142ee9f50..b3cfa7a93 100644 --- a/analytics/src/main/java/com/segment/analytics/ConnectionFactory.java +++ b/analytics/src/main/java/com/segment/analytics/ConnectionFactory.java @@ -1,6 +1,7 @@ package com.segment.analytics; import android.util.Base64; +import com.segment.analytics.core.BuildConfig; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; @@ -13,6 +14,7 @@ public class ConnectionFactory { private static final int DEFAULT_READ_TIMEOUT_MILLIS = 20 * 1000; // 20s private static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = 15 * 1000; // 15s + static final String USER_AGENT = "analytics-android/" + BuildConfig.VERSION_NAME; private String authorizationHeader(String writeKey) { return "Basic " + Base64.encodeToString((writeKey + ":").getBytes(), Base64.NO_WRAP); @@ -58,6 +60,7 @@ protected HttpURLConnection openConnection(String url) throws IOException { connection.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS); connection.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLIS); connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("User-Agent", USER_AGENT); connection.setDoInput(true); return connection; } diff --git a/analytics/src/test/java/com/segment/analytics/ClientTest.java b/analytics/src/test/java/com/segment/analytics/ClientTest.java index 265b9fb51..c4028ebbd 100644 --- a/analytics/src/test/java/com/segment/analytics/ClientTest.java +++ b/analytics/src/test/java/com/segment/analytics/ClientTest.java @@ -75,6 +75,7 @@ public void upload() throws Exception { assertThat(connection.connection.getResponseCode()).isEqualTo(200); // consume the response. RecordedRequestAssert.assertThat(server.takeRequest()) .hasRequestLine("POST /v1/import HTTP/1.1") + .containsHeader("User-Agent", ConnectionFactory.USER_AGENT) .containsHeader("Content-Type", "application/json") .containsHeader("Content-Encoding", "gzip") .containsHeader("Authorization", "Basic Zm9vOg=="); @@ -90,6 +91,7 @@ public void attribution() throws Exception { assertThat(connection.connection.getResponseCode()).isEqualTo(200); // consume the response. RecordedRequestAssert.assertThat(server.takeRequest()) .hasRequestLine("POST /v1/attribution HTTP/1.1") + .containsHeader("User-Agent", ConnectionFactory.USER_AGENT) .containsHeader("Content-Type", "application/json") .containsHeader("Authorization", "Basic Zm9vOg=="); } @@ -170,10 +172,10 @@ public void uploadFailureWithErrorStreamClosesStreamsAndThrowsException() throws fail(">= 300 return code should throw an exception"); } catch (Client.HTTPException e) { assertThat(e) - .hasMessage( - "HTTP 404: bar. " - + "Response: Could not read response body for rejected message: " - + "java.io.IOException: Underlying input stream returned zero bytes"); + .hasMessage( + "HTTP 404: bar. " + + "Response: Could not read response body for rejected message: " + + "java.io.IOException: Underlying input stream returned zero bytes"); } verify(mockConnection).disconnect(); verify(os).close(); @@ -189,6 +191,7 @@ public void fetchSettings() throws Exception { assertThat(connection.connection.getResponseCode()).isEqualTo(200); RecordedRequestAssert.assertThat(server.takeRequest()) .hasRequestLine("GET /v1/projects/foo/settings HTTP/1.1") + .containsHeader("User-Agent", ConnectionFactory.USER_AGENT) .containsHeader("Content-Type", "application/json"); }