Set the user agent header in the Java client (#13168)

This commit is contained in:
Mikael Mengistu 2019-08-21 16:15:39 -05:00 committed by GitHub
parent 5b2f3fb5f7
commit e0f95cfa6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 215 additions and 0 deletions

View File

@ -108,3 +108,27 @@ task generatePOM {
}
task createPackage(dependsOn: [jar,sourceJar,javadocJar,generatePOM])
task generateVersionClass {
inputs.property "version", project.version
outputs.dir "$buildDir/generated"
doFirst {
def versionFile = file("$buildDir/../src/main/java/com/microsoft/signalr/Version.java")
versionFile.parentFile.mkdirs()
versionFile.text =
"""
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
package com.microsoft.signalr;
class Version {
public static String getDetailedVersion() {
return "$project.version";
}
}
"""
}
}
compileJava.dependsOn generateVersionClass

View File

@ -327,6 +327,7 @@ public class HubConnection {
handshakeResponseSubject = CompletableSubject.create();
handshakeReceived = false;
CompletableSubject tokenCompletable = CompletableSubject.create();
localHeaders.put(UserAgentHelper.getUserAgentName(), UserAgentHelper.createUserAgentString());
if (headers != null) {
this.localHeaders.putAll(headers);
}

View File

@ -0,0 +1,54 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
package com.microsoft.signalr;
public class UserAgentHelper {
private final static String USER_AGENT = "User-Agent";
public static String getUserAgentName() {
return USER_AGENT;
}
public static String createUserAgentString() {
StringBuilder agentBuilder = new StringBuilder("Microsoft SignalR/");
// Parsing version numbers
String detailedVersion = Version.getDetailedVersion();
agentBuilder.append(getVersion(detailedVersion));
agentBuilder.append("; (");
agentBuilder.append(detailedVersion);
agentBuilder.append("; ");
// Getting the OS name
agentBuilder.append(getOS());
agentBuilder.append("; Java; ");
// Vendor and Version
agentBuilder.append(getJavaVersion());
agentBuilder.append("; ");
agentBuilder.append(getJavaVendor());
agentBuilder.append(")");
return agentBuilder.toString();
}
static String getVersion(String detailedVersion) {
// Getting the index of the second . so we can return just the major and minor version.
int shortVersionIndex = detailedVersion.indexOf(".", detailedVersion.indexOf(".") + 1);
return detailedVersion.substring(0, shortVersionIndex);
}
static String getJavaVendor() {
return System.getProperty("java.vendor");
}
static String getJavaVersion() {
return System.getProperty("java.version");
}
static String getOS() {
return System.getProperty("os.name");
}
}

View File

@ -0,0 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
package com.microsoft.signalr;
class Version {
public static String getDetailedVersion() {
return "99.99.99-dev";
}
}

View File

@ -2185,6 +2185,77 @@ class HubConnectionTest {
}
}
@Test
public void userAgentHeaderIsSet() {
AtomicReference<String> header = new AtomicReference<>();
TestHttpClient client = new TestHttpClient()
.on("POST", "http://example.com/negotiate",
(req) -> {
header.set(req.getHeaders().get("User-Agent"));
return Single.just(new HttpResponse(200, "", "{\"connectionId\":\"bVOiRPG8-6YiJ6d7ZcTOVQ\",\""
+ "availableTransports\":[{\"transport\":\"WebSockets\",\"transferFormats\":[\"Text\",\"Binary\"]}]}"));
});
MockTransport transport = new MockTransport();
HubConnection hubConnection = HubConnectionBuilder.create("http://example.com")
.withTransportImplementation(transport)
.withHttpClient(client)
.build();
hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait();
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
hubConnection.stop();
assertTrue(header.get().startsWith("Microsoft SignalR/"));
}
@Test
public void userAgentHeaderCanBeOverwritten() {
AtomicReference<String> header = new AtomicReference<>();
TestHttpClient client = new TestHttpClient()
.on("POST", "http://example.com/negotiate",
(req) -> {
header.set(req.getHeaders().get("User-Agent"));
return Single.just(new HttpResponse(200, "", "{\"connectionId\":\"bVOiRPG8-6YiJ6d7ZcTOVQ\",\""
+ "availableTransports\":[{\"transport\":\"WebSockets\",\"transferFormats\":[\"Text\",\"Binary\"]}]}"));
});
MockTransport transport = new MockTransport();
HubConnection hubConnection = HubConnectionBuilder.create("http://example.com")
.withTransportImplementation(transport)
.withHttpClient(client)
.withHeader("User-Agent", "Updated Value")
.build();
hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait();
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
hubConnection.stop();
assertEquals("Updated Value", header.get());
}
@Test
public void userAgentCanBeCleared() {
AtomicReference<String> header = new AtomicReference<>();
TestHttpClient client = new TestHttpClient()
.on("POST", "http://example.com/negotiate",
(req) -> {
header.set(req.getHeaders().get("User-Agent"));
return Single.just(new HttpResponse(200, "", "{\"connectionId\":\"bVOiRPG8-6YiJ6d7ZcTOVQ\",\""
+ "availableTransports\":[{\"transport\":\"WebSockets\",\"transferFormats\":[\"Text\",\"Binary\"]}]}"));
});
MockTransport transport = new MockTransport();
HubConnection hubConnection = HubConnectionBuilder.create("http://example.com")
.withTransportImplementation(transport)
.withHttpClient(client)
.withHeader("User-Agent", "")
.build();
hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait();
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
hubConnection.stop();
assertEquals("", header.get());
}
@Test
public void headersAreSetAndSentThroughBuilder() {
AtomicReference<String> header = new AtomicReference<>();

View File

@ -0,0 +1,54 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
package com.microsoft.signalr;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public class UserAgentTest {
private static Stream<Arguments> Versions() {
return Stream.of(
Arguments.of("1.0.0", "1.0"),
Arguments.of("3.1.4-preview9-12345", "3.1"),
Arguments.of("3.1.4-preview9-12345-extrastuff", "3.1"),
Arguments.of("99.99.99-dev", "99.99"));
}
@ParameterizedTest
@MethodSource("Versions")
public void getVersionFromDetailedVersion(String detailedVersion, String version) {
assertEquals(version, UserAgentHelper.getVersion(detailedVersion));
}
@Test
public void verifyJavaVendor(){
assertEquals(System.getProperty("java.vendor"), UserAgentHelper.getJavaVendor());
}
@Test
public void verifyJavaVersion(){
assertEquals(System.getProperty("java.version"), UserAgentHelper.getJavaVersion());
}
@Test
public void checkUserAgentString(){
String userAgent = UserAgentHelper.createUserAgentString();
assertNotNull(userAgent);
String detailedVersion = Version.getDetailedVersion();
String handMadeUserAgent = "Microsoft SignalR/" + UserAgentHelper.getVersion(detailedVersion) +
"; (" + detailedVersion + "; " + UserAgentHelper.getOS() + "; Java; " +
UserAgentHelper.getJavaVersion() + "; " + UserAgentHelper.getJavaVendor() + ")";
assertEquals(handMadeUserAgent, userAgent);
}
}