Added HTTP conversion abstraction for RestTemplate

This commit is contained in:
Arjen Poutsma
2009-02-21 13:34:55 +00:00
parent 4a02cd96ea
commit 2de9e2a38d
10 changed files with 636 additions and 4 deletions

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2002-2009 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.web.converter;
import java.io.IOException;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.MediaType;
import org.springframework.web.http.MockHttpInputMessage;
import org.springframework.web.http.MockHttpOutputMessage;
/**
* @author Arjen Poutsma
*/
public class ByteArrayHttpMessageConverterTests {
private ByteArrayHttpMessageConverter converter;
@Before
public void setUp() {
converter = new ByteArrayHttpMessageConverter();
}
@Test
public void read() throws IOException {
byte[] body = new byte[]{0x1, 0x2};
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
inputMessage.getHeaders().setContentType(new MediaType("application", "octet-stream"));
byte[] result = converter.read(byte[].class, inputMessage);
assertArrayEquals("Invalid result", body, result);
}
@Test
public void write() throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
byte[] body = new byte[]{0x1, 0x2};
converter.write(body, outputMessage);
assertArrayEquals("Invalid result", body, outputMessage.getBodyAsBytes());
assertEquals("Invalid content-type", new MediaType("application", "octet-stream"),
outputMessage.getHeaders().getContentType());
assertEquals("Invalid content-length", 2, outputMessage.getHeaders().getContentLength());
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2002-2009 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.web.converter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collections;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.MediaType;
import org.springframework.web.http.MockHttpInputMessage;
import org.springframework.web.http.MockHttpOutputMessage;
/**
* @author Arjen Poutsma
*/
public class StringHttpMessageConverterTests {
private StringHttpMessageConverter converter;
@Before
public void setUp() {
converter = new StringHttpMessageConverter();
}
@Test
public void read() throws IOException {
String body = "Hello World";
Charset charset = Charset.forName("UTF-8");
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(charset));
inputMessage.getHeaders().setContentType(new MediaType("text", "plain", charset));
String result = converter.read(String.class, inputMessage);
assertEquals("Invalid result", body, result);
}
@Test
public void write() throws IOException {
Charset charset = Charset.forName("UTF-8");
converter.setSupportedMediaTypes(Collections.singletonList(new MediaType("text", "plain", charset)));
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
String body = "H<EFBFBD>llo W<>rld";
converter.write(body, outputMessage);
assertEquals("Invalid result", body, outputMessage.getBodyAsString(charset));
assertEquals("Invalid content-type", new MediaType("text", "plain", charset),
outputMessage.getHeaders().getContentType());
assertEquals("Invalid content-length", 13, outputMessage.getHeaders().getContentLength());
assertFalse("Invalid accept-charset", outputMessage.getHeaders().getAcceptCharset().isEmpty());
}
}

View File

@@ -50,7 +50,19 @@ public class HttpHeadersTests {
mediaTypes.add(mediaType2);
headers.setAccept(mediaTypes);
assertEquals("Invalid Accept header", mediaTypes, headers.getAccept());
assertEquals("Invalid Accept header", "text/html,text/plain", headers.getFirst("Accept"));
assertEquals("Invalid Accept header", "text/html, text/plain", headers.getFirst("Accept"));
}
@Test
public void acceptCharsets() {
Charset charset1 = Charset.forName("UTF-8");
Charset charset2 = Charset.forName("ISO-8859-1");
List<Charset> charsets = new ArrayList<Charset>(2);
charsets.add(charset1);
charsets.add(charset2);
headers.setAcceptCharset(charsets);
assertEquals("Invalid Accept header", charsets, headers.getAcceptCharset());
assertEquals("Invalid Accept header", "utf-8, iso-8859-1", headers.getFirst("Accept-Charset"));
}
@Test