gnodet-bot commented on code in PR #26565: URL: https://github.com/apache/camel/pull/26565#discussion_r4040690366
########## core/camel-util/src/test/java/org/apache/camel/util/EscapeHelperTest.java: ########## @@ -0,0 +1,108 @@ +/* + * 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.util; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +public class EscapeHelperTest { + + @Test + public void testHtml() { + assertEquals("Monday & Tuesday", EscapeHelper.html("Monday & Tuesday")); + assertEquals("<b>bold</b>", EscapeHelper.html("<b>bold</b>")); + assertEquals("say "hi" & 'bye'", EscapeHelper.html("say \"hi\" & 'bye'")); + assertEquals("caf\u00e9", EscapeHelper.html("caf\u00e9")); + } + + @Test + public void testXml() { + assertEquals("Monday & Tuesday", EscapeHelper.xml("Monday & Tuesday")); + assertEquals("<a href="x">it's</a>", EscapeHelper.xml("<a href=\"x\">it's</a>")); + } + + @Test + public void testJson() { + assertEquals("say \\\"hi\\\"", EscapeHelper.json("say \"hi\"")); + assertEquals("c:\\\\temp", EscapeHelper.json("c:\\temp")); + assertEquals("line1\\nline2\\ttab\\r\\b\\f", EscapeHelper.json("line1\nline2\ttab\r\b\f")); + assertEquals("a\\u0001b", EscapeHelper.json("a\u0001b")); + assertEquals("it's <b>", EscapeHelper.json("it's <b>")); + } + + @Test + public void testJs() { + assertEquals("it\\'s \\\"quoted\\\"", EscapeHelper.js("it's \"quoted\"")); + assertEquals("<\\/script>", EscapeHelper.js("</script>")); + assertEquals("a\\nb", EscapeHelper.js("a\nb")); + assertEquals("a/b", EscapeHelper.js("a/b")); + } + + @Test + public void testSql() { + assertEquals("O''Reilly", EscapeHelper.sql("O'Reilly")); + assertEquals("'''' ", EscapeHelper.sql("'' ")); + assertEquals("plain", EscapeHelper.sql("plain")); + } + + @Test + public void testUrl() { + assertEquals("Camel%20in%20Action", EscapeHelper.url("Camel in Action")); + assertEquals("a%26b%3Dc%2Fd%3Fe", EscapeHelper.url("a&b=c/d?e")); + assertEquals("caf%C3%A9", EscapeHelper.url("caf\u00e9")); + assertEquals("plain-text_1.2*", EscapeHelper.url("plain-text_1.2*")); + } + + @Test + public void testNoChangeReturnsSameInstance() { + String value = "nothing to escape"; + assertSame(value, EscapeHelper.html(value)); + assertSame(value, EscapeHelper.xml(value)); Review Comment: ⚠️ **Missing test: `url()` with `+` in input.** The `url()` implementation relies on `URLEncoder.encode().replace("+", "%20")` — the correctness of this approach depends on `URLEncoder` encoding a literal `+` from the input as `%2B` (not `+`). Without a test that pins this, a future reader has no documented proof that `url("a+b")` returns `"a%2Bb"` and not `"a%20b"`. ```suggestion assertEquals("Camel%20in%20Action", EscapeHelper.url("Camel in Action")); assertEquals("a%26b%3Dc%2Fd%3Fe", EscapeHelper.url("a&b=c/d?e")); assertEquals("caf%C3%A9", EscapeHelper.url("caf\u00e9")); assertEquals("plain-text_1.2*", EscapeHelper.url("plain-text_1.2*")); assertEquals("a%2Bb", EscapeHelper.url("a+b")); // + in input → %2B, not %20 ``` ########## core/camel-util/src/main/java/org/apache/camel/util/EscapeHelper.java: ########## @@ -0,0 +1,237 @@ +/* + * 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.util; + +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.Locale; + +/** + * Helper for escaping special characters so a value can be embedded safely in HTML, XML, JSON, JavaScript, SQL or a + * URL. + */ +public final class EscapeHelper { + + /** + * The supported escape kinds. + */ + public enum Kind { + HTML, + XML, + JSON, + JS, + SQL, + URL; + + /** + * Resolves the escape kind from its name (case-insensitive). + * + * @param name the name such as html, xml, json, js, sql, or url + * @return the kind, or <tt>null</tt> if the name is not a supported kind + */ + public static Kind fromName(String name) { + if (name == null) { + return null; + } + String key = name.trim().toUpperCase(Locale.ROOT); + if ("JAVASCRIPT".equals(key)) { + return JS; + } + for (Kind kind : values()) { + if (kind.name().equals(key)) { + return kind; + } + } + return null; + } + + /** + * Escapes the value according to this kind. + */ + public String escape(String value) { Review Comment: 💡 **Missing `@param`/`@return` Javadoc on `Kind.escape(String)`.** All other public methods in this file have full Javadoc; this one only has a description line. ```suggestion /** * Escapes the value according to this kind. * * @param value the value to escape * @return the escaped value, or {@code null} if {@code value} is {@code null} */ public String escape(String value) { ``` -- 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]
