Search in sources :

Example 1 with Configurator

use of jakarta.websocket.ClientEndpointConfig.Configurator in project tomcat by apache.

the class TestWebSocketFrameClient method testConnectToServerEndpoint.

@Test
public void testConnectToServerEndpoint() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterFirehoseServer.ConfigInline.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    // BZ 62596
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().configurator(new Configurator() {

        @Override
        public void beforeRequest(Map<String, List<String>> headers) {
            headers.put("Dummy", Collections.singletonList(String.join("", Collections.nCopies(4000, "A"))));
            super.beforeRequest(headers);
        }
    }).build();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + TesterFirehoseServer.PATH));
    CountDownLatch latch = new CountDownLatch(TesterFirehoseServer.MESSAGE_COUNT);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText("Hello");
    System.out.println("Sent Hello message, waiting for data");
    // Ignore the latch result as the message count test below will tell us
    // if the right number of messages arrived
    handler.getLatch().await(TesterFirehoseServer.WAIT_TIME_MILLIS, TimeUnit.MILLISECONDS);
    Queue<String> messages = handler.getMessages();
    Assert.assertEquals(TesterFirehoseServer.MESSAGE_COUNT, messages.size());
    for (String message : messages) {
        Assert.assertEquals(TesterFirehoseServer.MESSAGE, message);
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) Configurator(jakarta.websocket.ClientEndpointConfig.Configurator) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) Map(java.util.Map) Session(jakarta.websocket.Session) Test(org.junit.Test)

Aggregations

ClientEndpointConfig (jakarta.websocket.ClientEndpointConfig)1 Configurator (jakarta.websocket.ClientEndpointConfig.Configurator)1 Session (jakarta.websocket.Session)1 WebSocketContainer (jakarta.websocket.WebSocketContainer)1 URI (java.net.URI)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Context (org.apache.catalina.Context)1 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)1 Tomcat (org.apache.catalina.startup.Tomcat)1 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)1 Test (org.junit.Test)1