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

The following examples show how to use org.springframework.mock.web.test.MockHttpServletRequest#setServerName() . 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 spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void fromRequestWithForwardedHostAndPort() {
	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");

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

	assertEquals("https://84.198.58.199/mvc-showcase", result.toString());
}
 
Example 2
Source File: UriComponentsBuilderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-12816
public void fromHttpRequestWithForwardedProtoMultiValueHeader() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(8080);
	request.setRequestURI("/mvc-showcase");
	request.addHeader("X-Forwarded-Host", "a.example.org");
	request.addHeader("X-Forwarded-Port", "443");
	request.addHeader("X-Forwarded-Proto", "https,https");

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

	assertEquals("https://a.example.org/mvc-showcase", result.toString());
}
 
Example 3
Source File: UriComponentsBuilderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void fromHttpRequestForwardedHeaderWithHostPortAndServerPort()  {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addHeader("Forwarded", "proto=https; host=84.198.58.199:9090");
	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(9090, result.getPort());
	assertEquals("https://84.198.58.199:9090/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-12771
public void fromHttpRequestWithForwardedProtoAndDefaultPort() {
	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");

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

	assertEquals("https://84.198.58.199/mvc-showcase", result.toString());
}
 
Example 5
Source File: CorsFilterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void sameOriginRequest() throws ServletException, IOException {

	MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.GET.name(), "https://domain1.com/test.html");
	request.addHeader(HttpHeaders.ORIGIN, "https://domain1.com");
	request.setScheme("https");
	request.setServerName("domain1.com");
	request.setServerPort(443);
	MockHttpServletResponse response = new MockHttpServletResponse();

	FilterChain filterChain = (filterRequest, filterResponse) -> {
		assertNull(response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
		assertNull(response.getHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS));
	};
	filter.doFilter(request, response, filterChain);
}
 
Example 6
Source File: ServletUriComponentsBuilderTests.java    From java-technology-stack 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 7
Source File: WebUtilsTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void testWithForwardedHeader(String serverName, int port, String forwardedHeader,
		String originHeader) throws Exception {

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setServerName(serverName);
	if (port != -1) {
		request.setServerPort(port);
	}
	request.addHeader("Forwarded", forwardedHeader);
	request.addHeader(HttpHeaders.ORIGIN, originHeader);

	HttpServletRequest requestToUse = adaptFromForwardedHeaders(request);
	ServerHttpRequest httpRequest = new ServletServerHttpRequest(requestToUse);

	assertTrue(WebUtils.isSameOrigin(httpRequest));
}
 
Example 8
Source File: UriComponentsBuilderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-12816
public void fromHttpRequestWithForwardedProtoMultiValueHeader() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(8080);
	request.setRequestURI("/mvc-showcase");
	request.addHeader("X-Forwarded-Host", "a.example.org");
	request.addHeader("X-Forwarded-Port", "443");
	request.addHeader("X-Forwarded-Proto", "https,https");

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

	assertEquals("https://a.example.org/mvc-showcase", result.toString());
}
 
Example 9
Source File: WebUtilsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void testWithXForwardedHeaders(String serverName, int port, String forwardedProto,
		String forwardedHost, int forwardedPort, String originHeader) throws Exception {

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setServerName(serverName);
	if (port != -1) {
		request.setServerPort(port);
	}
	if (forwardedProto != null) {
		request.addHeader("X-Forwarded-Proto", forwardedProto);
	}
	if (forwardedHost != null) {
		request.addHeader("X-Forwarded-Host", forwardedHost);
	}
	if (forwardedPort != -1) {
		request.addHeader("X-Forwarded-Port", String.valueOf(forwardedPort));
	}
	request.addHeader(HttpHeaders.ORIGIN, originHeader);

	HttpServletRequest requestToUse = adaptFromForwardedHeaders(request);
	ServerHttpRequest httpRequest = new ServletServerHttpRequest(requestToUse);

	assertTrue(WebUtils.isSameOrigin(httpRequest));
}
 
Example 10
Source File: UriComponentsBuilderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void fromHttpRequestWithForwardedProtoAndDefaultPort() {
	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");

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

	assertEquals("https://84.198.58.199/mvc-showcase", result.toString());
}
 
Example 11
Source File: UriComponentsBuilderTests.java    From java-technology-stack 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 12
Source File: UriComponentsBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void fromHttpRequestWithForwardedHost() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("https");
	request.setServerName("localhost");
	request.setServerPort(-1);
	request.setRequestURI("/mvc-showcase");
	request.addHeader("X-Forwarded-Host", "anotherHost");

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

	assertEquals("https://anotherHost/mvc-showcase", result.toString());
}
 
Example 13
Source File: UriComponentsBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void fromHttpRequestMultipleForwardedHeaderComma()  {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addHeader("Forwarded", "host=84.198.58.199 ;proto=https, 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 14
Source File: UriComponentsBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test  // SPR-14761
public void fromHttpRequestWithForwardedIPv4Host() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("https");
	request.setServerName("localhost");
	request.setServerPort(-1);
	request.setRequestURI("/mvc-showcase");
	request.addHeader("Forwarded", "host=192.168.0.1");

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

	assertEquals("https://192.168.0.1/mvc-showcase", result.toString());
}
 
Example 15
Source File: UriComponentsBuilderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test  // SPR-11872
public void fromHttpRequestWithForwardedHostWithDefaultPort() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(10080);
	request.addHeader("X-Forwarded-Host", "example.org");

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

	assertEquals("example.org", result.getHost());
	assertEquals(-1, result.getPort());
}
 
Example 16
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 17
Source File: UriComponentsBuilderTests.java    From spring-analysis-note 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());
}
 
Example 18
Source File: UriComponentsBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void fromHttpRequestWithForwardedHost() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setScheme("http");
	request.setServerName("localhost");
	request.setServerPort(-1);
	request.setRequestURI("/mvc-showcase");
	request.addHeader("X-Forwarded-Host", "anotherHost");

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

	assertEquals("http://anotherHost/mvc-showcase", result.toString());
}
 
Example 19
Source File: DefaultServerRequestTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void uri() {
	MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/");
	servletRequest.setServerName("example.com");
	servletRequest.setScheme("https");
	servletRequest.setServerPort(443);

	DefaultServerRequest request =
			new DefaultServerRequest(servletRequest, this.messageConverters);

	assertEquals(URI.create("https://example.com/"), request.uri());
}
 
Example 20
Source File: WebUtilsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private boolean checkSameOrigin(String serverName, int port, String originHeader) {
	MockHttpServletRequest servletRequest = new MockHttpServletRequest();
	ServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
	servletRequest.setServerName(serverName);
	if (port != -1) {
		servletRequest.setServerPort(port);
	}
	request.getHeaders().set(HttpHeaders.ORIGIN, originHeader);
	return WebUtils.isSameOrigin(request);
}