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 28aec2f  org.apache.juneau.http tests.
28aec2f is described below

commit 28aec2f8081518ef188a4a4eb2ae245e9a58c950
Author: JamesBognar <[email protected]>
AuthorDate: Mon Jul 13 11:34:16 2020 -0400

    org.apache.juneau.http tests.
---
 .../apache/juneau/http/SerializedHttpEntity.java   | 45 +++++++++-------------
 .../juneau/rest/client2/RestClient_Body_Test.java  | 32 ++++++++++++++-
 2 files changed, 50 insertions(+), 27 deletions(-)

diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/SerializedHttpEntity.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/SerializedHttpEntity.java
index df12155..3503742 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/SerializedHttpEntity.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/SerializedHttpEntity.java
@@ -30,7 +30,6 @@ import org.apache.juneau.serializer.*;
 public class SerializedHttpEntity extends BasicHttpEntity {
        private final Serializer serializer;
        private HttpPartSchema schema;
-       private byte[] cache;
 
        /**
         * Creator.
@@ -82,12 +81,10 @@ public class SerializedHttpEntity extends BasicHttpEntity {
 
        @Override /* BasicHttpEntity */
        public void writeTo(OutputStream os) throws IOException {
-               os = new NoCloseOutputStream(os);
-               Object o = getRawContent();
-               if (o instanceof InputStream || o instanceof Reader || o 
instanceof File) {
-                       super.writeTo(os);
-               } else {
+               if (isSerializable()) {
                        try {
+                               os = new NoCloseOutputStream(os);
+                               Object o = getRawContent();
                                if (serializer == null) {
                                        // If no serializer specified, just 
close the stream.
                                        os.close();
@@ -101,45 +98,41 @@ public class SerializedHttpEntity extends BasicHttpEntity {
                        } catch (SerializeException e) {
                                throw new BasicRuntimeException(e, 
"Serialization error on request body.");
                        }
+               } else {
+                       super.writeTo(os);
                }
        }
 
        @Override /* BasicHttpEntity */
        public boolean isRepeatable() {
-               Object o = getRawContent();
-               return (! (o instanceof InputStream || o instanceof Reader));
+               if (isSerializable())
+                       return true;
+               return super.isRepeatable();
        }
 
        @Override /* BasicHttpEntity */
        public long getContentLength() {
-               return -1;
-       }
-
-       @Override /* BasicHttpEntity */
-       public Header getContentType() {
-               Header x = super.getContentType();
-               if (x != null)
-                       return x;
-               Object o = getRawContent();
-               if (o instanceof InputStream || o instanceof Reader || o 
instanceof File)
-                       return null;
-               return null;
+               if (isSerializable())
+                       return -1;
+               return super.getContentLength();
        }
 
        @Override /* BasicHttpEntity */
        public InputStream getContent() {
-               Object o = getRawContent();
-               if (o instanceof InputStream || o instanceof Reader || o 
instanceof File)
-                       return super.getContent();
-               if (cache == null) {
+               if (isSerializable()) {
                        try (ByteArrayOutputStream baos = new 
ByteArrayOutputStream()) {
                                writeTo(baos);
-                               cache = baos.toByteArray();
+                               return new 
ByteArrayInputStream(baos.toByteArray());
                        } catch (IOException e) {
                                throw new RuntimeException(e);
                        }
                }
-               return new ByteArrayInputStream(cache);
+               return super.getContent();
+       }
+
+       private boolean isSerializable() {
+               Object o = getRawContent();
+               return ! (o instanceof InputStream || o instanceof Reader || o 
instanceof File);
        }
 
        // <FluentSetters>
diff --git 
a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Body_Test.java
 
b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Body_Test.java
index 6e2a004..b99458f 100644
--- 
a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Body_Test.java
+++ 
b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClient_Body_Test.java
@@ -197,7 +197,7 @@ public class RestClient_Body_Test {
 
                SerializedHttpEntity x6 = serializedHttpEntity(f,null);
                client().build().post("/",x6).run()
-                       .assertHeader("X-Content-Length").doesNotExist()
+                       .assertHeader("X-Content-Length").is("0")
                        .assertHeader("X-Content-Encoding").doesNotExist()
                        .assertHeader("X-Content-Type").doesNotExist()
                        .getBody().assertObject(ABean.class).json().is("{a:0}");
@@ -211,6 +211,36 @@ public class RestClient_Body_Test {
 
                SerializedHttpEntity x8 = new SerializedHttpEntity(x7, 
null).cache();
                assertThrown(()->x8.getContent()).contains("bad");
+
+               SerializedHttpEntity x9 = serializedHttpEntity(new 
StringReader("foo"), null);
+               assertStream(x9.getContent()).string().is("foo");
+
+               SerializedHttpEntity x10 = serializedHttpEntity(new 
ByteArrayInputStream("foo".getBytes()), null);
+               assertStream(x10.getContent()).string().is("foo");
+
+               SerializedHttpEntity x11 = serializedHttpEntity(f, null);
+               assertStream(x11.getContent()).string().is("");
+
+               SerializedHttpEntity x12 = new 
SerializedHttpEntity(ABean.get(), null) {
+                       @Override
+                       public void writeTo(OutputStream os) throws IOException 
{
+                               throw new IOException("bad");
+                       }
+               };
+
+               assertThrown(()->x12.getContent()).contains("bad");
+
+               SerializedHttpEntity x13 = serializedHttpEntity(new 
StringReader("foo"), null).chunked();
+               client().build().post("/",x13).run()
+                       .assertHeader("X-Transfer-Encoding").is("chunked");
+
+               SerializedHttpEntity x14 = serializedHttpEntity(new 
StringReader("foo"), null).contentEncoding("identity");
+               client().build().post("/",x14).run()
+                       .assertHeader("X-Content-Encoding").is("identity");
+
+               SerializedHttpEntity x15 = serializedHttpEntity(new 
StringReader("foo"), null).contentLength(3l);
+               client().debug().build().post("/",x15).run()
+                       .assertHeader("X-Content-Length").is("3");
        }
 
        
//------------------------------------------------------------------------------------------------------------------

Reply via email to