This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 9b9458f RestClient tests.
9b9458f is described below
commit 9b9458f6fd897b969cc021286cc99eb2dda42d05
Author: JamesBognar <[email protected]>
AuthorDate: Thu Jul 2 16:48:17 2020 -0400
RestClient tests.
---
.../assertions/FluentByteArrayAssertion.java | 9 +-
.../juneau/assertions/FluentLongAssertion.java | 14 +
.../juneau/assertions/FluentObjectAssertion.java | 3 +-
.../assertions/FluentThrowableAssertion.java | 13 +-
.../client2/RestClient_Response_Body_Test.java | 345 ++++++++++++++++++++-
.../juneau/rest/client2/RestResponseBody.java | 31 +-
6 files changed, 381 insertions(+), 34 deletions(-)
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentByteArrayAssertion.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentByteArrayAssertion.java
index b5ad79a..b4b913a 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentByteArrayAssertion.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentByteArrayAssertion.java
@@ -24,7 +24,6 @@ import org.apache.juneau.internal.*;
* @param <R> The return type.
*/
@FluentSetters(returns="FluentByteArrayAssertion<R>")
-@SuppressWarnings({ "rawtypes", "unchecked" })
public class FluentByteArrayAssertion<R> extends FluentAssertion<R> {
private byte[] contents;
@@ -68,7 +67,7 @@ public class FluentByteArrayAssertion<R> extends
FluentAssertion<R> {
* @return A new fluent string assertion.
*/
public FluentStringAssertion<R> string(Charset cs) {
- return new FluentStringAssertion(contents == null ? null : new
String(contents, cs), this);
+ return new FluentStringAssertion<>(contents == null ? null :
new String(contents, cs), returns());
}
/**
@@ -83,7 +82,7 @@ public class FluentByteArrayAssertion<R> extends
FluentAssertion<R> {
* @return A new fluent string assertion.
*/
public FluentStringAssertion<R> base64() {
- return new FluentStringAssertion(contents == null ? null :
base64Encode(contents), this);
+ return new FluentStringAssertion<>(contents == null ? null :
base64Encode(contents), returns());
}
/**
@@ -98,7 +97,7 @@ public class FluentByteArrayAssertion<R> extends
FluentAssertion<R> {
* @return A new string consisting of hexadecimal characters.
*/
public FluentStringAssertion<R> hex() {
- return new FluentStringAssertion(contents == null ? null :
toHex(contents), this);
+ return new FluentStringAssertion<>(contents == null ? null :
toHex(contents), returns());
}
/**
@@ -113,7 +112,7 @@ public class FluentByteArrayAssertion<R> extends
FluentAssertion<R> {
* @return A new string consisting of hexadecimal characters.
*/
public FluentStringAssertion<R> spacedHex() {
- return new FluentStringAssertion(contents == null ? null :
toSpacedHex(contents), this);
+ return new FluentStringAssertion<>(contents == null ? null :
toSpacedHex(contents), returns());
}
// <FluentSetters>
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentLongAssertion.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentLongAssertion.java
index 12fe810..5f5f25a 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentLongAssertion.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentLongAssertion.java
@@ -81,6 +81,20 @@ public class FluentLongAssertion<R> extends
FluentAssertion<R> {
/**
* Asserts that the value equals the specified value.
*
+ * <p>
+ * Equivalent to {@link #equals(Long)}.
+ *
+ * @param value The value to check against.
+ * @return The response object (for method chaining).
+ * @throws AssertionError If assertion failed.
+ */
+ public R is(int value) throws AssertionError {
+ return equals((long)value);
+ }
+
+ /**
+ * Asserts that the value equals the specified value.
+ *
* @param value The value to check against.
* @return The response object (for method chaining).
* @throws AssertionError If assertion failed.
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentObjectAssertion.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentObjectAssertion.java
index 5783a64..ec86c0b 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentObjectAssertion.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentObjectAssertion.java
@@ -25,7 +25,6 @@ import org.apache.juneau.serializer.*;
* @param <R> The return type.
*/
@FluentSetters(returns="FluentObjectAssertion<R>")
-@SuppressWarnings({ "rawtypes", "unchecked" })
public class FluentObjectAssertion<R> extends FluentAssertion<R> {
private final Object value;
@@ -102,7 +101,7 @@ public class FluentObjectAssertion<R> extends
FluentAssertion<R> {
public FluentStringAssertion<R> serialized(WriterSerializer ws) {
try {
String s = ws.serialize(this.value);
- return new FluentStringAssertion(s, this);
+ return new FluentStringAssertion<>(s, returns());
} catch (SerializeException e) {
throw new RuntimeException(e);
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentThrowableAssertion.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentThrowableAssertion.java
index feb4782..29f6211 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentThrowableAssertion.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/assertions/FluentThrowableAssertion.java
@@ -22,7 +22,6 @@ import org.apache.juneau.internal.*;
* @param <R> The return type.
*/
@FluentSetters(returns="FluentThrowableAssertion<R>")
-@SuppressWarnings({ "rawtypes", "unchecked" })
public class FluentThrowableAssertion<R> extends FluentAssertion<R> {
private final Throwable value;
@@ -161,7 +160,7 @@ public class FluentThrowableAssertion<R> extends
FluentAssertion<R> {
* @return An assertion against the throwable message. Never
<jk>null</jk>.
*/
public FluentStringAssertion<R> message() {
- return new FluentStringAssertion(value == null ? null :
value.getMessage(), this);
+ return new FluentStringAssertion<>(value == null ? null :
value.getMessage(), returns());
}
/**
@@ -176,7 +175,7 @@ public class FluentThrowableAssertion<R> extends
FluentAssertion<R> {
* @return An assertion against the throwable localized message. Never
<jk>null</jk>.
*/
public FluentStringAssertion<R> localizedMessage() {
- return new FluentStringAssertion(value == null ? null :
value.getLocalizedMessage(), this);
+ return new FluentStringAssertion<>(value == null ? null :
value.getLocalizedMessage(), returns());
}
/**
@@ -191,7 +190,7 @@ public class FluentThrowableAssertion<R> extends
FluentAssertion<R> {
* @return An assertion against the throwable stacktrace. Never
<jk>null</jk>.
*/
public FluentStringAssertion<R> stackTrace() {
- return new FluentStringAssertion(value == null ? null :
StringUtils.getStackTrace(value), this);
+ return new FluentStringAssertion<>(value == null ? null :
StringUtils.getStackTrace(value), returns());
}
/**
@@ -206,7 +205,7 @@ public class FluentThrowableAssertion<R> extends
FluentAssertion<R> {
* @return An assertion against the caused-by. Never <jk>null</jk>.
*/
public FluentThrowableAssertion<R> causedBy() {
- return new FluentThrowableAssertion(value == null ? null :
value.getCause(), this);
+ return new FluentThrowableAssertion<>(value == null ? null :
value.getCause(), returns());
}
/**
@@ -225,10 +224,10 @@ public class FluentThrowableAssertion<R> extends
FluentAssertion<R> {
Throwable t = value;
while (t != null) {
if (throwableClass.isInstance(t))
- return new FluentThrowableAssertion(t, this);
+ return new FluentThrowableAssertion<>(t,
returns());
t = t.getCause();
}
- return new FluentThrowableAssertion(null, this);
+ return new FluentThrowableAssertion<>(null, returns());
}
// <FluentSetters>
diff --git
a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Response_Body_Test.java
b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Response_Body_Test.java
index f451923..5c93413 100644
---
a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Response_Body_Test.java
+++
b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Response_Body_Test.java
@@ -13,14 +13,29 @@
package org.apache.juneau.rest.client2;
import static org.apache.juneau.assertions.Assertions.*;
+import static org.junit.Assert.*;
import static org.junit.runners.MethodSorters.*;
import java.io.*;
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.regex.*;
+import org.apache.http.*;
+import org.apache.http.conn.*;
+import org.apache.http.entity.*;
+import org.apache.http.entity.ContentType;
+import org.apache.http.message.*;
+import org.apache.juneau.*;
+import org.apache.juneau.http.header.*;
+import org.apache.juneau.internal.*;
import org.apache.juneau.json.*;
+import org.apache.juneau.parser.*;
import org.apache.juneau.rest.*;
import org.apache.juneau.rest.annotation.*;
+import org.apache.juneau.rest.client2.RestRequest;
import org.apache.juneau.rest.mock2.*;
+import org.apache.juneau.utils.*;
import org.apache.juneau.xml.*;
import org.junit.*;
@@ -48,6 +63,33 @@ public class RestClient_Response_Body_Test {
public ABean getBean() {
return bean;
}
+ @RestMethod
+ public void head() {
+ }
+ }
+
+ public static class TestClient extends MockRestClient {
+ public HttpEntity responseEntity;
+ public Header[] headers = {};
+ public TestClient entity(HttpEntity entity) {
+ this.responseEntity = entity;
+ return this;
+ }
+ public TestClient headers(Header...o) {
+ this.headers = o;
+ return this;
+ }
+ public TestClient(PropertyStore ps) {
+ super(ps);
+ }
+ @Override
+ protected MockRestResponse createResponse(RestRequest request,
HttpResponse httpResponse, Parser parser) throws RestCallException {
+ HttpResponse r = new BasicHttpResponse(new
ProtocolVersion("http", 1,1),200,"");
+ r.setEntity(responseEntity);
+ for (Header h : headers)
+ r.addHeader(h);
+ return new MockRestResponse(this, request, r, parser);
+ }
}
@Test
@@ -66,10 +108,270 @@ public class RestClient_Response_Body_Test {
@Test
public void a03_asInputStream() throws Exception {
- RestResponse x = client().build().get("/bean").run();
- InputStream is = x.getBody().asInputStream();
+ RestResponse r1 = client().build().get("/bean").run();
+ InputStream is = r1.getBody().asInputStream();
assertStream(is).string().is("{f:1}");
-
assertThrown(()->x.getBody().asInputStream()).contains("Response has already
been consumed.");
+
assertThrown(()->r1.getBody().asInputStream()).contains("Response has already
been consumed.");
+
+ // Non-repeatable entity.
+ TestClient x = testClient().entity(inputStreamEntity("{f:2}"));
+ RestResponse r2 = x.get("/bean").run();
+ r2.getBody().asInputStream();
+
assertThrown(()->r2.getBody().asInputStream()).contains("Response has already
been consumed");
+
+ // Repeatable entity.
+ x.entity(new StringEntity("{f:2}"));
+ RestResponse r3 = x.get("/bean").run();
+ r3.getBody().asInputStream();
+ is = r3.getBody().asInputStream();
+ assertStream(is).string().is("{f:2}");
+ is = x.get("/bean").run().getBody().asInputStream();
+ ((EofSensorInputStream)is).abortConnection();
+
+ RestCallInterceptor rci = new BasicRestCallInterceptor() {
+ @Override
+ public void onClose(RestRequest req, RestResponse res)
throws Exception {
+ throw new NullPointerException("foo");
+ }
+ };
+
+ TestClient x2 =
client().interceptors(rci).build(TestClient.class).entity(new
StringEntity("{f:2}"));
+
assertThrown(()->x2.get("/bean").run().getBody().cache().asInputStream()).contains("foo");
+ is = x2.get("/bean").run().getBody().asInputStream();
+ assertStream(is).string().is("{f:2}");
+ is = x2.get("/bean").run().getBody().asInputStream();
+ is.close();
+ is = x2.get("/bean").run().getBody().asInputStream();
+ ((EofSensorInputStream)is).abortConnection();
+ }
+
+ @Test
+ public void a04_asReader() throws Exception {
+ TestClient x = testClient();
+ x.entity(inputStreamEntity("{f:1}"));
+ Reader r = x.get("/bean").run().getBody().asReader();
+ assertReader(r).is("{f:1}");
+
+ x.entity(inputStreamEntity("{f:1}"));
+ r = x.get("/bean").run().getBody().asReader(IOUtils.UTF8);
+ assertReader(r).is("{f:1}");
+
+ x.entity(inputStreamEntity("{f:1}"));
+ r = x.get("/bean").run().getBody().asReader(null);
+ assertReader(r).is("{f:1}");
+ }
+
+ @Test
+ public void a05_asBytes() throws Exception {
+ byte[] x =
client().build().get("/bean").run().getBody().asBytes();
+ assertBytes(x).string().is("{f:1}");
+
+ x =
client().build().get("/bean").run().getBody().cache().assertBytes().string().is("{f:1}").getBody().asBytes();
+ assertBytes(x).string().is("{f:1}");
+
+ assertThrown(()->testClient().entity(new
InputStreamEntity(badStream())).get().run().getBody().asBytes()).contains("foo");
+ }
+
+ @Test
+ public void a06_pipeTo() throws Exception {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ client().build().get("/bean").run().getBody().pipeTo(baos);
+ assertBytes(baos.toByteArray()).string().is("{f:1}");
+
+ StringWriter sw = new StringWriter();
+ client().build().get("/bean").run().getBody().pipeTo(sw);
+ assertString(sw.toString()).is("{f:1}");
+
+ sw = new StringWriter();
+
client().build().get("/bean").run().getBody().pipeTo(sw,IOUtils.UTF8);
+ assertString(sw.toString()).is("{f:1}");
+ }
+
+ public static class A7a {
+ String x;
+ public static A7a fromReader(Reader r) throws IOException {
+ A7a x = new A7a();
+ x.x = IOUtils.read(r);
+ return x;
+ }
+ }
+
+ public static class A7b {
+ String x;
+ public static A7b fromInputStream(InputStream is) throws
IOException {
+ A7b x = new A7b();
+ x.x = IOUtils.read(is);
+ return x;
+ }
+ }
+
+ public static class A7c {
+ public A7c() {
+ throw new RuntimeException("foo");
+ }
+ }
+
+ @Test
+ public void a07_asType() throws Exception {
+ List<Integer> x1 =
testClient().entity(stringEntity("[1,2]")).get().run().getBody().as(List.class,Integer.class);
+ assertObject(x1).json().is("[1,2]");
+
+ Mutable<List<Integer>> x2 = mutable();
+
testClient().entity(stringEntity("[1,2]")).get().run().getBody().as(x2,List.class,Integer.class);
+ assertObject(x2.get()).json().is("[1,2]");
+
+ ABean x3 =
testClient().entity(stringEntity("{f:1}")).get().run().getBody().as(ABean.class);
+ assertObject(x3).json().is("{f:1}");
+
+ Mutable<ABean> x4 = mutable();
+
testClient().entity(stringEntity("{f:1}")).get().run().getBody().as(x4,ABean.class);
+ assertObject(x4.get()).json().is("{f:1}");
+
+ HttpEntity x5 =
testClient().entity(stringEntity("{f:1}")).get().run().getBody().as(RestResponseBody.class);
+ assertTrue(x5 instanceof RestResponseBody);
+
+ HttpEntity x6 =
testClient().entity(stringEntity("{f:1}")).get().run().getBody().as(HttpEntity.class);
+ assertTrue(x6 instanceof RestResponseBody);
+
+
plainTestClient().entity(stringEntity("foo")).get().run().getBody().assertObject(A7a.class).passes(x->((A7a)x).x.equals("foo"));
+
plainTestClient().entity(stringEntity("foo")).get().run().getBody().assertObject(A7b.class).passes(x->((A7b)x).x.equals("foo"));
+
assertThrown(()->plainTestClient().entity(stringEntity("foo")).headers(header("Content-Type","foo")).get().run().getBody().as(A7c.class)).exists().contains("Unsupported
media-type","'foo'");
+
assertThrown(()->testClient().entity(stringEntity("")).get().run().getBody().as(A7c.class)).contains("foo");
+
+ Mutable<ABean> x7 = mutable();
+
testClient().entity(stringEntity("{f:1}")).get().run().getBody().as(x7,cm(ABean.class));
+ assertObject(x7.get()).json().is("{f:1}");
+
+ Future<ABean> x8 =
testClient().entity(stringEntity("{f:1}")).get().run().getBody().asFuture(ABean.class);
+ assertObject(x8.get()).json().is("{f:1}");
+
+ Mutable<Future<ABean>> x9 = mutable();
+
testClient().entity(stringEntity("{f:1}")).get().run().getBody().asFuture(x9,ABean.class);
+ assertObject(x9.get().get()).json().is("{f:1}");
+
+ Future<ABean> x10 =
testClient().entity(stringEntity("{f:1}")).get().run().getBody().asFuture(cm(ABean.class));
+ assertObject(x10.get()).json().is("{f:1}");
+
+ Mutable<Future<ABean>> x11 = mutable();
+
testClient().entity(stringEntity("{f:1}")).get().run().getBody().asFuture(x11,cm(ABean.class));
+ assertObject(x11.get().get()).json().is("{f:1}");
+
+ Future<List<Integer>> x12 =
testClient().entity(stringEntity("[1,2]")).get().run().getBody().asFuture(List.class,Integer.class);
+ assertObject(x12.get()).json().is("[1,2]");
+
+ Mutable<Future<List<Integer>>> x13 = mutable();
+
testClient().entity(stringEntity("[1,2]")).get().run().getBody().asFuture(x13,List.class,Integer.class);
+ assertObject(x13.get().get()).json().is("[1,2]");
+
+ String x14 =
testClient().entity(stringEntity("{f:1}")).get().run().getBody().asString();
+ assertString(x14).is("{f:1}");
+
+ assertThrown(()->testClient().entity(new
InputStreamEntity(badStream())).get().run().getBody().asString()).contains("foo");
+
+ Mutable<String> x15 = mutable();
+
testClient().entity(stringEntity("{f:1}")).get().run().getBody().asString(x15);
+ assertString(x15.get()).is("{f:1}");
+
+ Future<String> x16 =
testClient().entity(stringEntity("{f:1}")).get().run().getBody().asStringFuture();
+ assertString(x16.get()).is("{f:1}");
+
+ Mutable<Future<String>> x17 = mutable();
+
testClient().entity(stringEntity("{f:1}")).get().run().getBody().asStringFuture(x17);
+ assertString(x17.get().get()).is("{f:1}");
+
+ String x18 =
testClient().entity(stringEntity("12345")).get().run().getBody().asAbbreviatedString(4);
+ assertString(x18).is("1...");
+
+ Mutable<String> x19 = mutable();
+
testClient().entity(stringEntity("12345")).get().run().getBody().asAbbreviatedString(x19,4);
+ assertString(x19.get()).is("1...");
+
+ PojoRest x20 =
testClient().entity(stringEntity("{f:1}")).get().run().getBody().asPojoRest(ABean.class);
+ assertString(x20.get("f")).is("1");
+
+ Mutable<PojoRest> x21 = mutable();
+
testClient().entity(stringEntity("{f:1}")).get().run().getBody().asPojoRest(x21,ABean.class);
+ assertString(x21.get().get("f")).is("1");
+
+ PojoRest x22 =
testClient().entity(stringEntity("{f:1}")).get().run().getBody().asPojoRest();
+ assertString(x22.get("f")).is("1");
+
+ Mutable<PojoRest> x23 = mutable();
+
testClient().entity(stringEntity("{f:1}")).get().run().getBody().asPojoRest(x23);
+ assertString(x23.get().get("f")).is("1");
+
+ Matcher x24 =
testClient().entity(stringEntity("foo=123")).get().run().getBody().asMatcher(Pattern.compile("foo=(.*)"));
+ assertTrue(x24.matches());
+ assertString(x24.group(1)).is("123");
+
+ Mutable<Matcher> x25 = mutable();
+
testClient().entity(stringEntity("foo=123")).get().run().getBody().asMatcher(x25,Pattern.compile("foo=(.*)"));
+ assertTrue(x25.get().matches());
+ assertString(x25.get().group(1)).is("123");
+
+ Matcher x26 =
testClient().entity(stringEntity("foo=123")).get().run().getBody().asMatcher("foo=(.*)");
+ assertTrue(x26.matches());
+ assertString(x26.group(1)).is("123");
+
+ Mutable<Matcher> x27 = mutable();
+
testClient().entity(stringEntity("foo=123")).get().run().getBody().asMatcher(x27,"foo=(.*)");
+ assertTrue(x27.get().matches());
+ assertString(x27.get().group(1)).is("123");
+ }
+
+
//------------------------------------------------------------------------------------------------------------------
+ // HttpEntity passthrough methods.
+
//------------------------------------------------------------------------------------------------------------------
+
+ @SuppressWarnings("deprecation")
+ @Test
+ public void b01_httpEntityMethods() throws Exception {
+ RestResponseBody x1 =
testClient().entity(stringEntity("foo")).get().run().getBody();
+ assertTrue(x1.isRepeatable());
+
+ RestResponseBody x2 =
testClient().entity(inputStreamEntity("foo")).get().run().getBody();
+ assertFalse(x2.isRepeatable());
+ assertLong(x2.getContentLength()).is(-1l);
+ x2.cache().asString();
+ assertTrue(x2.isRepeatable());
+ assertLong(x2.getContentLength()).is(3l);
+
+ assertFalse(x2.isChunked());
+
+
testClient().entity(inputStreamEntity("foo")).get().run().getBody().getContentEncoding().assertString().isNull();
+
+ InputStreamEntity x3 = inputStreamEntity("foo");
+ x3.setContentType("text/foo");
+ x3.setContentEncoding("identity");
+ testClient().entity(x3).get().run().getBody().toResponse()
+
.getBody().getContentType().assertString().is("text/foo")
+
.getBody().getContentEncoding().assertString().is("identity");
+
+ InputStream x4 =
testClient().entity(inputStreamEntity("foo")).get().run().getBody().asInputStream();
+ assertStream(x4).string().is("foo");
+
+ ByteArrayOutputStream x5 = new ByteArrayOutputStream();
+
testClient().entity(inputStreamEntity("foo")).get().run().getBody().writeTo(x5);
+ assertBytes(x5.toByteArray()).string().is("foo");
+
+
assertTrue(testClient().entity(inputStreamEntity("foo")).get().run().getBody().isStreaming());
+
assertFalse(testClient().entity(inputStreamEntity("foo")).get().run().getBody().cache().isStreaming());
+
assertFalse(testClient().entity(stringEntity("foo")).get().run().getBody().isStreaming());
+
+
testClient().entity(inputStreamEntity("foo")).get().run().getBody().consumeContent();
+ }
+
+ @SuppressWarnings("deprecation")
+ @Test
+ public void b02_head() throws Exception {
+
assertFalse(client().build().head("").run().getBody().isRepeatable());
+
assertFalse(client().build().head("").run().getBody().isChunked());
+
assertLong(client().build().head("").run().getBody().getContentLength()).is(-1);
+
client().build().head("").run().getBody().getContentType().assertString().isNull();
+
client().build().head("").run().getBody().getContentEncoding().assertString().isNull();
+ client().build().head("").run().getBody().writeTo(new
ByteArrayOutputStream());
+
assertFalse(client().build().head("").run().getBody().isStreaming());
+ client().build().head("").run().getBody().consumeContent();
}
//------------------------------------------------------------------------------------------------------------------
@@ -79,4 +381,41 @@ public class RestClient_Response_Body_Test {
private static RestClientBuilder client() {
return MockRestClient.create(A.class).simpleJson();
}
+
+ private static TestClient plainTestClient() {
+ return MockRestClient.create(A.class).build(TestClient.class);
+ }
+
+ private static TestClient testClient() {
+ return
MockRestClient.create(A.class).simpleJson().build(TestClient.class);
+ }
+
+ private static StringEntity stringEntity(String in) {
+ return new StringEntity(in, (ContentType)null);
+ }
+
+ private static Header header(String name, Object val) {
+ return BasicStringHeader.of(name, val);
+ }
+
+ private static InputStreamEntity inputStreamEntity(String in) {
+ return new InputStreamEntity(new
ByteArrayInputStream(in.getBytes()));
+ }
+
+ private static <T> ClassMeta<T> cm(Class<T> t) {
+ return BeanContext.DEFAULT.getClassMeta(t);
+ }
+
+ private static InputStream badStream() {
+ return new InputStream() {
+ @Override
+ public int read() throws IOException {
+ throw new IOException("foo");
+ }
+ };
+ }
+
+ private static <T> Mutable<T> mutable() {
+ return Mutable.create();
+ }
}
diff --git
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestResponseBody.java
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestResponseBody.java
index 323cc3b..4218c62 100644
---
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestResponseBody.java
+++
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestResponseBody.java
@@ -17,6 +17,7 @@ import static org.apache.juneau.internal.StringUtils.*;
import java.io.*;
import java.lang.reflect.*;
+import java.nio.charset.*;
import java.util.concurrent.*;
import java.util.regex.*;
@@ -299,10 +300,11 @@ public class RestResponseBody implements HttpEntity {
String ct = getContentType().asString();
// First look for "charset=" in Content-Type header of response.
- if (ct != null && ct.contains("charset="))
- cs = ct.substring(ct.indexOf("charset=")+8).trim();
+ if (ct != null)
+ if (ct.contains("charset="))
+ cs =
ct.substring(ct.indexOf("charset=")+8).trim();
- return asReader(cs);
+ return asReader(cs == null ? IOUtils.UTF8 :
Charset.forName(cs));
}
/**
@@ -326,15 +328,10 @@ public class RestResponseBody implements HttpEntity {
* <br>For responses without a body(e.g. HTTP 204), returns an
empty reader.
* @throws IOException If an exception occurred.
*/
- public Reader asReader(String charset) throws IOException {
- try {
- return new InputStreamReader(asInputStream(), charset
== null ? "UTF-8" : charset);
- } catch (UnsupportedEncodingException e) {
- throw new IOException(e);
- }
+ public Reader asReader(Charset charset) throws IOException {
+ return new InputStreamReader(asInputStream(), charset == null ?
IOUtils.UTF8 : charset);
}
-
/**
* Returns the HTTP response message body as a byte array.
*
@@ -428,7 +425,7 @@ public class RestResponseBody implements HttpEntity {
* @return The response object (for method chaining).
* @throws IOException If an IO exception occurred.
*/
- public RestResponse pipeTo(Writer w, String charset) throws IOException
{
+ public RestResponse pipeTo(Writer w, Charset charset) throws
IOException {
return pipeTo(w, charset, false);
}
@@ -481,7 +478,7 @@ public class RestResponseBody implements HttpEntity {
* @return The response object (for method chaining).
* @throws IOException If an IO exception occurred.
*/
- public RestResponse pipeTo(Writer w, String charset, boolean byLines)
throws IOException {
+ public RestResponse pipeTo(Writer w, Charset charset, boolean byLines)
throws IOException {
IOPipe.create(asReader(charset), w).byLines(byLines).run();
return response;
}
@@ -838,8 +835,8 @@ public class RestResponseBody implements HttpEntity {
return
type.getInputStreamMutater().mutate(asInputStream());
throw new ParseException(
- "Unsupported media-type in request header
''Content-Type'': ''{0}''\n\tSupported media-types: {1}",
- response.getStringHeader("Content-Type"),
parser == null ? null : parser.getMediaTypes()
+ "Unsupported media-type in request header
''Content-Type'': ''{0}''",
+ response.getStringHeader("Content-Type")
);
} catch (ParseException | IOException e) {
@@ -1728,7 +1725,7 @@ public class RestResponseBody implements HttpEntity {
*/
@Override /* HttpEntity */
public long getContentLength() {
- return cached ? cache.length : entity.getContentLength();
+ return cache != null ? cache.length : entity.getContentLength();
}
/**
@@ -1796,14 +1793,14 @@ public class RestResponseBody implements HttpEntity {
* Tells whether this entity depends on an underlying stream.
*
* <ul class='notes'>
- * <li>This method always returns <jk>true</jk> if the response
body is cached (see {@link #cache()}.
+ * <li>This method always returns <jk>false</jk> if the response
body is cached (see {@link #cache()}.
* </ul>
*
* @return <jk>true</jk> if the entity content is streamed,
<jk>false</jk> otherwise.
*/
@Override /* HttpEntity */
public boolean isStreaming() {
- return cached ? true : entity.isStreaming();
+ return cached ? false : entity.isStreaming();
}
/**