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

The following examples show how to use org.springframework.mock.web.test.MockHttpServletRequest#setContent() . 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: ServletAnnotationControllerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void unsupportedRequestBody() throws ServletException, IOException {
	@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(RequestResponseBodyController.class));
			RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
			adapterDef.getPropertyValues().add("messageConverters", new ByteArrayHttpMessageConverter());
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
			wac.refresh();
			return wac;
		}
	};
	servlet.init(new MockServletConfig());

	MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
	String requestBody = "Hello World";
	request.setContent(requestBody.getBytes("UTF-8"));
	request.addHeader("Content-Type", "application/pdf");
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals(415, response.getStatus());
	assertNotNull("No Accept response header set", response.getHeader("Accept"));
}
 
Example 2
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void badRequestRequestBody() throws ServletException, IOException {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("messageConverters", new NotReadableMessageConverter());
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, RequestResponseBodyController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
	String requestBody = "Hello World";
	request.setContent(requestBody.getBytes("UTF-8"));
	request.addHeader("Content-Type", "application/pdf");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("Invalid response status code", HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
}
 
Example 3
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void badRequestRequestBody() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("messageConverters", new NotReadableMessageConverter());
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, RequestResponseBodyController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
	String requestBody = "Hello World";
	request.setContent(requestBody.getBytes("UTF-8"));
	request.addHeader("Content-Type", "application/pdf");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("Invalid response status code", HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
}
 
Example 4
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void responseBodyAsHtmlWithProducesCondition() throws Exception {
	initServlet(wac -> {
		ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
		factoryBean.afterPropertiesSet();
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, TextRestController.class);

	byte[] content = "alert('boo')".getBytes(StandardCharsets.ISO_8859_1);
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a3.html");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/html;charset=ISO-8859-1", response.getContentType());
	assertNull(response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
Example 5
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void responseBodyArgMismatch() throws Exception {
	initServlet(wac -> {
		Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
		marshaller.setClassesToBeBound(A.class, B.class);
		try {
			marshaller.afterPropertiesSet();
		}
		catch (Exception ex) {
			throw new BeanCreationException(ex.getMessage(), ex);
		}
		MarshallingHttpMessageConverter messageConverter = new MarshallingHttpMessageConverter(marshaller);

		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("messageConverters", messageConverter);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, RequestBodyArgMismatchController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
	String requestBody = "<b/>";
	request.setContent(requestBody.getBytes("UTF-8"));
	request.addHeader("Content-Type", "application/xml; charset=utf-8");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(400, response.getStatus());
}
 
Example 6
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-6877
public void overlappingMessageConvertersRequestBody() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
		messageConverters.add(new StringHttpMessageConverter());
		messageConverters
				.add(new SimpleMessageConverter(new MediaType("application","json"), MediaType.ALL));
		adapterDef.getPropertyValues().add("messageConverters", messageConverters);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, RequestResponseBodyController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
	request.setContent("Hello World".getBytes("UTF-8"));
	request.addHeader("Content-Type", "text/plain; charset=utf-8");
	request.addHeader("Accept", "application/json, text/javascript, */*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("Invalid content-type", "application/json;charset=ISO-8859-1", response.getHeader("Content-Type"));
}
 
Example 7
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void responseBodyAsHtmlWithProducesCondition() throws Exception {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
			factoryBean.afterPropertiesSet();
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, TextRestController.class);

	byte[] content = "alert('boo')".getBytes(Charset.forName("ISO-8859-1"));
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a3.html");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/html", response.getContentType());
	assertNull(response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
Example 8
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void responseBodyAsHtmlWithSuffixPresent() throws Exception {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
			factoryBean.afterPropertiesSet();
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, TextRestController.class);

	byte[] content = "alert('boo')".getBytes(Charset.forName("ISO-8859-1"));
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a2.html");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/html", response.getContentType());
	assertNull(response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
Example 9
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void unsupportedRequestBody() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("messageConverters", new ByteArrayHttpMessageConverter());
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, RequestResponseBodyController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
	String requestBody = "Hello World";
	request.setContent(requestBody.getBytes("UTF-8"));
	request.addHeader("Content-Type", "application/pdf");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(415, response.getStatus());
	assertNotNull("No Accept response header set", response.getHeader("Accept"));
}
 
Example 10
Source File: RequestLoggingFilterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void payloadInputStream() throws Exception {
	filter.setIncludePayload(true);

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

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

	FilterChain filterChain = new FilterChain() {
		@Override
		public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse)
				throws IOException, ServletException {
			((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
			byte[] buf = FileCopyUtils.copyToByteArray(filterRequest.getInputStream());
			assertArrayEquals(requestBody, buf);
		}
	};

	filter.doFilter(request, response, filterChain);

	assertNotNull(filter.afterRequestMessage);
	assertTrue(filter.afterRequestMessage.contains("Hello World"));
}
 
Example 11
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void responseBodyNoAcceptableMediaType() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		StringHttpMessageConverter converter = new StringHttpMessageConverter();
		adapterDef.getPropertyValues().add("messageConverters", converter);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, RequestResponseBodyProducesController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
	String requestBody = "Hello World";
	request.setContent(requestBody.getBytes("UTF-8"));
	request.addHeader("Content-Type", "text/plain; charset=utf-8");
	request.addHeader("Accept", "application/pdf, application/msword");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(406, response.getStatus());
}
 
Example 12
Source File: RequestLoggingFilterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void payloadMaxLength() throws Exception {
	filter.setIncludePayload(true);
	filter.setMaxPayloadLength(3);

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

	final byte[] requestBody = "Hello World".getBytes(StandardCharsets.UTF_8);
	request.setContent(requestBody);

	FilterChain filterChain = (filterRequest, filterResponse) -> {
		((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);
		byte[] buf = FileCopyUtils.copyToByteArray(filterRequest.getInputStream());
		assertArrayEquals(requestBody, buf);
		ContentCachingRequestWrapper wrapper =
				WebUtils.getNativeRequest(filterRequest, ContentCachingRequestWrapper.class);
		assertArrayEquals("Hel".getBytes(StandardCharsets.UTF_8), wrapper.getContentAsByteArray());
	};

	filter.doFilter(request, response, filterChain);

	assertNotNull(filter.afterRequestMessage);
	assertTrue(filter.afterRequestMessage.contains("Hel"));
	assertFalse(filter.afterRequestMessage.contains("Hello World"));
}
 
Example 13
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-6877
public void overlappingMessageConvertersRequestBody() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
		messageConverters.add(new StringHttpMessageConverter());
		messageConverters
				.add(new SimpleMessageConverter(new MediaType("application","json"), MediaType.ALL));
		adapterDef.getPropertyValues().add("messageConverters", messageConverters);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, RequestResponseBodyController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
	request.setContent("Hello World".getBytes("UTF-8"));
	request.addHeader("Content-Type", "text/plain; charset=utf-8");
	request.addHeader("Accept", "application/json, text/javascript, */*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("Invalid content-type", "application/json;charset=ISO-8859-1", response.getHeader("Content-Type"));
}
 
Example 14
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void unsupportedRequestBody() throws ServletException, IOException {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("messageConverters", new ByteArrayHttpMessageConverter());
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, RequestResponseBodyController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
	String requestBody = "Hello World";
	request.setContent(requestBody.getBytes("UTF-8"));
	request.addHeader("Content-Type", "application/pdf");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(415, response.getStatus());
	assertNotNull("No Accept response header set", response.getHeader("Accept"));
}
 
Example 15
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void httpPatch() throws Exception {
	initServletWithControllers(RequestResponseBodyController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("PATCH", "/something");
	String requestBody = "Hello world!";
	request.setContent(requestBody.getBytes("UTF-8"));
	request.addHeader("Content-Type", "text/plain; charset=utf-8");
	request.addHeader("Accept", "text/*, */*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(200, response.getStatus());
	assertEquals(requestBody, response.getContentAsString());
}
 
Example 16
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void requestBodyResponseBody() throws ServletException, IOException {
	initServletWithControllers(RequestResponseBodyController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
	String requestBody = "Hello World";
	request.setContent(requestBody.getBytes("UTF-8"));
	request.addHeader("Content-Type", "text/plain; charset=utf-8");
	request.addHeader("Accept", "text/*, */*");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(200, response.getStatus());
	assertEquals(requestBody, response.getContentAsString());
}
 
Example 17
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void responseBodyNoAcceptHeader() throws ServletException, IOException {
	initServletWithControllers(RequestResponseBodyController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
	String requestBody = "Hello World";
	request.setContent(requestBody.getBytes("UTF-8"));
	request.addHeader("Content-Type", "text/plain; charset=utf-8");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(200, response.getStatus());
	assertEquals(requestBody, response.getContentAsString());
}
 
Example 18
Source File: FormContentFilterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void wrapFormEncodedOnly() throws Exception {
	String[] contentTypes = new String[] {"text/plain", "multipart/form-data"};
	for (String contentType : contentTypes) {
		MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/");
		request.setContent("".getBytes("ISO-8859-1"));
		request.setContentType(contentType);
		this.filterChain = new MockFilterChain();
		this.filter.doFilter(request, this.response, this.filterChain);
		assertSame(request, this.filterChain.getRequest());
	}
}
 
Example 19
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void responseBodyArgMismatch() throws ServletException, IOException {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
			marshaller.setClassesToBeBound(A.class, B.class);
			try {
				marshaller.afterPropertiesSet();
			}
			catch (Exception ex) {
				throw new BeanCreationException(ex.getMessage(), ex);
			}
			MarshallingHttpMessageConverter messageConverter = new MarshallingHttpMessageConverter(marshaller);

			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("messageConverters", messageConverter);
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, RequestBodyArgMismatchController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
	String requestBody = "<b/>";
	request.setContent(requestBody.getBytes("UTF-8"));
	request.addHeader("Content-Type", "application/xml; charset=utf-8");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(400, response.getStatus());
}
 
Example 20
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void responseBodyArgMismatch() throws Exception {
	initServlet(wac -> {
		Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
		marshaller.setClassesToBeBound(A.class, B.class);
		try {
			marshaller.afterPropertiesSet();
		}
		catch (Exception ex) {
			throw new BeanCreationException(ex.getMessage(), ex);
		}
		MarshallingHttpMessageConverter messageConverter = new MarshallingHttpMessageConverter(marshaller);

		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("messageConverters", messageConverter);
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, RequestBodyArgMismatchController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
	String requestBody = "<b/>";
	request.setContent(requestBody.getBytes("UTF-8"));
	request.addHeader("Content-Type", "application/xml; charset=utf-8");
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals(400, response.getStatus());
}