Skip to content

Commit e2c37ec

Browse files
committed
Fixes #2505
1 parent 11761b4 commit e2c37ec

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,13 @@ public void onOpen(Session session, final EndpointConfig endpointConfig) {
225225
cookies.addAll(CookieUtil.ServerCookieDecoder.STRICT.decode(cookieHeader));
226226
}
227227

228-
Enumeration<String> attributeNames = handshakeSession.getAttributeNames();
229-
Map<String, Object> attributes = new ConcurrentHashMap<>();
230-
while (attributeNames.hasMoreElements()) {
231-
String attributeName = attributeNames.nextElement();
232-
attributes.put(attributeName, handshakeSession.getAttribute(attributeName));
228+
final Map<String, Object> attributes = new ConcurrentHashMap<>();
229+
if (handshakeSession != null) {
230+
Enumeration<String> attributeNames = handshakeSession.getAttributeNames();
231+
while (attributeNames.hasMoreElements()) {
232+
String attributeName = attributeNames.nextElement();
233+
attributes.put(attributeName, handshakeSession.getAttribute(attributeName));
234+
}
233235
}
234236

235237
request = new AtmosphereRequestImpl.Builder()

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

+45-3
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,34 @@
2323
import jakarta.websocket.SendResult;
2424
import jakarta.websocket.Session;
2525
import jakarta.websocket.server.HandshakeRequest;
26+
import jakarta.websocket.server.ServerEndpointConfig;
2627
import org.atmosphere.container.JSR356Endpoint;
27-
import org.atmosphere.cpr.ApplicationConfig;
2828
import org.atmosphere.cpr.AtmosphereConfig;
2929
import org.atmosphere.cpr.AtmosphereFramework;
3030
import org.atmosphere.cpr.AtmosphereRequest;
3131
import org.atmosphere.websocket.WebSocketProcessor;
32+
import org.mockito.Mockito;
3233
import org.mockito.invocation.InvocationOnMock;
3334
import org.mockito.stubbing.Answer;
3435
import org.testng.annotations.BeforeMethod;
3536
import org.testng.annotations.Test;
3637

38+
import java.io.IOException;
3739
import java.lang.reflect.Field;
3840
import java.net.URI;
41+
import java.util.Collections;
3942
import java.util.Enumeration;
4043
import java.util.HashMap;
4144
import java.util.Map;
4245
import java.util.Vector;
4346

47+
import static org.atmosphere.cpr.ApplicationConfig.JSR356_MAPPING_PATH;
4448
import static org.mockito.ArgumentMatchers.any;
4549
import static org.mockito.ArgumentMatchers.anyString;
4650
import static org.mockito.ArgumentMatchers.eq;
4751
import static org.mockito.Mockito.doAnswer;
4852
import static org.mockito.Mockito.mock;
53+
import static org.mockito.Mockito.never;
4954
import static org.mockito.Mockito.times;
5055
import static org.mockito.Mockito.verify;
5156
import static org.mockito.Mockito.when;
@@ -157,10 +162,9 @@ public void testAttributePropagationFromHandshakeSessionToAtmosphereRequest() th
157162
AtmosphereFramework mockFramework = mock(AtmosphereFramework.class);
158163
AtmosphereConfig mockConfig = mock(AtmosphereConfig.class);
159164

160-
// Stub the getAtmosphereConfig and getInitParameter methods
161165
when(mockFramework.getAtmosphereConfig()).thenReturn(mockConfig);
162166
when(mockFramework.getServletContext()).thenReturn(mock(ServletContext.class));
163-
when(mockConfig.getInitParameter(ApplicationConfig.JSR356_MAPPING_PATH)).thenReturn("/");
167+
when(mockConfig.getInitParameter(JSR356_MAPPING_PATH)).thenReturn("/");
164168
when(mockConfig.getServletContext()).thenReturn(mock(ServletContext.class));
165169

166170
WebSocketProcessor webSocketProcessor = mock(WebSocketProcessor.class);
@@ -180,4 +184,42 @@ public void testAttributePropagationFromHandshakeSessionToAtmosphereRequest() th
180184
"Attribute value should match the value from the HttpSession");
181185
}
182186
}
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+
}
183225
}

0 commit comments

Comments
 (0)