gnodet-bot commented on code in PR #27851: URL: https://github.com/apache/camel/pull/27851#discussion_r4277620091
########## components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowFormCharsetTest.java: ########## @@ -0,0 +1,105 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.undertow; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.Map; + +import org.apache.camel.Message; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.jupiter.api.Test; + +import static java.nio.charset.StandardCharsets.ISO_8859_1; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Form fields are decoded as UTF-8 when the request does not declare a charset, as a browser sends them. + */ +class UndertowFormCharsetTest extends BaseUndertowTest { + + private static final String TEXT = "Grüße"; + private static final String BOUNDARY = "r27boundary"; + + @Test + void testUrlEncodedFormWithoutCharsetIsUtf8() throws Exception { + Message received = post("application/x-www-form-urlencoded", "name=Gr%C3%BC%C3%9Fe".getBytes(UTF_8)); + + assertEquals(TEXT, received.getHeader("name")); + assertEquals(TEXT, received.getBody(Map.class).get("name")); + } + + @Test + void testMultipartTextFieldWithoutCharsetIsUtf8() throws Exception { + String multipart = "--" + BOUNDARY + "\r\n" + + "Content-Disposition: form-data; name=\"name\"\r\n\r\n" + + TEXT + "\r\n" + + "--" + BOUNDARY + "--\r\n"; + Message received = post("multipart/form-data; boundary=" + BOUNDARY, multipart.getBytes(UTF_8)); + + assertEquals(TEXT, received.getHeader("name")); + assertEquals(TEXT, received.getBody(Map.class).get("name")); + } + + @Test + void testUrlEncodedFormKeepsDeclaredCharset() throws Exception { + // a charset that the request declares is still used + Message received = post("application/x-www-form-urlencoded; charset=ISO-8859-1", + ("name=" + TEXT).getBytes(ISO_8859_1)); Review Comment: 💡 **Nit:** `testUrlEncodedFormKeepsDeclaredCharset` sends raw ISO-8859-1 high bytes (`("name=" + TEXT).getBytes(ISO_8859_1)`) instead of percent-encoded form values like `"name=Gr%FC%DFe"`. Browsers and spec-compliant clients percent-encode before sending even when a charset is declared, so the other tests (`testUrlEncodedFormWithoutCharsetIsUtf8`, `testUrlEncodedIso88591FormWithoutCharsetGetsReplacementCharacters`) already use percent-encoded bodies. Aligning this test would make the suite more consistent and test the real-world code path more faithfully. Up to you — the current approach still exercises the charset declaration being respected. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
