1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.codec.net;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21
22 import java.nio.charset.StandardCharsets;
23 import java.util.BitSet;
24
25 import org.junit.jupiter.params.ParameterizedTest;
26 import org.junit.jupiter.params.provider.ValueSource;
27
28 /**
29 * Tests the reproduction in <a href="https://issues.apache.org/jira/browse/CODEC-345">CODEC-345</a>.
30 */
31 class Codec345Test {
32
33 @ParameterizedTest
34 @ValueSource(strings = { "/pages/1/Test+Page", "/display/TST/Caf%C3%A9" })
35 void testEncodeUrlWithCallerSuppliedSafeCharacters(final String input) throws Exception {
36 // RFC 2396 abs_path, as used by HtmlUnit's UrlUtils and HttpClient 3.x's URI.
37 final BitSet allowed = new BitSet(256);
38 for (int c = 'a'; c <= 'z'; c++) {
39 allowed.set(c);
40 }
41 for (int c = 'A'; c <= 'Z'; c++) {
42 allowed.set(c);
43 }
44 for (int c = '0'; c <= '9'; c++) {
45 allowed.set(c);
46 }
47 for (final char c : "-_.!~*'()".toCharArray()) {
48 allowed.set(c); // mark
49 }
50 allowed.set('%'); // escaped
51 for (final char c : ":@&=+$,;/".toCharArray()) {
52 allowed.set(c); // pchar
53 }
54 final byte[] encoded = URLCodec.encodeUrl(allowed, input.getBytes(StandardCharsets.UTF_8));
55 assertEquals(input, new String(encoded, StandardCharsets.US_ASCII));
56 assertEquals(input, new String(URLCodec.decodeUrl(allowed, encoded), StandardCharsets.UTF_8));
57 }
58 }