[Java] Add ability to configure headers (#3090)
This commit is contained in:
parent
9049bf709c
commit
e08774b6f0
|
|
@ -4,6 +4,8 @@
|
||||||
package com.microsoft.signalr;
|
package com.microsoft.signalr;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import io.reactivex.Single;
|
import io.reactivex.Single;
|
||||||
|
|
||||||
|
|
@ -15,6 +17,7 @@ public class HttpHubConnectionBuilder {
|
||||||
private boolean skipNegotiate;
|
private boolean skipNegotiate;
|
||||||
private Single<String> accessTokenProvider;
|
private Single<String> accessTokenProvider;
|
||||||
private Duration handshakeResponseTimeout;
|
private Duration handshakeResponseTimeout;
|
||||||
|
private Map<String, String> headers;
|
||||||
|
|
||||||
HttpHubConnectionBuilder(String url) {
|
HttpHubConnectionBuilder(String url) {
|
||||||
this.url = url;
|
this.url = url;
|
||||||
|
|
@ -61,7 +64,20 @@ public class HttpHubConnectionBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HttpHubConnectionBuilder withHeaders(Map<String, String> headers) {
|
||||||
|
this.headers = headers;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpHubConnectionBuilder withHeader(String name, String value) {
|
||||||
|
if (headers == null) {
|
||||||
|
this.headers = new HashMap<>();
|
||||||
|
}
|
||||||
|
this.headers.put(name, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public HubConnection build() {
|
public HubConnection build() {
|
||||||
return new HubConnection(url, transport, skipNegotiate, logger, httpClient, accessTokenProvider, handshakeResponseTimeout);
|
return new HubConnection(url, transport, skipNegotiate, logger, httpClient, accessTokenProvider, handshakeResponseTimeout, headers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -72,7 +72,8 @@ public class HubConnection {
|
||||||
this.tickRate = tickRate;
|
this.tickRate = tickRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
HubConnection(String url, Transport transport, boolean skipNegotiate, Logger logger, HttpClient httpClient, Single<String> accessTokenProvider, Duration handshakeResponseTimeout) {
|
HubConnection(String url, Transport transport, boolean skipNegotiate, Logger logger, HttpClient httpClient,
|
||||||
|
Single<String> accessTokenProvider, Duration handshakeResponseTimeout, Map<String, String> headers) {
|
||||||
if (url == null || url.isEmpty()) {
|
if (url == null || url.isEmpty()) {
|
||||||
throw new IllegalArgumentException("A valid url is required.");
|
throw new IllegalArgumentException("A valid url is required.");
|
||||||
}
|
}
|
||||||
|
|
@ -106,6 +107,10 @@ public class HubConnection {
|
||||||
this.handshakeResponseTimeout = handshakeResponseTimeout;
|
this.handshakeResponseTimeout = handshakeResponseTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (headers != null) {
|
||||||
|
this.headers.putAll(headers);
|
||||||
|
}
|
||||||
|
|
||||||
this.skipNegotiate = skipNegotiate;
|
this.skipNegotiate = skipNegotiate;
|
||||||
|
|
||||||
this.callback = (payload) -> {
|
this.callback = (payload) -> {
|
||||||
|
|
|
||||||
|
|
@ -1037,7 +1037,7 @@ class HubConnectionTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void accessTokenProviderIsOverriddenFromRedirectNegotiate() {
|
public void accessTokenProviderIsOverriddenFromRedirectNegotiate() {
|
||||||
AtomicReference<String> token = new AtomicReference<>();
|
AtomicReference<String> token = new AtomicReference<>();
|
||||||
TestHttpClient client = new TestHttpClient()
|
TestHttpClient client = new TestHttpClient()
|
||||||
.on("POST", "http://example.com/negotiate", (req) -> CompletableFuture.completedFuture(new HttpResponse(200, "", "{\"url\":\"http://testexample.com/\",\"accessToken\":\"newToken\"}")))
|
.on("POST", "http://example.com/negotiate", (req) -> CompletableFuture.completedFuture(new HttpResponse(200, "", "{\"url\":\"http://testexample.com/\",\"accessToken\":\"newToken\"}")))
|
||||||
|
|
@ -1098,7 +1098,60 @@ class HubConnectionTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void hubConnectionCanBeStartedAfterBeingStopped() {
|
public void headersAreSetAndSentThroughBuilder() {
|
||||||
|
AtomicReference<String> header = new AtomicReference<>();
|
||||||
|
TestHttpClient client = new TestHttpClient()
|
||||||
|
.on("POST", "http://example.com/negotiate",
|
||||||
|
(req) -> {
|
||||||
|
header.set(req.getHeaders().get("ExampleHeader"));
|
||||||
|
return CompletableFuture
|
||||||
|
.completedFuture(new HttpResponse(200, "", "{\"connectionId\":\"bVOiRPG8-6YiJ6d7ZcTOVQ\",\""
|
||||||
|
+ "availableTransports\":[{\"transport\":\"WebSockets\",\"transferFormats\":[\"Text\",\"Binary\"]}]}"));
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
MockTransport transport = new MockTransport();
|
||||||
|
HubConnection hubConnection = HubConnectionBuilder.create("http://example.com")
|
||||||
|
.withTransport(transport)
|
||||||
|
.withHttpClient(client)
|
||||||
|
.withHeader("ExampleHeader", "ExampleValue")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
hubConnection.start().blockingAwait(1000, TimeUnit.MILLISECONDS);
|
||||||
|
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
||||||
|
hubConnection.stop();
|
||||||
|
assertEquals("ExampleValue", header.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sameHeaderSetTwiceGetsOverwritten() {
|
||||||
|
AtomicReference<String> header = new AtomicReference<>();
|
||||||
|
TestHttpClient client = new TestHttpClient()
|
||||||
|
.on("POST", "http://example.com/negotiate",
|
||||||
|
(req) -> {
|
||||||
|
header.set(req.getHeaders().get("ExampleHeader"));
|
||||||
|
return CompletableFuture
|
||||||
|
.completedFuture(new HttpResponse(200, "", "{\"connectionId\":\"bVOiRPG8-6YiJ6d7ZcTOVQ\",\""
|
||||||
|
+ "availableTransports\":[{\"transport\":\"WebSockets\",\"transferFormats\":[\"Text\",\"Binary\"]}]}"));
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
MockTransport transport = new MockTransport();
|
||||||
|
HubConnection hubConnection = HubConnectionBuilder.create("http://example.com")
|
||||||
|
.withTransport(transport)
|
||||||
|
.withHttpClient(client)
|
||||||
|
.withHeader("ExampleHeader", "ExampleValue")
|
||||||
|
.withHeader("ExampleHeader", "New Value")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
hubConnection.start().blockingAwait(1000, TimeUnit.MILLISECONDS);
|
||||||
|
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
||||||
|
hubConnection.stop();
|
||||||
|
assertEquals("New Value", header.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void hubConnectionCanBeStartedAfterBeingStopped() throws Exception {
|
||||||
MockTransport transport = new MockTransport();
|
MockTransport transport = new MockTransport();
|
||||||
HubConnection hubConnection = HubConnectionBuilder
|
HubConnection hubConnection = HubConnectionBuilder
|
||||||
.create("http://example.com")
|
.create("http://example.com")
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,6 @@ package com.microsoft.signalr;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue