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

The following examples show how to use org.springframework.mock.web.test.MockHttpServletRequest#setParameter() . 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: HandlerMethodAnnotationDetectionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequestMappingMethod() throws Exception {
	String datePattern = "MM:dd:yyyy";
	SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
	String dateA = "11:01:2011";
	String dateB = "11:02:2011";

	MockHttpServletRequest request = new MockHttpServletRequest("POST", "/path1/path2");
	request.setParameter("datePattern", datePattern);
	request.addHeader("header1", dateA);
	request.addHeader("header2", dateB);

	HandlerExecutionChain chain = handlerMapping.getHandler(request);
	assertNotNull(chain);

	ModelAndView mav = handlerAdapter.handle(request, new MockHttpServletResponse(), chain.getHandler());

	assertEquals("model attr1:", dateFormat.parse(dateA), mav.getModel().get("attr1"));
	assertEquals("model attr2:", dateFormat.parse(dateB), mav.getModel().get("attr2"));

	MockHttpServletResponse response = new MockHttpServletResponse();
	exceptionResolver.resolveException(request, response, chain.getHandler(), new Exception("failure"));
	assertEquals("text/plain;charset=ISO-8859-1", response.getHeader("Content-Type"));
	assertEquals("failure", response.getContentAsString());
}
 
Example 2
Source File: RequestMappingInfoTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void matchParamsCondition() {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
	request.setParameter("foo", "bar");

	RequestMappingInfo info =
			new RequestMappingInfo(
					new PatternsRequestCondition("/foo"), null,
					new ParamsRequestCondition("foo=bar"), null, null, null, null);
	RequestMappingInfo match = info.getMatchingCondition(request);

	assertNotNull(match);

	info = new RequestMappingInfo(
			new PatternsRequestCondition("/foo"), null,
			new ParamsRequestCondition("foo!=bar"), null, null, null, null);
	match = info.getMatchingCondition(request);

	assertNull(match);
}
 
Example 3
Source File: HandlerMethodAnnotationDetectionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testRequestMappingMethod() throws Exception {
	String datePattern = "MM:dd:yyyy";
	SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
	String dateA = "11:01:2011";
	String dateB = "11:02:2011";

	MockHttpServletRequest request = new MockHttpServletRequest("POST", "/path1/path2");
	request.setParameter("datePattern", datePattern);
	request.addHeader("header1", dateA);
	request.addHeader("header2", dateB);

	HandlerExecutionChain chain = handlerMapping.getHandler(request);
	assertNotNull(chain);

	ModelAndView mav = handlerAdapter.handle(request, new MockHttpServletResponse(), chain.getHandler());

	assertEquals("model attr1:", dateFormat.parse(dateA), mav.getModel().get("attr1"));
	assertEquals("model attr2:", dateFormat.parse(dateB), mav.getModel().get("attr2"));

	MockHttpServletResponse response = new MockHttpServletResponse();
	exceptionResolver.resolveException(request, response, chain.getHandler(), new Exception("failure"));
	assertEquals("text/plain;charset=ISO-8859-1", response.getHeader("Content-Type"));
	assertEquals("failure", response.getContentAsString());
}
 
Example 4
Source File: WebMvcConfigurationSupportExtensionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void contentNegotiation() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json");
	NativeWebRequest webRequest = new ServletWebRequest(request);

	ContentNegotiationManager manager = this.config.requestMappingHandlerMapping().getContentNegotiationManager();
	assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo.xml");
	assertEquals(Arrays.asList(MediaType.APPLICATION_XML), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo.rss");
	assertEquals(Arrays.asList(MediaType.valueOf("application/rss+xml")), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo.atom");
	assertEquals(Arrays.asList(MediaType.APPLICATION_ATOM_XML), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo");
	request.setParameter("f", "json");
	assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(webRequest));
}
 
Example 5
Source File: HandlerMethodAnnotationDetectionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testRequestMappingMethod() throws Exception {
	String datePattern = "MM:dd:yyyy";
	SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
	String dateA = "11:01:2011";
	String dateB = "11:02:2011";

	MockHttpServletRequest request = new MockHttpServletRequest("POST", "/path1/path2");
	request.setParameter("datePattern", datePattern);
	request.addHeader("header1", dateA);
	request.addHeader("header2", dateB);

	HandlerExecutionChain chain = handlerMapping.getHandler(request);
	assertNotNull(chain);

	ModelAndView mav = handlerAdapter.handle(request, new MockHttpServletResponse(), chain.getHandler());

	assertEquals("model attr1:", dateFormat.parse(dateA), mav.getModel().get("attr1"));
	assertEquals("model attr2:", dateFormat.parse(dateB), mav.getModel().get("attr2"));

	MockHttpServletResponse response = new MockHttpServletResponse();
	exceptionResolver.resolveException(request, response, chain.getHandler(), new Exception("failure"));
	assertEquals("text/plain;charset=ISO-8859-1", response.getHeader("Content-Type"));
	assertEquals("failure", response.getContentAsString());
}
 
Example 6
Source File: CompositeRequestConditionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void match() {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	request.setParameter("param1", "paramValue1");
	request.addHeader("header1", "headerValue1");

	RequestCondition<?> getPostCond = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST);
	RequestCondition<?> getCond = new RequestMethodsRequestCondition(RequestMethod.GET);

	CompositeRequestCondition condition = new CompositeRequestCondition(this.param1, getPostCond);
	CompositeRequestCondition matchingCondition = new CompositeRequestCondition(this.param1, getCond);

	assertEquals(matchingCondition, condition.getMatchingCondition(request));
}
 
Example 7
Source File: RequestConditionHolderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void match() {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	request.setParameter("name1", "value1");

	RequestMethodsRequestCondition rm = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST);
	RequestConditionHolder custom = new RequestConditionHolder(rm);
	RequestMethodsRequestCondition expected = new RequestMethodsRequestCondition(RequestMethod.GET);

	assertEquals(expected, custom.getMatchingCondition(request).getCondition());
}
 
Example 8
Source File: CompositeRequestConditionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void match() {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	request.setParameter("param1", "paramValue1");
	request.addHeader("header1", "headerValue1");

	RequestCondition<?> getPostCond = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST);
	RequestCondition<?> getCond = new RequestMethodsRequestCondition(RequestMethod.GET);

	CompositeRequestCondition condition = new CompositeRequestCondition(this.param1, getPostCond);
	CompositeRequestCondition matchingCondition = new CompositeRequestCondition(this.param1, getCond);

	assertEquals(matchingCondition, condition.getMatchingCondition(request));
}
 
Example 9
Source File: WebMvcConfigurationSupportExtensionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void contentNegotiation() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json");
	NativeWebRequest webRequest = new ServletWebRequest(request);

	RequestMappingHandlerMapping mapping = this.config.requestMappingHandlerMapping();
	ContentNegotiationManager manager = mapping.getContentNegotiationManager();
	assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo.xml");
	assertEquals(Collections.singletonList(APPLICATION_XML), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo.rss");
	assertEquals(Collections.singletonList(MediaType.valueOf("application/rss+xml")),
			manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo.atom");
	assertEquals(Collections.singletonList(APPLICATION_ATOM_XML), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo");
	request.setParameter("f", "json");
	assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/resources/foo.gif");
	SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) this.config.resourceHandlerMapping();
	handlerMapping.setApplicationContext(this.context);
	HandlerExecutionChain chain = handlerMapping.getHandler(request);
	assertNotNull(chain);
	ResourceHttpRequestHandler handler = (ResourceHttpRequestHandler) chain.getHandler();
	assertNotNull(handler);
	assertSame(manager, handler.getContentNegotiationManager());
}
 
Example 10
Source File: RequestMappingInfoTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void matchCustomCondition() {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
	request.setParameter("foo", "bar");

	RequestMappingInfo info = paths("/foo").params("foo=bar").build();
	RequestMappingInfo match = info.getMatchingCondition(request);

	assertNotNull(match);

	info = paths("/foo").params("foo!=bar").params("foo!=bar").build();
	match = info.getMatchingCondition(request);

	assertNull(match);
}
 
Example 11
Source File: RequestMappingInfoTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void matchParamsCondition() {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
	request.setParameter("foo", "bar");

	RequestMappingInfo info = paths("/foo").params("foo=bar").build();
	RequestMappingInfo match = info.getMatchingCondition(request);

	assertNotNull(match);

	info = paths("/foo").params("foo!=bar").build();
	match = info.getMatchingCondition(request);

	assertNull(match);
}
 
Example 12
Source File: RequestConditionHolderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void match() {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	request.setParameter("name1", "value1");

	RequestMethodsRequestCondition rm = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST);
	RequestConditionHolder custom = new RequestConditionHolder(rm);
	RequestMethodsRequestCondition expected = new RequestMethodsRequestCondition(RequestMethod.GET);

	assertEquals(expected, custom.getMatchingCondition(request).getCondition());
}
 
Example 13
Source File: DefaultServerRequestTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void params() {
	MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/");
	servletRequest.setParameter("foo", "bar");

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

	assertEquals(Optional.of("bar"), request.param("foo"));
}
 
Example 14
Source File: WebMvcConfigurationSupportExtensionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void contentNegotiation() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json");
	NativeWebRequest webRequest = new ServletWebRequest(request);

	RequestMappingHandlerMapping mapping = this.config.requestMappingHandlerMapping(
			this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(),
			this.config.mvcResourceUrlProvider());
	ContentNegotiationManager manager = mapping.getContentNegotiationManager();
	assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo.xml");
	assertEquals(Collections.singletonList(APPLICATION_XML), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo.rss");
	assertEquals(Collections.singletonList(MediaType.valueOf("application/rss+xml")),
			manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo.atom");
	assertEquals(Collections.singletonList(APPLICATION_ATOM_XML), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/foo");
	request.setParameter("f", "json");
	assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest));

	request.setRequestURI("/resources/foo.gif");
	SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) this.config.resourceHandlerMapping(
			this.config.mvcUrlPathHelper(), this.config.mvcPathMatcher(),
			this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(),
			this.config.mvcResourceUrlProvider());
	handlerMapping.setApplicationContext(this.context);
	HandlerExecutionChain chain = handlerMapping.getHandler(request);
	assertNotNull(chain);
	ResourceHttpRequestHandler handler = (ResourceHttpRequestHandler) chain.getHandler();
	assertNotNull(handler);
	assertSame(manager, handler.getContentNegotiationManager());
}
 
Example 15
Source File: RequestMappingInfoHandlerMappingTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getHandlerBestMatch() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
	request.setParameter("p", "anything");
	HandlerMethod handlerMethod = getHandler(request);

	assertEquals(this.fooParamMethod.getMethod(), handlerMethod.getMethod());
}
 
Example 16
Source File: RequestMappingInfoTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void matchCustomCondition() {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
	request.setParameter("foo", "bar");

	RequestMappingInfo info = paths("/foo").params("foo=bar").build();
	RequestMappingInfo match = info.getMatchingCondition(request);

	assertNotNull(match);

	info = paths("/foo").params("foo!=bar").params("foo!=bar").build();
	match = info.getMatchingCondition(request);

	assertNull(match);
}
 
Example 17
Source File: RequestMappingInfoTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void matchParamsCondition() {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
	request.setParameter("foo", "bar");

	RequestMappingInfo info = paths("/foo").params("foo=bar").build();
	RequestMappingInfo match = info.getMatchingCondition(request);

	assertNotNull(match);

	info = paths("/foo").params("foo!=bar").build();
	match = info.getMatchingCondition(request);

	assertNull(match);
}
 
Example 18
Source File: RequestConditionHolderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void match() {
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
	request.setParameter("name1", "value1");

	RequestMethodsRequestCondition rm = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST);
	RequestConditionHolder custom = new RequestConditionHolder(rm);
	RequestMethodsRequestCondition expected = new RequestMethodsRequestCondition(RequestMethod.GET);

	assertEquals(expected, custom.getMatchingCondition(request).getCondition());
}
 
Example 19
Source File: DefaultServerRequestTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void absentQueryParam() {
	MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/");
	servletRequest.setParameter("foo", "");

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

	assertEquals(Optional.empty(), request.param("bar"));
}
 
Example 20
Source File: DefaultServerRequestTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void emptyQueryParam() {
	MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/");
	servletRequest.setParameter("foo", "");

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

	assertEquals(Optional.of(""), request.param("foo"));
}