@Test @SuppressWarnings("resource") public void getPrincipalWithConstructorArg() { TestPrincipal user = new TestPrincipal("joe"); JettyWebSocketSession session = new JettyWebSocketSession(attributes, user); assertSame(user, session.getPrincipal()); }
@Test @SuppressWarnings("resource") public void getPrincipalWithConstructorArg() { TestPrincipal user = new TestPrincipal("joe"); StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null, user); assertSame(user, session.getPrincipal()); }
@Test @SuppressWarnings("resource") public void getPrincipalWithNativeSession() { TestPrincipal user = new TestPrincipal("joe"); Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUserPrincipal()).willReturn(user); StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null); session.initializeNativeSession(nativeSession); assertSame(user, session.getPrincipal()); }
@Test @SuppressWarnings("resource") public void getPrincipalFromNativeSession() { TestPrincipal user = new TestPrincipal("joe"); UpgradeRequest request = Mockito.mock(UpgradeRequest.class); given(request.getUserPrincipal()).willReturn(user); UpgradeResponse response = Mockito.mock(UpgradeResponse.class); given(response.getAcceptedSubProtocol()).willReturn(null); Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUpgradeRequest()).willReturn(request); given(nativeSession.getUpgradeResponse()).willReturn(response); JettyWebSocketSession session = new JettyWebSocketSession(attributes); session.initializeNativeSession(nativeSession); reset(nativeSession); assertSame(user, session.getPrincipal()); verifyNoMoreInteractions(nativeSession); }
@Test public void handleTransportRequestXhrSendWithDifferentUser() throws Exception { String sockJsPath = sessionUrlPrefix + "xhr"; setRequest("POST", sockJsPrefix + sockJsPath); this.service.handleRequest(this.request, this.response, sockJsPath, this.wsHandler); assertEquals(200, this.servletResponse.getStatus()); // session created verify(this.xhrHandler).handleRequest(this.request, this.response, this.wsHandler, this.session); this.session.setPrincipal(new TestPrincipal("little red riding hood")); this.servletRequest.setUserPrincipal(new TestPrincipal("wolf")); resetResponse(); reset(this.xhrSendHandler); sockJsPath = sessionUrlPrefix + "xhr_send"; setRequest("POST", sockJsPrefix + sockJsPath); this.service.handleRequest(this.request, this.response, sockJsPath, this.wsHandler); assertEquals(404, this.servletResponse.getStatus()); verifyNoMoreInteractions(this.xhrSendHandler); }