Skip to content

Commit ef61e1a

Browse files
committed
Fixes #2505
1 parent 8a6e531 commit ef61e1a

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import javax.websocket.Session;
3939
import javax.websocket.server.HandshakeRequest;
4040
import java.io.IOException;
41-
import java.lang.reflect.Method;
4241
import java.net.InetSocketAddress;
4342
import java.net.URI;
4443
import java.nio.Buffer;
@@ -226,11 +225,13 @@ public void onOpen(Session session, final EndpointConfig endpointConfig) {
226225
cookies.addAll(CookieUtil.ServerCookieDecoder.STRICT.decode(cookieHeader));
227226
}
228227

229-
Enumeration<String> attributeNames = handshakeSession.getAttributeNames();
230-
Map<String, Object> attributes = new ConcurrentHashMap<>();
231-
while (attributeNames.hasMoreElements()) {
232-
String attributeName = attributeNames.nextElement();
233-
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+
}
234235
}
235236

236237
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
@@ -16,11 +16,11 @@
1616
package org.atmosphere.container.version;
1717

1818
import org.atmosphere.container.JSR356Endpoint;
19-
import org.atmosphere.cpr.ApplicationConfig;
2019
import org.atmosphere.cpr.AtmosphereConfig;
2120
import org.atmosphere.cpr.AtmosphereFramework;
2221
import org.atmosphere.cpr.AtmosphereRequest;
2322
import org.atmosphere.websocket.WebSocketProcessor;
23+
import org.mockito.Mockito;
2424
import org.mockito.invocation.InvocationOnMock;
2525
import org.mockito.stubbing.Answer;
2626
import org.testng.annotations.BeforeMethod;
@@ -34,18 +34,23 @@
3434
import javax.websocket.SendResult;
3535
import javax.websocket.Session;
3636
import javax.websocket.server.HandshakeRequest;
37+
import javax.websocket.server.ServerEndpointConfig;
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)