@Test public void testClose_server1() throws IOException { final Polling polling = Mockito.spy(new Polling(new Object())); final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.doAnswer(invocationOnMock -> "GET").when(request).getMethod(); Mockito.doAnswer(invocationOnMock -> { final HashMap<String, String> queryMap = new HashMap<>(); queryMap.put("transport", Polling.NAME); return queryMap; }).when(request).getAttribute("query"); final HttpServletResponseImpl response = new HttpServletResponseImpl(); polling.close(); polling.onRequest(request, response); Mockito.verify(polling, Mockito.times(1)) .emit(Mockito.eq("drain")); Mockito.verify(polling, Mockito.times(1)) .emit(Mockito.eq("close")); final String responseString = new String(response.getByteOutputStream().toByteArray(), StandardCharsets.UTF_8); ServerParser.decodePayload(responseString, (packet, index, total) -> { assertEquals(2, total); if (index == 0) { assertEquals(Packet.NOOP, packet.type); } else if (index == 1) { assertEquals(Packet.CLOSE, packet.type); } return true; }); }
@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; }