Skip to content

Commit

Permalink
Merge pull request #2370 from dovikos/patch/multipart-request
Browse files Browse the repository at this point in the history
Use Netty HttpPostRequestEncoder with "HTML5" encoder mode to prevent…
  • Loading branch information
ptrthomas authored Jul 27, 2023
2 parents c5442f3 + 214a36f commit a5d1abb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.multipart.Attribute;
import io.netty.handler.codec.http.multipart.DefaultHttpDataFactory;
import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder;
import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.EncoderMode;
import io.netty.util.CharsetUtil;
import io.netty.handler.codec.http.multipart.InterfaceHttpData;
import io.netty.handler.codec.http.multipart.MemoryFileUpload;
import java.io.File;
Expand Down Expand Up @@ -92,7 +95,7 @@ public MultiPartBuilder(boolean multipart, HttpClient client) {
this.multipart = multipart;
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.valueOf("POST"), "/");
try {
encoder = new HttpPostRequestEncoder(request, multipart);
encoder = new HttpPostRequestEncoder(new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE), request, multipart, CharsetUtil.UTF_8, EncoderMode.HTML5);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
5 changes: 5 additions & 0 deletions karate-core/src/test/java/com/intuit/karate/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public static void matchContains(Object actual, Object expected) {
Match.Result mr = Match.evaluate(actual).contains(expected);
assertTrue(mr.pass, mr.message);
}

public static void notContains(Object actual, Object expected) {
Match.Result mr = Match.evaluate(actual).isNotContaining(expected);
assertTrue(mr.pass, mr.message);
}

public static ScenarioEngine engine() {
return new ScenarioEngine(new Config(), runtime(), new HashMap(), new Logger());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,23 @@ void testMultiPartFile() {
matchVar("response", "{ foo: [{ name: 'foo', value: '#notnull', contentType: 'text/plain', charset: 'UTF-8', filename: 'foo.txt', transferEncoding: '7bit' }] }");
}

@Test
void testMultiPartFiles() {
background().scenario(
"pathMatches('/hello')",
"def response = requestParts");
run(
URL_STEP,
"def file1 = { name: 'file', filename: 'file1.txt', value: 'Hello 1' }",
"def file2 = { name: 'file', filename: 'file2.txt', value: 'Hello 2' }",
"multipart files ([file1, file2])",
"path 'hello'",
"method post"
);
runtime.engine.assign(AssignType.STRING, "prevReqBody", "karate.prevRequest.body", false);
notContains(get("prevReqBody"), "multipart/mixed");
}

@Test
void testMultiPartFileNullCharset() {
background().scenario(
Expand Down

0 comments on commit a5d1abb

Please sign in to comment.