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

The following examples show how to use org.springframework.mock.web.test.MockHttpServletRequest#setContextPath() . 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: ResourceUrlEncodingFilterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void encodeUrlWithContext() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context/foo");
	request.setContextPath("/context");

	testEncodeUrl(request, "/context/resources/bar.css",
			"/context/resources/bar-11e16cf79faee7ac698c805cf28248d2.css");
}
 
Example 2
Source File: ResourceUrlProviderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test // SPR-16526
public void getStaticResourceWithMissingContextPath() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setContextPath("/contextpath-longer-than-request-path");
	request.setRequestURI("/contextpath-longer-than-request-path/style.css");
	String url = "/resources/foo.css";
	String resolvedUrl = this.urlProvider.getForRequestUrl(request, url);
	assertNull(resolvedUrl);
}
 
Example 3
Source File: ResourceUrlEncodingFilterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test // SPR-13018
public void encodeEmptyUrlWithContext() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context/foo");
	request.setContextPath("/context");

	testEncodeUrl(request, "?foo=1", "?foo=1");
}
 
Example 4
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void emptyValueMapping() throws Exception {
	initServletWithControllers(ControllerWithEmptyValueMapping.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
	request.setContextPath("/foo");
	request.setServletPath("");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("test", response.getContentAsString());
}
 
Example 5
Source File: ResourceUrlEncodingFilterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test // SPR-13847
public void encodeUrlPreventStringOutOfBounds() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context-path/index");
	request.setContextPath("/context-path");
	request.setServletPath("");

	testEncodeUrl(request, "index?key=value", "index?key=value");
}
 
Example 6
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void emptyValueMapping() throws Exception {
	initServletWithControllers(ControllerWithEmptyValueMapping.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
	request.setContextPath("/foo");
	request.setServletPath("");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("test", response.getContentAsString());
}
 
Example 7
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void errorThrownFromHandlerMethod() throws Exception {
	initServletWithControllers(ControllerWithErrorThrown.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
	request.setContextPath("/foo");
	request.setServletPath("");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("test", response.getContentAsString());
}
 
Example 8
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void emptyValueMapping() throws Exception {
	initServletWithControllers(ControllerWithEmptyValueMapping.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
	request.setContextPath("/foo");
	request.setServletPath("");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("test", response.getContentAsString());
}
 
Example 9
Source File: UrlFilenameViewControllerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void withContextMapping() throws Exception {
	UrlFilenameViewController ctrl = new UrlFilenameViewController();
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/docs/cvs/commit.html");
	request.setContextPath("/myapp");
	MockHttpServletResponse response = new MockHttpServletResponse();
	ModelAndView mv = ctrl.handleRequest(request, response);
	assertEquals("docs/cvs/commit", mv.getViewName());
	assertTrue(mv.getModel().isEmpty());
}
 
Example 10
Source File: ResourceUrlEncodingFilterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void encodeContextPathUrlWithSuffix() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context/");
	request.setContextPath("/context");
	request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, this.resourceUrlProvider);
	MockHttpServletResponse response = new MockHttpServletResponse();

	this.filter.doFilterInternal(request, response, (request1, response1) -> {
		String result = ((HttpServletResponse) response1).encodeURL("/context/resources/bar.css");
		assertEquals("/context/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", result);
	});
}
 
Example 11
Source File: ResourceUrlEncodingFilterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test // SPR-13847
public void encodeUrlPreventStringOutOfBounds() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context-path/index");
	request.setContextPath("/context-path");
	request.setServletPath("");

	testEncodeUrl(request, "index?key=value", "index?key=value");
}
 
Example 12
Source File: ControllerClassNameHandlerMappingTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void withContextPath() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/welcome");
	request.setContextPath("/myapp");
	HandlerExecutionChain chain = this.hm.getHandler(request);
	assertEquals(this.wac.getBean("welcome"), chain.getHandler());
}
 
Example 13
Source File: ResourceUrlEncodingFilterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void encodeContextPathUrlWithSuffix() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context/");
	request.setContextPath("/context");

	testEncodeUrl(request, "/context/resources/bar.css",
			"/context/resources/bar-11e16cf79faee7ac698c805cf28248d2.css");
}
 
Example 14
Source File: ResourceUrlEncodingFilterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test // SPR-17535
public void encodeUrlWithFragment() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
	request.setContextPath("/");

	testEncodeUrl(request, "/resources/bar.css#something",
			"/resources/bar-11e16cf79faee7ac698c805cf28248d2.css#something");

	testEncodeUrl(request,
			"/resources/bar.css?foo=bar&url=http://example.org#something",
			"/resources/bar-11e16cf79faee7ac698c805cf28248d2.css?foo=bar&url=http://example.org#something");
}
 
Example 15
Source File: ResourceUrlEncodingFilterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void encodeUrlWithContextAndForwardedRequest() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context/foo");
	request.setContextPath("/context");

	this.filter.doFilter(request, new MockHttpServletResponse(), (req, res) -> {
		req.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, this.urlProvider);
		request.setRequestURI("/forwarded");
		request.setContextPath("/");
		String result = ((HttpServletResponse) res).encodeURL("/context/resources/bar.css");
		assertEquals("/context/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", result);
	});
}
 
Example 16
Source File: ResourceUrlEncodingFilterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void encodeUrlWithContext() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context/foo");
	request.setContextPath("/context");

	testEncodeUrl(request, "/context/resources/bar.css",
			"/context/resources/bar-11e16cf79faee7ac698c805cf28248d2.css");
}
 
Example 17
Source File: ResourceUrlProviderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test // SPR-16526
public void getStaticResourceWithMissingContextPath() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setContextPath("/contextpath-longer-than-request-path");
	request.setRequestURI("/contextpath-longer-than-request-path/style.css");
	String url = "/resources/foo.css";
	String resolvedUrl = this.urlProvider.getForRequestUrl(request, url);
	assertNull(resolvedUrl);
}
 
Example 18
Source File: RequestScopeTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void getFromScope() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setContextPath("/path");
	RequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	String name = "requestScopedObject";
	assertNull(request.getAttribute(name));
	TestBean bean = (TestBean) this.beanFactory.getBean(name);
	assertEquals("/path", bean.getName());
	assertSame(bean, request.getAttribute(name));
	assertSame(bean, this.beanFactory.getBean(name));
}
 
Example 19
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 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");

	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());
}
 
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());
}