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

The following examples show how to use org.springframework.mock.web.test.MockHttpServletRequest#setRequestURI() . 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: UriComponentsBuilderTests.java    From java-technology-stack 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 2
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 3
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 4
Source File: Spr7766Tests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
@Deprecated
public void test() throws Exception {
	AnnotationMethodHandlerAdapter adapter = new AnnotationMethodHandlerAdapter();
	ConfigurableWebBindingInitializer binder = new ConfigurableWebBindingInitializer();
	GenericConversionService service = new DefaultConversionService();
	service.addConverter(new ColorConverter());
	binder.setConversionService(service);
	adapter.setWebBindingInitializer(binder);
	Spr7766Controller controller = new Spr7766Controller();
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/colors");
	request.setPathInfo("/colors");
	request.addParameter("colors", "#ffffff,000000");
	MockHttpServletResponse response = new MockHttpServletResponse();
	adapter.handle(request, response, controller);
}
 
Example 5
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test(expected = TypeMismatchException.class)
public void testCustomConversionService() throws Exception {
	loadBeanDefinitions("mvc-config-custom-conversion-service.xml", 14);

	RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
	assertNotNull(mapping);
	mapping.setDefaultHandler(handlerMethod);

	// default web binding initializer behavior test
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	request.setRequestURI("/accounts/12345");
	request.addParameter("date", "2009-10-31");
	MockHttpServletResponse response = new MockHttpServletResponse();

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertEquals(1, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
	ConversionServiceExposingInterceptor interceptor = (ConversionServiceExposingInterceptor) chain.getInterceptors()[0];
	interceptor.preHandle(request, response, handler);
	assertSame(appContext.getBean("conversionService"), request.getAttribute(ConversionService.class.getName()));

	RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class);
	assertNotNull(adapter);
	adapter.handle(request, response, handlerMethod);
}
 
Example 6
Source File: UriComponentsBuilderTests.java    From java-technology-stack 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 7
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultServletHandlerWithOptionalAttributes() throws Exception {
	loadBeanDefinitions("mvc-config-default-servlet-optional-attrs.xml", 6);

	HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
	assertNotNull(adapter);

	DefaultServletHttpRequestHandler handler = appContext.getBean(DefaultServletHttpRequestHandler.class);
	assertNotNull(handler);

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE, mapping.getOrder());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/foo.css");
	request.setMethod("GET");

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertTrue(chain.getHandler() instanceof DefaultServletHttpRequestHandler);

	MockHttpServletResponse response = new MockHttpServletResponse();
	ModelAndView mv = adapter.handle(request, response, chain.getHandler());
	assertNull(mv);
}
 
Example 8
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 9
Source File: UriComponentsBuilderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void fromHttpRequestMultipleForwardedHeader()  {
	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 10
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 11
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testInterceptors() throws Exception {
	loadBeanDefinitions("mvc-config-interceptors.xml");

	RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
	assertNotNull(mapping);
	mapping.setDefaultHandler(handlerMethod);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	request.setRequestURI("/accounts/12345");
	request.addParameter("locale", "en");
	request.addParameter("theme", "green");

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof ThemeChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof UserRoleAuthorizationInterceptor);

	request.setRequestURI("/admin/users");
	chain = mapping.getHandler(request);
	assertEquals(2, chain.getInterceptors().length);

	request.setRequestURI("/logged/accounts/12345");
	chain = mapping.getHandler(request);
	assertEquals(3, chain.getInterceptors().length);

	request.setRequestURI("/foo/logged");
	chain = mapping.getHandler(request);
	assertEquals(3, chain.getInterceptors().length);
}
 
Example 12
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 13
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void regularParameterAsSingleString() throws Exception {
	initServletWithControllers(MultipartController.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/singleString");
	request.setMethod("POST");
	request.addParameter("content", "Juergen");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("Juergen", response.getContentAsString());
}
 
Example 14
Source File: ServletAnnotationControllerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void regularParameterAsSingleString() throws Exception {
	initServlet(MultipartController.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/singleString");
	request.setMethod("POST");
	request.addParameter("content", "Juergen");
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("Juergen", response.getContentAsString());
}
 
Example 15
Source File: UriComponentsBuilderTests.java    From spring-analysis-note 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 16
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 17
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void regularParametersAsStringArray() throws Exception {
	initServletWithControllers(MultipartController.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/stringArray");
	request.setMethod("POST");
	request.addParameter("content", "Juergen");
	request.addParameter("content", "Eva");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("Juergen-Eva", response.getContentAsString());
}
 
Example 18
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void regularParameterAsStringArray() throws Exception {
	initServletWithControllers(MultipartController.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/stringArray");
	request.setMethod("POST");
	request.addParameter("content", "Juergen");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("Juergen", response.getContentAsString());
}
 
Example 19
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void regularParameterAsSingleString() throws Exception {
	initServletWithControllers(MultipartController.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/singleString");
	request.setMethod("POST");
	request.addParameter("content", "Juergen");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("Juergen", response.getContentAsString());
}
 
Example 20
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/** WebSphere gives trailing servlet path slashes by default!! */
@Test
public void testViewControllersOnWebSphere() throws Exception {
	loadBeanDefinitions("mvc-config-view-controllers.xml", 19);

	SimpleUrlHandlerMapping mapping2 = appContext.getBean(SimpleUrlHandlerMapping.class);
	SimpleControllerHandlerAdapter adapter = appContext.getBean(SimpleControllerHandlerAdapter.class);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("GET");
	request.setRequestURI("/myapp/app/bar");
	request.setContextPath("/myapp");
	request.setServletPath("/app/");
	request.setAttribute("com.ibm.websphere.servlet.uri_non_decoded", "/myapp/app/bar");
	HandlerExecutionChain chain = mapping2.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	ModelAndView mv2 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
	assertEquals("baz", mv2.getViewName());

	request.setRequestURI("/myapp/app/");
	request.setContextPath("/myapp");
	request.setServletPath("/app/");
	chain = mapping2.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	ModelAndView mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
	assertEquals("root", mv3.getViewName());

	request.setRequestURI("/myapp/");
	request.setContextPath("/myapp");
	request.setServletPath("/");
	chain = mapping2.getHandler(request);
	assertEquals(4, chain.getInterceptors().length);
	assertTrue(chain.getInterceptors()[1] instanceof ConversionServiceExposingInterceptor);
	assertTrue(chain.getInterceptors()[2] instanceof LocaleChangeInterceptor);
	assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
	mv3 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
	assertEquals("root", mv3.getViewName());
}