Skip to content

Commit fde9279

Browse files
rwinchsnicoll
authored andcommitted
Fix http URLs
See gh-22839
1 parent b5fba14 commit fde9279

File tree

36 files changed

+216
-208
lines changed

36 files changed

+216
-208
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcher.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141
* <p>Alternatively, one can also specify the port. For example, the following would match
4242
* any request to the host {@code "code.jquery.com"} with the port of {@code 80}.
4343
*
44-
* <pre class="code">WebRequestMatcher cdnMatcher = new HostMatcher("code.jquery.com:80");</pre>
44+
* <pre class="code">WebRequestMatcher cdnMatcher = new HostMatcher("code.jquery.example:80");</pre>
4545
*
46-
* <p>The above {@code cdnMatcher} would match {@code "http://code.jquery.com/jquery.js"}
47-
* which has a default port of {@code 80} and {@code "http://code.jquery.com:80/jquery.js"}.
48-
* However, it would not match {@code "https://code.jquery.com/jquery.js"}
46+
* <p>The above {@code cdnMatcher} would match {@code "http://code.jquery.example/jquery.js"}
47+
* which has a default port of {@code 80} and {@code "http://code.jquery.example:80/jquery.js"}.
48+
* However, it would not match {@code "https://code.jquery.example/jquery.js"}
4949
* which has a default port of {@code 443}.
5050
*
5151
* @author Rob Winch

spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,30 @@ public class MockRestRequestMatchersTests {
4242

4343
@Test
4444
public void requestTo() throws Exception {
45-
this.request.setURI(new URI("http://www.foo.com/bar"));
45+
this.request.setURI(new URI("http://www.foo.example/bar"));
4646

47-
MockRestRequestMatchers.requestTo("http://www.foo.com/bar").match(this.request);
47+
MockRestRequestMatchers.requestTo("http://www.foo.example/bar").match(this.request);
4848
}
4949

5050
@Test // SPR-15819
5151
public void requestToUriTemplate() throws Exception {
52-
this.request.setURI(new URI("http://www.foo.com/bar"));
52+
this.request.setURI(new URI("http://www.foo.example/bar"));
5353

54-
MockRestRequestMatchers.requestToUriTemplate("http://www.foo.com/{bar}", "bar").match(this.request);
54+
MockRestRequestMatchers.requestToUriTemplate("http://www.foo.example/{bar}", "bar").match(this.request);
5555
}
5656

5757
@Test
5858
public void requestToNoMatch() throws Exception {
59-
this.request.setURI(new URI("http://www.foo.com/bar"));
59+
this.request.setURI(new URI("http://www.foo.example/bar"));
6060

6161
assertThatThrownBy(
62-
() -> MockRestRequestMatchers.requestTo("http://www.foo.com/wrong").match(this.request))
62+
() -> MockRestRequestMatchers.requestTo("http://www.foo.example/wrong").match(this.request))
6363
.isInstanceOf(AssertionError.class);
6464
}
6565

6666
@Test
6767
public void requestToContains() throws Exception {
68-
this.request.setURI(new URI("http://www.foo.com/bar"));
68+
this.request.setURI(new URI("http://www.foo.example/bar"));
6969

7070
MockRestRequestMatchers.requestTo(containsString("bar")).match(this.request);
7171
}
@@ -157,14 +157,14 @@ public void headersWithMissingValue() throws Exception {
157157

158158
@Test
159159
public void queryParam() throws Exception {
160-
this.request.setURI(new URI("http://www.foo.com/a?foo=bar&foo=baz"));
160+
this.request.setURI(new URI("http://www.foo.example/a?foo=bar&foo=baz"));
161161

162162
MockRestRequestMatchers.queryParam("foo", "bar", "baz").match(this.request);
163163
}
164164

165165
@Test
166166
public void queryParamMissing() throws Exception {
167-
this.request.setURI(new URI("http://www.foo.com/a"));
167+
this.request.setURI(new URI("http://www.foo.example/a"));
168168

169169
assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", "bar").match(this.request))
170170
.isInstanceOf(AssertionError.class)
@@ -173,7 +173,7 @@ public void queryParamMissing() throws Exception {
173173

174174
@Test
175175
public void queryParamMissingValue() throws Exception {
176-
this.request.setURI(new URI("http://www.foo.com/a?foo=bar&foo=baz"));
176+
this.request.setURI(new URI("http://www.foo.example/a?foo=bar&foo=baz"));
177177

178178
assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", "bad").match(this.request))
179179
.isInstanceOf(AssertionError.class)
@@ -182,14 +182,14 @@ public void queryParamMissingValue() throws Exception {
182182

183183
@Test
184184
public void queryParamContains() throws Exception {
185-
this.request.setURI(new URI("http://www.foo.com/a?foo=bar&foo=baz"));
185+
this.request.setURI(new URI("http://www.foo.example/a?foo=bar&foo=baz"));
186186

187187
MockRestRequestMatchers.queryParam("foo", containsString("ba")).match(this.request);
188188
}
189189

190190
@Test
191191
public void queryParamContainsWithMissingValue() throws Exception {
192-
this.request.setURI(new URI("http://www.foo.com/a?foo=bar&foo=baz"));
192+
this.request.setURI(new URI("http://www.foo.example/a?foo=bar&foo=baz"));
193193

194194
assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request))
195195
.isInstanceOf(AssertionError.class)

spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HostRequestMatcherTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ public class HostRequestMatcherTests extends AbstractWebRequestMatcherTests {
3131
public void localhost() throws Exception {
3232
WebRequestMatcher matcher = new HostRequestMatcher("localhost");
3333
assertMatches(matcher, "http://localhost/jquery-1.11.0.min.js");
34-
assertDoesNotMatch(matcher, "http://example.com/jquery-1.11.0.min.js");
34+
assertDoesNotMatch(matcher, "http://company.example/jquery-1.11.0.min.js");
3535
}
3636

3737
@Test
3838
public void multipleHosts() throws Exception {
3939
WebRequestMatcher matcher = new HostRequestMatcher("localhost", "example.com");
4040
assertMatches(matcher, "http://localhost/jquery-1.11.0.min.js");
41-
assertMatches(matcher, "http://example.com/jquery-1.11.0.min.js");
41+
assertMatches(matcher, "https://example.com/jquery-1.11.0.min.js");
4242
}
4343

4444
@Test

spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class HtmlUnitRequestBuilderTests {
7171

7272
@Before
7373
public void setup() throws Exception {
74-
webRequest = new WebRequest(new URL("http://example.com:80/test/this/here"));
74+
webRequest = new WebRequest(new URL("https://example.com/test/this/here"));
7575
webRequest.setHttpMethod(HttpMethod.GET);
7676
requestBuilder = new HtmlUnitRequestBuilder(sessions, webClient, webRequest);
7777
}
@@ -174,7 +174,7 @@ public void buildRequestContextPathUsesFirstSegmentByDefault() {
174174

175175
@Test
176176
public void buildRequestContextPathUsesNoFirstSegmentWithDefault() throws MalformedURLException {
177-
webRequest.setUrl(new URL("http://example.com/"));
177+
webRequest.setUrl(new URL("https://example.com/"));
178178
String contextPath = requestBuilder.buildRequest(servletContext).getContextPath();
179179

180180
assertThat(contextPath).isEqualTo("");
@@ -342,7 +342,8 @@ public void buildRequestLocalName() {
342342
}
343343

344344
@Test
345-
public void buildRequestLocalPort() {
345+
public void buildRequestLocalPort() throws Exception {
346+
webRequest.setUrl(new URL("http://localhost:80/test/this/here"));
346347
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
347348

348349
assertThat(actualRequest.getLocalPort()).isEqualTo(80);
@@ -599,6 +600,7 @@ public void buildRequestRemoteHost() throws Exception {
599600

600601
@Test
601602
public void buildRequestRemotePort() throws Exception {
603+
webRequest.setUrl(new URL("http://localhost:80/test/this/here"));
602604
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
603605

604606
assertThat(actualRequest.getRemotePort()).isEqualTo(80);
@@ -615,7 +617,7 @@ public void buildRequestRemotePort8080() throws Exception {
615617

616618
@Test
617619
public void buildRequestRemotePort80WithDefault() throws Exception {
618-
webRequest.setUrl(new URL("http://example.com/"));
620+
webRequest.setUrl(new URL("http://company.example/"));
619621

620622
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
621623

@@ -647,11 +649,12 @@ public void buildRequestUri() {
647649
@Test
648650
public void buildRequestUrl() {
649651
String uri = requestBuilder.buildRequest(servletContext).getRequestURL().toString();
650-
assertThat(uri).isEqualTo("http://example.com/test/this/here");
652+
assertThat(uri).isEqualTo("https://example.com/test/this/here");
651653
}
652654

653655
@Test
654656
public void buildRequestSchemeHttp() throws Exception {
657+
webRequest.setUrl(new URL("http://localhost:80/test/this/here"));
655658
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
656659

657660
assertThat(actualRequest.getScheme()).isEqualTo("http");
@@ -674,6 +677,7 @@ public void buildRequestServerName() throws Exception {
674677

675678
@Test
676679
public void buildRequestServerPort() throws Exception {
680+
webRequest.setUrl(new URL("http://localhost:80/test/this/here"));
677681
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
678682

679683
assertThat(actualRequest.getServerPort()).isEqualTo(80);

spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockMvcConnectionBuilderSupportTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ public void mockMvcExampleDotCom() throws Exception {
108108

109109
assertMockMvcUsed(conn, "http://localhost/");
110110
assertMockMvcUsed(conn, "https://example.com/");
111-
assertMockMvcNotUsed(conn, "http://other.com/");
111+
assertMockMvcNotUsed(conn, "http://other.example/");
112112
}
113113

114114
@Test
115115
public void mockMvcAlwaysUseMockMvc() throws Exception {
116116
WebConnection conn = this.builder.alwaysUseMockMvc().createConnection(this.client);
117-
assertMockMvcUsed(conn, "http://other.com/");
117+
assertMockMvcUsed(conn, "http://other.example/");
118118
}
119119

120120
@Test

spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/MockWebResponseBuilderTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class MockWebResponseBuilderTests {
5050

5151
@Before
5252
public void setup() throws Exception {
53-
this.webRequest = new WebRequest(new URL("http://example.com:80/test/this/here"));
53+
this.webRequest = new WebRequest(new URL("http://company.example:80/test/this/here"));
5454
this.responseBuilder = new MockWebResponseBuilder(System.currentTimeMillis(), this.webRequest, this.response);
5555
}
5656

@@ -66,7 +66,7 @@ public void constructorWithNullWebRequest() {
6666
@Test
6767
public void constructorWithNullResponse() throws Exception {
6868
assertThatIllegalArgumentException().isThrownBy(() ->
69-
new MockWebResponseBuilder(0L, new WebRequest(new URL("http://example.com:80/test/this/here")), null));
69+
new MockWebResponseBuilder(0L, new WebRequest(new URL("http://company.example:80/test/this/here")), null));
7070
}
7171

7272

spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,22 @@ public void getUriWithQueryString() throws URISyntaxException {
8181

8282
@Test // SPR-16414
8383
public void getUriWithQueryParam() throws URISyntaxException {
84+
mockRequest.setScheme("https");
85+
mockRequest.setServerPort(443);
8486
mockRequest.setServerName("example.com");
8587
mockRequest.setRequestURI("/path");
8688
mockRequest.setQueryString("query=foo");
87-
assertThat(request.getURI()).isEqualTo(new URI("http://example.com/path?query=foo"));
89+
assertThat(request.getURI()).isEqualTo(new URI("https://example.com/path?query=foo"));
8890
}
8991

9092
@Test // SPR-16414
9193
public void getUriWithMalformedQueryParam() throws URISyntaxException {
94+
mockRequest.setScheme("https");
95+
mockRequest.setServerPort(443);
9296
mockRequest.setServerName("example.com");
9397
mockRequest.setRequestURI("/path");
9498
mockRequest.setQueryString("query=foo%%x");
95-
assertThat(request.getURI()).isEqualTo(new URI("http://example.com/path"));
99+
assertThat(request.getURI()).isEqualTo(new URI("https://example.com/path"));
96100
}
97101

98102
@Test // SPR-13876

spring-web/src/test/java/org/springframework/web/cors/DefaultCorsProcessorTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class DefaultCorsProcessorTests {
5050
public void setup() {
5151
this.request = new MockHttpServletRequest();
5252
this.request.setRequestURI("/test.html");
53-
this.request.setServerName("domain1.com");
53+
this.request.setServerName("domain1.example");
5454
this.conf = new CorsConfiguration();
5555
this.response = new MockHttpServletResponse();
5656
this.response.setStatus(HttpServletResponse.SC_OK);
@@ -71,7 +71,7 @@ public void requestWithoutOriginHeader() throws Exception {
7171
@Test
7272
public void sameOriginRequest() throws Exception {
7373
this.request.setMethod(HttpMethod.GET.name());
74-
this.request.addHeader(HttpHeaders.ORIGIN, "http://domain1.com");
74+
this.request.addHeader(HttpHeaders.ORIGIN, "http://domain1.example");
7575

7676
this.processor.processRequest(this.conf, this.request, this.response);
7777
assertThat(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isFalse();
@@ -124,7 +124,7 @@ public void actualRequestCredentials() throws Exception {
124124
this.request.addHeader(HttpHeaders.ORIGIN, "https://domain2.com");
125125
this.conf.addAllowedOrigin("https://domain1.com");
126126
this.conf.addAllowedOrigin("https://domain2.com");
127-
this.conf.addAllowedOrigin("http://domain3.com");
127+
this.conf.addAllowedOrigin("http://domain3.example");
128128
this.conf.setAllowCredentials(true);
129129

130130
this.processor.processRequest(this.conf, this.request, this.response);
@@ -296,7 +296,7 @@ public void preflightRequestCredentials() throws Exception {
296296
this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS, "Header1");
297297
this.conf.addAllowedOrigin("https://domain1.com");
298298
this.conf.addAllowedOrigin("https://domain2.com");
299-
this.conf.addAllowedOrigin("http://domain3.com");
299+
this.conf.addAllowedOrigin("http://domain3.example");
300300
this.conf.addAllowedHeader("Header1");
301301
this.conf.setAllowCredentials(true);
302302

@@ -318,7 +318,7 @@ public void preflightRequestCredentialsWithOriginWildcard() throws Exception {
318318
this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS, "Header1");
319319
this.conf.addAllowedOrigin("https://domain1.com");
320320
this.conf.addAllowedOrigin("*");
321-
this.conf.addAllowedOrigin("http://domain3.com");
321+
this.conf.addAllowedOrigin("http://domain3.example");
322322
this.conf.addAllowedHeader("Header1");
323323
this.conf.setAllowCredentials(true);
324324

spring-web/src/test/java/org/springframework/web/cors/reactive/CorsUtilsTests.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class CorsUtilsTests {
3939

4040
@Test
4141
public void isCorsRequest() {
42-
ServerHttpRequest request = get("http://domain.com/").header(HttpHeaders.ORIGIN, "https://domain.com").build();
42+
ServerHttpRequest request = get("http://domain.example/").header(HttpHeaders.ORIGIN, "https://domain.com").build();
4343
assertThat(CorsUtils.isCorsRequest(request)).isTrue();
4444
}
4545

@@ -69,32 +69,32 @@ public void isNotPreFlightRequest() {
6969

7070
@Test // SPR-16262
7171
public void isSameOriginWithXForwardedHeaders() {
72-
String server = "mydomain1.com";
73-
testWithXForwardedHeaders(server, -1, "https", null, -1, "https://mydomain1.com");
74-
testWithXForwardedHeaders(server, 123, "https", null, -1, "https://mydomain1.com");
75-
testWithXForwardedHeaders(server, -1, "https", "mydomain2.com", -1, "https://mydomain2.com");
76-
testWithXForwardedHeaders(server, 123, "https", "mydomain2.com", -1, "https://mydomain2.com");
77-
testWithXForwardedHeaders(server, -1, "https", "mydomain2.com", 456, "https://mydomain2.com:456");
78-
testWithXForwardedHeaders(server, 123, "https", "mydomain2.com", 456, "https://mydomain2.com:456");
72+
String server = "mydomain1.example";
73+
testWithXForwardedHeaders(server, -1, "https", null, -1, "https://mydomain1.example");
74+
testWithXForwardedHeaders(server, 123, "https", null, -1, "https://mydomain1.example");
75+
testWithXForwardedHeaders(server, -1, "https", "mydomain2.example", -1, "https://mydomain2.example");
76+
testWithXForwardedHeaders(server, 123, "https", "mydomain2.example", -1, "https://mydomain2.example");
77+
testWithXForwardedHeaders(server, -1, "https", "mydomain2.example", 456, "https://mydomain2.example:456");
78+
testWithXForwardedHeaders(server, 123, "https", "mydomain2.example", 456, "https://mydomain2.example:456");
7979
}
8080

8181
@Test // SPR-16262
8282
public void isSameOriginWithForwardedHeader() {
83-
String server = "mydomain1.com";
84-
testWithForwardedHeader(server, -1, "proto=https", "https://mydomain1.com");
85-
testWithForwardedHeader(server, 123, "proto=https", "https://mydomain1.com");
86-
testWithForwardedHeader(server, -1, "proto=https; host=mydomain2.com", "https://mydomain2.com");
87-
testWithForwardedHeader(server, 123, "proto=https; host=mydomain2.com", "https://mydomain2.com");
88-
testWithForwardedHeader(server, -1, "proto=https; host=mydomain2.com:456", "https://mydomain2.com:456");
89-
testWithForwardedHeader(server, 123, "proto=https; host=mydomain2.com:456", "https://mydomain2.com:456");
83+
String server = "mydomain1.example";
84+
testWithForwardedHeader(server, -1, "proto=https", "https://mydomain1.example");
85+
testWithForwardedHeader(server, 123, "proto=https", "https://mydomain1.example");
86+
testWithForwardedHeader(server, -1, "proto=https; host=mydomain2.example", "https://mydomain2.example");
87+
testWithForwardedHeader(server, 123, "proto=https; host=mydomain2.example", "https://mydomain2.example");
88+
testWithForwardedHeader(server, -1, "proto=https; host=mydomain2.example:456", "https://mydomain2.example:456");
89+
testWithForwardedHeader(server, 123, "proto=https; host=mydomain2.example:456", "https://mydomain2.example:456");
9090
}
9191

9292
@Test // SPR-16362
9393
@SuppressWarnings("deprecation")
9494
public void isSameOriginWithDifferentSchemes() {
9595
MockServerHttpRequest request = MockServerHttpRequest
96-
.get("http://mydomain1.com")
97-
.header(HttpHeaders.ORIGIN, "https://mydomain1.com")
96+
.get("http://mydomain1.example")
97+
.header(HttpHeaders.ORIGIN, "https://mydomain1.example")
9898
.build();
9999
assertThat(CorsUtils.isSameOrigin(request)).isFalse();
100100
}

spring-web/src/test/java/org/springframework/web/cors/reactive/DefaultCorsProcessorTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void setup() {
5858
@Test
5959
public void requestWithoutOriginHeader() throws Exception {
6060
MockServerHttpRequest request = MockServerHttpRequest
61-
.method(HttpMethod.GET, "http://domain1.com/test.html")
61+
.method(HttpMethod.GET, "http://domain1.example/test.html")
6262
.build();
6363
ServerWebExchange exchange = MockServerWebExchange.from(request);
6464
this.processor.process(this.conf, exchange);
@@ -73,8 +73,8 @@ public void requestWithoutOriginHeader() throws Exception {
7373
@Test
7474
public void sameOriginRequest() throws Exception {
7575
MockServerHttpRequest request = MockServerHttpRequest
76-
.method(HttpMethod.GET, "http://domain1.com/test.html")
77-
.header(HttpHeaders.ORIGIN, "http://domain1.com")
76+
.method(HttpMethod.GET, "http://domain1.example/test.html")
77+
.header(HttpHeaders.ORIGIN, "http://domain1.example")
7878
.build();
7979
ServerWebExchange exchange = MockServerWebExchange.from(request);
8080
this.processor.process(this.conf, exchange);
@@ -129,7 +129,7 @@ public void actualRequestCredentials() throws Exception {
129129
ServerWebExchange exchange = actualRequest();
130130
this.conf.addAllowedOrigin("https://domain1.com");
131131
this.conf.addAllowedOrigin("https://domain2.com");
132-
this.conf.addAllowedOrigin("http://domain3.com");
132+
this.conf.addAllowedOrigin("http://domain3.example");
133133
this.conf.setAllowCredentials(true);
134134
this.processor.process(this.conf, exchange);
135135

@@ -306,7 +306,7 @@ public void preflightRequestCredentials() throws Exception {
306306

307307
this.conf.addAllowedOrigin("https://domain1.com");
308308
this.conf.addAllowedOrigin("https://domain2.com");
309-
this.conf.addAllowedOrigin("http://domain3.com");
309+
this.conf.addAllowedOrigin("http://domain3.example");
310310
this.conf.addAllowedHeader("Header1");
311311
this.conf.setAllowCredentials(true);
312312

@@ -330,7 +330,7 @@ public void preflightRequestCredentialsWithOriginWildcard() throws Exception {
330330

331331
this.conf.addAllowedOrigin("https://domain1.com");
332332
this.conf.addAllowedOrigin("*");
333-
this.conf.addAllowedOrigin("http://domain3.com");
333+
this.conf.addAllowedOrigin("http://domain3.example");
334334
this.conf.addAllowedHeader("Header1");
335335
this.conf.setAllowCredentials(true);
336336

0 commit comments

Comments
 (0)