@Override protected void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response, boolean initialRequest) throws IOException { if (initialRequest) { writeFrame(SockJsFrame.openFrame()); } else if (!getMessageCache().isEmpty()) { flushCache(); } else { scheduleHeartbeat(); } }
@Override protected void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response, boolean initialRequest) throws IOException { byte[] prelude = getPrelude(request); response.getBody().write(prelude); response.flush(); if (initialRequest) { writeFrame(SockJsFrame.openFrame()); } flushCache(); }
@Test public void handleFrameOpenWhenStatusNotNew() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); assertThat(this.session.isOpen(), is(true)); this.session.handleFrame(SockJsFrame.openFrame().getContent()); assertThat(this.session.disconnectStatus, equalTo(new CloseStatus(1006, "Server lost session"))); }
@Test public void handleFrameOpenWithWebSocketHandlerException() throws Exception { willThrow(new IllegalStateException("Fake error")).given(this.handler).afterConnectionEstablished(this.session); this.session.handleFrame(SockJsFrame.openFrame().getContent()); assertThat(this.session.isOpen(), is(true)); }
@Test public void send() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.sendMessage(new TextMessage("foo")); assertThat(this.session.sentMessage, equalTo(new TextMessage("[\"foo\"]"))); }
@Test public void closeWithStatus() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.close(new CloseStatus(3000, "reason")); assertThat(this.session.disconnectStatus, equalTo(new CloseStatus(3000, "reason"))); }
@Test public void handleFrameMessageWithBadData() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.handleFrame("a['bad data"); assertThat(this.session.isOpen(), equalTo(false)); assertThat(this.session.disconnectStatus, equalTo(CloseStatus.BAD_DATA)); verify(this.handler).afterConnectionEstablished(this.session); verifyNoMoreInteractions(this.handler); }
@Test public void close() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.close(); assertThat(this.session.isOpen(), equalTo(false)); assertThat(this.session.disconnectStatus, equalTo(CloseStatus.NORMAL)); verify(this.handler).afterConnectionEstablished(this.session); verifyNoMoreInteractions(this.handler); }
@Test public void closeWithNullStatus() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("Invalid close status"); this.session.close(null); }
@Test public void closeWithStatusOutOfRange() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("Invalid close status"); this.session.close(new CloseStatus(2999, "reason")); }
@Test public void handleFrameClose() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.handleFrame(SockJsFrame.closeFrame(1007, "").getContent()); assertThat(this.session.isOpen(), equalTo(false)); assertThat(this.session.disconnectStatus, equalTo(new CloseStatus(1007, ""))); verify(this.handler).afterConnectionEstablished(this.session); verifyNoMoreInteractions(this.handler); }
@Test public void writeFrameIoException() throws Exception { this.session.setExceptionOnWrite(new IOException()); this.session.delegateConnectionEstablished(); try { this.session.writeFrame(SockJsFrame.openFrame()); fail("expected exception"); } catch (SockJsTransportFailureException ex) { assertEquals(CloseStatus.SERVER_ERROR, this.session.getCloseStatus()); verify(this.webSocketHandler).afterConnectionClosed(this.session, CloseStatus.SERVER_ERROR); } }
@Test public void openFrame() { SockJsFrame frame = SockJsFrame.openFrame(); assertEquals("o", frame.getContent()); assertEquals(SockJsFrameType.OPEN, frame.getType()); assertNull(frame.getFrameData()); }
@Test public void handleFrameOpen() throws Exception { assertThat(this.session.isOpen(), is(false)); this.session.handleFrame(SockJsFrame.openFrame().getContent()); assertThat(this.session.isOpen(), is(true)); assertTrue(this.connectFuture.isDone()); assertThat(this.connectFuture.get(), sameInstance(this.session)); verify(this.handler).afterConnectionEstablished(this.session); verifyNoMoreInteractions(this.handler); }
@Test public void afterTransportClosed() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.afterTransportClosed(CloseStatus.SERVER_ERROR); assertThat(this.session.isOpen(), equalTo(false)); verify(this.handler).afterConnectionEstablished(this.session); verify(this.handler).afterConnectionClosed(this.session, CloseStatus.SERVER_ERROR); verifyNoMoreInteractions(this.handler); }
@Test public void handleFrameMessageWhenNotOpen() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.close(); reset(this.handler); this.session.handleFrame(SockJsFrame.messageFrame(CODEC, "foo", "bar").getContent()); verifyNoMoreInteractions(this.handler); }
@Test public void handleFrameMessageWithWebSocketHandlerException() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); willThrow(new IllegalStateException("Fake error")).given(this.handler) .handleMessage(this.session, new TextMessage("foo")); willThrow(new IllegalStateException("Fake error")).given(this.handler) .handleMessage(this.session, new TextMessage("bar")); this.session.handleFrame(SockJsFrame.messageFrame(CODEC, "foo", "bar").getContent()); assertThat(this.session.isOpen(), equalTo(true)); verify(this.handler).afterConnectionEstablished(this.session); verify(this.handler).handleMessage(this.session, new TextMessage("foo")); verify(this.handler).handleMessage(this.session, new TextMessage("bar")); verifyNoMoreInteractions(this.handler); }
@Test public void handleFrameMessage() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.handleFrame(SockJsFrame.messageFrame(CODEC, "foo", "bar").getContent()); verify(this.handler).afterConnectionEstablished(this.session); verify(this.handler).handleMessage(this.session, new TextMessage("foo")); verify(this.handler).handleMessage(this.session, new TextMessage("bar")); verifyNoMoreInteractions(this.handler); }
@Test public void frameFormats() throws Exception { this.servletRequest.setQueryString("c=callback"); this.servletRequest.addParameter("c", "callback"); SockJsFrame frame = SockJsFrame.openFrame(); SockJsFrameFormat format = new XhrPollingTransportHandler().getFrameFormat(this.request); String formatted = format.format(frame); assertEquals(frame.getContent() + "\n", formatted); format = new XhrStreamingTransportHandler().getFrameFormat(this.request); formatted = format.format(frame); assertEquals(frame.getContent() + "\n", formatted); format = new HtmlFileTransportHandler().getFrameFormat(this.request); formatted = format.format(frame); assertEquals("<script>\np(\"" + frame.getContent() + "\");\n</script>\r\n", formatted); format = new EventSourceTransportHandler().getFrameFormat(this.request); formatted = format.format(frame); assertEquals("data: " + frame.getContent() + "\r\n\r\n", formatted); }