Skip to content

Commit 83cdd58

Browse files
committed
Update localAddress handling in StandardWebSocketClient
Closes gh-34331
1 parent 52c187b commit 83cdd58

File tree

3 files changed

+12
-23
lines changed

3 files changed

+12
-23
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,9 @@ public interface WebSocketSession extends Closeable {
7373
/**
7474
* Return the address on which the request was received.
7575
* <p><strong>Note:</strong> The localAddress is not always possible to access,
76-
* which is the case with the Standard WebSocket client. In 6.2.x
76+
* which is the case with the Standard WebSocket client API, and accordingly
7777
* {@link org.springframework.web.socket.client.standard.StandardWebSocketClient}
78-
* returns an address based on the local host and the port of the target
79-
* address (not the same as the local port). In 7.0, the same will return
80-
* {@code null} instead.
78+
* returns {@code null}.
8179
*/
8280
@Nullable InetSocketAddress getLocalAddress();
8381

spring-websocket/src/main/java/org/springframework/web/socket/client/standard/StandardWebSocketClient.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -149,12 +149,10 @@ protected CompletableFuture<WebSocketSession> executeInternal(WebSocketHandler w
149149
HttpHeaders headers, final URI uri, List<String> protocols,
150150
List<WebSocketExtension> extensions, Map<String, Object> attributes) {
151151

152-
int port = getPort(uri);
153-
InetSocketAddress localAddress = new InetSocketAddress(getLocalHost(), port);
154-
InetSocketAddress remoteAddress = new InetSocketAddress(uri.getHost(), port);
152+
InetSocketAddress remoteAddress = new InetSocketAddress(uri.getHost(), getPort(uri));
155153

156-
StandardWebSocketSession session = new StandardWebSocketSession(headers,
157-
attributes, localAddress, remoteAddress);
154+
StandardWebSocketSession session =
155+
new StandardWebSocketSession(headers, attributes, null, remoteAddress);
158156

159157
ClientEndpointConfig endpointConfig = ClientEndpointConfig.Builder.create()
160158
.configurator(new StandardWebSocketClientConfigurator(headers))

spring-websocket/src/test/java/org/springframework/web/socket/client/standard/StandardWebSocketClientTests.java

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,18 +60,11 @@ class StandardWebSocketClientTests {
6060
void getLocalAddress() throws Exception {
6161
URI uri = URI.create("ws://localhost/abc");
6262
WebSocketSession session = this.wsClient.execute(this.wsHandler, this.headers, uri).get();
63+
assertThat(session.getLocalAddress()).isNull();
6364

64-
assertThat(session.getLocalAddress()).isNotNull();
65-
assertThat(session.getLocalAddress().getPort()).isEqualTo(80);
66-
}
67-
68-
@Test
69-
void getLocalAddressWss() throws Exception {
70-
URI uri = URI.create("wss://localhost/abc");
71-
WebSocketSession session = this.wsClient.execute(this.wsHandler, this.headers, uri).get();
72-
73-
assertThat(session.getLocalAddress()).isNotNull();
74-
assertThat(session.getLocalAddress().getPort()).isEqualTo(443);
65+
uri = URI.create("wss://localhost/abc");
66+
session = this.wsClient.execute(this.wsHandler, this.headers, uri).get();
67+
assertThat(session.getLocalAddress()).isNull();
7568
}
7669

7770
@Test
@@ -88,7 +81,7 @@ void getRemoteAddress() throws Exception {
8881

8982
assertThat(session.getRemoteAddress()).isNotNull();
9083
assertThat(session.getRemoteAddress().getHostName()).isEqualTo("localhost");
91-
assertThat(session.getLocalAddress().getPort()).isEqualTo(443);
84+
assertThat(session.getLocalAddress()).isNull();
9285
}
9386

9487
@Test

0 commit comments

Comments
 (0)