SPR-5904 - Multipart/mixed requests using RestTemplate

This commit is contained in:
Arjen Poutsma
2010-03-10 13:53:29 +00:00
parent f30b0a86f7
commit def90d1016
20 changed files with 616 additions and 379 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -210,5 +210,15 @@ public class HttpHeadersTests {
assertEquals("Invalid Cache-Control header", "no-cache", headers.getFirst("cache-control"));
}
@Test
public void contentDisposition() {
headers.setContentDispositionFormData("name", null);
assertEquals("Invalid Content-Disposition header", "form-data; name=\"name\"", headers.getFirst("Content-Disposition"));
headers.setContentDispositionFormData("name", "filename");
assertEquals("Invalid Content-Disposition header", "form-data; name=\"name\"; filename=\"filename\"", headers.getFirst("Content-Disposition"));
}
}

View File

@@ -31,7 +31,7 @@ public class MediaTypeTests {
@Test
public void includes() throws Exception {
MediaType textPlain = new MediaType("text", "plain");
MediaType textPlain = MediaType.TEXT_PLAIN;
assertTrue("Equal types is not inclusive", textPlain.includes(textPlain));
MediaType allText = new MediaType("text");
assertTrue("All subtypes is not inclusive", allText.includes(textPlain));

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.converter;
import java.io.IOException;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.util.FileCopyUtils;
/**
* @author Arjen Poutsma
*/
public class ResourceHttpMessageConverterTests {
private ResourceHttpMessageConverter converter;
@Before
public void setUp() {
converter = new ResourceHttpMessageConverter();
}
@Test
public void canRead() {
assertTrue(converter.canRead(Resource.class, new MediaType("application", "octet-stream")));
}
@Test
public void canWrite() {
assertTrue(converter.canWrite(Resource.class, new MediaType("application", "octet-stream")));
assertTrue(converter.canWrite(Resource.class, MediaType.ALL));
}
@Test
public void read() throws IOException {
byte[] body = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("logo.jpg"));
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG);
converter.read(Resource.class, inputMessage);
}
@Test
public void write() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
Resource body = new ClassPathResource("logo.jpg", getClass());
converter.write(body, null, outputMessage);
assertEquals("Invalid content-type", MediaType.IMAGE_JPEG,
outputMessage.getHeaders().getContentType());
assertEquals("Invalid content-length", body.getFile().length(), outputMessage.getHeaders().getContentLength());
}
}

View File

@@ -19,7 +19,10 @@ package org.springframework.http.converter.multipart;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.List;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
@@ -59,18 +62,19 @@ public class MultipartHttpMessageConverterTest {
@Test
public void write() throws Exception {
MultipartMap body = new MultipartMap();
body.addTextPart("name 1", "value 1");
body.addTextPart("name 2", "value 2+1");
body.addTextPart("name 2", "value 2+2");
body.add("name 1", "value 1");
body.add("name 2", "value 2+1");
body.add("name 2", "value 2+2");
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
body.addBinaryPart("logo", logo);
byte[] xml = "<root><child/></root>".getBytes("UTF-8");
body.addPart("xml", xml, new MediaType("application", "xml"));
body.add("logo", logo);
Source xml = new StreamSource(new StringReader("<root><child/></root>"));
body.add("xml", xml);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(body, null, outputMessage);
final MediaType contentType = outputMessage.getHeaders().getContentType();
final byte[] result = outputMessage.getBodyAsBytes();
System.out.println(new String(result));
assertNotNull(contentType);
assertNotNull(contentType.getParameter("boundary"));
@@ -114,13 +118,12 @@ public class MultipartHttpMessageConverterTest {
assertFalse(item.isFormField());
assertEquals("logo", item.getFieldName());
assertEquals("logo.jpg", item.getName());
assertEquals("application/octet-stream", item.getContentType());
assertEquals("image/jpeg", item.getContentType());
assertEquals(logo.getFile().length(), item.getSize());
item = (FileItem) items.get(4);
assertEquals("xml", item.getFieldName());
assertEquals("application/xml", item.getContentType());
assertEquals(xml.length, item.getSize());
}

View File

@@ -146,11 +146,11 @@ public class RestTemplateIntegrationTests {
@Test
public void multipart() throws UnsupportedEncodingException {
MultipartMap body = new MultipartMap();
body.addTextPart("name 1", "value 1");
body.addTextPart("name 2", "value 2+1");
body.addTextPart("name 2", "value 2+2");
body.add("name 1", "value 1");
body.add("name 2", "value 2+1");
body.add("name 2", "value 2+2");
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
body.addBinaryPart("logo", logo);
body.add("logo", logo);
template.postForLocation(URI + "/multipart", body);
}
@@ -261,7 +261,7 @@ public class RestTemplateIntegrationTests {
assertFalse(item.isFormField());
assertEquals("logo", item.getFieldName());
assertEquals("logo.jpg", item.getName());
assertEquals("application/octet-stream", item.getContentType());
assertEquals("image/jpeg", item.getContentType());
}
catch (FileUploadException ex) {
throw new ServletException(ex);