simple onOpen test
Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
parent
f170191003
commit
330a3e7f58
|
@ -0,0 +1,215 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// 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.core;
|
||||
|
||||
import java.net.Socket;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.eclipse.jetty.server.HttpConnectionFactory;
|
||||
import org.eclipse.jetty.server.NetworkConnector;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.server.handler.ContextHandler;
|
||||
import org.eclipse.jetty.util.BufferUtil;
|
||||
import org.eclipse.jetty.util.Callback;
|
||||
import org.eclipse.jetty.util.DecoratedObjectFactory;
|
||||
import org.eclipse.jetty.util.component.AbstractLifeCycle;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
import org.eclipse.jetty.util.thread.QueuedThreadPool;
|
||||
import org.eclipse.jetty.websocket.core.internal.Parser;
|
||||
import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator;
|
||||
import org.eclipse.jetty.websocket.core.server.WebSocketUpgradeHandler;
|
||||
import org.eclipse.jetty.websocket.core.server.internal.RFC6455Handshaker;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.eclipse.jetty.util.Callback.NOOP;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
/**
|
||||
* Tests of a core server with a fake client
|
||||
*/
|
||||
public class WebSocketOpenTest extends WebSocketTester
|
||||
{
|
||||
private static Logger LOG = Log.getLogger(WebSocketOpenTest.class);
|
||||
|
||||
private WebSocketServer server;
|
||||
private Socket client;
|
||||
|
||||
@AfterEach
|
||||
public void after() throws Exception
|
||||
{
|
||||
if (server != null)
|
||||
server.stop();
|
||||
}
|
||||
|
||||
public void setup(Consumer<FrameHandler.CoreSession> onOpen) throws Exception
|
||||
{
|
||||
|
||||
TestFrameHandler serverHandler = new TestFrameHandler(onOpen);
|
||||
server = new WebSocketServer(0, serverHandler);
|
||||
server.start();
|
||||
client = newClient(server.getLocalPort());
|
||||
|
||||
assertThat(server.handler.getCoreSession().toString(), containsString("OPEN"));
|
||||
LOG.info("Server: OPEN");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendFrameInOnOpen() throws Exception
|
||||
{
|
||||
setup(s->TestFrameHandler.sendText(s,"Hello"));
|
||||
Parser.ParsedFrame frame = receiveFrame(client.getInputStream());
|
||||
assertThat(frame.getPayloadAsUTF8(),is("Hello"));
|
||||
}
|
||||
|
||||
|
||||
static class TestFrameHandler implements FrameHandler
|
||||
{
|
||||
private CoreSession session;
|
||||
private Consumer<CoreSession> onOpen;
|
||||
|
||||
public CoreSession getCoreSession()
|
||||
{
|
||||
return session;
|
||||
}
|
||||
|
||||
TestFrameHandler(Consumer<CoreSession> onOpen)
|
||||
{
|
||||
this.onOpen = onOpen;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onOpen(CoreSession coreSession)
|
||||
{
|
||||
LOG.info("onOpen {}", coreSession);
|
||||
session = coreSession;
|
||||
onOpen.accept(coreSession);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFrame(Frame frame, Callback callback)
|
||||
{
|
||||
LOG.info("onFrame: " + BufferUtil.toDetailString(frame.getPayload()));
|
||||
callback.succeeded();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClosed(CloseStatus closeStatus)
|
||||
{
|
||||
LOG.info("onClosed {}", closeStatus);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable cause) throws Exception
|
||||
{
|
||||
LOG.info("onError {} ", cause == null?null:cause.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDemanding()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public void sendText(String text)
|
||||
{
|
||||
sendText(session, text);
|
||||
}
|
||||
|
||||
static void sendText(FrameHandler.CoreSession session, String text)
|
||||
{
|
||||
Frame frame = new Frame(OpCode.TEXT);
|
||||
frame.setFin(true);
|
||||
frame.setPayload(text);
|
||||
|
||||
session.sendFrame(frame, NOOP, false);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
static class WebSocketServer extends AbstractLifeCycle
|
||||
{
|
||||
private static Logger LOG = Log.getLogger(WebSocketServer.class);
|
||||
private final Server server;
|
||||
private final TestFrameHandler handler;
|
||||
|
||||
public void doStart() throws Exception
|
||||
{
|
||||
server.start();
|
||||
}
|
||||
|
||||
public void doStop() throws Exception
|
||||
{
|
||||
server.stop();
|
||||
}
|
||||
|
||||
public int getLocalPort()
|
||||
{
|
||||
return server.getBean(NetworkConnector.class).getLocalPort();
|
||||
}
|
||||
|
||||
public WebSocketServer(int port, TestFrameHandler frameHandler)
|
||||
{
|
||||
this.handler = frameHandler;
|
||||
server = new Server();
|
||||
server.getBean(QueuedThreadPool.class).setName("WSCoreServer");
|
||||
ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory());
|
||||
|
||||
connector.addBean(new RFC6455Handshaker());
|
||||
connector.setPort(port);
|
||||
connector.setIdleTimeout(1000000);
|
||||
server.addConnector(connector);
|
||||
|
||||
ContextHandler context = new ContextHandler("/");
|
||||
server.setHandler(context);
|
||||
WebSocketNegotiator negotiator = new TestWebSocketNegotiator(new DecoratedObjectFactory(), new WebSocketExtensionRegistry(),
|
||||
connector.getByteBufferPool(), frameHandler);
|
||||
|
||||
WebSocketUpgradeHandler upgradeHandler = new TestWebSocketUpgradeHandler(negotiator);
|
||||
context.setHandler(upgradeHandler);
|
||||
}
|
||||
|
||||
public void sendFrame(Frame frame)
|
||||
{
|
||||
handler.getCoreSession().sendFrame(frame, NOOP, false);
|
||||
}
|
||||
|
||||
public void sendText(String text)
|
||||
{
|
||||
LOG.info("sending {}...", text);
|
||||
TestFrameHandler.sendText(handler.session, text);
|
||||
}
|
||||
|
||||
public void close()
|
||||
{
|
||||
handler.getCoreSession().close(CloseStatus.NORMAL, "WebSocketServer Initiated Close", Callback.NOOP);
|
||||
}
|
||||
|
||||
public boolean isOpen()
|
||||
{
|
||||
return handler.getCoreSession().isOutputOpen();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue