|
|
|
|
@@ -16,6 +16,10 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.http.client;
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.URI;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
@@ -23,10 +27,8 @@ import java.util.Locale;
|
|
|
|
|
import java.util.concurrent.Future;
|
|
|
|
|
|
|
|
|
|
import org.junit.After;
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.DisposableBean;
|
|
|
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
|
|
@@ -44,16 +46,16 @@ public abstract class AbstractAsyncHttpRequestFactoryTestCase extends AbstractJe
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public final void createFactory() throws Exception {
|
|
|
|
|
factory = createRequestFactory();
|
|
|
|
|
if (factory instanceof InitializingBean) {
|
|
|
|
|
((InitializingBean) factory).afterPropertiesSet();
|
|
|
|
|
this.factory = createRequestFactory();
|
|
|
|
|
if (this.factory instanceof InitializingBean) {
|
|
|
|
|
((InitializingBean) this.factory).afterPropertiesSet();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public final void destroyFactory() throws Exception {
|
|
|
|
|
if (factory instanceof DisposableBean) {
|
|
|
|
|
((DisposableBean) factory).destroy();
|
|
|
|
|
if (this.factory instanceof DisposableBean) {
|
|
|
|
|
((DisposableBean) this.factory).destroy();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -63,14 +65,15 @@ public abstract class AbstractAsyncHttpRequestFactoryTestCase extends AbstractJe
|
|
|
|
|
@Test
|
|
|
|
|
public void status() throws Exception {
|
|
|
|
|
URI uri = new URI(baseUrl + "/status/notfound");
|
|
|
|
|
AsyncClientHttpRequest request = factory.createAsyncRequest(uri, HttpMethod.GET);
|
|
|
|
|
AsyncClientHttpRequest request = this.factory.createAsyncRequest(uri, HttpMethod.GET);
|
|
|
|
|
assertEquals("Invalid HTTP method", HttpMethod.GET, request.getMethod());
|
|
|
|
|
assertEquals("Invalid HTTP URI", uri, request.getURI());
|
|
|
|
|
Future<ClientHttpResponse> futureResponse = request.executeAsync();
|
|
|
|
|
ClientHttpResponse response = futureResponse.get();
|
|
|
|
|
try {
|
|
|
|
|
assertEquals("Invalid status code", HttpStatus.NOT_FOUND, response.getStatusCode());
|
|
|
|
|
} finally {
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
response.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -78,7 +81,7 @@ public abstract class AbstractAsyncHttpRequestFactoryTestCase extends AbstractJe
|
|
|
|
|
@Test
|
|
|
|
|
public void statusCallback() throws Exception {
|
|
|
|
|
URI uri = new URI(baseUrl + "/status/notfound");
|
|
|
|
|
AsyncClientHttpRequest request = factory.createAsyncRequest(uri, HttpMethod.GET);
|
|
|
|
|
AsyncClientHttpRequest request = this.factory.createAsyncRequest(uri, HttpMethod.GET);
|
|
|
|
|
assertEquals("Invalid HTTP method", HttpMethod.GET, request.getMethod());
|
|
|
|
|
assertEquals("Invalid HTTP URI", uri, request.getURI());
|
|
|
|
|
ListenableFuture<ClientHttpResponse> listenableFuture = request.executeAsync();
|
|
|
|
|
@@ -108,7 +111,7 @@ public abstract class AbstractAsyncHttpRequestFactoryTestCase extends AbstractJe
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void echo() throws Exception {
|
|
|
|
|
AsyncClientHttpRequest request = factory.createAsyncRequest(new URI(baseUrl + "/echo"), HttpMethod.PUT);
|
|
|
|
|
AsyncClientHttpRequest request = this.factory.createAsyncRequest(new URI(baseUrl + "/echo"), HttpMethod.PUT);
|
|
|
|
|
assertEquals("Invalid HTTP method", HttpMethod.PUT, request.getMethod());
|
|
|
|
|
String headerName = "MyHeader";
|
|
|
|
|
String headerValue1 = "value1";
|
|
|
|
|
@@ -143,7 +146,7 @@ public abstract class AbstractAsyncHttpRequestFactoryTestCase extends AbstractJe
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void multipleWrites() throws Exception {
|
|
|
|
|
AsyncClientHttpRequest request = factory.createAsyncRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
|
|
|
|
|
AsyncClientHttpRequest request = this.factory.createAsyncRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
|
|
|
|
|
final byte[] body = "Hello World".getBytes("UTF-8");
|
|
|
|
|
|
|
|
|
|
if (request instanceof StreamingHttpOutputMessage) {
|
|
|
|
|
@@ -170,7 +173,7 @@ public abstract class AbstractAsyncHttpRequestFactoryTestCase extends AbstractJe
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void headersAfterExecute() throws Exception {
|
|
|
|
|
AsyncClientHttpRequest request = factory.createAsyncRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
|
|
|
|
|
AsyncClientHttpRequest request = this.factory.createAsyncRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
|
|
|
|
|
request.getHeaders().add("MyHeader", "value");
|
|
|
|
|
byte[] body = "Hello World".getBytes("UTF-8");
|
|
|
|
|
FileCopyUtils.copy(body, request.getBody());
|
|
|
|
|
@@ -202,7 +205,7 @@ public abstract class AbstractAsyncHttpRequestFactoryTestCase extends AbstractJe
|
|
|
|
|
protected void assertHttpMethod(String path, HttpMethod method) throws Exception {
|
|
|
|
|
ClientHttpResponse response = null;
|
|
|
|
|
try {
|
|
|
|
|
AsyncClientHttpRequest request = factory.createAsyncRequest(new URI(baseUrl + "/methods/" + path), method);
|
|
|
|
|
AsyncClientHttpRequest request = this.factory.createAsyncRequest(new URI(baseUrl + "/methods/" + path), method);
|
|
|
|
|
Future<ClientHttpResponse> futureResponse = request.executeAsync();
|
|
|
|
|
response = futureResponse.get();
|
|
|
|
|
assertEquals("Invalid response status", HttpStatus.OK, response.getStatusCode());
|
|
|
|
|
|