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

Update x-goog-api-client header to use gl-java and gdcl tokens #1354

Merged
merged 2 commits into from Aug 19, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -131,47 +131,39 @@ protected AbstractGoogleClientRequest(AbstractGoogleClient abstractGoogleClient,
requestHeaders.setUserAgent(USER_AGENT_SUFFIX);
}
// Set the header for the Api Client version (Java and OS version)
requestHeaders.set(
API_VERSION_HEADER,
ApiClientVersion.getDefault().build(abstractGoogleClient.getClass().getSimpleName())
);
requestHeaders.set(API_VERSION_HEADER, ApiClientVersion.DEFAULT_VERSION);
}

/**
* Internal class to help build the X-Goog-Api-Client header. This header identifies the
* API Client version and environment.
*
* See <a href="https://cloud.google.com/apis/docs/system-parameters"></a>
* Internal class to help build the X-Goog-Api-Client header. This header identifies the API
* Client version and environment.
*
* <p>See <a href="https://cloud.google.com/apis/docs/system-parameters"></a>
*/
static class ApiClientVersion {
private static final ApiClientVersion DEFAULT_VERSION = new ApiClientVersion();
private final String headerTemplate;
static final String DEFAULT_VERSION = new ApiClientVersion().toString();
private final String versionString;

ApiClientVersion() {
this(getJavaVersion(), OS_NAME.value(), OS_VERSION.value(), GoogleUtils.VERSION);
}

ApiClientVersion(String javaVersion, String osName, String osVersion, String clientVersion) {
StringBuilder sb = new StringBuilder("java/");
StringBuilder sb = new StringBuilder("gl-java/");
sb.append(formatSemver(javaVersion));
sb.append(" http-google-%s/");
sb.append(" gdcl/");
sb.append(formatSemver(clientVersion));
if (osName != null && osVersion != null) {
sb.append(" ");
sb.append(formatName(osName));
sb.append("/");
sb.append(formatSemver(osVersion));
}
this.headerTemplate = sb.toString();
}

String build(String clientName) {
return String.format(headerTemplate, formatName(clientName));
this.versionString = sb.toString();
}

private static ApiClientVersion getDefault() {
return DEFAULT_VERSION;
public String toString() {
return versionString;
}

private static String getJavaVersion() {
Expand Down
Expand Up @@ -214,43 +214,52 @@ public void testUserAgentSuffix() throws Exception {
request.executeUnparsed();
}

public void testUserAgent() throws Exception {
public void testUserAgent() throws IOException {
AssertUserAgentTransport transport = new AssertUserAgentTransport();
transport.expectedUserAgent = AbstractGoogleClientRequest.USER_AGENT_SUFFIX + " " + HttpRequest.USER_AGENT_SUFFIX;
// Don't specify an Application Name.
MockGoogleClient client = new MockGoogleClient.Builder(
transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).build();
MockGoogleClientRequest<Void> request =
new MockGoogleClientRequest<Void>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
new MockGoogleClientRequest<>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
request.executeUnparsed();
}

public void testSetsApiClientHeader() throws Exception {
HttpTransport transport = new AssertHeaderTransport("X-Goog-Api-Client", "java/\\d+\\.\\d+\\.\\d+.*");
public void testSetsApiClientHeader() throws IOException {
HttpTransport transport = new AssertHeaderTransport("X-Goog-Api-Client", "gl-java/\\d+\\.\\d+\\.\\d+.*");
MockGoogleClient client = new MockGoogleClient.Builder(
transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).build();
MockGoogleClientRequest<Void> request =
new MockGoogleClientRequest<Void>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
new MockGoogleClientRequest<>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
request.executeUnparsed();
}

public void testSetsApiClientHeaderWithOsVersion() throws Exception {
public void testSetsApiClientHeaderWithOsVersion() {
System.setProperty("os.name", "My OS");
System.setProperty("os.version", "1.2.3");

String version = new ApiClientVersion().build("My Client");
String version = new ApiClientVersion().toString();
assertTrue("Api version should contain the os version", version.matches(".* my-os/1.2.3"));
}

public void testSetsApiClientHeaderWithoutOsVersion() throws Exception {
public void testSetsApiClientHeaderWithoutOsVersion() {
System.setProperty("os.name", "My OS");
System.clearProperty("os.version");
assertNull(System.getProperty("os.version"));

String version = new ApiClientVersion().build("My Client");
String version = new ApiClientVersion().toString();
assertFalse("Api version should not contain the os version", version.matches(".*my-os.*"));
}

public void testSetsApiClientHeaderDiscoveryVersion() throws IOException {
HttpTransport transport = new AssertHeaderTransport("X-Goog-Api-Client", ".*gdcl/\\d+\\.\\d+\\.\\d+.*");
MockGoogleClient client = new MockGoogleClient.Builder(
transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).build();
MockGoogleClientRequest<Void> request =
new MockGoogleClientRequest<>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
request.executeUnparsed();
}

public void testReturnRawInputStream_defaultFalse() throws Exception {
HttpTransport transport = new MockHttpTransport() {
@Override
Expand Down