@Test public void testOnRequest_data() { final String messageData = "Test Data"; final Polling polling = Mockito.spy(new Polling(new Object())); polling.on("packet", args -> { final Packet<?> packet = (Packet<Object>) args[0]; assertEquals(Packet.MESSAGE, packet.type); assertEquals(messageData, packet.data); }); final Packet<String> requestPacket = new Packet<>(Packet.MESSAGE, messageData); ServerParser.encodePayloadAsBinary(new ArrayList<Packet<?>>() {{ add(requestPacket); }}, data -> { final ByteArrayInputStream requestInputStream = new ByteArrayInputStream(data); final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.doAnswer(invocationOnMock -> "POST").when(request).getMethod(); Mockito.doAnswer(invocationOnMock -> { final HashMap<String, String> queryMap = new HashMap<>(); queryMap.put("transport", Polling.NAME); return queryMap; }).when(request).getAttribute("query"); Mockito.doAnswer(invocationOnMock -> "application/octet-stream").when(request).getContentType(); Mockito.doAnswer(invocationOnMock -> data.length).when(request).getContentLength(); try { Mockito.doAnswer(invocationOnMock -> new ServletInputStreamWrapper(requestInputStream)).when(request).getInputStream(); } catch (IOException ignore) { } final HttpServletResponseImpl response = new HttpServletResponseImpl(); try { polling.onRequest(request, response); } catch (IOException ignore) { } Mockito.verify(polling, Mockito.times(1)) .emit(Mockito.eq("packet"), Mockito.any(Packet.class)); }); }
@Override public void onRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { synchronized (mLockObject) { switch (request.getMethod().toLowerCase()) { case "get": onPollRequest(request, response); break; case "post": onDataRequest(request, response); break; default: response.setStatus(500); response.getWriter().write(""); break; } } }
Polling extends Transport implements AsyncListener { @Override public void onRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { synchronized (mLockObject) { switch (request.getMethod().toLowerCase()) { case "get": onPollRequest(request, response); break; case "post": onDataRequest(request, response); break; default: response.setStatus(500); response.getWriter().write(""); break; } } } }
Polling extends Transport implements AsyncListener { @Override public void onRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { synchronized (mLockObject) { switch (request.getMethod().toLowerCase()) { case "get": onPollRequest(request, response); break; case "post": onDataRequest(request, response); break; default: response.setStatus(500); response.getWriter().write(""); break; } } } Polling(Object lockObject); }
Polling extends Transport implements AsyncListener { @Override public void onRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { synchronized (mLockObject) { switch (request.getMethod().toLowerCase()) { case "get": onPollRequest(request, response); break; case "post": onDataRequest(request, response); break; default: response.setStatus(500); response.getWriter().write(""); break; } } } Polling(Object lockObject); @Override void onRequest(HttpServletRequest request, HttpServletResponse response); @Override void send(List<Packet<?>> packets); @Override boolean isWritable(); @Override String getName(); @Override void onStartAsync(AsyncEvent asyncEvent); @Override void onComplete(AsyncEvent asyncEvent); @Override void onTimeout(AsyncEvent asyncEvent); @Override void onError(AsyncEvent asyncEvent); }
Polling extends Transport implements AsyncListener { @Override public void onRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { synchronized (mLockObject) { switch (request.getMethod().toLowerCase()) { case "get": onPollRequest(request, response); break; case "post": onDataRequest(request, response); break; default: response.setStatus(500); response.getWriter().write(""); break; } } } Polling(Object lockObject); @Override void onRequest(HttpServletRequest request, HttpServletResponse response); @Override void send(List<Packet<?>> packets); @Override boolean isWritable(); @Override String getName(); @Override void onStartAsync(AsyncEvent asyncEvent); @Override void onComplete(AsyncEvent asyncEvent); @Override void onTimeout(AsyncEvent asyncEvent); @Override void onError(AsyncEvent asyncEvent); static final String NAME; }