Skip to content

Commit 3466b31

Browse files
authored
fix: upload handlers always end with /upload (#7597)
To be compatible with Receivers, UploadHandlers used with Upload component are registered with the getUrlPostfix `upload`. Fixes vaadin/flow#21689
1 parent 0c8a841 commit 3466b31

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

  • vaadin-upload-flow-parent/vaadin-upload-flow/src

‎vaadin-upload-flow-parent/vaadin-upload-flow/src/main/java/com/vaadin/flow/component/upload/Upload.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,12 @@ public void setReceiver(Receiver receiver) {
727727
public void setUploadHandler(UploadHandler handler) {
728728
Objects.requireNonNull(handler, "UploadHandler cannot be null");
729729
StreamResourceRegistry.ElementStreamResource elementStreamResource = new StreamResourceRegistry.ElementStreamResource(
730-
handler, this.getElement());
730+
handler, this.getElement()) {
731+
@Override
732+
public String getName() {
733+
return "upload";
734+
}
735+
};
731736
runBeforeClientResponse(ui -> getElement().setAttribute("target",
732737
elementStreamResource));
733738
if (!hasListener(UploadStartEvent.class)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2000-2025 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.upload;
17+
18+
import org.junit.Assert;
19+
import org.junit.Test;
20+
import org.mockito.Mockito;
21+
22+
import com.vaadin.flow.component.UI;
23+
import com.vaadin.flow.function.SerializableConsumer;
24+
25+
import net.jcip.annotations.NotThreadSafe;
26+
27+
@NotThreadSafe
28+
public class UploadTest {
29+
30+
@Test
31+
public void uploadHandlerSet_generatedUrlEndsWithUpload() {
32+
UI original = UI.getCurrent();
33+
UI ui = Mockito.mock(UI.class);
34+
UI.setCurrent(ui);
35+
Mockito.when(ui.getUIId()).thenReturn(5);
36+
37+
try {
38+
Upload testUpload = new Upload((event) -> {
39+
}) {
40+
@Override
41+
void runBeforeClientResponse(SerializableConsumer<UI> command) {
42+
command.accept(Mockito.mock(UI.class));
43+
}
44+
};
45+
46+
testUpload.setUploadHandler(event -> {
47+
});
48+
49+
String targetUploadUrl = testUpload.getElement()
50+
.getAttribute("target");
51+
Assert.assertTrue("Upload url should end with 'upload'",
52+
targetUploadUrl.endsWith("upload"));
53+
} finally {
54+
UI.setCurrent(original);
55+
}
56+
57+
}
58+
59+
}

0 commit comments

Comments
 (0)