Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Netty HttpPostRequestEncoder with "HTML5" encoder mode to prevent… #2370

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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