@Override protected void writeFrameInternal(SockJsFrame frame) throws IOException { Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized"); if (logger.isTraceEnabled()) { logger.trace("Writing " + frame); } TextMessage message = new TextMessage(frame.getContent()); this.webSocketSession.sendMessage(message); }
@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 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 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 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 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 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 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 messageArrayFrameEmpty() { SockJsFrame frame = new SockJsFrame("a"); assertEquals("a[]", frame.getContent()); assertEquals(SockJsFrameType.MESSAGE, frame.getType()); assertEquals("[]", frame.getFrameData()); frame = new SockJsFrame("a[]"); assertEquals("a[]", frame.getContent()); assertEquals(SockJsFrameType.MESSAGE, frame.getType()); assertEquals("[]", frame.getFrameData()); }
@Test public void closeFrameEmpty() { SockJsFrame frame = new SockJsFrame("c"); assertEquals("c[]", frame.getContent()); assertEquals(SockJsFrameType.CLOSE, frame.getType()); assertEquals("[]", frame.getFrameData()); frame = new SockJsFrame("c[]"); assertEquals("c[]", frame.getContent()); assertEquals(SockJsFrameType.CLOSE, frame.getType()); assertEquals("[]", frame.getFrameData()); }
@Test public void closeFrame() { SockJsFrame frame = SockJsFrame.closeFrame(3000, "Go Away!"); assertEquals("c[3000,\"Go Away!\"]", frame.getContent()); assertEquals(SockJsFrameType.CLOSE, frame.getType()); assertEquals("[3000,\"Go Away!\"]", frame.getFrameData()); }
@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 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 openFrame() { SockJsFrame frame = SockJsFrame.openFrame(); assertEquals("o", frame.getContent()); assertEquals(SockJsFrameType.OPEN, frame.getType()); assertNull(frame.getFrameData()); }
@Test public void heartbeatFrame() { SockJsFrame frame = SockJsFrame.heartbeatFrame(); assertEquals("h", frame.getContent()); assertEquals(SockJsFrameType.HEARTBEAT, frame.getType()); assertNull(frame.getFrameData()); }
@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 messageArrayFrame() { SockJsFrame frame = SockJsFrame.messageFrame(new Jackson2SockJsMessageCodec(), "m1", "m2"); assertEquals("a[\"m1\",\"m2\"]", frame.getContent()); assertEquals(SockJsFrameType.MESSAGE, frame.getType()); assertEquals("[\"m1\",\"m2\"]", 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); }