Fix WebSocketClientHandshaker not generating correct handshake request when path is empty (#10095)

Motivation:

WebSocketClientHandshaker#upgradeUrl doesn't comperly compute relative url when path is empty and produces url such as `?access_token=foo` instead of `/?access_token=foo`.

Modifications:

* fix WebSocketClientHandshaker#upgradeUrl
* add tests for urls without path, with and without query

Result:

WebSocketClientHandshaker properly connects to url without path.
This commit is contained in:
Stephane Landelle 2020-03-10 15:17:22 +01:00 committed by Norman Maurer
parent 217365dd65
commit d639f55764
2 changed files with 26 additions and 5 deletions

View File

@ -546,12 +546,9 @@ public abstract class WebSocketClientHandshaker {
}
String path = wsURL.getRawPath();
path = path == null || path.isEmpty() ? "/" : path;
String query = wsURL.getRawQuery();
if (query != null && !query.isEmpty()) {
path = path + '?' + query;
}
return path == null || path.isEmpty() ? "/" : path;
return query != null && !query.isEmpty() ? path + '?' + query : path;
}
static CharSequence websocketHostValue(URI wsURL) {

View File

@ -217,6 +217,30 @@ public abstract class WebSocketClientHandshakerTest {
}
}
@Test
public void testUpgradeUrlWithoutPath() {
URI uri = URI.create("ws://localhost:9999");
WebSocketClientHandshaker handshaker = newHandshaker(uri);
FullHttpRequest request = handshaker.newHandshakeRequest();
try {
assertEquals("/", request.uri());
} finally {
request.release();
}
}
@Test
public void testUpgradeUrlWithoutPathWithQuery() {
URI uri = URI.create("ws://localhost:9999?a=b%20c");
WebSocketClientHandshaker handshaker = newHandshaker(uri);
FullHttpRequest request = handshaker.newHandshakeRequest();
try {
assertEquals("/?a=b%20c", request.uri());
} finally {
request.release();
}
}
@Test
public void testAbsoluteUpgradeUrlWithQuery() {
URI uri = URI.create("ws://localhost:9999/path%20with%20ws?a=b%20c");