refactored HTTP support into top-level package "org.springframework.http"; revised RestTemplate facility in package "org.springframework.web.client"
This commit is contained in:
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.http;
|
||||
package org.springframework.http;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
@@ -27,7 +27,7 @@ import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.util.MediaType;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* 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.http;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class MediaTypeTests {
|
||||
|
||||
@Test
|
||||
public void includes() throws Exception {
|
||||
MediaType type1 = new MediaType("text", "plain");
|
||||
MediaType type2 = new MediaType("text", "plain");
|
||||
assertTrue("Equal types is not inclusive", type1.includes(type2));
|
||||
type1 = new MediaType("text");
|
||||
assertTrue("All subtypes is not inclusive", type1.includes(type2));
|
||||
type1 = MediaType.ALL;
|
||||
assertTrue("All types is not inclusive", type1.includes(type2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception {
|
||||
MediaType mediaType = new MediaType("text", "plain", Collections.singletonMap("q", "0.7"));
|
||||
String result = mediaType.toString();
|
||||
assertEquals("Invalid toString() returned", "text/plain;q=0.7", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseMediaType() throws Exception {
|
||||
String s = "audio/*; q=0.2";
|
||||
MediaType mediaType = MediaType.parseMediaType(s);
|
||||
assertEquals("Invalid type", "audio", mediaType.getType());
|
||||
assertEquals("Invalid subtype", "*", mediaType.getSubtype());
|
||||
assertEquals("Invalid quality factor", 0.2D, mediaType.getQualityValue(), 0D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseURLConnectionMediaType() throws Exception {
|
||||
String s = "*; q=.2";
|
||||
MediaType mediaType = MediaType.parseMediaType(s);
|
||||
assertEquals("Invalid type", "*", mediaType.getType());
|
||||
assertEquals("Invalid subtype", "*", mediaType.getSubtype());
|
||||
assertEquals("Invalid quality factor", 0.2D, mediaType.getQualityValue(), 0D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseMediaTypes() throws Exception {
|
||||
String s = "text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c";
|
||||
List<MediaType> mediaTypes = MediaType.parseMediaTypes(s);
|
||||
assertNotNull("No media types returned", mediaTypes);
|
||||
assertEquals("Invalid amount of media types", 4, mediaTypes.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareTo() throws Exception {
|
||||
MediaType audioBasic = new MediaType("audio", "basic");
|
||||
MediaType audio = new MediaType("audio");
|
||||
MediaType audio03 = new MediaType("audio", "*", Collections.singletonMap("q", "0.3"));
|
||||
MediaType audio07 = new MediaType("audio", "*", Collections.singletonMap("q", "0.7"));
|
||||
MediaType all = MediaType.ALL;
|
||||
|
||||
// equal
|
||||
assertEquals("Invalid comparison result", 0, audioBasic.compareTo(audioBasic));
|
||||
assertEquals("Invalid comparison result", 0, audio.compareTo(audio));
|
||||
assertEquals("Invalid comparison result", 0, audio07.compareTo(audio07));
|
||||
|
||||
// specific to unspecific
|
||||
assertTrue("Invalid comparison result", audioBasic.compareTo(audio) < 0);
|
||||
assertTrue("Invalid comparison result", audioBasic.compareTo(all) < 0);
|
||||
assertTrue("Invalid comparison result", audio.compareTo(all) < 0);
|
||||
|
||||
// unspecific to specific
|
||||
assertTrue("Invalid comparison result", audio.compareTo(audioBasic) > 0);
|
||||
assertTrue("Invalid comparison result", all.compareTo(audioBasic) > 0);
|
||||
assertTrue("Invalid comparison result", all.compareTo(audio) > 0);
|
||||
|
||||
// qualifiers
|
||||
assertTrue("Invalid comparison result", audio.compareTo(audio07) < 0);
|
||||
assertTrue("Invalid comparison result", audio07.compareTo(audio03) < 0);
|
||||
assertTrue("Invalid comparison result", audio03.compareTo(all) > 0);
|
||||
|
||||
// sort
|
||||
List<MediaType> expected = new ArrayList<MediaType>();
|
||||
expected.add(audioBasic);
|
||||
expected.add(audio);
|
||||
expected.add(all);
|
||||
expected.add(audio07);
|
||||
expected.add(audio03);
|
||||
|
||||
List<MediaType> result = new ArrayList<MediaType>(expected);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Collections.shuffle(result);
|
||||
Collections.sort(result);
|
||||
|
||||
for (int j = 0; j < result.size(); j++) {
|
||||
assertEquals("Invalid media type at " + j, expected.get(j), result.get(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.http;
|
||||
package org.springframework.http;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.http;
|
||||
package org.springframework.http;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.http.client;
|
||||
package org.springframework.http.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
@@ -38,8 +38,8 @@ 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;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
public abstract class AbstractHttpRequestFactoryTestCase {
|
||||
|
||||
@@ -14,15 +14,16 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.http.client.commons;
|
||||
package org.springframework.http.client;
|
||||
|
||||
import org.springframework.web.http.client.AbstractHttpRequestFactoryTestCase;
|
||||
import org.springframework.web.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.AbstractHttpRequestFactoryTestCase;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.CommonsClientHttpRequestFactory;
|
||||
|
||||
public class CommonsHttpRequestFactoryTest extends AbstractHttpRequestFactoryTestCase {
|
||||
public class CommonsHttpRequestFactoryTests extends AbstractHttpRequestFactoryTestCase {
|
||||
|
||||
@Override
|
||||
protected ClientHttpRequestFactory createRequestFactory() {
|
||||
return new CommonsClientHttpRequestFactory();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.http.client;
|
||||
package org.springframework.http.client;
|
||||
|
||||
public class SimpleHttpRequestFactoryTests extends AbstractHttpRequestFactoryTestCase {
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.converter;
|
||||
package org.springframework.http.converter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -22,9 +22,9 @@ 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;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.MockHttpInputMessage;
|
||||
import org.springframework.http.MockHttpOutputMessage;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.converter;
|
||||
package org.springframework.http.converter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
@@ -24,9 +24,9 @@ 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;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.MockHttpInputMessage;
|
||||
import org.springframework.http.MockHttpOutputMessage;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.http.server;
|
||||
package org.springframework.http.server;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -24,8 +24,8 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.http.HttpHeaders;
|
||||
import org.springframework.web.http.HttpMethod;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.http.server;
|
||||
package org.springframework.http.server;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -24,8 +24,8 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.http.HttpHeaders;
|
||||
import org.springframework.web.http.HttpStatus;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -14,12 +14,13 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.client.core;
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Set;
|
||||
import javax.servlet.GenericServlet;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
@@ -37,11 +38,9 @@ import org.mortbay.jetty.Server;
|
||||
import org.mortbay.jetty.servlet.Context;
|
||||
import org.mortbay.jetty.servlet.ServletHolder;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.CommonsClientHttpRequestFactory;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.HttpServerErrorException;
|
||||
import org.springframework.web.http.HttpMethod;
|
||||
import org.springframework.web.http.client.commons.CommonsClientHttpRequestFactory;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -103,11 +102,12 @@ public class RestTemplateIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void optionsForAllow() {
|
||||
EnumSet<HttpMethod> allowed = template.optionsForAllow("http://localhost:8889/get");
|
||||
Set<HttpMethod> allowed = template.optionsForAllow("http://localhost:8889/get");
|
||||
assertEquals("Invalid response",
|
||||
EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS, HttpMethod.HEAD, HttpMethod.TRACE), allowed);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Servlet that returns and error message for a given status code.
|
||||
*/
|
||||
@@ -125,6 +125,7 @@ public class RestTemplateIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class GetServlet extends HttpServlet {
|
||||
|
||||
private final byte[] buf;
|
||||
@@ -145,6 +146,7 @@ public class RestTemplateIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class PostServlet extends HttpServlet {
|
||||
|
||||
private final String s;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.client.core;
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
@@ -22,31 +22,29 @@ import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.util.MediaType;
|
||||
import org.springframework.web.client.HttpClientException;
|
||||
import org.springframework.web.client.HttpIOException;
|
||||
import org.springframework.web.client.HttpServerErrorException;
|
||||
import org.springframework.web.converter.ByteArrayHttpMessageConverter;
|
||||
import org.springframework.web.converter.HttpMessageConverter;
|
||||
import org.springframework.web.converter.StringHttpMessageConverter;
|
||||
import org.springframework.web.http.HttpHeaders;
|
||||
import org.springframework.web.http.HttpMethod;
|
||||
import org.springframework.web.http.HttpStatus;
|
||||
import org.springframework.web.http.client.ClientHttpRequest;
|
||||
import org.springframework.web.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.web.http.client.ClientHttpResponse;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.ClientHttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public class RestTemplateTest {
|
||||
public class RestTemplateTests {
|
||||
|
||||
private RestTemplate template;
|
||||
|
||||
@@ -56,7 +54,7 @@ public class RestTemplateTest {
|
||||
|
||||
private ClientHttpResponse response;
|
||||
|
||||
private HttpErrorHandler errorHandler;
|
||||
private ResponseErrorHandler errorHandler;
|
||||
|
||||
private HttpMessageConverter converter;
|
||||
|
||||
@@ -65,7 +63,7 @@ public class RestTemplateTest {
|
||||
requestFactory = createMock(ClientHttpRequestFactory.class);
|
||||
request = createMock(ClientHttpRequest.class);
|
||||
response = createMock(ClientHttpResponse.class);
|
||||
errorHandler = createMock(HttpErrorHandler.class);
|
||||
errorHandler = createMock(ResponseErrorHandler.class);
|
||||
converter = createMock(HttpMessageConverter.class);
|
||||
template = new RestTemplate(requestFactory);
|
||||
template.setErrorHandler(errorHandler);
|
||||
@@ -200,7 +198,7 @@ public class RestTemplateTest {
|
||||
template.getForObject("http://example.com/{p}", String.class, "resource");
|
||||
fail("UnsupportedMediaTypeException expected");
|
||||
}
|
||||
catch (HttpClientException ex) {
|
||||
catch (RestClientException ex) {
|
||||
// expected
|
||||
}
|
||||
verifyMocks();
|
||||
@@ -309,7 +307,7 @@ public class RestTemplateTest {
|
||||
|
||||
replayMocks();
|
||||
|
||||
EnumSet<HttpMethod> result = template.optionsForAllow("http://example.com");
|
||||
Set<HttpMethod> result = template.optionsForAllow("http://example.com");
|
||||
assertEquals("Invalid OPTIONS result", expected, result);
|
||||
|
||||
verifyMocks();
|
||||
@@ -330,7 +328,7 @@ public class RestTemplateTest {
|
||||
template.getForObject("http://example.com/resource", String.class);
|
||||
fail("RestClientException expected");
|
||||
}
|
||||
catch (HttpIOException ex) {
|
||||
catch (ResourceAccessException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user