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

Send RFC 7231 Formatted User Agent #582

Merged
merged 1 commit into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 7 additions & 4 deletions analytics/src/test/java/com/segment/analytics/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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==");
Expand All @@ -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==");
}
Expand Down Expand Up @@ -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();
Expand All @@ -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");
}

Expand Down