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

The following examples show how to use org.springframework.mock.web.test.MockHttpServletRequest#setContentType() . 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: RequestPartMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void resolveOptionalPartArgument() throws Exception {
	MockPart expected = new MockPart("optionalPart", "Hello World".getBytes());
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("POST");
	request.setContentType("multipart/form-data");
	request.addPart(expected);
	request.addPart(new MockPart("otherPart", "Hello World".getBytes()));
	webRequest = new ServletWebRequest(request);

	Object actualValue = resolver.resolveArgument(optionalPart, null, webRequest, null);
	assertTrue(actualValue instanceof Optional);
	assertEquals("Invalid result", expected, ((Optional<?>) actualValue).get());

	actualValue = resolver.resolveArgument(optionalPart, null, webRequest, null);
	assertTrue(actualValue instanceof Optional);
	assertEquals("Invalid result", expected, ((Optional<?>) actualValue).get());
}
 
Example 2
Source File: RequestParamMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void resolvePartArray() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	MockPart expected1 = new MockPart("pfilearray", "Hello World 1".getBytes());
	MockPart expected2 = new MockPart("pfilearray", "Hello World 2".getBytes());
	request.setMethod("POST");
	request.setContentType("multipart/form-data");
	request.addPart(expected1);
	request.addPart(expected2);
	request.addPart(new MockPart("other", "Hello World 3".getBytes()));
	webRequest = new ServletWebRequest(request);

	MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
	Object result = resolver.resolveArgument(param, null, webRequest, null);

	assertTrue(result instanceof Part[]);
	Part[] parts = (Part[]) result;
	assertEquals(2, parts.length);
	assertEquals(parts[0], expected1);
	assertEquals(parts[1], expected2);
}
 
Example 3
Source File: RequestParamMethodArgumentResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void resolvePartNotAnnot() throws Exception {
	MockPart expected = new MockPart("part", "Hello World".getBytes());
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("POST");
	request.setContentType("multipart/form-data");
	request.addPart(expected);
	webRequest = new ServletWebRequest(request);

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

	assertTrue(result instanceof Part);
	assertEquals("Invalid result", expected, result);
}
 
Example 4
Source File: RequestPartMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void resolveOptionalRequestPartNotPresent() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("POST");
	request.setContentType("multipart/form-data");
	webRequest = new ServletWebRequest(request);

	Object actualValue = resolver.resolveArgument(optionalRequestPart, null, webRequest, null);
	assertEquals("Invalid argument value", Optional.empty(), actualValue);

	actualValue = resolver.resolveArgument(optionalRequestPart, null, webRequest, null);
	assertEquals("Invalid argument value", Optional.empty(), actualValue);
}
 
Example 5
Source File: RequestPartMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void resolveOptionalPartListNotPresent() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("POST");
	request.setContentType("multipart/form-data");
	webRequest = new ServletWebRequest(request);

	Object actualValue = resolver.resolveArgument(optionalPartList, null, webRequest, null);
	assertEquals("Invalid argument value", Optional.empty(), actualValue);

	actualValue = resolver.resolveArgument(optionalPartList, null, webRequest, null);
	assertEquals("Invalid argument value", Optional.empty(), actualValue);
}
 
Example 6
Source File: RequestMappingInfoHandlerMappingTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void testMediaTypeNotSupported(String url) throws Exception {
	try {
		MockHttpServletRequest request = new MockHttpServletRequest("PUT", url);
		request.setContentType("application/json");
		this.handlerMapping.getHandler(request);
		fail("HttpMediaTypeNotSupportedException expected");
	}
	catch (HttpMediaTypeNotSupportedException ex) {
		assertEquals("Invalid supported consumable media types",
				Arrays.asList(new MediaType("application", "xml")), ex.getSupportedMediaTypes());
	}
}
 
Example 7
Source File: RequestPartMethodArgumentResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveOptionalPartArgument() throws Exception {
	MockPart expected = new MockPart("optionalPart", "Hello World".getBytes());
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("POST");
	request.setContentType("multipart/form-data");
	request.addPart(expected);
	webRequest = new ServletWebRequest(request);

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

	assertTrue(result instanceof Optional);
	assertEquals("Invalid result", expected, ((Optional) result).get());
}
 
Example 8
Source File: ConsumesRequestConditionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void consumesParseError() {
	ConsumesRequestCondition condition = new ConsumesRequestCondition("text/plain");

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setContentType("01");

	assertNull(condition.getMatchingCondition(request));
}
 
Example 9
Source File: RequestParamMethodArgumentResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void resolvePart() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	MockPart expected = new MockPart("pfile", "Hello World".getBytes());
	request.setMethod("POST");
	request.setContentType("multipart/form-data");
	request.addPart(expected);
	webRequest = new ServletWebRequest(request);

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

	assertTrue(result instanceof Part);
	assertEquals("Invalid result", expected, result);
}
 
Example 10
Source File: ConsumesRequestConditionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void getMatchingCondition() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setContentType("text/plain");

	ConsumesRequestCondition condition = new ConsumesRequestCondition("text/plain", "application/xml");

	ConsumesRequestCondition result = condition.getMatchingCondition(request);
	assertConditions(result, "text/plain");

	condition = new ConsumesRequestCondition("application/xml");

	result = condition.getMatchingCondition(request);
	assertNull(result);
}
 
Example 11
Source File: CommonsMultipartResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void doTestWithApplicationContext(boolean lazy) throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());
	wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
	wac.refresh();
	MockCommonsMultipartResolver resolver = new MockCommonsMultipartResolver();
	resolver.setMaxUploadSize(1000);
	resolver.setMaxInMemorySize(100);
	resolver.setDefaultEncoding("enc");
	if (lazy) {
		resolver.setResolveLazily(false);
	}
	resolver.setServletContext(wac.getServletContext());
	assertEquals(1000, resolver.getFileUpload().getSizeMax());
	assertEquals(100, resolver.getFileItemFactory().getSizeThreshold());
	assertEquals("enc", resolver.getFileUpload().getHeaderEncoding());
	assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));

	MockHttpServletRequest originalRequest = new MockHttpServletRequest();
	originalRequest.setMethod("POST");
	originalRequest.setContentType("multipart/form-data");
	originalRequest.addHeader("Content-type", "multipart/form-data");
	originalRequest.addParameter("getField", "getValue");
	assertTrue(resolver.isMultipart(originalRequest));
	MultipartHttpServletRequest request = resolver.resolveMultipart(originalRequest);

	doTestParameters(request);

	doTestFiles(request);

	doTestBinding(resolver, originalRequest, request);

	wac.close();
}
 
Example 12
Source File: RequestMappingInfoHandlerMappingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void getHandlerTestInvalidContentType() throws Exception {
	try {
		MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/person/1");
		request.setContentType("bogus");
		this.handlerMapping.getHandler(request);
		fail("HttpMediaTypeNotSupportedException expected");
	}
	catch (HttpMediaTypeNotSupportedException ex) {
		assertEquals("Invalid mime type \"bogus\": does not contain '/'", ex.getMessage());
	}
}
 
Example 13
Source File: ConsumesRequestConditionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void consumesSingleNoMatch() {
	ConsumesRequestCondition condition = new ConsumesRequestCondition("text/plain");

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setContentType("application/xml");

	assertNull(condition.getMatchingCondition(request));
}
 
Example 14
Source File: ConsumesRequestConditionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void consumesSingleNoMatch() {
	ConsumesRequestCondition condition = new ConsumesRequestCondition("text/plain");

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setContentType("application/xml");

	assertNull(condition.getMatchingCondition(request));
}
 
Example 15
Source File: ConsumesRequestConditionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void consumesWildcardMatch() {
	ConsumesRequestCondition condition = new ConsumesRequestCondition("text/*");

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setContentType("text/plain");

	assertNotNull(condition.getMatchingCondition(request));
}
 
Example 16
Source File: ConsumesRequestConditionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void consumesMatch() {
	ConsumesRequestCondition condition = new ConsumesRequestCondition("text/plain");

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setContentType("text/plain");

	assertNotNull(condition.getMatchingCondition(request));
}
 
Example 17
Source File: ConsumesRequestConditionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void consumesMatch() {
	ConsumesRequestCondition condition = new ConsumesRequestCondition("text/plain");

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setContentType("text/plain");

	assertNotNull(condition.getMatchingCondition(request));
}
 
Example 18
Source File: DefaultServerRequestTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test(expected = HttpMediaTypeNotSupportedException.class)
public void bodyUnacceptable() throws Exception {
	MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/");
	servletRequest.setContentType(MediaType.TEXT_PLAIN_VALUE);
	servletRequest.setContent("foo".getBytes(UTF_8));

	DefaultServerRequest request =
			new DefaultServerRequest(servletRequest, Collections.emptyList());

	request.body(String.class);
}
 
Example 19
Source File: ConsumesRequestConditionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void consumesMultipleMatch() {
	ConsumesRequestCondition condition = new ConsumesRequestCondition("text/plain", "application/xml");

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setContentType("text/plain");

	assertNotNull(condition.getMatchingCondition(request));
}
 
Example 20
Source File: ObjectToStringHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
@Test
public void read() throws IOException {
	MockHttpServletRequest request = new MockHttpServletRequest();

	request.setContentType(MediaType.TEXT_PLAIN_VALUE);

	Short shortValue = Short.valueOf((short) 781);

	request.setContent(shortValue.toString().getBytes(
			StringHttpMessageConverter.DEFAULT_CHARSET));

	assertEquals(shortValue, this.converter.read(Short.class, new ServletServerHttpRequest(request)));

	Float floatValue = Float.valueOf(123);

	request.setCharacterEncoding("UTF-16");
	request.setContent(floatValue.toString().getBytes("UTF-16"));

	assertEquals(floatValue, this.converter.read(Float.class, new ServletServerHttpRequest(request)));

	Long longValue = Long.valueOf(55819182821331L);

	request.setCharacterEncoding("UTF-8");
	request.setContent(longValue.toString().getBytes("UTF-8"));

	assertEquals(longValue, this.converter.read(Long.class, new ServletServerHttpRequest(request)));
}