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 ddd0aae  Tests.
ddd0aae is described below

commit ddd0aae3851c91644a5128f4207582e18160a789
Author: JamesBognar <[email protected]>
AuthorDate: Fri Apr 10 18:40:50 2020 -0400

    Tests.
---
 .../apache/juneau/rest/client2/RestClientTest.java | 77 ++++++++++++++++------
 .../juneau/rest/client2/RestCallException.java     | 71 +++++++++++++-------
 2 files changed, 103 insertions(+), 45 deletions(-)

diff --git 
a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientTest.java
 
b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientTest.java
index b752f42..e60e808 100644
--- 
a/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientTest.java
+++ 
b/juneau-rest/juneau-rest-client-utest/src/test/java/org/apache/juneau/rest/client2/RestClientTest.java
@@ -2723,16 +2723,12 @@ public class RestClientTest {
 
        @Test
        public void m04_parser_parserListener() throws Exception {
-               RestClient rc = null;
-
-               rc = MockRestClient
-                       .create(A.class)
-                       .parser(JsonParser.class)
-                       .parserListener(M4L.class)
-                       .build();
-
                try {
-                       rc
+                       MockRestClient
+                               .create(A.class)
+                               .parser(JsonParser.class)
+                               .parserListener(M4L.class)
+                               .build()
                                .post("/echoBody", "{f:'1'}")
                                .run()
                                .getBody().as(M4.class);
@@ -2743,18 +2739,57 @@ public class RestClientTest {
                }
        }
 
-//     @Test
-//     public void m05_parser_strictBoolean() throws Exception { fail(); }
-////   public RestClientBuilder strict(boolean value) {
-//
-//     @Test
-//     public void m06_parser_strict() throws Exception { fail(); }
-////   public RestClientBuilder strict() {
-//
-//     @Test
-//     public void m07_parser_trimStringsPBoolean() throws Exception { fail(); 
}
-////   public RestClientBuilder trimStringsP(boolean value) {
-//
+       public static class M5 {
+               public int f;
+       }
+
+       @Test
+       public void m05_parser_strict() throws Exception {
+               try {
+                       MockRestClient
+                               .create(A.class)
+                               .json()
+                               .strict()
+                               .build()
+                               .post("/echoBody", new StringReader("{f:1}"))
+                               .run()
+                               .getBody().as(M5.class);
+                       fail("Exception expected");
+               } catch (Exception e) {
+                       assertTrue(e.getMessage().contains("Unquoted attribute 
detected."));
+               }
+
+               try {
+                       MockRestClient
+                               .create(A.class)
+                               .json()
+                               .strict(true)
+                               .build()
+                               .post("/echoBody", new StringReader("{f:1}"))
+                               .run()
+                               .getBody().as(M5.class);
+                       fail("Exception expected");
+               } catch (Exception e) {
+                       assertTrue(e.getMessage().contains("Unquoted attribute 
detected."));
+               }
+
+               MockRestClient
+                       .create(A.class)
+                       .json()
+                       .strict(false)
+                       .build()
+                       .post("/echoBody", new StringReader("{f:1}"))
+                       .run()
+                       .getBody().as(M5.class);
+       }
+
+       @Test
+       public void m07_parser_trimStringsOnRead() throws Exception {
+
+
+       }
+//     public RestClientBuilder trimStringsP(boolean value) {
+
 //     @Test
 //     public void m08_parser_trimStringsP() throws Exception { fail(); }
 ////   public RestClientBuilder trimStringsP() {
diff --git 
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestCallException.java
 
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestCallException.java
index 5bab939..9cf4722 100644
--- 
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestCallException.java
+++ 
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client2/RestCallException.java
@@ -12,10 +12,7 @@
 // 
***************************************************************************************************************************
 package org.apache.juneau.rest.client2;
 
-import static java.lang.String.*;
-import static org.apache.juneau.internal.StringUtils.*;
 import static org.apache.juneau.internal.IOUtils.*;
-import static org.apache.juneau.internal.StringUtils.format;
 
 import java.io.*;
 import java.net.*;
@@ -25,6 +22,7 @@ import java.util.regex.*;
 import org.apache.http.*;
 import org.apache.http.client.*;
 import org.apache.http.util.*;
+import org.apache.juneau.internal.*;
 import org.apache.juneau.reflect.*;
 
 /**
@@ -82,7 +80,7 @@ public final class RestCallException extends HttpException {
         * @param e The inner cause of the exception.
         */
        public RestCallException(Exception e) {
-               super(e.getLocalizedMessage(), e);
+               super(clean(e.getLocalizedMessage()), e);
                if (e instanceof FileNotFoundException) {
                        responseCode = 404;
                } else if (e.getMessage() != null) {
@@ -101,15 +99,7 @@ public final class RestCallException extends HttpException {
         * @param response The HTTP response object.
         */
        public RestCallException(String msg, HttpResponse response) {
-               super(format("{0}\n{1}\nstatus=''{2}''\nResponse: \n{3}", msg, 
response.getStatusLine().getStatusCode(), readEntity(response)));
-       }
-
-       private static String readEntity(HttpResponse response) {
-               try {
-                       return EntityUtils.toString(response.getEntity(), UTF8);
-               } catch (Exception e) {
-                       throw new RuntimeException(e);
-               }
+               super(clean(format("{0}\n{1}\nstatus=''{2}''\nResponse: \n{3}", 
msg, response.getStatusLine().getStatusCode(), readEntity(response))));
        }
 
        /**
@@ -247,19 +237,52 @@ public final class RestCallException extends 
HttpException {
                return responseStatusMessage;
        }
 
-       /**
-        * Finds the message.
-        *
-        * @param cause The cause.
-        * @param msg The message.
-        * @param def The default value if both above are <jk>null</jk>.
-        * @return The resolved message.
-        */
-       protected static final String getMessage(Throwable cause, String msg, 
String def) {
+       
//------------------------------------------------------------------------------------------------------------------
+       // Helper methods
+       
//------------------------------------------------------------------------------------------------------------------
+
+       private static String getMessage(Throwable cause, String msg, String 
def) {
                if (msg != null)
-                       return msg;
+                       return clean(msg);
                if (cause != null)
-                       return cause.getMessage();
+                       return clean(cause.getMessage());
                return def;
        }
+
+       private static String format(String msg, Object...args) {
+               if (args.length == 0)
+                       return clean(msg);
+               return clean(StringUtils.format(msg, args));
+       }
+
+       // HttpException has a bug involving ASCII control characters so just 
replace them with spaces.
+       private static String clean(String message) {
+
+               if (message == null)
+                       return null;
+
+               boolean needsCleaning = false;
+               for (int i = 0; i < message.length() && !needsCleaning; i++)
+                       if (message.charAt(i) < 32)
+                               needsCleaning = true;
+
+               if (!needsCleaning)
+                       return message;
+
+               StringBuilder sb = new StringBuilder(message.length());
+               for (int i = 0; i < message.length(); i++) {
+                       char c = message.charAt(i);
+                       sb.append(c < 32 ? ' ' : c);
+               }
+
+               return sb.toString();
+       }
+
+       private static String readEntity(HttpResponse response) {
+               try {
+                       return EntityUtils.toString(response.getEntity(), UTF8);
+               } catch (Exception e) {
+                       throw new RuntimeException(e);
+               }
+       }
 }

Reply via email to