55
55
import static org .mockito .Mockito .verify ;
56
56
import static org .mockito .Mockito .when ;
57
57
import static org .testng .Assert .assertEquals ;
58
+ import static org .testng .Assert .assertTrue ;
58
59
59
60
public class JSR356WebSocketTest {
60
61
@@ -220,6 +221,57 @@ public void testOnOpenWithNullHandshakeSession() throws IOException {
220
221
endpoint .onOpen (mockSession , mockConfig );
221
222
222
223
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 );
223
274
275
+ assertTrue (request .isSecure ());
224
276
}
225
- }
277
+ }
0 commit comments