Skip to content

Commit 4f9388a

Browse files
committed
Add a unit test for #2502
1 parent d54e08e commit 4f9388a

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

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

+66-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,31 @@
1515
*/
1616
package org.atmosphere.container.version;
1717

18+
import org.atmosphere.container.JSR356Endpoint;
19+
import org.atmosphere.cpr.ApplicationConfig;
20+
import org.atmosphere.cpr.AtmosphereConfig;
21+
import org.atmosphere.cpr.AtmosphereFramework;
22+
import org.atmosphere.cpr.AtmosphereRequest;
23+
import org.atmosphere.websocket.WebSocketProcessor;
1824
import org.mockito.invocation.InvocationOnMock;
1925
import org.mockito.stubbing.Answer;
2026
import org.testng.annotations.BeforeMethod;
2127
import org.testng.annotations.Test;
2228

29+
import javax.servlet.ServletContext;
30+
import javax.servlet.http.HttpSession;
31+
import javax.websocket.EndpointConfig;
2332
import javax.websocket.RemoteEndpoint;
2433
import javax.websocket.SendHandler;
2534
import javax.websocket.SendResult;
2635
import javax.websocket.Session;
36+
import javax.websocket.server.HandshakeRequest;
37+
import java.lang.reflect.Field;
38+
import java.net.URI;
39+
import java.util.Enumeration;
40+
import java.util.HashMap;
41+
import java.util.Map;
42+
import java.util.Vector;
2743

2844
import static org.mockito.ArgumentMatchers.any;
2945
import static org.mockito.ArgumentMatchers.anyString;
@@ -33,8 +49,7 @@
3349
import static org.mockito.Mockito.times;
3450
import static org.mockito.Mockito.verify;
3551
import static org.mockito.Mockito.when;
36-
37-
import org.atmosphere.cpr.AtmosphereFramework;
52+
import static org.testng.Assert.assertEquals;
3853

3954
public class JSR356WebSocketTest {
4055

@@ -45,6 +60,9 @@ public class JSR356WebSocketTest {
4560
@BeforeMethod
4661
public void setUp() throws Exception {
4762
session = mock(Session.class);
63+
when(session.isOpen()).thenReturn(true);
64+
when(session.getRequestURI()).thenReturn(URI.create("/"));
65+
4866
asyncRemoteEndpoint = mock(RemoteEndpoint.Async.class);
4967
when(session.getAsyncRemote()).thenReturn(asyncRemoteEndpoint);
5068
webSocket = new JSR356WebSocket(session, new AtmosphereFramework().getAtmosphereConfig()) {
@@ -116,5 +134,50 @@ public void run() {
116134
}
117135
}).when(asyncRemoteEndpoint).sendText(anyString(), any(SendHandler.class));
118136
}
119-
137+
138+
@Test
139+
public void testAttributePropagationFromHandshakeSessionToAtmosphereRequest() throws NoSuchFieldException, IllegalAccessException {
140+
HandshakeRequest mockHandshakeRequest = mock(HandshakeRequest.class);
141+
HttpSession mockHttpSession = mock(HttpSession.class);
142+
143+
Map<String, Object> sessionAttributes = new HashMap<>();
144+
sessionAttributes.put("attribute1", "value1");
145+
sessionAttributes.put("attribute2", "value2");
146+
147+
when(mockHandshakeRequest.getHttpSession()).thenReturn(mockHttpSession);
148+
when(mockHttpSession.getAttributeNames()).thenReturn(new Vector<>(sessionAttributes.keySet()).elements());
149+
when(mockHttpSession.getAttribute("attribute1")).thenReturn(sessionAttributes.get("attribute1"));
150+
when(mockHttpSession.getAttribute("attribute2")).thenReturn(sessionAttributes.get("attribute2"));
151+
152+
EndpointConfig mockEndpointConfig = mock(EndpointConfig.class);
153+
when(mockEndpointConfig.getUserProperties()).thenReturn(new HashMap<String, Object>() {{
154+
put("handshakeRequest", mockHandshakeRequest);
155+
}});
156+
157+
AtmosphereFramework mockFramework = mock(AtmosphereFramework.class);
158+
AtmosphereConfig mockConfig = mock(AtmosphereConfig.class);
159+
160+
// Stub the getAtmosphereConfig and getInitParameter methods
161+
when(mockFramework.getAtmosphereConfig()).thenReturn(mockConfig);
162+
when(mockFramework.getServletContext()).thenReturn(mock(ServletContext.class));
163+
when(mockConfig.getInitParameter(ApplicationConfig.JSR356_MAPPING_PATH)).thenReturn("/");
164+
when(mockConfig.getServletContext()).thenReturn(mock(ServletContext.class));
165+
166+
WebSocketProcessor webSocketProcessor = mock(WebSocketProcessor.class);
167+
168+
JSR356Endpoint endpoint = new JSR356Endpoint(mockFramework, webSocketProcessor);
169+
endpoint.handshakeRequest(mockHandshakeRequest);
170+
endpoint.onOpen(session, mockEndpointConfig);
171+
172+
Field requestField = JSR356Endpoint.class.getDeclaredField("request");
173+
requestField.setAccessible(true);
174+
AtmosphereRequest request = (AtmosphereRequest) requestField.get(endpoint);
175+
176+
Enumeration<String> attributeNames = request.getAttributeNames();
177+
while (attributeNames.hasMoreElements()) {
178+
String attributeName = attributeNames.nextElement();
179+
assertEquals(request.getAttribute(attributeName), sessionAttributes.get(attributeName),
180+
"Attribute value should match the value from the HttpSession");
181+
}
182+
}
120183
}

0 commit comments

Comments
 (0)