org.springframework.mock.web.test.MockHttpServletRequest Java Examples

The following examples show how to use org.springframework.mock.web.test.MockHttpServletRequest. 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: MarshallingViewTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void renderModelKeyWithJaxbElement() throws Exception {
	String toBeMarshalled = "value";
	String modelKey = "key";
	view.setModelKey(modelKey);
	Map<String, Object> model = new HashMap<>();
	model.put(modelKey, new JAXBElement<>(new QName("model"), String.class, toBeMarshalled));

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();

	given(marshallerMock.supports(String.class)).willReturn(true);
	marshallerMock.marshal(eq(toBeMarshalled), isA(StreamResult.class));

	view.render(model, request, response);
	assertEquals("Invalid content type", "application/xml", response.getContentType());
	assertEquals("Invalid content length", 0, response.getContentLength());
}
 
Example #2
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 #3
Source File: MarshallingViewTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void renderModelKey() throws Exception {
	Object toBeMarshalled = new Object();
	String modelKey = "key";
	view.setModelKey(modelKey);
	Map<String, Object> model = new HashMap<String, Object>();
	model.put(modelKey, toBeMarshalled);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();

	given(marshallerMock.supports(Object.class)).willReturn(true);
	marshallerMock.marshal(eq(toBeMarshalled), isA(StreamResult.class));

	view.render(model, request, response);
	assertEquals("Invalid content type", "application/xml", response.getContentType());
	assertEquals("Invalid content length", 0, response.getContentLength());
}
 
Example #4
Source File: RequestScopeTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void circleLeadsToException() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	RequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	try {
		String name = "requestScopedObjectCircle1";
		assertNull(request.getAttribute(name));

		this.beanFactory.getBean(name);
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.contains(BeanCurrentlyInCreationException.class));
	}
}
 
Example #5
Source File: ViewResolutionIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private MockHttpServletResponse runTest(Class<?> configClass) throws ServletException, IOException {
	String basePath = "org/springframework/web/servlet/config/annotation";
	MockServletContext servletContext = new MockServletContext(basePath);
	MockServletConfig servletConfig = new MockServletConfig(servletContext);
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	MockHttpServletResponse response = new MockHttpServletResponse();

	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	context.register(configClass);
	context.setServletContext(servletContext);
	context.refresh();
	DispatcherServlet servlet = new DispatcherServlet(context);
	servlet.init(servletConfig);
	servlet.service(request, response);
	return response;
}
 
Example #6
Source File: ServletRequestDataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testBindingWithNestedObjectCreation() throws Exception {
	TestBean tb = new TestBean();

	ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "person");
	binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(new TestBean());
		}
	});

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addParameter("spouse", "someValue");
	request.addParameter("spouse.name", "test");
	binder.bind(request);

	assertNotNull(tb.getSpouse());
	assertEquals("test", tb.getSpouse().getName());
}
 
Example #7
Source File: ResponseBodyEmitterReturnValueHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {

	List<HttpMessageConverter<?>> converters = Arrays.asList(
			new StringHttpMessageConverter(), new MappingJackson2HttpMessageConverter());

	this.handler = new ResponseBodyEmitterReturnValueHandler(converters);
	this.mavContainer = new ModelAndViewContainer();

	this.request = new MockHttpServletRequest();
	this.response = new MockHttpServletResponse();
	this.webRequest = new ServletWebRequest(this.request, this.response);

	AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
	WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
	this.request.setAsyncSupported(true);
}
 
Example #8
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 #9
Source File: AtomFeedViewTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void render() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();

	Map<String, String> model = new LinkedHashMap<>();
	model.put("2", "This is entry 2");
	model.put("1", "This is entry 1");

	view.render(model, request, response);
	assertEquals("Invalid content-type", "application/atom+xml", response.getContentType());
	String expected = "<feed xmlns=\"http://www.w3.org/2005/Atom\">" + "<title>Test Feed</title>" +
			"<entry><title>2</title><summary>This is entry 2</summary></entry>" +
			"<entry><title>1</title><summary>This is entry 1</summary></entry>" + "</feed>";
	assertThat(response.getContentAsString(), isSimilarTo(expected));
}
 
Example #10
Source File: RequestLoggingFilterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void payloadReader() throws Exception {
	filter.setIncludePayload(true);

	final MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
	MockHttpServletResponse response = new MockHttpServletResponse();

	final String requestBody = "Hello World";
	request.setContent(requestBody.getBytes("UTF-8"));

	FilterChain filterChain = new FilterChain() {
		@Override
		public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse)
				throws IOException, ServletException {
			((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
			String buf = FileCopyUtils.copyToString(filterRequest.getReader());
			assertEquals(requestBody, buf);
		}
	};

	filter.doFilter(request, response, filterChain);

	assertNotNull(filter.afterRequestMessage);
	assertTrue(filter.afterRequestMessage.contains(requestBody));
}
 
Example #11
Source File: SessionScopeTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void destructionAtSessionTermination() throws Exception {
	MockHttpSession session = new MockHttpSession();
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setSession(session);
	ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);

	RequestContextHolder.setRequestAttributes(requestAttributes);
	String name = "sessionScopedDisposableObject";
	assertNull(session.getAttribute(name));
	DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
	assertEquals(session.getAttribute(name), bean);
	assertSame(bean, this.beanFactory.getBean(name));

	requestAttributes.requestCompleted();
	session.invalidate();
	assertTrue(bean.wasDestroyed());
}
 
Example #12
Source File: DispatcherServletTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void cleanupAfterIncludeWithRemove() throws ServletException, IOException {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
	MockHttpServletResponse response = new MockHttpServletResponse();

	request.setAttribute("test1", "value1");
	request.setAttribute("test2", "value2");
	WebApplicationContext wac = new StaticWebApplicationContext();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
	simpleDispatcherServlet.service(request, response);

	assertEquals("value1", request.getAttribute("test1"));
	assertEquals("value2", request.getAttribute("test2"));
	assertEquals(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
	assertNull(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
	assertNull(request.getAttribute("command"));
}
 
Example #13
Source File: ServletRequestDataBinderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testFieldDefaultPreemptsFieldMarker() throws Exception {
	TestBean target = new TestBean();
	ServletRequestDataBinder binder = new ServletRequestDataBinder(target);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addParameter("!postProcessed", "on");
	request.addParameter("_postProcessed", "visible");
	request.addParameter("postProcessed", "on");
	binder.bind(request);
	assertTrue(target.isPostProcessed());

	request.removeParameter("postProcessed");
	binder.bind(request);
	assertTrue(target.isPostProcessed());

	request.removeParameter("!postProcessed");
	binder.bind(request);
	assertFalse(target.isPostProcessed());
}
 
Example #14
Source File: ShallowEtagHeaderFilterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void filterSendError() throws Exception {
	final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels");
	MockHttpServletResponse response = new MockHttpServletResponse();

	final byte[] responseBody = "Hello World".getBytes("UTF-8");
	FilterChain filterChain = (filterRequest, filterResponse) -> {
		assertEquals("Invalid request passed", request, filterRequest);
		response.setContentLength(100);
		FileCopyUtils.copy(responseBody, filterResponse.getOutputStream());
		((HttpServletResponse) filterResponse).sendError(HttpServletResponse.SC_FORBIDDEN);
	};
	filter.doFilter(request, response, filterChain);

	assertEquals("Invalid status", 403, response.getStatus());
	assertNull("Invalid ETag header", response.getHeader("ETag"));
	assertEquals("Invalid Content-Length header", 100, response.getContentLength());
	assertArrayEquals("Invalid content", responseBody, response.getContentAsByteArray());
}
 
Example #15
Source File: RequestPartMethodArgumentResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void resolvePartArrayArgument() throws Exception {
	MockPart part1 = new MockPart("requestPart1", "Hello World 1".getBytes());
	MockPart part2 = new MockPart("requestPart2", "Hello World 2".getBytes());
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("POST");
	request.setContentType("multipart/form-data");
	request.addPart(part1);
	request.addPart(part2);
	webRequest = new ServletWebRequest(request);

	Object result = resolver.resolveArgument(paramPartArray, null, webRequest, null);

	assertTrue(result instanceof Part[]);
	Part[] parts = (Part[]) result;
	assertThat(parts, Matchers.arrayWithSize(2));
	assertEquals(parts[0], part1);
	assertEquals(parts[1], part2);
}
 
Example #16
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void mavResolver() throws ServletException, IOException {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			ModelAndViewResolver[] mavResolvers = new ModelAndViewResolver[] {new MyModelAndViewResolver()};
			adapterDef.getPropertyValues().add("modelAndViewResolvers", mavResolvers);
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, ModelAndViewResolverController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("myValue", response.getContentAsString());

}
 
Example #17
Source File: AbstractTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
protected MockPageContext createPageContext() {
	MockServletContext sc = new MockServletContext();
	SimpleWebApplicationContext wac = new SimpleWebApplicationContext();
	wac.setServletContext(sc);
	wac.setNamespace("test");
	wac.refresh();

	MockHttpServletRequest request = new MockHttpServletRequest(sc);
	MockHttpServletResponse response = new MockHttpServletResponse();
	if (inDispatcherServlet()) {
		request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
		LocaleResolver lr = new AcceptHeaderLocaleResolver();
		request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr);
		ThemeResolver tr = new FixedThemeResolver();
		request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr);
		request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac);
	}
	else {
		sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	}

	return new MockPageContext(sc, request, response);
}
 
Example #18
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void mavResolver() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		ModelAndViewResolver[] mavResolvers = new ModelAndViewResolver[] {new MyModelAndViewResolver()};
		adapterDef.getPropertyValues().add("modelAndViewResolvers", mavResolvers);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, ModelAndViewResolverController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("myValue", response.getContentAsString());

}
 
Example #19
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void binderInitializingCommandProvidingFormController() throws Exception {
	initServlet(wac -> wac.registerBeanDefinition("viewResolver",
			new RootBeanDefinition(TestViewResolver.class)),
			MyBinderInitializingCommandProvidingFormController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
	request.addParameter("defaultName", "myDefaultName");
	request.addParameter("age", "value2");
	request.addParameter("date", "2007-10-02");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("myView-String:myDefaultName-typeMismatch-tb1-myOriginalValue", response.getContentAsString());
}
 
Example #20
Source File: HeadersRequestConditionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test // SPR-16674
public void compareToWithMoreSpecificMatchByValue() {
	MockHttpServletRequest request = new MockHttpServletRequest();

	HeadersRequestCondition condition1 = new HeadersRequestCondition("foo=a");
	HeadersRequestCondition condition2 = new HeadersRequestCondition("foo");

	int result = condition1.compareTo(condition2, request);
	assertTrue("Invalid comparison result: " + result, result < 0);

	result = condition2.compareTo(condition1, request);
	assertTrue("Invalid comparison result: " + result, result > 0);
}
 
Example #21
Source File: UriTemplateServletAnnotationControllerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void extension() throws Exception {
	initServlet(SimpleUriTemplateController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/42.xml");
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("test-42", response.getContentAsString());

}
 
Example #22
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void dataClassBindingWithOptional() throws Exception {
	initServletWithControllers(OptionalDataClassController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bind");
	request.addParameter("param1", "value1");
	request.addParameter("param2", "true");
	request.addParameter("param3", "3");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("value1-true-3", response.getContentAsString());
}
 
Example #23
Source File: CommonsMultipartResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void withServletContextAndFilter() throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());
	wac.registerSingleton("filterMultipartResolver", MockCommonsMultipartResolver.class, new MutablePropertyValues());
	wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
	wac.refresh();
	wac.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	CommonsMultipartResolver resolver = new CommonsMultipartResolver(wac.getServletContext());
	assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));

	MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
	filterConfig.addInitParameter("class", "notWritable");
	filterConfig.addInitParameter("unknownParam", "someValue");
	final MultipartFilter filter = new MultipartFilter();
	filter.init(filterConfig);

	final List<MultipartFile> files = new ArrayList<MultipartFile>();
	final FilterChain filterChain = new FilterChain() {
		@Override
		public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
			MultipartHttpServletRequest request = (MultipartHttpServletRequest) servletRequest;
			files.addAll(request.getFileMap().values());
		}
	};

	FilterChain filterChain2 = new PassThroughFilterChain(filter, filterChain);

	MockHttpServletRequest originalRequest = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();
	originalRequest.setMethod("POST");
	originalRequest.setContentType("multipart/form-data");
	originalRequest.addHeader("Content-type", "multipart/form-data");
	filter.doFilter(originalRequest, response, filterChain2);

	CommonsMultipartFile file1 = (CommonsMultipartFile) files.get(0);
	CommonsMultipartFile file2 = (CommonsMultipartFile) files.get(1);
	assertTrue(((MockFileItem) file1.getFileItem()).deleted);
	assertTrue(((MockFileItem) file2.getFileItem()).deleted);
}
 
Example #24
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void optionalParamPresent() throws Exception {
	initServletWithControllers(OptionalParamController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
	request.addParameter("id", "val");
	request.addParameter("flag", "true");
	request.addHeader("header", "otherVal");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("val-true-otherVal", response.getContentAsString());
}
 
Example #25
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 #26
Source File: UrlFilenameViewControllerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void multiLevel() throws Exception {
	UrlFilenameViewController ctrl = new UrlFilenameViewController();
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/docs/cvs/commit.html");
	MockHttpServletResponse response = new MockHttpServletResponse();
	ModelAndView mv = ctrl.handleRequest(request, response);
	assertEquals("docs/cvs/commit", mv.getViewName());
	assertTrue(mv.getModel().isEmpty());
}
 
Example #27
Source File: ResourceHttpRequestHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void resolvePathWithTraversal() throws Exception {
	for (HttpMethod method : HttpMethod.values()) {
		this.request = new MockHttpServletRequest("GET", "");
		this.response = new MockHttpServletResponse();
		testResolvePathWithTraversal(method);
	}
}
 
Example #28
Source File: AppCacheManifestTransformerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void syntaxErrorInManifest() throws Exception {
	this.request = new MockHttpServletRequest("GET", "/static/error.appcache");
	Resource resource = getResource("error.appcache");
	Resource result = this.transformer.transform(this.request, resource, this.chain);

	assertEquals(resource, result);
}
 
Example #29
Source File: UriTemplateServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void simple() throws Exception {
	initServletWithControllers(SimpleUriTemplateController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/42");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("test-42-7", response.getContentAsString());
}
 
Example #30
Source File: UriTemplateServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void multiple() throws Exception {
	initServletWithControllers(MultipleUriTemplateController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/42;q=24/bookings/21-other;q=12");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(200, response.getStatus());
	assertEquals("test-42-q24-21-other-q12", response.getContentAsString());
}