Skip to content

Commit

Permalink
[rewrite] #1281 improve multi-part handling on server
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Nov 14, 2020
1 parent 3066f22 commit de45d29
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 31 deletions.
36 changes: 15 additions & 21 deletions karate-core/src/main/java/com/intuit/karate/server/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,35 +332,29 @@ public void processBody() {
try {
for (InterfaceHttpData part : decoder.getBodyHttpDatas()) {
String name = part.getName();
if (multipart) {
if (multipart && part instanceof FileUpload) {
List<Map<String, Object>> list = multiParts.get(name);
if (list == null) {
list = new ArrayList();
multiParts.put(name, list);
}
Map<String, Object> map = new HashMap();
list.add(map);
if (part instanceof FileUpload) {
FileUpload fup = (FileUpload) part;
map.put("name", name);
map.put("filename", fup.getFilename());
Charset charset = fup.getCharset();
if (charset != null) {
map.put("charset", charset.name());
}
String ct = fup.getContentType();
map.put("contentType", ct);
map.put("value", fup.get()); // bytes
String transferEncoding = fup.getContentTransferEncoding();
if (transferEncoding != null) {
map.put("transferEncoding", transferEncoding);
}
} else { // simple multi-part key-value pair
Attribute attribute = (Attribute) part;
map.put("name", name);
map.put("value", attribute.getValue());
FileUpload fup = (FileUpload) part;
map.put("name", name);
map.put("filename", fup.getFilename());
Charset charset = fup.getCharset();
if (charset != null) {
map.put("charset", charset.name());
}
} else { // url-encoded form-field
String ct = fup.getContentType();
map.put("contentType", ct);
map.put("value", fup.get()); // bytes
String transferEncoding = fup.getContentTransferEncoding();
if (transferEncoding != null) {
map.put("transferEncoding", transferEncoding);
}
} else { // form-field, url-encoded if not multipart
Attribute attribute = (Attribute) part;
List<String> list = params.get(name);
if (list == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,14 @@ void testFormFieldPost() {
void testMultiPartField() {
background().scenario(
"pathMatches('/hello')",
"def response = requestParts");
"def response = requestParams");
run(
URL_STEP,
"multipart field foo = 'bar'",
"path '/hello'",
"method post"
);
matchVar("response", "{ foo: [{ name: 'foo', value: '#notnull' }] }");
matchVar("response", "{ foo: ['bar'] }");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ void testResponseHeaders() {
void testMultiPart() {
background().scenario(
"pathMatches('/hello')",
"def foo = requestParts.foo[0].value",
"def foo = requestParams.foo[0]",
"string bar = requestParts.bar[0].value",
"def response = { foo: '#(foo)', bar: '#(bar)' }"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,12 @@ public Response invoke(HttpRequest hr) {
if (request.isMultiPart()) {
request.getMultiParts().forEach((name, v) -> {
for (Map<String, Object> map : v) {
String fileName = (String) map.get("filename");
if (fileName != null) {
req.addPart(new MockPart(map));
} else {
String value = (String) map.get("value");
req.addParameter(name, value);
}
req.addPart(new MockPart(map));
}
});
request.getParams().forEach((name, v) -> {
for (String value : v) {
req.addParameter(name, value);
}
});
}
Expand Down

0 comments on commit de45d29

Please sign in to comment.