Search in sources :

Example 1 with UpgradeRequest

use of org.eclipse.jetty.websocket.api.UpgradeRequest in project spring-framework by spring-projects.

the class JettyWebSocketSessionTests method getAcceptedProtocol.

@Test
@SuppressWarnings("resource")
public void getAcceptedProtocol() {
    String protocol = "foo";
    UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
    given(request.getUserPrincipal()).willReturn(null);
    UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
    given(response.getAcceptedSubProtocol()).willReturn(protocol);
    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);
    assertThat(session.getAcceptedProtocol()).isSameAs(protocol);
    verifyNoMoreInteractions(nativeSession);
}
Also used : UpgradeResponse(org.eclipse.jetty.websocket.api.UpgradeResponse) UpgradeRequest(org.eclipse.jetty.websocket.api.UpgradeRequest) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.jupiter.api.Test)

Example 2 with UpgradeRequest

use of org.eclipse.jetty.websocket.api.UpgradeRequest in project jetty.project by eclipse.

the class WebSocketClientTest method testParameterMap.

@Test
public void testParameterMap() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri().resolve("/test?snack=cashews&amount=handful&brand=off");
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection ssocket = server.accept();
    ssocket.upgrade();
    future.get(30, TimeUnit.SECONDS);
    Assert.assertTrue(wsocket.openLatch.await(1, TimeUnit.SECONDS));
    Session session = wsocket.getSession();
    UpgradeRequest req = session.getUpgradeRequest();
    Assert.assertThat("Upgrade Request", req, notNullValue());
    Map<String, List<String>> parameterMap = req.getParameterMap();
    Assert.assertThat("Parameter Map", parameterMap, notNullValue());
    Assert.assertThat("Parameter[snack]", parameterMap.get("snack"), is(Arrays.asList(new String[] { "cashews" })));
    Assert.assertThat("Parameter[amount]", parameterMap.get("amount"), is(Arrays.asList(new String[] { "handful" })));
    Assert.assertThat("Parameter[brand]", parameterMap.get("brand"), is(Arrays.asList(new String[] { "off" })));
    Assert.assertThat("Parameter[cost]", parameterMap.get("cost"), nullValue());
}
Also used : UpgradeRequest(org.eclipse.jetty.websocket.api.UpgradeRequest) IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) List(java.util.List) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 3 with UpgradeRequest

use of org.eclipse.jetty.websocket.api.UpgradeRequest in project jetty.project by eclipse.

the class RequestHeadersTest method testAccessRequestCookies.

@Test
public void testAccessRequestCookies() throws Exception {
    BlockheadClient client = new BlockheadClient(server.getServerUri());
    client.setTimeout(1, TimeUnit.SECONDS);
    try {
        client.connect();
        client.addHeader("Cookie: fruit=Pear; type=Anjou\r\n");
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        UpgradeRequest req = echoCreator.getLastRequest();
        Assert.assertThat("Last Request", req, notNullValue());
        List<HttpCookie> cookies = req.getCookies();
        Assert.assertThat("Request cookies", cookies, notNullValue());
        Assert.assertThat("Request cookies.size", cookies.size(), is(2));
        for (HttpCookie cookie : cookies) {
            Assert.assertThat("Cookie name", cookie.getName(), anyOf(is("fruit"), is("type")));
            Assert.assertThat("Cookie value", cookie.getValue(), anyOf(is("Pear"), is("Anjou")));
        }
    } finally {
        client.close();
    }
}
Also used : BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) UpgradeRequest(org.eclipse.jetty.websocket.api.UpgradeRequest) HttpCookie(java.net.HttpCookie) Test(org.junit.Test)

Example 4 with UpgradeRequest

use of org.eclipse.jetty.websocket.api.UpgradeRequest in project spring-framework by spring-projects.

the class JettyWebSocketSessionTests method getPrincipalNotAvailable.

@Test
@SuppressWarnings("resource")
public void getPrincipalNotAvailable() {
    UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
    given(request.getUserPrincipal()).willReturn(null);
    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);
    assertThat(session.getPrincipal()).isNull();
    verifyNoMoreInteractions(nativeSession);
}
Also used : UpgradeResponse(org.eclipse.jetty.websocket.api.UpgradeResponse) UpgradeRequest(org.eclipse.jetty.websocket.api.UpgradeRequest) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.jupiter.api.Test)

Example 5 with UpgradeRequest

use of org.eclipse.jetty.websocket.api.UpgradeRequest in project spring-framework by spring-projects.

the class JettyWebSocketSessionTests method getPrincipalFromNativeSession.

@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);
    assertThat(session.getPrincipal()).isSameAs(user);
    verifyNoMoreInteractions(nativeSession);
}
Also used : UpgradeResponse(org.eclipse.jetty.websocket.api.UpgradeResponse) TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) UpgradeRequest(org.eclipse.jetty.websocket.api.UpgradeRequest) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.jupiter.api.Test)

Aggregations

UpgradeRequest (org.eclipse.jetty.websocket.api.UpgradeRequest)5 Session (org.eclipse.jetty.websocket.api.Session)4 UpgradeResponse (org.eclipse.jetty.websocket.api.UpgradeResponse)3 Test (org.junit.jupiter.api.Test)3 Test (org.junit.Test)2 HttpCookie (java.net.HttpCookie)1 URI (java.net.URI)1 List (java.util.List)1 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)1 BlockheadClient (org.eclipse.jetty.websocket.common.test.BlockheadClient)1 IBlockheadServerConnection (org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection)1 ServletUpgradeRequest (org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest)1 TestPrincipal (org.springframework.core.testfixture.security.TestPrincipal)1