forked from jetty/jetty.project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue jetty#3458 - testing of Jetty WebSocket API ExtensionConfig
wire up the jetty-api extensions by copying the ExtensionConfig from the jetty-api upgrade request to the core upgrade request make UpgradeListener interface methods default for convenience introduce test to test the functionality of API ExtensionConfig Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
- Loading branch information
1 parent
19d8c52
commit b704dea
Showing
4 changed files
with
186 additions
and
10 deletions.
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
138 changes: 138 additions & 0 deletions
138
...ts/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketExtensionConfigTest.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,138 @@ | ||
// | ||
// ======================================================================== | ||
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd. | ||
// ------------------------------------------------------------------------ | ||
// All rights reserved. This program and the accompanying materials | ||
// are made available under the terms of the Eclipse Public License v1.0 | ||
// and Apache License v2.0 which accompanies this distribution. | ||
// | ||
// The Eclipse Public License is available at | ||
// http://www.eclipse.org/legal/epl-v10.html | ||
// | ||
// The Apache License v2.0 is available at | ||
// http://www.opensource.org/licenses/apache2.0.php | ||
// | ||
// You may elect to redistribute this code under either of these licenses. | ||
// ======================================================================== | ||
// | ||
|
||
package org.eclipse.jetty.websocket.tests; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.eclipse.jetty.client.HttpRequest; | ||
import org.eclipse.jetty.client.HttpResponse; | ||
import org.eclipse.jetty.http.HttpHeader; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.ServerConnector; | ||
import org.eclipse.jetty.servlet.ServletContextHandler; | ||
import org.eclipse.jetty.websocket.api.Session; | ||
import org.eclipse.jetty.websocket.api.UpgradeRequest; | ||
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig; | ||
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; | ||
import org.eclipse.jetty.websocket.client.WebSocketClient; | ||
import org.eclipse.jetty.websocket.core.client.UpgradeListener; | ||
import org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer; | ||
import org.eclipse.jetty.websocket.server.JettyWebSocketServletContainerInitializer; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
public class JettyWebSocketExtensionConfigTest | ||
{ | ||
Server server; | ||
WebSocketClient client; | ||
|
||
@BeforeEach | ||
public void start() throws Exception | ||
{ | ||
server = new Server(); | ||
ServerConnector connector = new ServerConnector(server); | ||
connector.setPort(8080); | ||
server.addConnector(connector); | ||
|
||
ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); | ||
contextHandler.setContextPath("/"); | ||
server.setHandler(contextHandler); | ||
|
||
JettyWebSocketServerContainer container = JettyWebSocketServletContainerInitializer.configureContext(contextHandler); | ||
container.addMapping("/", (req, resp)-> | ||
{ | ||
assertEquals(req.getExtensions().stream().filter(e -> e.getName().equals("permessage-deflate")).count(), 1); | ||
assertEquals(resp.getExtensions().stream().filter(e -> e.getName().equals("permessage-deflate")).count(), 1); | ||
|
||
ExtensionConfig permessageDeflate = ExtensionConfig.parse("permessage-deflate"); | ||
ExtensionConfig nonRequestedExtension = ExtensionConfig.parse("identity"); | ||
assertNotNull(permessageDeflate); | ||
assertNotNull(nonRequestedExtension); | ||
|
||
try | ||
{ | ||
resp.setExtensions(List.of(permessageDeflate, nonRequestedExtension)); | ||
fail("should not allow extensions not requested"); | ||
} | ||
catch (IllegalArgumentException e) | ||
{ | ||
resp.setExtensions(List.of(permessageDeflate)); | ||
} | ||
|
||
// Check identity extension was not added because it was not requested | ||
assertEquals(req.getExtensions().stream().filter(e -> e.getName().equals("identity")).count(), 0); | ||
assertEquals(resp.getExtensions().stream().filter(e -> e.getName().equals("identity")).count(), 0); | ||
|
||
return new EchoSocket(); | ||
}); | ||
server.start(); | ||
|
||
client = new WebSocketClient(); | ||
client.start(); | ||
} | ||
|
||
@AfterEach | ||
public void stop() throws Exception | ||
{ | ||
client.stop(); | ||
server.stop(); | ||
} | ||
|
||
@Test | ||
public void test() throws Exception | ||
{ | ||
URI uri = URI.create("ws://localhost:8080/filterPath"); | ||
EventSocket socket = new EventSocket(); | ||
|
||
UpgradeRequest request = new ClientUpgradeRequest(); | ||
request.addExtensions(ExtensionConfig.parse("permessage-deflate")); | ||
|
||
|
||
UpgradeListener listener = new UpgradeListener() | ||
{ | ||
@Override | ||
public void onHandshakeResponse(HttpRequest request, HttpResponse response) | ||
{ | ||
assertThat(response.getHeaders().get(HttpHeader.SEC_WEBSOCKET_EXTENSIONS), equalTo("permessage-deflate")); | ||
} | ||
}; | ||
|
||
CompletableFuture<Session> connect = client.connect(socket, uri, request, listener); | ||
try(Session session = connect.get(5, TimeUnit.SECONDS)) | ||
{ | ||
session.getRemote().sendString("hello world"); | ||
} | ||
assertTrue(socket.closed.await(10, TimeUnit.SECONDS)); | ||
|
||
String msg = socket.receivedMessages.poll(); | ||
assertThat(msg, is("hello world")); | ||
} | ||
} |
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