1818
1919import nbbrd .io .text .Parser ;
2020import org .junit .jupiter .api .Test ;
21+ import wiremock .org .apache .hc .core5 .net .URIBuilder ;
2122
2223import java .io .IOException ;
24+ import java .net .MalformedURLException ;
25+ import java .net .URISyntaxException ;
2326import java .net .URL ;
2427import java .util .List ;
28+ import java .util .function .Consumer ;
2529
2630import static java .util .Arrays .asList ;
2731import static nbbrd .io .http .URLQueryBuilder .of ;
@@ -39,87 +43,141 @@ public void testFactory() throws IOException {
3943 assertThatNullPointerException ()
4044 .isThrownBy (() -> of (null ));
4145
42- assertThat (of (withoutTrailingSlash ))
46+ assertThat (of (urlWithoutSlash ).build ())
47+ .isEqualTo (uriBuilderAsURL (urlWithoutSlash , DO_NOTHING ))
4348 .hasToString ("http://localhost" );
4449
45- assertThat (of (withTrailingSlash ))
50+ assertThat (of (urlWithSlash ).build ())
51+ .isEqualTo (uriBuilderAsURL (urlWithSlash , DO_NOTHING ))
4652 .hasToString ("http://localhost/" );
4753 }
4854
4955 @ SuppressWarnings ("DataFlowIssue" )
5056 @ Test
5157 public void testPath () throws IOException {
5258 assertThatNullPointerException ()
53- .isThrownBy (() -> of (withoutTrailingSlash ).path ((String ) null ));
59+ .isThrownBy (() -> of (urlWithoutSlash ).path ((String ) null ));
5460
55- assertThatNullPointerException ()
56- .isThrownBy (() -> of (withoutTrailingSlash ).path ((List <String >) null ));
57-
58- assertThat (of (withoutTrailingSlash ).path ("hello" ).path ("" ).path ("worl/d" ).build ())
61+ assertThat (of (urlWithoutSlash ).path ("hello" ).path ("" ).path ("worl/d" ).build ())
62+ .isEqualTo (uriBuilderAsURL (urlWithoutSlash , o -> o .appendPathSegments ("hello" , "" , "worl/d" )))
5963 .hasToString ("http://localhost/hello//worl%2Fd" );
6064
61- assertThat (of (withTrailingSlash ).path ("hello" ).path ("" ).path ("worl/d" ).build ())
65+ assertThat (of (urlWithSlash ).path ("hello" ).path ("" ).path ("worl/d" ).build ())
66+ .isNotEqualTo (uriBuilderAsURL (urlWithSlash , o -> o .appendPathSegments ("hello" , "" , "worl/d" )))
6267 .hasToString ("http://localhost/hello//worl%2Fd" );
68+ }
69+
70+ @ SuppressWarnings ("DataFlowIssue" )
71+ @ Test
72+ public void testPathList () throws IOException {
73+ assertThatNullPointerException ()
74+ .isThrownBy (() -> of (urlWithoutSlash ).path ((List <String >) null ));
6375
64- assertThat (of (withoutTrailingSlash ).path (asList ("hello" , "" , "worl/d" )).build ())
76+ assertThat (of (urlWithoutSlash ).path (asList ("hello" , "" , "worl/d" )).build ())
77+ .isEqualTo (uriBuilderAsURL (urlWithoutSlash , o -> o .appendPathSegments ("hello" , "" , "worl/d" )))
6578 .hasToString ("http://localhost/hello//worl%2Fd" );
6679
67- assertThat (of (withTrailingSlash ).path (asList ("hello" , "" , "worl/d" )).build ())
80+ assertThat (of (urlWithSlash ).path (asList ("hello" , "" , "worl/d" )).build ())
81+ .isNotEqualTo (uriBuilderAsURL (urlWithSlash , o -> o .appendPathSegments ("hello" , "" , "worl/d" )))
6882 .hasToString ("http://localhost/hello//worl%2Fd" );
6983 }
7084
7185 @ Test
7286 @ SuppressWarnings ({"DataFlowIssue" })
7387 public void testParam () throws IOException {
7488 assertThatNullPointerException ()
75- .isThrownBy (() -> of (withoutTrailingSlash ).param (null , "" ));
89+ .isThrownBy (() -> of (urlWithoutSlash ).param (null , "" ));
7690
7791 assertThatNullPointerException ()
78- .isThrownBy (() -> of (withoutTrailingSlash ).param ("" , null ));
92+ .isThrownBy (() -> of (urlWithoutSlash ).param ("" , null ));
7993
80- assertThatNullPointerException ()
81- .isThrownBy (() -> of (withoutTrailingSlash ).param (null ));
82-
83- assertThat (of (withoutTrailingSlash ).param ("p1" , "v1" ).param ("p&=2" , "v&=2" ).build ())
94+ assertThat (of (urlWithoutSlash ).param ("p1" , "v1" ).param ("p&=2" , "v&=2" ).build ())
95+ .isEqualTo (uriBuilderAsURL (urlWithoutSlash , o -> o .addParameter ("p1" , "v1" ).addParameter ("p&=2" , "v&=2" )))
8496 .hasToString ("http://localhost?p1=v1&p%26%3D2=v%26%3D2" );
8597
86- assertThat (of (withTrailingSlash ).param ("p1" , "v1" ).param ("p&=2" , "v&=2" ).build ())
98+ assertThat (of (urlWithSlash ).param ("p1" , "v1" ).param ("p&=2" , "v&=2" ).build ())
99+ .isEqualTo (uriBuilderAsURL (urlWithSlash , o -> o .addParameter ("p1" , "v1" ).addParameter ("p&=2" , "v&=2" )))
87100 .hasToString ("http://localhost/?p1=v1&p%26%3D2=v%26%3D2" );
88101
89- assertThat (of (withoutTrailingSlash ).path ("hello" ).path ("worl/d" ).param ("p1" , "v1" ).param ("p&=2" , "v&=2" ).build ())
102+ assertThat (of (urlWithoutSlash ).path ("hello" ).path ("worl/d" ).param ("p1" , "v1" ).param ("p&=2" , "v&=2" ).build ())
103+ .isEqualTo (uriBuilderAsURL (urlWithoutSlash , o -> o .appendPathSegments ("hello" , "worl/d" ).addParameter ("p1" , "v1" ).addParameter ("p&=2" , "v&=2" )))
90104 .hasToString ("http://localhost/hello/worl%2Fd?p1=v1&p%26%3D2=v%26%3D2" );
91105
92- assertThat (of (withTrailingSlash ).path ("hello" ).path ("worl/d" ).param ("p1" , "v1" ).param ("p&=2" , "v&=2" ).build ())
106+ assertThat (of (urlWithSlash ).path ("hello" ).path ("worl/d" ).param ("p1" , "v1" ).param ("p&=2" , "v&=2" ).build ())
107+ .isNotEqualTo (uriBuilderAsURL (urlWithSlash , o -> o .appendPathSegments ("hello" , "worl/d" ).addParameter ("p1" , "v1" ).addParameter ("p&=2" , "v&=2" )))
93108 .hasToString ("http://localhost/hello/worl%2Fd?p1=v1&p%26%3D2=v%26%3D2" );
94109
95- assertThat (of (withoutTrailingSlash ).param ("b" , "2" ).param ("a" , "1" ).build ())
110+ assertThat (of (urlWithoutSlash ).param ("b" , "2" ).param ("a" , "1" ).build ())
111+ .isEqualTo (uriBuilderAsURL (urlWithoutSlash , o -> o .addParameter ("b" , "2" ).addParameter ("a" , "1" )))
96112 .hasToString ("http://localhost?b=2&a=1" );
97113
98- assertThat (of (withTrailingSlash ).param ("b" , "2" ).param ("a" , "1" ).build ())
114+ assertThat (of (urlWithSlash ).param ("b" , "2" ).param ("a" , "1" ).build ())
115+ .isEqualTo (uriBuilderAsURL (urlWithSlash , o -> o .addParameter ("b" , "2" ).addParameter ("a" , "1" )))
99116 .hasToString ("http://localhost/?b=2&a=1" );
117+ }
100118
101- assertThat (of (withoutTrailingSlash ).param ("b" ).param ("a" ).build ())
119+ @ Test
120+ @ SuppressWarnings ({"DataFlowIssue" })
121+ public void testParamWithoutValue () throws IOException {
122+ assertThatNullPointerException ()
123+ .isThrownBy (() -> of (urlWithoutSlash ).param (null ));
124+
125+ assertThat (of (urlWithoutSlash ).param ("b" ).param ("a" ).build ())
126+ .isEqualTo (uriBuilderAsURL (urlWithoutSlash , o -> o .addParameter ("b" , null ).addParameter ("a" , null )))
102127 .hasToString ("http://localhost?b&a" );
103128
104- assertThat (of (withTrailingSlash ).param ("b" ).param ("a" ).build ())
129+ assertThat (of (urlWithSlash ).param ("b" ).param ("a" ).build ())
130+ .isEqualTo (uriBuilderAsURL (urlWithSlash , o -> o .addParameter ("b" , null ).addParameter ("a" , null )))
105131 .hasToString ("http://localhost/?b&a" );
106132 }
107133
108134 @ Test
109135 public void testTrailingSlash () throws IOException {
110- assertThat (of (withoutTrailingSlash ).trailingSlash (true ).build ())
136+ assertThat (of (urlWithoutSlash ).trailingSlash (true ).build ())
111137 .hasToString ("http://localhost/" );
112138
113- assertThat (of (withoutTrailingSlash ).trailingSlash (true ).param ("p1" , "v1" ).param ("p&=2" , "v&=2" ).build ())
139+ assertThat (of (urlWithoutSlash ).trailingSlash (true ).param ("p1" , "v1" ).param ("p&=2" , "v&=2" ).build ())
114140 .hasToString ("http://localhost/?p1=v1&p%26%3D2=v%26%3D2" );
115141
116- assertThat (of (withoutTrailingSlash ).trailingSlash (true ).path ("hello" ).path ("worl/d" ).build ())
142+ assertThat (of (urlWithoutSlash ).trailingSlash (true ).path ("hello" ).path ("worl/d" ).build ())
117143 .hasToString ("http://localhost/hello/worl%2Fd/" );
118144
119- assertThat (of (withoutTrailingSlash ).trailingSlash (true ).path ("hello" ).path ("worl/d" ).param ("p1" , "v1" ).param ("p&=2" , "v&=2" ).build ())
145+ assertThat (of (urlWithoutSlash ).trailingSlash (true ).path ("hello" ).path ("worl/d" ).param ("p1" , "v1" ).param ("p&=2" , "v&=2" ).build ())
120146 .hasToString ("http://localhost/hello/worl%2Fd/?p1=v1&p%26%3D2=v%26%3D2" );
121147 }
122148
123- private final URL withoutTrailingSlash = Parser .onURL ().parseValue ("http://localhost" ).orElseThrow (RuntimeException ::new );
124- private final URL withTrailingSlash = Parser .onURL ().parseValue ("http://localhost/" ).orElseThrow (RuntimeException ::new );
149+ @ Test
150+ public void testEncodingOfSpaceCharacter () throws IOException {
151+ assertThat (of (urlWithoutSlash ).path ("a b+" ).build ())
152+ .isEqualTo (uriBuilderAsURL (urlWithoutSlash , o -> o .appendPathSegments ("a b+" )))
153+ .hasToString ("http://localhost/a%20b%2B" );
154+
155+ assertThat (of (urlWithSlash ).path ("a b+" ).build ())
156+ .isNotEqualTo (uriBuilderAsURL (urlWithSlash , o -> o .appendPathSegments ("a b+" )))
157+ .hasToString ("http://localhost/a%20b%2B" );
158+
159+ assertThat (of (urlWithoutSlash ).param ("x y+" , "a b+" ).build ())
160+ .isEqualTo (uriBuilderAsURL (urlWithoutSlash , o -> o .addParameter ("x y+" ,"a b+" )))
161+ .hasToString ("http://localhost?x%20y%2B=a%20b%2B" );
162+
163+ assertThat (of (urlWithSlash ).param ("x y+" , "a b+" ).build ())
164+ .isEqualTo (uriBuilderAsURL (urlWithSlash , o -> o .addParameter ("x y+" ,"a b+" )))
165+ .hasToString ("http://localhost/?x%20y%2B=a%20b%2B" );
166+ }
167+
168+ private final URL urlWithoutSlash = Parser .onURL ().parseValue ("http://localhost" ).orElseThrow (RuntimeException ::new );
169+ private final URL urlWithSlash = Parser .onURL ().parseValue ("http://localhost/" ).orElseThrow (RuntimeException ::new );
170+
171+ private static URL uriBuilderAsURL (URL base , Consumer <? super URIBuilder > consumer ) {
172+ try {
173+ URIBuilder builder = new URIBuilder (base .toURI ());
174+ consumer .accept (builder );
175+ return builder .build ().toURL ();
176+ } catch (URISyntaxException | MalformedURLException ex ) {
177+ throw new RuntimeException (ex );
178+ }
179+ }
180+
181+ private static final Consumer <Object > DO_NOTHING = ignore -> {
182+ };
125183}
0 commit comments