16
16
17
17
package org .springframework .web .servlet .support ;
18
18
19
+ import static org .junit .Assert .assertEquals ;
20
+ import static org .junit .Assert .assertNull ;
21
+
19
22
import org .junit .Before ;
20
23
import org .junit .Test ;
21
-
22
24
import org .springframework .mock .web .test .MockHttpServletRequest ;
23
25
import org .springframework .web .context .request .RequestContextHolder ;
24
26
import org .springframework .web .context .request .ServletRequestAttributes ;
25
27
import org .springframework .web .util .UriComponents ;
26
28
27
- import static org .junit .Assert .*;
28
-
29
29
/**
30
30
* Unit tests for
31
31
* {@link org.springframework.web.servlet.support.ServletUriComponentsBuilder}.
@@ -47,59 +47,59 @@ public void setup() {
47
47
48
48
@ Test
49
49
public void fromRequest () {
50
- request .setRequestURI ("/mvc-showcase/data/param" );
51
- request .setQueryString ("foo=123" );
52
- String result = ServletUriComponentsBuilder .fromRequest (request ).build ().toUriString ();
50
+ this . request .setRequestURI ("/mvc-showcase/data/param" );
51
+ this . request .setQueryString ("foo=123" );
52
+ String result = ServletUriComponentsBuilder .fromRequest (this . request ).build ().toUriString ();
53
53
assertEquals ("http://localhost/mvc-showcase/data/param?foo=123" , result );
54
54
}
55
55
56
56
@ Test
57
57
public void fromRequestEncodedPath () {
58
- request .setRequestURI ("/mvc-showcase/data/foo%20bar" );
59
- String result = ServletUriComponentsBuilder .fromRequest (request ).build ().toUriString ();
58
+ this . request .setRequestURI ("/mvc-showcase/data/foo%20bar" );
59
+ String result = ServletUriComponentsBuilder .fromRequest (this . request ).build ().toUriString ();
60
60
assertEquals ("http://localhost/mvc-showcase/data/foo%20bar" , result );
61
61
}
62
62
63
63
@ Test
64
64
public void fromRequestAtypicalHttpPort () {
65
- request .setServerPort (8080 );
66
- String result = ServletUriComponentsBuilder .fromRequest (request ).build ().toUriString ();
65
+ this . request .setServerPort (8080 );
66
+ String result = ServletUriComponentsBuilder .fromRequest (this . request ).build ().toUriString ();
67
67
assertEquals ("http://localhost:8080" , result );
68
68
}
69
69
70
70
@ Test
71
71
public void fromRequestAtypicalHttpsPort () {
72
- request .setScheme ("https" );
73
- request .setServerPort (9043 );
74
- String result = ServletUriComponentsBuilder .fromRequest (request ).build ().toUriString ();
72
+ this . request .setScheme ("https" );
73
+ this . request .setServerPort (9043 );
74
+ String result = ServletUriComponentsBuilder .fromRequest (this . request ).build ().toUriString ();
75
75
assertEquals ("https://localhost:9043" , result );
76
76
}
77
77
78
78
@ Test
79
79
public void fromRequestUri () {
80
- request .setRequestURI ("/mvc-showcase/data/param" );
81
- request .setQueryString ("foo=123" );
82
- String result = ServletUriComponentsBuilder .fromRequestUri (request ).build ().toUriString ();
80
+ this . request .setRequestURI ("/mvc-showcase/data/param" );
81
+ this . request .setQueryString ("foo=123" );
82
+ String result = ServletUriComponentsBuilder .fromRequestUri (this . request ).build ().toUriString ();
83
83
assertEquals ("http://localhost/mvc-showcase/data/param" , result );
84
84
}
85
85
86
86
@ Test
87
87
public void fromRequestWithForwardedHost () {
88
- request .addHeader ("X-Forwarded-Host" , "anotherHost" );
89
- request .setRequestURI ("/mvc-showcase/data/param" );
90
- request .setQueryString ("foo=123" );
91
- String result = ServletUriComponentsBuilder .fromRequest (request ).build ().toUriString ();
88
+ this . request .addHeader ("X-Forwarded-Host" , "anotherHost" );
89
+ this . request .setRequestURI ("/mvc-showcase/data/param" );
90
+ this . request .setQueryString ("foo=123" );
91
+ String result = ServletUriComponentsBuilder .fromRequest (this . request ).build ().toUriString ();
92
92
assertEquals ("http://anotherHost/mvc-showcase/data/param?foo=123" , result );
93
93
}
94
94
95
95
// SPR-10701
96
96
97
97
@ Test
98
98
public void fromRequestWithForwardedHostIncludingPort () {
99
- request .addHeader ("X-Forwarded-Host" , "webtest.foo.bar.com:443" );
100
- request .setRequestURI ("/mvc-showcase/data/param" );
101
- request .setQueryString ("foo=123" );
102
- UriComponents result = ServletUriComponentsBuilder .fromRequest (request ).build ();
99
+ this . request .addHeader ("X-Forwarded-Host" , "webtest.foo.bar.com:443" );
100
+ this . request .setRequestURI ("/mvc-showcase/data/param" );
101
+ this . request .setQueryString ("foo=123" );
102
+ UriComponents result = ServletUriComponentsBuilder .fromRequest (this . request ).build ();
103
103
104
104
assertEquals ("webtest.foo.bar.com" , result .getHost ());
105
105
assertEquals (443 , result .getPort ());
@@ -132,7 +132,7 @@ public void fromRequestWithForwardedHostAndPort() {
132
132
public void fromRequestWithForwardedHostWithDefaultPort () {
133
133
this .request .setServerPort (10080 );
134
134
this .request .addHeader ("X-Forwarded-Host" , "example.org" );
135
- UriComponents result = ServletUriComponentsBuilder .fromRequest (request ).build ();
135
+ UriComponents result = ServletUriComponentsBuilder .fromRequest (this . request ).build ();
136
136
137
137
assertEquals ("example.org" , result .getHost ());
138
138
assertEquals ("should have used the default port of the forwarded request" , -1 , result .getPort ());
@@ -143,7 +143,7 @@ public void fromRequestWithForwardedHostWithForwardedScheme() {
143
143
this .request .setServerPort (10080 );
144
144
this .request .addHeader ("X-Forwarded-Proto" , "https" );
145
145
this .request .addHeader ("X-Forwarded-Host" , "example.org" );
146
- UriComponents result = ServletUriComponentsBuilder .fromRequest (request ).build ();
146
+ UriComponents result = ServletUriComponentsBuilder .fromRequest (this . request ).build ();
147
147
148
148
assertEquals ("example.org" , result .getHost ());
149
149
assertEquals ("should have derived scheme from header" , "https" , result .getScheme ());
@@ -154,7 +154,7 @@ public void fromRequestWithForwardedHostWithForwardedScheme() {
154
154
public void fromRequestWithForwardedPrefix () {
155
155
this .request .setRequestURI ("/bar" );
156
156
this .request .addHeader ("X-Forwarded-Prefix" , "/foo" );
157
- UriComponents result = ServletUriComponentsBuilder .fromRequest (request ).build ();
157
+ UriComponents result = ServletUriComponentsBuilder .fromRequest (this . request ).build ();
158
158
159
159
assertEquals ("http://localhost/foo/bar" , result .toUriString ());
160
160
}
@@ -163,51 +163,51 @@ public void fromRequestWithForwardedPrefix() {
163
163
public void fromRequestWithForwardedPrefixTrailingSlash () {
164
164
this .request .setRequestURI ("/bar" );
165
165
this .request .addHeader ("X-Forwarded-Prefix" , "/foo/" );
166
- UriComponents result = ServletUriComponentsBuilder .fromRequest (request ).build ();
166
+ UriComponents result = ServletUriComponentsBuilder .fromRequest (this . request ).build ();
167
167
168
168
assertEquals ("http://localhost/foo/bar" , result .toUriString ());
169
169
}
170
170
171
171
@ Test
172
172
public void fromContextPath () {
173
- request .setRequestURI ("/mvc-showcase/data/param" );
174
- request .setQueryString ("foo=123" );
175
- String result = ServletUriComponentsBuilder .fromContextPath (request ).build ().toUriString ();
173
+ this . request .setRequestURI ("/mvc-showcase/data/param" );
174
+ this . request .setQueryString ("foo=123" );
175
+ String result = ServletUriComponentsBuilder .fromContextPath (this . request ).build ().toUriString ();
176
176
assertEquals ("http://localhost/mvc-showcase" , result );
177
177
}
178
178
179
179
@ Test
180
180
public void fromContextPathWithForwardedPrefix () {
181
- request .addHeader ("X-Forwarded-Prefix" , "/prefix" );
182
- request .setContextPath ("/mvc-showcase" );
183
- request .setRequestURI ("/mvc-showcase/simple" );
184
- String result = ServletUriComponentsBuilder .fromContextPath (request ).build ().toUriString ();
181
+ this . request .addHeader ("X-Forwarded-Prefix" , "/prefix" );
182
+ this . request .setContextPath ("/mvc-showcase" );
183
+ this . request .setRequestURI ("/mvc-showcase/simple" );
184
+ String result = ServletUriComponentsBuilder .fromContextPath (this . request ).build ().toUriString ();
185
185
assertEquals ("http://localhost/prefix/mvc-showcase" , result );
186
186
}
187
187
188
188
@ Test
189
189
public void fromServletMapping () {
190
- request .setRequestURI ("/mvc-showcase/app/simple" );
191
- request .setServletPath ("/app" );
192
- request .setQueryString ("foo=123" );
193
- String result = ServletUriComponentsBuilder .fromServletMapping (request ).build ().toUriString ();
190
+ this . request .setRequestURI ("/mvc-showcase/app/simple" );
191
+ this . request .setServletPath ("/app" );
192
+ this . request .setQueryString ("foo=123" );
193
+ String result = ServletUriComponentsBuilder .fromServletMapping (this . request ).build ().toUriString ();
194
194
assertEquals ("http://localhost/mvc-showcase/app" , result );
195
195
}
196
196
197
197
@ Test
198
198
public void fromServletMappingWithForwardedPrefix () {
199
- request .addHeader ("X-Forwarded-Prefix" , "/prefix" );
200
- request .setContextPath ("/mvc-showcase" );
201
- request .setServletPath ("/app" );
202
- request .setRequestURI ("/mvc-showcase/app/simple" );
203
- String result = ServletUriComponentsBuilder .fromServletMapping (request ).build ().toUriString ();
199
+ this . request .addHeader ("X-Forwarded-Prefix" , "/prefix" );
200
+ this . request .setContextPath ("/mvc-showcase" );
201
+ this . request .setServletPath ("/app" );
202
+ this . request .setRequestURI ("/mvc-showcase/app/simple" );
203
+ String result = ServletUriComponentsBuilder .fromServletMapping (this . request ).build ().toUriString ();
204
204
assertEquals ("http://localhost/prefix/mvc-showcase/app" , result );
205
205
}
206
206
207
207
@ Test
208
208
public void fromCurrentRequest () {
209
- request .setRequestURI ("/mvc-showcase/data/param" );
210
- request .setQueryString ("foo=123" );
209
+ this . request .setRequestURI ("/mvc-showcase/data/param" );
210
+ this . request .setQueryString ("foo=123" );
211
211
RequestContextHolder .setRequestAttributes (new ServletRequestAttributes (this .request ));
212
212
try {
213
213
String result = ServletUriComponentsBuilder .fromCurrentRequest ().build ().toUriString ();
0 commit comments