19
19
20
20
import java .io .IOException ;
21
21
import java .io .OutputStream ;
22
- import java .io .Reader ;
23
22
import java .io .StringWriter ;
24
23
import java .util .Collections ;
25
24
import java .util .Optional ;
31
30
import org .mockito .Mockito ;
32
31
33
32
import com .vaadin .flow .component .UI ;
33
+ import com .vaadin .flow .function .DeploymentConfiguration ;
34
34
import com .vaadin .flow .server .DefaultDeploymentConfiguration ;
35
35
import com .vaadin .flow .server .HandlerHelper .RequestType ;
36
36
import com .vaadin .flow .server .MockVaadinContext ;
46
46
import com .vaadin .flow .server .startup .ApplicationConfiguration ;
47
47
import com .vaadin .flow .shared .ApplicationConstants ;
48
48
import com .vaadin .pro .licensechecker .dau .EnforcementException ;
49
+ import com .vaadin .tests .util .MockUI ;
49
50
50
51
import elemental .json .JsonObject ;
51
52
import elemental .json .impl .JsonUtil ;
@@ -128,9 +129,73 @@ public void writeSessionExpired_whenUINotFound() throws IOException {
128
129
responseContent );
129
130
}
130
131
132
+ @ Test
133
+ public void clientRequestsPreviousIdAndPayload_resendPreviousResponse ()
134
+ throws IOException {
135
+
136
+ UI ui = getUi ();
137
+ VaadinSession session = ui .getSession ();
138
+ VaadinService service = session .getService ();
139
+ DeploymentConfiguration conf = Mockito
140
+ .mock (DeploymentConfiguration .class );
141
+ Mockito .when (service .getDeploymentConfiguration ()).thenReturn (conf );
142
+ Mockito .when (conf .isRequestTiming ()).thenReturn (false );
143
+
144
+ String requestBody = """
145
+ {
146
+ "csrfToken": "d1f44a6f-bbe5-4493-a8a9-3f5f234a2a93",
147
+ "rpc": [
148
+ {
149
+ "type": "mSync",
150
+ "node": 12,
151
+ "feature": 1,
152
+ "property": "value",
153
+ "value": "a"
154
+ },
155
+ {
156
+ "type": "event",
157
+ "node": 12,
158
+ "event": "change",
159
+ "data": {}
160
+ }
161
+ ],
162
+ "syncId": 0,
163
+ "clientId": 0
164
+ }
165
+ """ ;
166
+ Mockito .when (request .getService ()).thenReturn (service );
167
+ Mockito .when (conf .isSyncIdCheckEnabled ()).thenReturn (true );
168
+
169
+ Optional <SynchronizedRequestHandler .ResponseWriter > result = handler
170
+ .synchronizedHandleRequest (session , request , response ,
171
+ requestBody );
172
+ Assert .assertTrue ("ResponseWriter should be present" ,
173
+ result .isPresent ());
174
+ result .get ().writeResponse ();
175
+ String responseContent = CommunicationUtil
176
+ .getStringWhenWriteString (outputStream );
177
+
178
+ // Init clean response
179
+ response = Mockito .mock (VaadinResponse .class );
180
+ outputStream = Mockito .mock (OutputStream .class );
181
+ Mockito .when (response .getOutputStream ()).thenReturn (outputStream );
182
+
183
+ result = handler .synchronizedHandleRequest (session , request , response ,
184
+ requestBody );
185
+ Assert .assertTrue ("ResponseWriter should be present" ,
186
+ result .isPresent ());
187
+ result .get ().writeResponse ();
188
+ String resendResponseContent = CommunicationUtil
189
+ .getStringWhenWriteString (outputStream );
190
+
191
+ // response shouldn't contain async
192
+ Assert .assertEquals ("Server should send same content again" ,
193
+ responseContent , resendResponseContent );
194
+ }
195
+
131
196
@ Test
132
197
public void should_modifyUidl_when_MPR () throws Exception {
133
- UI ui = mock ( UI . class );
198
+ UI ui = getUi ( );
134
199
135
200
UidlRequestHandler handler = spy (new UidlRequestHandler ());
136
201
StringWriter writer = new StringWriter ();
@@ -151,7 +216,7 @@ public void should_modifyUidl_when_MPR() throws Exception {
151
216
152
217
@ Test
153
218
public void should_changeURL_when_v7LocationProvided () throws Exception {
154
- UI ui = mock ( UI . class );
219
+ UI ui = getUi ( );
155
220
156
221
UidlRequestHandler handler = spy (new UidlRequestHandler ());
157
222
StringWriter writer = new StringWriter ();
@@ -172,7 +237,7 @@ public void should_changeURL_when_v7LocationProvided() throws Exception {
172
237
@ Test
173
238
public void should_updateHash_when_v7LocationNotProvided ()
174
239
throws Exception {
175
- UI ui = mock ( UI . class );
240
+ UI ui = getUi ( );
176
241
177
242
UidlRequestHandler handler = spy (new UidlRequestHandler ());
178
243
StringWriter writer = new StringWriter ();
@@ -192,7 +257,7 @@ public void should_updateHash_when_v7LocationNotProvided()
192
257
193
258
@ Test
194
259
public void should_not_modify_non_MPR_Uidl () throws Exception {
195
- UI ui = mock ( UI . class );
260
+ UI ui = getUi ( );
196
261
197
262
UidlRequestHandler handler = spy (new UidlRequestHandler ());
198
263
StringWriter writer = new StringWriter ();
@@ -217,7 +282,7 @@ public void should_not_modify_non_MPR_Uidl() throws Exception {
217
282
@ Test
218
283
public void should_not_update_browser_history_if_no_hash_in_location ()
219
284
throws Exception {
220
- UI ui = mock ( UI . class );
285
+ UI ui = getUi ( );
221
286
222
287
UidlRequestHandler handler = spy (new UidlRequestHandler ());
223
288
StringWriter writer = new StringWriter ();
@@ -351,4 +416,29 @@ private JsonObject getUidlWithNoHashInLocation() {
351
416
// @formatter:on
352
417
}
353
418
419
+ /**
420
+ * Mock ui with session.
421
+ *
422
+ * @return
423
+ */
424
+ private static UI getUi () {
425
+ VaadinService service = mock (VaadinService .class );
426
+ VaadinSession session = new VaadinSession (service ) {
427
+ @ Override
428
+ public boolean hasLock () {
429
+ return true ;
430
+ }
431
+
432
+ @ Override
433
+ public VaadinService getService () {
434
+ return service ;
435
+ }
436
+ };
437
+
438
+ UI ui = new MockUI (session );
439
+
440
+ when (service .findUI (Mockito .any ())).thenReturn (ui );
441
+
442
+ return ui ;
443
+ }
354
444
}
0 commit comments