Skip to content

Commit 56b3a0f

Browse files
committed
fix PR. add test cases
1 parent cdc13ee commit 56b3a0f

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/main/java/robaho/net/httpserver/extras/MultipartFormParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ public static Map<String, List<Part>> parse(String encoding, String content_type
120120
if (meta.filename == null) {
121121
var bos = new ByteArrayOutputStream();
122122
os = bos;
123-
addToResults = () -> results.computeIfAbsent(meta.name, k -> new LinkedList<Part>()).add(new Part(null, null, bos.toString(charset), null));
123+
addToResults = () -> results.computeIfAbsent(meta.name, k -> new LinkedList<Part>()).add(new Part(meta.contentType, null, bos.toString(charset), null));
124124
} else {
125125
File file = Path.of(storage.toString(), meta.filename).toFile();
126126
file.deleteOnExit();
127127
os = new NoSyncBufferedOutputStream(new FileOutputStream(file));
128-
addToResults = () -> results.computeIfAbsent(meta.name, k -> new LinkedList<Part>()).add(new Part(null, meta.filename, null, file));
128+
addToResults = () -> results.computeIfAbsent(meta.name, k -> new LinkedList<Part>()).add(new Part(meta.contentType, meta.filename, null, file));
129129
}
130130

131131
try (os) {
@@ -190,7 +190,7 @@ private static PartMetadata parseHeaders(List<String> headers) {
190190
}
191191

192192
} else if ("content-type".equalsIgnoreCase(parts[0])) {
193-
contentType = parts[1];
193+
contentType = parts[1].trim();
194194
}
195195
}
196196
return new PartMetadata(contentType, name, filename);

src/test/java/robaho/net/httpserver/extras/MultipartFormParserTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ public void testFiles() throws UnsupportedEncodingException, IOException {
5252
s += "111Y\r\n";
5353
s += "111Z\rCCCC\nCCCC\r\nCCCCC@\r\n";
5454

55+
Assert.assertEquals(values.get(0).contentType(),"text/plain");
5556
Assert.assertEquals(s.getBytes("UTF-8"), Files.readAllBytes((values.get(0).file()).toPath()), "file1 failed");
5657

58+
5759
s = "\r\n";
5860
s += "@22X";
5961
s += "222Y\r\n";
6062
s += "222Z\r222W\n2220\r\n666@";
6163

64+
Assert.assertEquals(values.get(1).contentType(),"text/plain");
6265
Assert.assertEquals(s.getBytes("UTF-8"), Files.readAllBytes((values.get(1).file()).toPath()), "file2 failed");
6366
}
6467

@@ -118,6 +121,7 @@ public void testFormSample() throws IOException {
118121

119122
Assert.assertEquals(results.size(), 1);
120123
List<Part> values = results.get("myfile");
124+
Assert.assertEquals(values.get(0).contentType(), "text/plain");
121125
Assert.assertEquals(values.size(), 1);
122126
}
123127

@@ -133,6 +137,12 @@ public void testMultiFileFormSample() throws IOException {
133137

134138
Assert.assertEquals(results.size(), 2);
135139
List<Part> values = results.get("myfile");
140+
Assert.assertEquals(values.get(0).contentType(), "text/plain");
141+
Assert.assertEquals(values.size(), 1);
142+
143+
values = results.get("myfile2");
144+
Assert.assertEquals(values.get(0).contentType(), "image/png");
136145
Assert.assertEquals(values.size(), 1);
146+
137147
}
138148
}

0 commit comments

Comments
 (0)