Skip to content

Commit 32b20ab

Browse files
committed
Copy isSecure() property from JSR356 Session
Without this, it's impossible to know if a connection is done using SSL or not.
1 parent 260de9f commit 32b20ab

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

modules/cpr/src/main/java/org/atmosphere/container/JSR356Endpoint.java

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ public void onOpen(Session session, final EndpointConfig endpointConfig) {
248248
.remoteInetSocketAddress((Callable<InetSocketAddress>) () -> (InetSocketAddress) endpointConfig.getUserProperties().get(JAVAX_WEBSOCKET_ENDPOINT_REMOTE_ADDRESS))
249249
.localInetSocketAddress((Callable<InetSocketAddress>) () -> (InetSocketAddress) endpointConfig.getUserProperties().get(JAVAX_WEBSOCKET_ENDPOINT_LOCAL_ADDRESS))
250250
.attributes(attributes)
251+
.isSSecure(session.isSecure())
251252
.build()
252253
.queryString(session.getQueryString());
253254

modules/cpr/src/test/java/org/atmosphere/container/version/JSR356WebSocketTest.java

+53-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import static org.mockito.Mockito.verify;
5656
import static org.mockito.Mockito.when;
5757
import static org.testng.Assert.assertEquals;
58+
import static org.testng.Assert.assertTrue;
5859

5960
public class JSR356WebSocketTest {
6061

@@ -220,6 +221,57 @@ public void testOnOpenWithNullHandshakeSession() throws IOException {
220221
endpoint.onOpen(mockSession, mockConfig);
221222

222223
verify(mockSession, never()).close(Mockito.any());
224+
}
225+
226+
@Test
227+
public void testSecureRequestPropertyPropagated() throws NoSuchFieldException, IllegalAccessException {
228+
Session mockSession = Mockito.mock(Session.class);
229+
HttpSession mockHttpSession = mock(HttpSession.class);
230+
HandshakeRequest mockHandshakeRequest = Mockito.mock(HandshakeRequest.class);
231+
ServerEndpointConfig mockConfig = Mockito.mock(ServerEndpointConfig.class);
232+
WebSocketProcessor mockProcessor = Mockito.mock(WebSocketProcessor.class);
233+
RemoteEndpoint.Async mockAsyncRemote = Mockito.mock(RemoteEndpoint.Async.class);
234+
235+
AtmosphereFramework mockFramework = mock(AtmosphereFramework.class);
236+
AtmosphereConfig mockAtmosphereConfig = mock(AtmosphereConfig.class);
237+
238+
when(mockProcessor.handshake(Mockito.any(AtmosphereRequest.class))).thenReturn(true);
239+
when(mockFramework.getAtmosphereConfig()).thenReturn(mockAtmosphereConfig);
240+
when(mockFramework.getServletContext()).thenReturn(mock(ServletContext.class));
241+
when(mockAtmosphereConfig.getInitParameter(JSR356_MAPPING_PATH)).thenReturn("/");
242+
when(mockAtmosphereConfig.getServletContext()).thenReturn(mock(ServletContext.class));
243+
when(mockFramework.getAtmosphereConfig()).thenReturn(mockAtmosphereConfig);
244+
when(mockFramework.getServletContext()).thenReturn(mock(ServletContext.class));
245+
when(mockAtmosphereConfig.getInitParameter(JSR356_MAPPING_PATH)).thenReturn("/");
246+
when(mockAtmosphereConfig.getServletContext()).thenReturn(mock(ServletContext.class));
247+
248+
Map<String, Object> sessionAttributes = new HashMap<>();
249+
sessionAttributes.put("attribute1", "value1");
250+
sessionAttributes.put("attribute2", "value2");
251+
252+
when(mockHandshakeRequest.getHttpSession()).thenReturn(mockHttpSession);
253+
when(mockHttpSession.getAttributeNames()).thenReturn(new Vector<>(sessionAttributes.keySet()).elements());
254+
when(mockHttpSession.getAttribute("attribute1")).thenReturn(sessionAttributes.get("attribute1"));
255+
when(mockHttpSession.getAttribute("attribute2")).thenReturn(sessionAttributes.get("attribute2"));
256+
257+
JSR356Endpoint endpoint = new JSR356Endpoint(mockFramework, mockProcessor);
258+
259+
when(mockSession.getAsyncRemote()).thenReturn(mockAsyncRemote);
260+
when(mockSession.isOpen()).thenReturn(true);
261+
when(mockSession.getRequestURI()).thenReturn(URI.create("/"));
262+
when(mockSession.isSecure()).thenReturn(true);
263+
264+
when(mockHandshakeRequest.getHttpSession()).thenReturn(mockHttpSession);
265+
when(mockHandshakeRequest.getHeaders()).thenReturn(Collections.emptyMap());
266+
267+
endpoint.handshakeRequest(mockHandshakeRequest);
268+
269+
endpoint.onOpen(mockSession, mockConfig);
270+
271+
Field requestField = JSR356Endpoint.class.getDeclaredField("request");
272+
requestField.setAccessible(true);
273+
AtmosphereRequest request = (AtmosphereRequest) requestField.get(endpoint);
223274

275+
assertTrue(request.isSecure());
224276
}
225-
}
277+
}

0 commit comments

Comments
 (0)