Added HTTP abstraction for RestTemplate
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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.http;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.util.MediaType;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class HttpHeadersTests {
|
||||
|
||||
private HttpHeaders headers;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
headers = new HttpHeaders();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accept() {
|
||||
MediaType mediaType1 = new MediaType("text", "html");
|
||||
MediaType mediaType2 = new MediaType("text", "plain");
|
||||
List<MediaType> mediaTypes = new ArrayList<MediaType>(2);
|
||||
mediaTypes.add(mediaType1);
|
||||
mediaTypes.add(mediaType2);
|
||||
headers.setAccept(mediaTypes);
|
||||
assertEquals("Invalid Accept header", mediaTypes, headers.getAccept());
|
||||
assertEquals("Invalid Accept header", "text/html,text/plain", headers.getFirst("Accept"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allow() {
|
||||
EnumSet<HttpMethod> methods = EnumSet.of(HttpMethod.GET, HttpMethod.POST);
|
||||
headers.setAllow(methods);
|
||||
assertEquals("Invalid Allow header", methods, headers.getAllow());
|
||||
assertEquals("Invalid Allow header", "GET,POST", headers.getFirst("Allow"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentLength() {
|
||||
long length = 42L;
|
||||
headers.setContentLength(length);
|
||||
assertEquals("Invalid Content-Length header", length, headers.getContentLength());
|
||||
assertEquals("Invalid Content-Length header", "42", headers.getFirst("Content-Length"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentType() {
|
||||
MediaType contentType = new MediaType("text", "html", Charset.forName("UTF-8"));
|
||||
headers.setContentType(contentType);
|
||||
assertEquals("Invalid Content-Type header", contentType, headers.getContentType());
|
||||
assertEquals("Invalid Content-Type header", "text/html;charset=UTF-8", headers.getFirst("Content-Type"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void location() throws URISyntaxException {
|
||||
URI location = new URI("http://www.example.com/hotels");
|
||||
headers.setLocation(location);
|
||||
assertEquals("Invalid Location header", location, headers.getLocation());
|
||||
assertEquals("Invalid Location header", "http://www.example.com/hotels", headers.getFirst("Location"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.http;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class MockHttpInputMessage implements HttpInputMessage {
|
||||
|
||||
private final HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
private final InputStream body;
|
||||
|
||||
public MockHttpInputMessage(byte[] contents) {
|
||||
Assert.notNull(contents, "'contents' must not be null");
|
||||
this.body = new ByteArrayInputStream(contents);
|
||||
}
|
||||
|
||||
public MockHttpInputMessage(InputStream body) {
|
||||
Assert.notNull(body, "'body' must not be null");
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public HttpHeaders getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
public InputStream getBody() throws IOException {
|
||||
return body;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.http;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class MockHttpOutputMessage implements HttpOutputMessage {
|
||||
|
||||
private final HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
private final ByteArrayOutputStream body = new ByteArrayOutputStream();
|
||||
|
||||
public HttpHeaders getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
public OutputStream getBody() throws IOException {
|
||||
return body;
|
||||
}
|
||||
|
||||
public byte[] getBodyAsBytes() {
|
||||
return body.toByteArray();
|
||||
}
|
||||
|
||||
public String getBodyAsString(Charset charset) {
|
||||
byte[] bytes = getBodyAsBytes();
|
||||
return new String(bytes, charset);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* 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.http.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.Enumeration;
|
||||
import javax.servlet.GenericServlet;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.mortbay.jetty.Server;
|
||||
import org.mortbay.jetty.servlet.Context;
|
||||
import org.mortbay.jetty.servlet.ServletHolder;
|
||||
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.http.HttpMethod;
|
||||
import org.springframework.web.http.HttpStatus;
|
||||
|
||||
public abstract class AbstractHttpRequestFactoryTestCase {
|
||||
|
||||
private ClientHttpRequestFactory factory;
|
||||
|
||||
private static Server jettyServer;
|
||||
|
||||
@BeforeClass
|
||||
public static void startJettyServer() throws Exception {
|
||||
jettyServer = new Server(8889);
|
||||
Context jettyContext = new Context(jettyServer, "/");
|
||||
jettyContext.addServlet(new ServletHolder(new EchoServlet()), "/echo");
|
||||
jettyContext.addServlet(new ServletHolder(new ErrorServlet(404)), "/errors/notfound");
|
||||
jettyServer.start();
|
||||
}
|
||||
|
||||
@Before
|
||||
public final void createFactory() {
|
||||
factory = createRequestFactory();
|
||||
}
|
||||
|
||||
protected abstract ClientHttpRequestFactory createRequestFactory();
|
||||
|
||||
@AfterClass
|
||||
public static void stopJettyServer() throws Exception {
|
||||
if (jettyServer != null) {
|
||||
jettyServer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void status() throws Exception {
|
||||
ClientHttpRequest request =
|
||||
factory.createRequest(new URI("http://localhost:8889/errors/notfound"), HttpMethod.GET);
|
||||
assertEquals("Invalid HTTP method", HttpMethod.GET, request.getMethod());
|
||||
ClientHttpResponse response = request.execute();
|
||||
assertEquals("Invalid status code", HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void echo() throws Exception {
|
||||
ClientHttpRequest request = factory.createRequest(new URI("http://localhost:8889/echo"), HttpMethod.PUT);
|
||||
assertEquals("Invalid HTTP method", HttpMethod.PUT, request.getMethod());
|
||||
String headerName = "MyHeader";
|
||||
String headerValue1 = "value1";
|
||||
request.getHeaders().add(headerName, headerValue1);
|
||||
String headerValue2 = "value2";
|
||||
request.getHeaders().add(headerName, headerValue2);
|
||||
byte[] body = "Hello World".getBytes("UTF-8");
|
||||
FileCopyUtils.copy(body, request.getBody());
|
||||
ClientHttpResponse response = request.execute();
|
||||
assertEquals("Invalid status code", HttpStatus.OK, response.getStatusCode());
|
||||
assertTrue("Header not found", response.getHeaders().containsKey(headerName));
|
||||
assertEquals("Header value not found", Arrays.asList(headerValue1, headerValue2),
|
||||
response.getHeaders().get(headerName));
|
||||
byte[] result = FileCopyUtils.copyToByteArray(response.getBody());
|
||||
assertTrue("Invalid body", Arrays.equals(body, result));
|
||||
}
|
||||
|
||||
/**
|
||||
* Servlet that returns and error message for a given status code.
|
||||
*/
|
||||
private static class ErrorServlet extends GenericServlet {
|
||||
|
||||
private final int sc;
|
||||
|
||||
private ErrorServlet(int sc) {
|
||||
this.sc = sc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
|
||||
((HttpServletResponse) response).sendError(sc);
|
||||
}
|
||||
}
|
||||
|
||||
private static class EchoServlet extends HttpServlet {
|
||||
|
||||
@Override
|
||||
protected void doPut(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
echo(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
echo(request, response);
|
||||
}
|
||||
|
||||
private void echo(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
for (Enumeration e1 = request.getHeaderNames(); e1.hasMoreElements();) {
|
||||
String headerName = (String) e1.nextElement();
|
||||
for (Enumeration e2 = request.getHeaders(headerName); e2.hasMoreElements();) {
|
||||
String headerValue = (String) e2.nextElement();
|
||||
response.addHeader(headerName, headerValue);
|
||||
}
|
||||
}
|
||||
FileCopyUtils.copy(request.getInputStream(), response.getOutputStream());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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.http.client;
|
||||
|
||||
public class SimpleHttpRequestFactoryTests extends AbstractHttpRequestFactoryTestCase {
|
||||
|
||||
@Override
|
||||
protected ClientHttpRequestFactory createRequestFactory() {
|
||||
return new SimpleClientHttpRequestFactory();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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.http.client.commons;
|
||||
|
||||
import org.springframework.web.http.client.AbstractHttpRequestFactoryTestCase;
|
||||
import org.springframework.web.http.client.ClientHttpRequestFactory;
|
||||
|
||||
public class CommonsHttpRequestFactoryTest extends AbstractHttpRequestFactoryTestCase {
|
||||
|
||||
@Override
|
||||
protected ClientHttpRequestFactory createRequestFactory() {
|
||||
return new CommonsClientHttpRequestFactory();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user