15
15
*/
16
16
package org .atmosphere .container .version ;
17
17
18
+ import jakarta .servlet .ServletContext ;
19
+ import jakarta .servlet .http .HttpSession ;
20
+ import jakarta .websocket .EndpointConfig ;
21
+ import jakarta .websocket .RemoteEndpoint ;
22
+ import jakarta .websocket .SendHandler ;
23
+ import jakarta .websocket .SendResult ;
24
+ import jakarta .websocket .Session ;
25
+ import jakarta .websocket .server .HandshakeRequest ;
26
+ import org .atmosphere .container .JSR356Endpoint ;
27
+ import org .atmosphere .cpr .ApplicationConfig ;
28
+ import org .atmosphere .cpr .AtmosphereConfig ;
29
+ import org .atmosphere .cpr .AtmosphereFramework ;
30
+ import org .atmosphere .cpr .AtmosphereRequest ;
31
+ import org .atmosphere .websocket .WebSocketProcessor ;
18
32
import org .mockito .invocation .InvocationOnMock ;
19
33
import org .mockito .stubbing .Answer ;
20
34
import org .testng .annotations .BeforeMethod ;
21
35
import org .testng .annotations .Test ;
22
36
23
- import jakarta .websocket .RemoteEndpoint ;
24
- import jakarta .websocket .SendHandler ;
25
- import jakarta .websocket .SendResult ;
26
- import jakarta .websocket .Session ;
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 ;
27
43
28
44
import static org .mockito .ArgumentMatchers .any ;
29
45
import static org .mockito .ArgumentMatchers .anyString ;
33
49
import static org .mockito .Mockito .times ;
34
50
import static org .mockito .Mockito .verify ;
35
51
import static org .mockito .Mockito .when ;
36
-
37
- import org .atmosphere .cpr .AtmosphereFramework ;
52
+ import static org .testng .Assert .assertEquals ;
38
53
39
54
public class JSR356WebSocketTest {
40
55
@@ -45,6 +60,9 @@ public class JSR356WebSocketTest {
45
60
@ BeforeMethod
46
61
public void setUp () throws Exception {
47
62
session = mock (Session .class );
63
+ when (session .isOpen ()).thenReturn (true );
64
+ when (session .getRequestURI ()).thenReturn (URI .create ("/" ));
65
+
48
66
asyncRemoteEndpoint = mock (RemoteEndpoint .Async .class );
49
67
when (session .getAsyncRemote ()).thenReturn (asyncRemoteEndpoint );
50
68
webSocket = new JSR356WebSocket (session , new AtmosphereFramework ().getAtmosphereConfig ()) {
@@ -116,5 +134,50 @@ public void run() {
116
134
}
117
135
}).when (asyncRemoteEndpoint ).sendText (anyString (), any (SendHandler .class ));
118
136
}
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
+ }
120
183
}
0 commit comments