23
23
import jakarta .websocket .SendResult ;
24
24
import jakarta .websocket .Session ;
25
25
import jakarta .websocket .server .HandshakeRequest ;
26
+ import jakarta .websocket .server .ServerEndpointConfig ;
26
27
import org .atmosphere .container .JSR356Endpoint ;
27
- import org .atmosphere .cpr .ApplicationConfig ;
28
28
import org .atmosphere .cpr .AtmosphereConfig ;
29
29
import org .atmosphere .cpr .AtmosphereFramework ;
30
30
import org .atmosphere .cpr .AtmosphereRequest ;
31
31
import org .atmosphere .websocket .WebSocketProcessor ;
32
+ import org .mockito .Mockito ;
32
33
import org .mockito .invocation .InvocationOnMock ;
33
34
import org .mockito .stubbing .Answer ;
34
35
import org .testng .annotations .BeforeMethod ;
35
36
import org .testng .annotations .Test ;
36
37
38
+ import java .io .IOException ;
37
39
import java .lang .reflect .Field ;
38
40
import java .net .URI ;
41
+ import java .util .Collections ;
39
42
import java .util .Enumeration ;
40
43
import java .util .HashMap ;
41
44
import java .util .Map ;
42
45
import java .util .Vector ;
43
46
47
+ import static org .atmosphere .cpr .ApplicationConfig .JSR356_MAPPING_PATH ;
44
48
import static org .mockito .ArgumentMatchers .any ;
45
49
import static org .mockito .ArgumentMatchers .anyString ;
46
50
import static org .mockito .ArgumentMatchers .eq ;
47
51
import static org .mockito .Mockito .doAnswer ;
48
52
import static org .mockito .Mockito .mock ;
53
+ import static org .mockito .Mockito .never ;
49
54
import static org .mockito .Mockito .times ;
50
55
import static org .mockito .Mockito .verify ;
51
56
import static org .mockito .Mockito .when ;
@@ -157,10 +162,9 @@ public void testAttributePropagationFromHandshakeSessionToAtmosphereRequest() th
157
162
AtmosphereFramework mockFramework = mock (AtmosphereFramework .class );
158
163
AtmosphereConfig mockConfig = mock (AtmosphereConfig .class );
159
164
160
- // Stub the getAtmosphereConfig and getInitParameter methods
161
165
when (mockFramework .getAtmosphereConfig ()).thenReturn (mockConfig );
162
166
when (mockFramework .getServletContext ()).thenReturn (mock (ServletContext .class ));
163
- when (mockConfig .getInitParameter (ApplicationConfig . JSR356_MAPPING_PATH )).thenReturn ("/" );
167
+ when (mockConfig .getInitParameter (JSR356_MAPPING_PATH )).thenReturn ("/" );
164
168
when (mockConfig .getServletContext ()).thenReturn (mock (ServletContext .class ));
165
169
166
170
WebSocketProcessor webSocketProcessor = mock (WebSocketProcessor .class );
@@ -180,4 +184,42 @@ public void testAttributePropagationFromHandshakeSessionToAtmosphereRequest() th
180
184
"Attribute value should match the value from the HttpSession" );
181
185
}
182
186
}
187
+
188
+ @ Test
189
+ public void testOnOpenWithNullHandshakeSession () throws IOException {
190
+ Session mockSession = Mockito .mock (Session .class );
191
+ HandshakeRequest mockHandshakeRequest = Mockito .mock (HandshakeRequest .class );
192
+ ServerEndpointConfig mockConfig = Mockito .mock (ServerEndpointConfig .class );
193
+ WebSocketProcessor mockProcessor = Mockito .mock (WebSocketProcessor .class );
194
+ RemoteEndpoint .Async mockAsyncRemote = Mockito .mock (RemoteEndpoint .Async .class );
195
+
196
+ AtmosphereFramework mockFramework = mock (AtmosphereFramework .class );
197
+ AtmosphereConfig mockAtmosphereConfig = mock (AtmosphereConfig .class );
198
+
199
+ when (mockProcessor .handshake (Mockito .any (AtmosphereRequest .class ))).thenReturn (true );
200
+ when (mockFramework .getAtmosphereConfig ()).thenReturn (mockAtmosphereConfig );
201
+ when (mockFramework .getServletContext ()).thenReturn (mock (ServletContext .class ));
202
+ when (mockAtmosphereConfig .getInitParameter (JSR356_MAPPING_PATH )).thenReturn ("/" );
203
+ when (mockAtmosphereConfig .getServletContext ()).thenReturn (mock (ServletContext .class ));
204
+ when (mockFramework .getAtmosphereConfig ()).thenReturn (mockAtmosphereConfig );
205
+ when (mockFramework .getServletContext ()).thenReturn (mock (ServletContext .class ));
206
+ when (mockAtmosphereConfig .getInitParameter (JSR356_MAPPING_PATH )).thenReturn ("/" );
207
+ when (mockAtmosphereConfig .getServletContext ()).thenReturn (mock (ServletContext .class ));
208
+
209
+ JSR356Endpoint endpoint = new JSR356Endpoint (mockFramework , mockProcessor );
210
+
211
+ when (mockSession .getAsyncRemote ()).thenReturn (mockAsyncRemote );
212
+ when (mockSession .isOpen ()).thenReturn (true );
213
+ when (mockSession .getRequestURI ()).thenReturn (URI .create ("/" ));
214
+
215
+ when (mockHandshakeRequest .getHttpSession ()).thenReturn (null );
216
+ when (mockHandshakeRequest .getHeaders ()).thenReturn (Collections .emptyMap ());
217
+
218
+ endpoint .handshakeRequest (mockHandshakeRequest );
219
+
220
+ endpoint .onOpen (mockSession , mockConfig );
221
+
222
+ verify (mockSession , never ()).close (Mockito .any ());
223
+
224
+ }
183
225
}
0 commit comments