diff options
author | ritchie <ritchie@gmx.at> | 2015-05-14 09:02:50 +0200 |
---|---|---|
committer | ritchie <ritchie@gmx.at> | 2015-05-14 09:02:50 +0200 |
commit | 5e2e2f19e06b9f4a01c6da83a2297eb18fe2b546 (patch) | |
tree | ebf8b2e1d2cd0516de354b3f417ac79f905278dc /websocket | |
parent | 6a77bee0dad6e34e9789b5065c4367286f619efd (diff) | |
download | nanohttpd-5e2e2f19e06b9f4a01c6da83a2297eb18fe2b546.tar.gz |
reponces now created by factorymethods that can be overwritten, solves
#94
Diffstat (limited to 'websocket')
6 files changed, 76 insertions, 78 deletions
diff --git a/websocket/src/main/java/fi/iki/elonen/NanoWebSocketServer.java b/websocket/src/main/java/fi/iki/elonen/NanoWebSocketServer.java index a37d194..979b2f3 100644 --- a/websocket/src/main/java/fi/iki/elonen/NanoWebSocketServer.java +++ b/websocket/src/main/java/fi/iki/elonen/NanoWebSocketServer.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. @@ -739,7 +739,7 @@ public abstract class NanoWebSocketServer extends NanoHTTPD { * hast java.util.Base64, I have this from stackoverflow: * http://stackoverflow.com/a/4265472 * </p> - * + * * @param buf * the byte array (not null) * @return the translated Base64 string (not null) @@ -825,12 +825,12 @@ public abstract class NanoWebSocketServer extends NanoHTTPD { Map<String, String> headers = session.getHeaders(); if (isWebsocketRequested(session)) { if (!NanoWebSocketServer.HEADER_WEBSOCKET_VERSION_VALUE.equalsIgnoreCase(headers.get(NanoWebSocketServer.HEADER_WEBSOCKET_VERSION))) { - return new Response(Response.Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "Invalid Websocket-Version " - + headers.get(NanoWebSocketServer.HEADER_WEBSOCKET_VERSION)); + return newFixedLengthResponse(Response.Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, + "Invalid Websocket-Version " + headers.get(NanoWebSocketServer.HEADER_WEBSOCKET_VERSION)); } if (!headers.containsKey(NanoWebSocketServer.HEADER_WEBSOCKET_KEY)) { - return new Response(Response.Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "Missing Websocket-Key"); + return newFixedLengthResponse(Response.Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "Missing Websocket-Key"); } WebSocket webSocket = openWebSocket(session); @@ -838,7 +838,8 @@ public abstract class NanoWebSocketServer extends NanoHTTPD { try { handshakeResponse.addHeader(NanoWebSocketServer.HEADER_WEBSOCKET_ACCEPT, makeAcceptKey(headers.get(NanoWebSocketServer.HEADER_WEBSOCKET_KEY))); } catch (NoSuchAlgorithmException e) { - return new Response(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "The SHA-1 Algorithm required for websockets is not available on the server."); + return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, + "The SHA-1 Algorithm required for websockets is not available on the server."); } if (headers.containsKey(NanoWebSocketServer.HEADER_WEBSOCKET_PROTOCOL)) { diff --git a/websocket/src/main/java/fi/iki/elonen/samples/echo/DebugWebSocketServer.java b/websocket/src/main/java/fi/iki/elonen/samples/echo/DebugWebSocketServer.java index 8f6aa78..27226ee 100644 --- a/websocket/src/main/java/fi/iki/elonen/samples/echo/DebugWebSocketServer.java +++ b/websocket/src/main/java/fi/iki/elonen/samples/echo/DebugWebSocketServer.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. diff --git a/websocket/src/main/java/fi/iki/elonen/samples/echo/EchoSocketSample.java b/websocket/src/main/java/fi/iki/elonen/samples/echo/EchoSocketSample.java index 3060146..cdd5acb 100644 --- a/websocket/src/main/java/fi/iki/elonen/samples/echo/EchoSocketSample.java +++ b/websocket/src/main/java/fi/iki/elonen/samples/echo/EchoSocketSample.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. 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); } } |