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 GitHub
parent c623a50d19
commit 2576a2dd74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 5 deletions

View File

@ -566,12 +566,9 @@ public abstract class WebSocketClientHandshaker {
} }
String path = wsURL.getRawPath(); String path = wsURL.getRawPath();
path = path == null || path.isEmpty() ? "/" : path;
String query = wsURL.getRawQuery(); String query = wsURL.getRawQuery();
if (query != null && !query.isEmpty()) { return query != null && !query.isEmpty() ? path + '?' + query : path;
path = path + '?' + query;
}
return path == null || path.isEmpty() ? "/" : path;
} }
static CharSequence websocketHostValue(URI wsURL) { 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 @Test
public void testAbsoluteUpgradeUrlWithQuery() { public void testAbsoluteUpgradeUrlWithQuery() {
URI uri = URI.create("ws://localhost:9999/path%20with%20ws?a=b%20c"); URI uri = URI.create("ws://localhost:9999/path%20with%20ws?a=b%20c");