Skip to content

Commit 0c6ca8c

Browse files
authored
Fix #523 by having the default filename of multipart/form-data in files.upload requests (#524)
* Add test cases verifying the behaviors reported at #523 * Set a placeholder for filename if absent * Re-package new tests
1 parent 439c650 commit 0c6ca8c

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package test_with_remote_apis.issues
2+
3+
import com.slack.api.Slack
4+
import junit.framework.TestCase.assertNull
5+
import org.junit.Test
6+
import java.io.File
7+
8+
class Issue523Test {
9+
10+
private val slack = Slack.getInstance()
11+
private val token = System.getenv("SLACK_SDK_TEST_USER_TOKEN")
12+
private val slackMethods = slack.methods(token)
13+
14+
@Test
15+
fun text_no_filename() {
16+
val file = File("src/test/resources/sample.txt")
17+
val response = slackMethods.filesUpload {
18+
it.fileData(file.readBytes()).filetype("text")
19+
}
20+
// assertEquals("no_file_data", response.error)
21+
assertNull(response.error)
22+
}
23+
24+
@Test
25+
fun text_filename() {
26+
val file = File("src/test/resources/sample.txt")
27+
val response = slackMethods.filesUpload {
28+
it.fileData(file.readBytes()).filetype("text").filename("test.txt")
29+
}
30+
assertNull(response.error)
31+
}
32+
33+
@Test
34+
fun image_no_filename() {
35+
val file = File("src/test/resources/sample.jpg")
36+
val response = slackMethods.filesUpload {
37+
it.fileData(file.readBytes()).filetype("jpg")
38+
}
39+
// assertEquals("no_file_data", response.error)
40+
assertNull(response.error)
41+
}
42+
43+
@Test
44+
fun image() {
45+
val file = File("src/test/resources/sample.jpg")
46+
val response = slackMethods.filesUpload {
47+
it.fileData(file.readBytes()).filetype("jpg").filename("test.jpg")
48+
}
49+
assertNull(response.error)
50+
}
51+
52+
}
4.29 KB
Loading
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Hello, World!!!!!
2+
3+
This is a sample text file.
4+
5+
日本語

slack-api-client/src/main/java/com/slack/api/methods/RequestFormBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,9 @@ public static MultipartBody.Builder toMultipartBody(FilesUploadRequest req) {
11291129
MultipartBody.Builder form = new MultipartBody.Builder();
11301130

11311131
if (req.getFileData() != null) {
1132+
if (req.getFilename() == null) {
1133+
req.setFilename("uploaded_file"); // filename is required for multipart/form-data
1134+
}
11321135
RequestBody file = RequestBody.create(req.getFileData(), MultipartBody.FORM);
11331136
form.addFormDataPart("file", req.getFilename(), file);
11341137
} else if (req.getFile() != null) {

slack-api-client/src/test/java/test_with_remote_apis/methods/files_Test.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
import java.io.File;
2727
import java.io.IOException;
28+
import java.nio.file.Files;
29+
import java.nio.file.Path;
30+
import java.nio.file.Paths;
2831
import java.util.Arrays;
2932

3033
import static org.hamcrest.CoreMatchers.*;
@@ -393,6 +396,44 @@ public void createFileForAThread() throws IOException, SlackApiException {
393396
}
394397
}
395398

399+
@Test
400+
public void issue523_text() throws IOException, SlackApiException {
401+
MethodsClient slackMethods = slack.methods(userToken);
402+
File file = new File("src/test/resources/sample.txt");
403+
byte[] fileData = Files.readAllBytes(Paths.get(file.toURI()));
404+
FilesUploadResponse response = slackMethods.filesUpload(r -> r.fileData(fileData).filename("sample.txt"));
405+
assertThat(response.getError(), is(nullValue()));
406+
}
407+
408+
@Test
409+
public void issue523_image() throws IOException, SlackApiException {
410+
MethodsClient slackMethods = slack.methods(userToken);
411+
File file = new File("src/test/resources/user_photo.jpg");
412+
byte[] fileData = Files.readAllBytes(Paths.get(file.toURI()));
413+
FilesUploadResponse response = slackMethods.filesUpload(r -> r.fileData(fileData).filename("sample.jpg"));
414+
assertThat(response.getError(), is(nullValue()));
415+
}
416+
417+
@Test
418+
public void issue523_text_no_filename() throws IOException, SlackApiException {
419+
MethodsClient slackMethods = slack.methods(userToken);
420+
File file = new File("src/test/resources/sample.txt");
421+
byte[] fileData = Files.readAllBytes(Paths.get(file.toURI()));
422+
FilesUploadResponse response = slackMethods.filesUpload(r -> r.fileData(fileData));
423+
// assertThat(response.getError(), is("no_file_data"));
424+
assertThat(response.getError(), is(nullValue()));
425+
}
426+
427+
@Test
428+
public void issue523_image_no_filename() throws IOException, SlackApiException {
429+
MethodsClient slackMethods = slack.methods(userToken);
430+
File file = new File("src/test/resources/user_photo.jpg");
431+
byte[] fileData = Files.readAllBytes(Paths.get(file.toURI()));
432+
FilesUploadResponse response = slackMethods.filesUpload(r -> r.fileData(fileData));
433+
// assertThat(response.getError(), is("no_file_data"));
434+
assertThat(response.getError(), is(nullValue()));
435+
}
436+
396437
@Test
397438
public void uploadAndPostMessage() throws IOException, SlackApiException {
398439
MethodsClient slackMethods = slack.methods(userToken);

0 commit comments

Comments
 (0)