-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Only map websockets to servlet path and not any sub paths (#14638)
* fix: Only map websockets to servlet path and not any sub paths
- Loading branch information
Showing
7 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...g-tests/test-spring-common/src/main/java/com/vaadin/flow/spring/test/CustomWebSocket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.vaadin.flow.spring.test; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.socket.CloseStatus; | ||
import org.springframework.web.socket.TextMessage; | ||
import org.springframework.web.socket.WebSocketHandler; | ||
import org.springframework.web.socket.WebSocketMessage; | ||
import org.springframework.web.socket.WebSocketSession; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocket; | ||
import org.springframework.web.socket.config.annotation.WebSocketConfigurer; | ||
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; | ||
|
||
@Configuration | ||
@EnableWebSocket | ||
public class CustomWebSocket implements WebSocketConfigurer { | ||
|
||
public static final String WEBSOCKET_URL = "/customWebSocket"; | ||
|
||
public static final String WEBSOCKET_RESPONSE_TEXT = "This is a response from Web Socket!"; | ||
|
||
@Override | ||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { | ||
registry.addHandler(new WebSocketHandler() { | ||
|
||
@Override | ||
public void afterConnectionEstablished(WebSocketSession session) { | ||
} | ||
|
||
@Override | ||
public void handleMessage(WebSocketSession session, | ||
WebSocketMessage<?> message) throws Exception { | ||
session.sendMessage(new TextMessage(WEBSOCKET_RESPONSE_TEXT)); | ||
} | ||
|
||
@Override | ||
public void handleTransportError(WebSocketSession session, | ||
Throwable exception) { | ||
|
||
} | ||
|
||
@Override | ||
public void afterConnectionClosed(WebSocketSession session, | ||
CloseStatus closeStatus) { | ||
|
||
} | ||
|
||
@Override | ||
public boolean supportsPartialMessages() { | ||
return false; | ||
} | ||
}, WEBSOCKET_URL); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...tests/test-spring-common/src/test/java/com/vaadin/flow/spring/test/CustomWebSocketIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.vaadin.flow.spring.test; | ||
|
||
import javax.websocket.ClientEndpoint; | ||
import javax.websocket.CloseReason; | ||
import javax.websocket.ContainerProvider; | ||
import javax.websocket.OnClose; | ||
import javax.websocket.OnMessage; | ||
import javax.websocket.Session; | ||
import javax.websocket.WebSocketContainer; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
|
||
@Category(SpringBootOnly.class) | ||
public class CustomWebSocketIT extends AbstractSpringTest { | ||
|
||
@ClientEndpoint | ||
public class CustomWebSocketEndpoint { | ||
|
||
private final CountDownLatch closureLatch = new CountDownLatch(1); | ||
|
||
private String message; | ||
|
||
@OnMessage | ||
public void onMessage(Session session, String message) | ||
throws IOException { | ||
this.message = message; | ||
session.close(); | ||
} | ||
|
||
@OnClose | ||
public void onClose(CloseReason reason) { | ||
closureLatch.countDown(); | ||
} | ||
|
||
public String waitForMessage() throws InterruptedException { | ||
closureLatch.await(1, TimeUnit.SECONDS); | ||
return message; | ||
} | ||
} | ||
|
||
@Test | ||
public void properWebsocketResponseIsReceived() throws Exception { | ||
WebSocketContainer container = ContainerProvider | ||
.getWebSocketContainer(); | ||
|
||
String testUrl = getTestURL().replace("http://", "ws://"); | ||
URI uri = URI.create(testUrl + CustomWebSocket.WEBSOCKET_URL); | ||
|
||
try { | ||
CustomWebSocketEndpoint clientEndpoint = new CustomWebSocketEndpoint(); | ||
Session session = container.connectToServer(clientEndpoint, uri); | ||
session.getBasicRemote().sendText("Hello"); | ||
Assert.assertEquals(CustomWebSocket.WEBSOCKET_RESPONSE_TEXT, | ||
clientEndpoint.waitForMessage()); | ||
session.close(); | ||
} catch (Exception ex) { | ||
throw ex; | ||
} | ||
} | ||
|
||
@Override | ||
protected String getTestPath() { | ||
return ""; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters