feat: add an extraHeaders option

Similar to the option of the JS client:

```java
opts = new Socket.Options();
opts.extraHeaders = singletonMap("authorization", singletonList("bearer abcd"));
socket = new Socket(opts);
```

Note: the refactor of the options (similar to [1]) will be done in a
future step.

[1] 5f47a50ee5
This commit is contained in:
Damien Arrachequesne
2020-12-11 12:24:07 +01:00
parent 41f89a38b7
commit dfe65e3b3b
5 changed files with 85 additions and 1 deletions

View File

@@ -10,11 +10,14 @@ import org.junit.runners.JUnit4;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
@@ -168,6 +171,38 @@ public class ServerConnectionTest extends Connection {
socket.close();
}
@Test(timeout = TIMEOUT)
public void pollingHeaders_withExtraHeadersOption() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> messages = new LinkedBlockingQueue<String>();
Socket.Options opts = createOptions();
opts.transports = new String[] {Polling.NAME};
opts.extraHeaders = singletonMap("X-EngineIO", singletonList("bar"));
socket = new Socket(opts);
socket.on(Socket.EVENT_TRANSPORT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Transport transport = (Transport)args[0];
transport.on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
@Override
public void call(Object... args) {
@SuppressWarnings("unchecked")
Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
List<String> values = headers.get("X-EngineIO");
messages.offer(values.get(0));
messages.offer(values.get(1));
}
});
}
});
socket.open();
assertThat(messages.take(), is("hi"));
assertThat(messages.take(), is("bar"));
socket.close();
}
@Test(timeout = TIMEOUT)
public void websocketHandshakeHeaders() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> messages = new LinkedBlockingQueue<String>();
@@ -206,6 +241,38 @@ public class ServerConnectionTest extends Connection {
socket.close();
}
@Test(timeout = TIMEOUT)
public void websocketHandshakeHeaders_withExtraHeadersOption() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> messages = new LinkedBlockingQueue<String>();
Socket.Options opts = createOptions();
opts.transports = new String[] {WebSocket.NAME};
opts.extraHeaders = singletonMap("X-EngineIO", singletonList("bar"));
socket = new Socket(opts);
socket.on(Socket.EVENT_TRANSPORT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Transport transport = (Transport)args[0];
transport.on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
@Override
public void call(Object... args) {
@SuppressWarnings("unchecked")
Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
List<String> values = headers.get("X-EngineIO");
messages.offer(values.get(0));
messages.offer(values.get(1));
}
});
}
});
socket.open();
assertThat(messages.take(), is("hi"));
assertThat(messages.take(), is("bar"));
socket.close();
}
@Test(timeout = TIMEOUT)
public void rememberWebsocket() throws InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();