1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.path;
20
21 import org.junit.jupiter.api.Test;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25
26
27
28
29 public class DefaultUrlNormalizerTest {
30
31 private UrlNormalizer normalizer = new DefaultUrlNormalizer();
32
33 private String normalize(String url) {
34 return normalizer.normalize(url);
35 }
36
37 @Test
38 public void testNullSafe() {
39 assertNull(normalize(null));
40 }
41
42 @Test
43 public void testTrailingSlash() {
44 assertEquals("", normalize(""));
45 assertEquals("http://server.org/dir", normalize("http://server.org/dir"));
46 assertEquals("http://server.org/dir/", normalize("http://server.org/dir/"));
47 }
48
49 @Test
50 public void testRemovalOfParentRefs() {
51 assertEquals("http://server.org/child", normalize("http://server.org/parent/../child"));
52 assertEquals("http://server.org/child", normalize("http://server.org/grand/parent/../../child"));
53
54 assertEquals("http://server.org//child", normalize("http://server.org/parent/..//child"));
55 assertEquals("http://server.org/child", normalize("http://server.org/parent//../child"));
56 }
57
58 @Test
59 public void testPreservationOfDoubleSlashes() {
60 assertEquals("scm:hg:ssh://localhost//home/user", normalize("scm:hg:ssh://localhost//home/user"));
61 assertEquals("file:////UNC/server", normalize("file:////UNC/server"));
62 assertEquals(
63 "[fetch=]http://server.org/[push=]ssh://server.org/",
64 normalize("[fetch=]http://server.org/[push=]ssh://server.org/"));
65 }
66
67 @Test
68 public void absolutePathTraversalPastRootIsOmitted() {
69 assertEquals("/", normalize("/../"));
70 }
71
72 @Test
73 public void parentDirectoryRemovedFromRelativeUriReference() {
74 assertEquals("", normalize("a/../"));
75 }
76
77 @Test
78 public void leadingParentDirectoryNotRemovedFromRelativeUriReference() {
79 assertEquals("../", normalize("../"));
80 }
81 }