diff options
Diffstat (limited to 'websocket/src/test/java/fi/iki/elonen')
3 files changed, 58 insertions, 61 deletions
diff --git a/websocket/src/test/java/fi/iki/elonen/WebSocketResponseHandlerTest.java b/websocket/src/test/java/fi/iki/elonen/WebSocketResponseHandlerTest.java index ec14156..2c673b1 100644 --- a/websocket/src/test/java/fi/iki/elonen/WebSocketResponseHandlerTest.java +++ b/websocket/src/test/java/fi/iki/elonen/WebSocketResponseHandlerTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -33,8 +33,10 @@ package fi.iki.elonen; * #L% */ -import static junit.framework.Assert.*; -import static org.mockito.Mockito.*; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertNull; +import static org.mockito.Mockito.when; import java.util.HashMap; import java.util.Map; @@ -61,67 +63,67 @@ public class WebSocketResponseHandlerTest { @Before public void setUp() { - nanoWebSocketServer = Mockito.mock(NanoWebSocketServer.class, Mockito.CALLS_REAL_METHODS); + this.nanoWebSocketServer = Mockito.mock(NanoWebSocketServer.class, Mockito.CALLS_REAL_METHODS); - headers = new HashMap<String, String>(); - headers.put("upgrade", "websocket"); - headers.put("connection", "Upgrade"); - headers.put("sec-websocket-key", "x3JJHMbDL1EzLkh9GBhXDw=="); - headers.put("sec-websocket-protocol", "chat, superchat"); - headers.put("sec-websocket-version", "13"); + this.headers = new HashMap<String, String>(); + this.headers.put("upgrade", "websocket"); + this.headers.put("connection", "Upgrade"); + this.headers.put("sec-websocket-key", "x3JJHMbDL1EzLkh9GBhXDw=="); + this.headers.put("sec-websocket-protocol", "chat, superchat"); + this.headers.put("sec-websocket-version", "13"); - when(session.getHeaders()).thenReturn(headers); + when(this.session.getHeaders()).thenReturn(this.headers); } @Test - public void testHandshakeReturnsResponseWithExpectedHeaders() { - Response handshakeResponse = nanoWebSocketServer.serve(session); + public void testConnectionHeaderHandlesKeepAlive_FixingFirefoxConnectIssue() { + this.headers.put("connection", "keep-alive, Upgrade"); + Response handshakeResponse = this.nanoWebSocketServer.serve(this.session); assertNotNull(handshakeResponse); - - assertEquals(handshakeResponse.getHeader(NanoWebSocketServer.HEADER_WEBSOCKET_ACCEPT), "HSmrc0sMlYUkAGmm5OPpG2HaGWk="); - assertEquals(handshakeResponse.getHeader(NanoWebSocketServer.HEADER_WEBSOCKET_PROTOCOL), "chat"); } @Test - public void testWrongWebsocketVersionReturnsErrorResponse() { - headers.put("sec-websocket-version", "12"); - - Response handshakeResponse = nanoWebSocketServer.serve(session); + public void testHandshakeReturnsResponseWithExpectedHeaders() { + Response handshakeResponse = this.nanoWebSocketServer.serve(this.session); assertNotNull(handshakeResponse); - assertEquals(Response.Status.BAD_REQUEST, handshakeResponse.getStatus()); + + assertEquals(handshakeResponse.getHeader(NanoWebSocketServer.HEADER_WEBSOCKET_ACCEPT), "HSmrc0sMlYUkAGmm5OPpG2HaGWk="); + assertEquals(handshakeResponse.getHeader(NanoWebSocketServer.HEADER_WEBSOCKET_PROTOCOL), "chat"); } @Test public void testMissingKeyReturnsErrorResponse() { - headers.remove("sec-websocket-key"); + this.headers.remove("sec-websocket-key"); - Response handshakeResponse = nanoWebSocketServer.serve(session); + Response handshakeResponse = this.nanoWebSocketServer.serve(this.session); assertNotNull(handshakeResponse); assertEquals(Response.Status.BAD_REQUEST, handshakeResponse.getStatus()); } @Test - public void testWrongUpgradeHeaderReturnsNullResponse() { - headers.put("upgrade", "not a websocket"); - Response handshakeResponse = nanoWebSocketServer.serve(session); + public void testWrongConnectionHeaderReturnsNullResponse() { + this.headers.put("connection", "Junk"); + Response handshakeResponse = this.nanoWebSocketServer.serve(this.session); assertNull(handshakeResponse.getHeader(NanoWebSocketServer.HEADER_UPGRADE)); } @Test - public void testWrongConnectionHeaderReturnsNullResponse() { - headers.put("connection", "Junk"); - Response handshakeResponse = nanoWebSocketServer.serve(session); + public void testWrongUpgradeHeaderReturnsNullResponse() { + this.headers.put("upgrade", "not a websocket"); + Response handshakeResponse = this.nanoWebSocketServer.serve(this.session); assertNull(handshakeResponse.getHeader(NanoWebSocketServer.HEADER_UPGRADE)); } @Test - public void testConnectionHeaderHandlesKeepAlive_FixingFirefoxConnectIssue() { - headers.put("connection", "keep-alive, Upgrade"); - Response handshakeResponse = nanoWebSocketServer.serve(session); + public void testWrongWebsocketVersionReturnsErrorResponse() { + this.headers.put("sec-websocket-version", "12"); + + Response handshakeResponse = this.nanoWebSocketServer.serve(this.session); assertNotNull(handshakeResponse); + assertEquals(Response.Status.BAD_REQUEST, handshakeResponse.getStatus()); } } diff --git a/websocket/src/test/java/fi/iki/elonen/samples/echo/EchoWebSocketsTest.java b/websocket/src/test/java/fi/iki/elonen/samples/echo/EchoWebSocketsTest.java index 96945f4..ff8b1d6 100644 --- a/websocket/src/test/java/fi/iki/elonen/samples/echo/EchoWebSocketsTest.java +++ b/websocket/src/test/java/fi/iki/elonen/samples/echo/EchoWebSocketsTest.java @@ -8,18 +8,18 @@ package fi.iki.elonen.samples.echo; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -51,13 +51,13 @@ public class EchoWebSocketsTest { @BeforeClass public static void setUp() throws Exception { - server = new DebugWebSocketServer(9191, true); - server.start(); + EchoWebSocketsTest.server = new DebugWebSocketServer(9191, true); + EchoWebSocketsTest.server.start(); } @AfterClass public static void tearDown() throws Exception { - server.stop(); + EchoWebSocketsTest.server.stop(); } @Test diff --git a/websocket/src/test/java/fi/iki/elonen/samples/echo/SimpleEchoSocket.java b/websocket/src/test/java/fi/iki/elonen/samples/echo/SimpleEchoSocket.java index af33f84..ca5023c 100644 --- a/websocket/src/test/java/fi/iki/elonen/samples/echo/SimpleEchoSocket.java +++ b/websocket/src/test/java/fi/iki/elonen/samples/echo/SimpleEchoSocket.java @@ -8,18 +8,18 @@ package fi.iki.elonen.samples.echo; * %% * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * + * * 3. Neither the name of the nanohttpd nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -54,21 +54,10 @@ public class SimpleEchoSocket { private final List<String> receivedMessages = new ArrayList<String>(); - public List<String> getReceivedMessages() { - return receivedMessages; - } - - public List<String> getToSendMessages() { - return toSendMessages; - } - private final List<String> toSendMessages = new ArrayList<String>(); private final CountDownLatch closeLatch; - @SuppressWarnings("unused") - private Session session; - public SimpleEchoSocket() { this.closeLatch = new CountDownLatch(1); } @@ -77,21 +66,27 @@ public class SimpleEchoSocket { return this.closeLatch.await(duration, unit); } + public List<String> getReceivedMessages() { + return this.receivedMessages; + } + + public List<String> getToSendMessages() { + return this.toSendMessages; + } + @OnWebSocketClose public void onClose(int statusCode, String reason) { System.out.printf("Connection closed: %d - %s%n", statusCode, reason); - this.session = null; this.closeLatch.countDown(); } @OnWebSocketConnect public void onConnect(Session session) { System.out.printf("Got connect: %s%n", session); - this.session = session; try { Future<Void> fut; - for (String message : toSendMessages) { + for (String message : this.toSendMessages) { fut = session.getRemote().sendStringByFuture(message); fut.get(5, TimeUnit.SECONDS); } @@ -104,6 +99,6 @@ public class SimpleEchoSocket { @OnWebSocketMessage public void onMessage(String msg) { System.out.printf("Got msg: %s%n", msg); - receivedMessages.add(msg); + this.receivedMessages.add(msg); } } |