Java Code Examples for org.springframework.mock.web.test.MockHttpServletRequest#setScheme()

The following examples show how to use org.springframework.mock.web.test.MockHttpServletRequest#setScheme() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServletUriComponentsBuilderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void fromRequestWithForwardedHostAndPort() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(80);
	request.setRequestURI("/mvc-showcase");
	request.addHeader("X-Forwarded-Proto", "https");
	request.addHeader("X-Forwarded-Host", "84.198.58.199");
	request.addHeader("X-Forwarded-Port", "443");

	HttpServletRequest requestToUse = adaptFromForwardedHeaders(request);
	UriComponents result =  ServletUriComponentsBuilder.fromRequest(requestToUse).build();

	assertEquals("https://84.198.58.199/mvc-showcase", result.toString());
}
 
Example 2
Source File: UriComponentsBuilderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void fromHttpRequestResetsPortBeforeSettingIt() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addHeader("X-Forwarded-Proto", "https");
	request.addHeader("X-Forwarded-Host", "84.198.58.199");
	request.addHeader("X-Forwarded-Port", 443);
	request.setScheme("http");
	request.setServerName("example.com");
	request.setServerPort(80);
	request.setRequestURI("/rest/mobile/users/1");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("https", result.getScheme());
	assertEquals("84.198.58.199", result.getHost());
	assertEquals(-1, result.getPort());
	assertEquals("/rest/mobile/users/1", result.getPath());
}
 
Example 3
Source File: UriComponentsBuilderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-16262
public void fromHttpRequestForwardedHeaderWithProtoAndServerPort()  {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addHeader("Forwarded", "proto=https");
	request.setScheme("http");
	request.setServerPort(8080);
	request.setServerName("example.com");
	request.setRequestURI("/rest/mobile/users/1");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("https", result.getScheme());
	assertEquals("example.com", result.getHost());
	assertEquals("/rest/mobile/users/1", result.getPath());
	assertEquals(-1, result.getPort());
	assertEquals("https://example.com/rest/mobile/users/1", result.toUriString());
}
 
Example 4
Source File: UriComponentsBuilderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-16262
public void fromHttpRequestForwardedHeaderWithProtoAndServerPort()  {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addHeader("Forwarded", "proto=https");
	request.setScheme("http");
	request.setServerPort(8080);
	request.setServerName("example.com");
	request.setRequestURI("/rest/mobile/users/1");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("https", result.getScheme());
	assertEquals("example.com", result.getHost());
	assertEquals("/rest/mobile/users/1", result.getPath());
	assertEquals(-1, result.getPort());
	assertEquals("https://example.com/rest/mobile/users/1", result.toUriString());
}
 
Example 5
Source File: UriComponentsBuilderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void fromHttpRequestForwardedHeaderWithoutHostPortAndWithServerPort()  {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addHeader("Forwarded", "proto=https; host=84.198.58.199");
	request.setScheme("http");
	request.setServerPort(8080);
	request.setServerName("example.com");
	request.setRequestURI("/rest/mobile/users/1");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("https", result.getScheme());
	assertEquals("84.198.58.199", result.getHost());
	assertEquals("/rest/mobile/users/1", result.getPath());
	assertEquals(-1, result.getPort());
	assertEquals("https://84.198.58.199/rest/mobile/users/1", result.toUriString());
}
 
Example 6
Source File: UriComponentsBuilderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void fromHttpRequestWithForwardedHostWithForwardedScheme() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(10080);
	request.addHeader("X-Forwarded-Host", "example.org");
	request.addHeader("X-Forwarded-Proto", "https");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("example.org", result.getHost());
	assertEquals("https", result.getScheme());
	assertEquals(-1, result.getPort());
}
 
Example 7
Source File: UriComponentsBuilderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void fromHttpRequest() throws URISyntaxException {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(-1);
	request.setRequestURI("/path");
	request.setQueryString("a=1");

	UriComponents result = UriComponentsBuilder.fromHttpRequest(new ServletServerHttpRequest(request)).build();
	assertEquals("http", result.getScheme());
	assertEquals("localhost", result.getHost());
	assertEquals(-1, result.getPort());
	assertEquals("/path", result.getPath());
	assertEquals("a=1", result.getQuery());
}
 
Example 8
Source File: UriComponentsBuilderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void fromHttpRequestMultipleForwardedHeader() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addHeader("Forwarded", "host=84.198.58.199;proto=https");
	request.addHeader("Forwarded", "proto=ftp; host=1.2.3.4");
	request.setScheme("http");
	request.setServerName("example.com");
	request.setRequestURI("/rest/mobile/users/1");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("https", result.getScheme());
	assertEquals("84.198.58.199", result.getHost());
	assertEquals("/rest/mobile/users/1", result.getPath());
}
 
Example 9
Source File: UriComponentsBuilderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void fromHttpRequestWithForwardedHostWithForwardedScheme() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(10080);
	request.addHeader("X-Forwarded-Host", "example.org");
	request.addHeader("X-Forwarded-Proto", "https");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("example.org", result.getHost());
	assertEquals("https", result.getScheme());
	assertEquals(-1, result.getPort());
}
 
Example 10
Source File: UriComponentsBuilderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-12771
public void fromHttpRequestResetsPortBeforeSettingIt() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addHeader("X-Forwarded-Proto", "https");
	request.addHeader("X-Forwarded-Host", "84.198.58.199");
	request.addHeader("X-Forwarded-Port", 443);
	request.setScheme("http");
	request.setServerName("example.com");
	request.setServerPort(80);
	request.setRequestURI("/rest/mobile/users/1");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("https", result.getScheme());
	assertEquals("84.198.58.199", result.getHost());
	assertEquals(-1, result.getPort());
	assertEquals("/rest/mobile/users/1", result.getPath());
}
 
Example 11
Source File: WebUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private boolean checkSameOrigin(String scheme, String serverName, int port, String originHeader) {
	MockHttpServletRequest servletRequest = new MockHttpServletRequest();
	ServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
	servletRequest.setScheme(scheme);
	servletRequest.setServerName(serverName);
	if (port != -1) {
		servletRequest.setServerPort(port);
	}
	servletRequest.addHeader(HttpHeaders.ORIGIN, originHeader);
	return WebUtils.isSameOrigin(request);
}
 
Example 12
Source File: UriComponentsBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test  // SPR-10701
public void fromHttpRequestWithForwardedHostIncludingPort() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(-1);
	request.setRequestURI("/mvc-showcase");
	request.addHeader("X-Forwarded-Host", "webtest.foo.bar.com:443");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("webtest.foo.bar.com", result.getHost());
	assertEquals(443, result.getPort());
}
 
Example 13
Source File: UriComponentsBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test  // SPR-12813
public void fromHttpRequestWithForwardedPortMultiValueHeader() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(9090);
	request.setRequestURI("/mvc-showcase");
	request.addHeader("X-Forwarded-Host", "a.example.org");
	request.addHeader("X-Forwarded-Port", "80,52022");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("http://a.example.org/mvc-showcase", result.toString());
}
 
Example 14
Source File: UriComponentsBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test  // SPR-11855
public void fromHttpRequestWithForwardedHostAndPort() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(8080);
	request.addHeader("X-Forwarded-Host", "foobarhost");
	request.addHeader("X-Forwarded-Port", "9090");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("foobarhost", result.getHost());
	assertEquals(9090, result.getPort());
}
 
Example 15
Source File: UriComponentsBuilderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void fromHttpRequestForwardedHeader() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addHeader("Forwarded", "proto=https; host=84.198.58.199");
	request.setScheme("http");
	request.setServerName("example.com");
	request.setRequestURI("/rest/mobile/users/1");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("https", result.getScheme());
	assertEquals("84.198.58.199", result.getHost());
	assertEquals("/rest/mobile/users/1", result.getPath());
}
 
Example 16
Source File: UriComponentsBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test  // SPR-14761
public void fromHttpRequestWithForwardedIPv6Host() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(-1);
	request.setRequestURI("/mvc-showcase");
	request.addHeader("X-Forwarded-Host", "[1abc:2abc:3abc::5ABC:6abc]");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("http://[1abc:2abc:3abc::5ABC:6abc]/mvc-showcase", result.toString());
}
 
Example 17
Source File: UriComponentsBuilderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void fromHttpRequestForwardedHeaderQuoted() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addHeader("Forwarded", "proto=\"https\"; host=\"84.198.58.199\"");
	request.setScheme("http");
	request.setServerName("example.com");
	request.setRequestURI("/rest/mobile/users/1");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("https", result.getScheme());
	assertEquals("84.198.58.199", result.getHost());
	assertEquals("/rest/mobile/users/1", result.getPath());
}
 
Example 18
Source File: UriComponentsBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test  // SPR-10701
public void fromHttpRequestWithForwardedHostIncludingPort() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(-1);
	request.setRequestURI("/mvc-showcase");
	request.addHeader("X-Forwarded-Host", "webtest.foo.bar.com:443");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("webtest.foo.bar.com", result.getHost());
	assertEquals(443, result.getPort());
}
 
Example 19
Source File: UriComponentsBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test  // SPR-11140
public void fromHttpRequestWithForwardedHostMultiValuedHeader() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(-1);
	request.addHeader("X-Forwarded-Host", "a.example.org, b.example.org, c.example.org");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("a.example.org", result.getHost());
	assertEquals(-1, result.getPort());
}
 
Example 20
Source File: UriComponentsBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test  // SPR-16863
public void fromHttpRequestWithForwardedSsl() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("example.org");
	request.setServerPort(10080);
	request.addHeader("X-Forwarded-Ssl", "on");

	HttpRequest httpRequest = new ServletServerHttpRequest(request);
	UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

	assertEquals("https", result.getScheme());
	assertEquals("example.org", result.getHost());
	assertEquals(-1, result.getPort());
}