1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -43,7 +43,6 @@ public class UriComponentsTests {
43
43
44
44
@ Test
45
45
public void expandAndEncode () {
46
-
47
46
UriComponents uri = UriComponentsBuilder
48
47
.fromPath ("/hotel list/{city} specials" ).queryParam ("q" , "{value}" ).build ()
49
48
.expand ("Z\u00fc rich" , "a+b" ).encode ();
@@ -53,7 +52,6 @@ public void expandAndEncode() {
53
52
54
53
@ Test
55
54
public void encodeAndExpand () {
56
-
57
55
UriComponents uri = UriComponentsBuilder
58
56
.fromPath ("/hotel list/{city} specials" ).queryParam ("q" , "{value}" ).encode ().build ()
59
57
.expand ("Z\u00fc rich" , "a+b" );
@@ -63,88 +61,86 @@ public void encodeAndExpand() {
63
61
64
62
@ Test
65
63
public void encodeAndExpandPartially () {
66
-
67
64
UriComponents uri = UriComponentsBuilder
68
65
.fromPath ("/hotel list/{city} specials" ).queryParam ("q" , "{value}" ).encode ()
69
- .uriVariables (Collections .singletonMap ("city" , "Z\u00fc rich" ))
70
- .build ();
66
+ .uriVariables (Collections .singletonMap ("city" , "Z\u00fc rich" )).build ();
71
67
72
68
assertThat (uri .expand ("a+b" ).toString ()).isEqualTo ("/hotel%20list/Z%C3%BCrich%20specials?q=a%2Bb" );
73
69
}
74
70
75
- @ Test // SPR-17168
71
+ @ Test // SPR-17168
76
72
public void encodeAndExpandWithDollarSign () {
77
73
UriComponents uri = UriComponentsBuilder .fromPath ("/path" ).queryParam ("q" , "{value}" ).encode ().build ();
78
74
assertThat (uri .expand ("JavaClass$1.class" ).toString ()).isEqualTo ("/path?q=JavaClass%241.class" );
79
75
}
80
76
81
77
@ Test
82
78
public void toUriEncoded () throws URISyntaxException {
83
- UriComponents uriComponents = UriComponentsBuilder .fromUriString (
84
- "https://example.com/hotel list/Z\u00fc rich" ).build ();
85
- assertThat (uriComponents .encode ().toUri ()).isEqualTo (new URI ("https://example.com/hotel%20list/Z%C3%BCrich" ));
79
+ UriComponents uri = UriComponentsBuilder .fromUriString ("https://example.com/hotel list/Z\u00fc rich" ).build ();
80
+ assertThat (uri .encode ().toUri ()).isEqualTo (new URI ("https://example.com/hotel%20list/Z%C3%BCrich" ));
86
81
}
87
82
88
83
@ Test
89
84
public void toUriNotEncoded () throws URISyntaxException {
90
- UriComponents uriComponents = UriComponentsBuilder .fromUriString (
91
- "https://example.com/hotel list/Z\u00fc rich" ).build ();
92
- assertThat (uriComponents .toUri ()).isEqualTo (new URI ("https://example.com/hotel%20list/Z\u00fc rich" ));
85
+ UriComponents uri = UriComponentsBuilder .fromUriString ("https://example.com/hotel list/Z\u00fc rich" ).build ();
86
+ assertThat (uri .toUri ()).isEqualTo (new URI ("https://example.com/hotel%20list/Z\u00fc rich" ));
93
87
}
94
88
95
89
@ Test
96
90
public void toUriAlreadyEncoded () throws URISyntaxException {
97
- UriComponents uriComponents = UriComponentsBuilder .fromUriString (
98
- "https://example.com/hotel%20list/Z%C3%BCrich" ).build (true );
99
- UriComponents encoded = uriComponents .encode ();
100
- assertThat (encoded .toUri ()).isEqualTo (new URI ("https://example.com/hotel%20list/Z%C3%BCrich" ));
91
+ UriComponents uri = UriComponentsBuilder .fromUriString ("https://example.com/hotel%20list/Z%C3%BCrich" ).build (true );
92
+ assertThat (uri .encode ().toUri ()).isEqualTo (new URI ("https://example.com/hotel%20list/Z%C3%BCrich" ));
101
93
}
102
94
103
95
@ Test
104
96
public void toUriWithIpv6HostAlreadyEncoded () throws URISyntaxException {
105
- UriComponents uriComponents = UriComponentsBuilder .fromUriString (
97
+ UriComponents uri = UriComponentsBuilder .fromUriString (
106
98
"http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich" ).build (true );
107
- UriComponents encoded = uriComponents .encode ();
108
- assertThat (encoded .toUri ()).isEqualTo (new URI ("http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich" ));
99
+
100
+ assertThat (uri .encode ().toUri ()).isEqualTo (
101
+ new URI ("http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich" ));
109
102
}
110
103
111
104
@ Test
112
105
public void expand () {
113
- UriComponents uriComponents = UriComponentsBuilder .fromUriString (
114
- "https://example.com" ). path ( "/{foo} {bar}" ). build ( );
115
- uriComponents = uriComponents . expand ( "1 2" , "3 4" );
116
- assertThat (uriComponents .getPath ()).isEqualTo ("/1 2 3 4" );
117
- assertThat (uriComponents .toUriString ()).isEqualTo ("https://example.com/1 2 3 4" );
106
+ UriComponents uri = UriComponentsBuilder .fromUriString ("https://example.com" ). path ( "/{foo} {bar}" ). build ();
107
+ uri = uri . expand ( "1 2" , "3 4" );
108
+
109
+ assertThat (uri .getPath ()).isEqualTo ("/1 2 3 4" );
110
+ assertThat (uri .toUriString ()).isEqualTo ("https://example.com/1 2 3 4" );
118
111
}
119
112
120
- @ Test // SPR-13311
113
+ @ Test // SPR-13311
121
114
public void expandWithRegexVar () {
122
115
String template = "/myurl/{name:[a-z]{1,5}}/show" ;
123
- UriComponents uriComponents = UriComponentsBuilder .fromUriString (template ).build ();
124
- uriComponents = uriComponents .expand (Collections .singletonMap ("name" , "test" ));
125
- assertThat (uriComponents .getPath ()).isEqualTo ("/myurl/test/show" );
116
+ UriComponents uri = UriComponentsBuilder .fromUriString (template ).build ();
117
+ uri = uri .expand (Collections .singletonMap ("name" , "test" ));
118
+
119
+ assertThat (uri .getPath ()).isEqualTo ("/myurl/test/show" );
126
120
}
127
121
128
- @ Test // SPR-17630
122
+ @ Test // SPR-17630
129
123
public void uirTemplateExpandWithMismatchedCurlyBraces () {
130
- assertThat (UriComponentsBuilder .fromUriString ("/myurl/?q={{{{" ).encode ().build ().toUriString ()).isEqualTo ("/myurl/?q=%7B%7B%7B%7B" );
124
+ UriComponents uri = UriComponentsBuilder .fromUriString ("/myurl/?q={{{{" ).encode ().build ();
125
+ assertThat (uri .toUriString ()).isEqualTo ("/myurl/?q=%7B%7B%7B%7B" );
131
126
}
132
127
133
- @ Test // gh-22447
128
+ @ Test // gh-22447
134
129
public void expandWithFragmentOrder () {
135
- UriComponents uriComponents = UriComponentsBuilder
130
+ UriComponents uri = UriComponentsBuilder
136
131
.fromUriString ("https://{host}/{path}#{fragment}" ).build ()
137
132
.expand ("example.com" , "foo" , "bar" );
138
133
139
- assertThat (uriComponents .toUriString ()).isEqualTo ("https://example.com/foo#bar" );
134
+ assertThat (uri .toUriString ()).isEqualTo ("https://example.com/foo#bar" );
140
135
}
141
136
142
- @ Test // SPR-12123
137
+ @ Test // SPR-12123
143
138
public void port () {
144
139
UriComponents uri1 = fromUriString ("https://example.com:8080/bar" ).build ();
145
140
UriComponents uri2 = fromUriString ("https://example.com/bar" ).port (8080 ).build ();
146
141
UriComponents uri3 = fromUriString ("https://example.com/bar" ).port ("{port}" ).build ().expand (8080 );
147
142
UriComponents uri4 = fromUriString ("https://example.com/bar" ).port ("808{digit}" ).build ().expand (0 );
143
+
148
144
assertThat (uri1 .getPort ()).isEqualTo (8080 );
149
145
assertThat (uri1 .toUriString ()).isEqualTo ("https://example.com:8080/bar" );
150
146
assertThat (uri2 .getPort ()).isEqualTo (8080 );
@@ -175,20 +171,22 @@ public void invalidEncodedSequence() {
175
171
176
172
@ Test
177
173
public void normalize () {
178
- UriComponents uriComponents = UriComponentsBuilder .fromUriString ("https://example.com/foo/../bar" ).build ();
179
- assertThat (uriComponents .normalize ().toString ()).isEqualTo ("https://example.com/bar" );
174
+ UriComponents uri = UriComponentsBuilder .fromUriString ("https://example.com/foo/../bar" ).build ();
175
+ assertThat (uri .normalize ().toString ()).isEqualTo ("https://example.com/bar" );
180
176
}
181
177
182
178
@ Test
183
179
public void serializable () throws Exception {
184
- UriComponents uriComponents = UriComponentsBuilder .fromUriString (
180
+ UriComponents uri = UriComponentsBuilder .fromUriString (
185
181
"https://example.com" ).path ("/{foo}" ).query ("bar={baz}" ).build ();
182
+
186
183
ByteArrayOutputStream bos = new ByteArrayOutputStream ();
187
184
ObjectOutputStream oos = new ObjectOutputStream (bos );
188
- oos .writeObject (uriComponents );
185
+ oos .writeObject (uri );
189
186
ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (bos .toByteArray ()));
190
187
UriComponents readObject = (UriComponents ) ois .readObject ();
191
- assertThat (uriComponents .toString ()).isEqualTo (readObject .toString ());
188
+
189
+ assertThat (uri .toString ()).isEqualTo (readObject .toString ());
192
190
}
193
191
194
192
@ Test
@@ -197,6 +195,7 @@ public void copyToUriComponentsBuilder() {
197
195
UriComponentsBuilder targetBuilder = UriComponentsBuilder .newInstance ();
198
196
source .copyToUriComponentsBuilder (targetBuilder );
199
197
UriComponents result = targetBuilder .build ().encode ();
198
+
200
199
assertThat (result .getPath ()).isEqualTo ("/foo/bar/ba%2Fz" );
201
200
assertThat (result .getPathSegments ()).isEqualTo (Arrays .asList ("foo" , "bar" , "ba%2Fz" ));
202
201
}
@@ -207,6 +206,7 @@ public void equalsHierarchicalUriComponents() {
207
206
UriComponents uric1 = UriComponentsBuilder .fromUriString (url ).path ("/{foo}" ).query ("bar={baz}" ).build ();
208
207
UriComponents uric2 = UriComponentsBuilder .fromUriString (url ).path ("/{foo}" ).query ("bar={baz}" ).build ();
209
208
UriComponents uric3 = UriComponentsBuilder .fromUriString (url ).path ("/{foo}" ).query ("bin={baz}" ).build ();
209
+
210
210
assertThat (uric1 ).isInstanceOf (HierarchicalUriComponents .class );
211
211
assertThat (uric1 ).isEqualTo (uric1 );
212
212
assertThat (uric1 ).isEqualTo (uric2 );
@@ -219,6 +219,7 @@ public void equalsOpaqueUriComponents() {
219
219
UriComponents uric1 = UriComponentsBuilder .fromUriString (baseUrl + "/foo/bar" ).build ();
220
220
UriComponents uric2 = UriComponentsBuilder .fromUriString (baseUrl + "/foo/bar" ).build ();
221
221
UriComponents uric3 = UriComponentsBuilder .fromUriString (baseUrl + "/foo/bin" ).build ();
222
+
222
223
assertThat (uric1 ).isInstanceOf (OpaqueUriComponents .class );
223
224
assertThat (uric1 ).isEqualTo (uric1 );
224
225
assertThat (uric1 ).isEqualTo (uric2 );
0 commit comments