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 09cbb473c9 Clean up juneau-rest-server
09cbb473c9 is described below
commit 09cbb473c99c8abfeaab2158b8a44f90a62b12e1
Author: James Bognar <[email protected]>
AuthorDate: Thu Jan 1 12:26:01 2026 -0500
Clean up juneau-rest-server
---
.../apache/juneau/rest/util/RestUtils_Test.java | 340 ++++++++++++++++++++-
1 file changed, 338 insertions(+), 2 deletions(-)
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/rest/util/RestUtils_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/rest/util/RestUtils_Test.java
index 2e435da1c2..1e777485f3 100644
--- a/juneau-utest/src/test/java/org/apache/juneau/rest/util/RestUtils_Test.java
+++ b/juneau-utest/src/test/java/org/apache/juneau/rest/util/RestUtils_Test.java
@@ -16,13 +16,17 @@
*/
package org.apache.juneau.rest.util;
-import static org.apache.juneau.commons.utils.CollectionUtils.*;
import static org.apache.juneau.commons.utils.StringUtils.*;
import static org.apache.juneau.junit.bct.BctAssertions.*;
import static org.apache.juneau.rest.util.RestUtils.*;
import static org.junit.jupiter.api.Assertions.*;
+import java.io.*;
+import java.util.*;
+
import org.apache.juneau.*;
+import org.apache.juneau.parser.ParseException;
+import org.apache.juneau.rest.mock.*;
import org.apache.juneau.urlencoding.*;
import org.junit.jupiter.api.*;
@@ -79,11 +83,227 @@ class RestUtils_Test extends TestBase {
assertEquals("//foo/bar", trimTrailingSlashes("//foo/bar//"));
}
+
//------------------------------------------------------------------------------------------------------------------
+ // getHttpResponseText(int)
+
//------------------------------------------------------------------------------------------------------------------
+
+ @Test void i01_testGetHttpResponseText() {
+ assertEquals("OK", getHttpResponseText(200));
+ assertEquals("Created", getHttpResponseText(201));
+ assertEquals("Accepted", getHttpResponseText(202));
+ assertEquals("No Content", getHttpResponseText(204));
+ assertEquals("Moved Permanently", getHttpResponseText(301));
+ assertEquals("Temporary Redirect", getHttpResponseText(302));
+ assertEquals("Not Modified", getHttpResponseText(304));
+ assertEquals("Bad Request", getHttpResponseText(400));
+ assertEquals("Unauthorized", getHttpResponseText(401));
+ assertEquals("Forbidden", getHttpResponseText(403));
+ assertEquals("Not Found", getHttpResponseText(404));
+ assertEquals("Method Not Allowed", getHttpResponseText(405));
+ assertEquals("Internal Server Error", getHttpResponseText(500));
+ assertEquals("Not Implemented", getHttpResponseText(501));
+ assertEquals("Service Unavailable", getHttpResponseText(503));
+ assertEquals("Gateway Timeout", getHttpResponseText(504));
+ }
+
+ @Test void i02_testGetHttpResponseTextInvalid() {
+ assertNull(getHttpResponseText(0));
+ assertNull(getHttpResponseText(99));
+ assertNull(getHttpResponseText(600));
+ assertNull(getHttpResponseText(-1));
+ }
+
+
//------------------------------------------------------------------------------------------------------------------
+ // getPathInfoUndecoded(HttpServletRequest)
+
//------------------------------------------------------------------------------------------------------------------
+
+ @Test void j01_testGetPathInfoUndecoded_basic() {
+ var req = MockServletRequest.create("GET", "/foo/bar");
+ assertEquals("/foo/bar", getPathInfoUndecoded(req));
+ }
+
+ @Test void j02_testGetPathInfoUndecoded_withContextPath() {
+ var req = MockServletRequest.create("GET",
"http://localhost:8080/context/foo/bar")
+ .contextPath("/context");
+ assertEquals("/foo/bar", getPathInfoUndecoded(req));
+ }
+
+ @Test void j03_testGetPathInfoUndecoded_withServletPath() {
+ var req = MockServletRequest.create("GET",
"http://localhost:8080/api/foo/bar")
+ .servletPath("/api");
+ assertEquals("/foo/bar", getPathInfoUndecoded(req));
+ }
+
+ @Test void j04_testGetPathInfoUndecoded_withContextAndServletPath() {
+ var req = MockServletRequest.create("GET",
"http://localhost:8080/context/api/foo/bar")
+ .contextPath("/context")
+ .servletPath("/api");
+ assertEquals("/foo/bar", getPathInfoUndecoded(req));
+ }
+
+ @Test void j05_testGetPathInfoUndecoded_noPathInfo() {
+ var req = MockServletRequest.create("GET",
"http://localhost:8080/context/api")
+ .contextPath("/context")
+ .servletPath("/api");
+ assertNull(getPathInfoUndecoded(req));
+ }
+
+ @Test void j06_testGetPathInfoUndecoded_encodedCharacters() {
+ var req = MockServletRequest.create("GET", "/foo%2Fbar%20baz");
+ assertEquals("/foo%2Fbar%20baz", getPathInfoUndecoded(req));
+ }
+
+ @Test void j07_testGetPathInfoUndecoded_rootPath() {
+ var req = MockServletRequest.create("GET", "/");
+ assertEquals("/", getPathInfoUndecoded(req));
+ }
+
+ @Test void j08_testGetPathInfoUndecoded_emptyContextAndServlet() {
+ var req = MockServletRequest.create("GET", "/foo/bar")
+ .contextPath("")
+ .servletPath("");
+ assertEquals("/foo/bar", getPathInfoUndecoded(req));
+ }
+
+
//------------------------------------------------------------------------------------------------------------------
+ // parseIfJson(String)
+
//------------------------------------------------------------------------------------------------------------------
+
+ @Test void k01_testParseIfJson_null() throws Exception {
+ assertNull(parseIfJson(null));
+ }
+
+ @Test void k02_testParseIfJson_plainString() throws Exception {
+ assertEquals("hello world", parseIfJson("hello world"));
+ assertEquals("foo", parseIfJson("foo"));
+ assertEquals("", parseIfJson(""));
+ assertEquals("123abc", parseIfJson("123abc"));
+ }
+
+ @Test void k03_testParseIfJson_jsonObject() throws Exception {
+ var result = parseIfJson("{\"name\":\"John\",\"age\":30}");
+ assertTrue(result instanceof Map);
+ var map = (Map<String, Object>) result;
+ assertEquals("John", map.get("name"));
+ assertEquals(30, map.get("age"));
+ }
+
+ @Test void k04_testParseIfJson_jsonObjectEmpty() throws Exception {
+ var result = parseIfJson("{}");
+ assertTrue(result instanceof Map);
+ var map = (Map<String, Object>) result;
+ assertTrue(map.isEmpty());
+ }
+
+ @Test void k05_testParseIfJson_jsonArray() throws Exception {
+ var result = parseIfJson("[1,2,3]");
+ assertTrue(result instanceof List);
+ var list = (List<Object>) result;
+ assertEquals(3, list.size());
+ assertEquals(1, list.get(0));
+ assertEquals(2, list.get(1));
+ assertEquals(3, list.get(2));
+ }
+
+ @Test void k06_testParseIfJson_jsonArrayEmpty() throws Exception {
+ var result = parseIfJson("[]");
+ assertTrue(result instanceof List);
+ var list = (List<Object>) result;
+ assertTrue(list.isEmpty());
+ }
+
+ @Test void k07_testParseIfJson_jsonBoolean() throws Exception {
+ assertEquals(true, parseIfJson("true"));
+ assertEquals(false, parseIfJson("false"));
+ }
+
+ @Test void k08_testParseIfJson_jsonNull() throws Exception {
+ assertNull(parseIfJson("null"));
+ }
+
+ @Test void k09_testParseIfJson_jsonNumber() throws Exception {
+ assertEquals(123, parseIfJson("123"));
+ var result = parseIfJson("123.45");
+ assertTrue(result instanceof Number);
+ assertEquals(123.45, ((Number)result).doubleValue(), 0.001);
+ assertEquals(-42, parseIfJson("-42"));
+ }
+
+ @Test void k10_testParseIfJson_jsonWithWhitespace() throws Exception {
+ var result = parseIfJson(" {\"key\":\"value\"} ");
+ assertTrue(result instanceof Map);
+ var map = (Map<String, Object>) result;
+ assertEquals("value", map.get("key"));
+ }
+
+ @Test void k11_testParseIfJson_invalidJson() {
+ // These strings are detected as JSON by isProbablyJson (start
with {/} or [/]) but are invalid
+ assertThrows(ParseException.class, () ->
parseIfJson("{key:xxx}"));
+ assertThrows(ParseException.class, () ->
parseIfJson("{key:invalid value}"));
+ }
+
+ @Test void k12_testParseIfJson_nestedStructures() throws Exception {
+ var result =
parseIfJson("{\"items\":[1,2,3],\"nested\":{\"a\":1,\"b\":2}}");
+ assertTrue(result instanceof Map);
+ var map = (Map<String, Object>) result;
+ assertTrue(map.get("items") instanceof List);
+ assertTrue(map.get("nested") instanceof Map);
+ }
+
+ @Test void k13_testParseIfJson_singleQuotedString() throws Exception {
+ var result = parseIfJson("'test string'");
+ assertEquals("test string", result);
+ }
+
+
//------------------------------------------------------------------------------------------------------------------
+ // parseQuery(String)
+
//------------------------------------------------------------------------------------------------------------------
+
+ @Test void g01_testParseQuery_null() {
+ var m = parseQuery((String)null);
+ assertTrue(m.isEmpty());
+ }
+
+ @Test void g02_testParseQuery_emptyString() {
+ var m = parseQuery("");
+ assertTrue(m.isEmpty());
+ }
+
+ @Test void g03_testParseQuery_whitespaceOnly() {
+ var m = parseQuery(" ");
+ assertTrue(m.isEmpty());
+ }
+
+
//------------------------------------------------------------------------------------------------------------------
+ // parseQuery(Reader)
+
//------------------------------------------------------------------------------------------------------------------
+
+ @Test void g04_testParseQuery_readerNull() {
+ var m = parseQuery((Reader)null);
+ assertTrue(m.isEmpty());
+ }
+
+ @Test void g05_testParseQuery_readerEmpty() throws Exception {
+ var m = parseQuery(new StringReader(""));
+ assertTrue(m.isEmpty());
+ }
+
+ @Test void g06_testParseQuery_readerWhitespaceOnly() throws Exception {
+ var m = parseQuery(new StringReader(" "));
+ assertTrue(m.isEmpty());
+ }
+
+ @Test void g07_testParseQuery_readerValid() throws Exception {
+ var m = parseQuery(new StringReader("f1=v1&f2=v2"));
+ assertEquals("v1", m.get("f1").get(0));
+ assertEquals("v2", m.get("f2").get(0));
+ }
+
//------------------------------------------------------------------------------------------------------------------
// Test URL-encoded strings parsed into plain-text values using
UrlEncodingParser.parseIntoSimpleMap().
//------------------------------------------------------------------------------------------------------------------
- @Test void g01_testParseIntoSimpleMap() {
+ @Test void g04_testParseIntoSimpleMap() {
var s =
"?f1=,()=&f2a=$b(true)&f2b=true&f3a=$n(123)&f3b=123&f4=$s(foo)";
var m = parseQuery(s);
assertEquals(",()=", m.get("f1").get(0));
@@ -128,4 +348,120 @@ class RestUtils_Test extends TestBase {
public static class B {
public String f1 = "f1";
}
+
+
//------------------------------------------------------------------------------------------------------------------
+ // toValidContextPath(String)
+
//------------------------------------------------------------------------------------------------------------------
+
+ @Test void m01_testToValidContextPath_null() {
+ assertEquals("", toValidContextPath(null));
+ }
+
+ @Test void m02_testToValidContextPath_empty() {
+ assertEquals("", toValidContextPath(""));
+ }
+
+ @Test void m03_testToValidContextPath_root() {
+ assertEquals("", toValidContextPath("/"));
+ }
+
+ @Test void m04_testToValidContextPath_noLeadingSlash() {
+ assertEquals("/api", toValidContextPath("api"));
+ }
+
+ @Test void m05_testToValidContextPath_withLeadingSlash() {
+ assertEquals("/api", toValidContextPath("/api"));
+ }
+
+ @Test void m06_testToValidContextPath_trailingSlash() {
+ assertEquals("/api", toValidContextPath("api/"));
+ }
+
+ @Test void m07_testToValidContextPath_bothSlashes() {
+ assertEquals("/api", toValidContextPath("/api/"));
+ }
+
+ @Test void m08_testToValidContextPath_multipleTrailingSlashes() {
+ assertEquals("/api", toValidContextPath("/api///"));
+ }
+
+ @Test void m09_testToValidContextPath_nestedPath() {
+ assertEquals("/api/users", toValidContextPath("/api/users"));
+ }
+
+ @Test void m10_testToValidContextPath_nestedPathTrailingSlash() {
+ assertEquals("/api/users", toValidContextPath("/api/users/"));
+ }
+
+ @Test void m11_testToValidContextPath_onlySlashes() {
+ assertEquals("", toValidContextPath("///"));
+ }
+
+
//------------------------------------------------------------------------------------------------------------------
+ // validatePathInfo(String)
+
//------------------------------------------------------------------------------------------------------------------
+
+ @Test void n01_testValidatePathInfo_null() {
+ assertNull(validatePathInfo(null));
+ }
+
+ @Test void n02_testValidatePathInfo_valid() {
+ assertEquals("/users", validatePathInfo("/users"));
+ }
+
+ @Test void n03_testValidatePathInfo_validNested() {
+ assertEquals("/users/123", validatePathInfo("/users/123"));
+ }
+
+ @Test void n04_testValidatePathInfo_empty() {
+ assertThrows(RuntimeException.class, () ->
validatePathInfo(""));
+ }
+
+ @Test void n05_testValidatePathInfo_noLeadingSlash() {
+ assertThrows(RuntimeException.class, () ->
validatePathInfo("users"));
+ }
+
+ @Test void n06_testValidatePathInfo_noLeadingSlashNested() {
+ assertThrows(RuntimeException.class, () ->
validatePathInfo("users/123"));
+ }
+
+
//------------------------------------------------------------------------------------------------------------------
+ // validateServletPath(String)
+
//------------------------------------------------------------------------------------------------------------------
+
+ @Test void o01_testValidateServletPath_empty() {
+ assertEquals("", validateServletPath(""));
+ }
+
+ @Test void o02_testValidateServletPath_valid() {
+ assertEquals("/api", validateServletPath("/api"));
+ }
+
+ @Test void o03_testValidateServletPath_validNested() {
+ assertEquals("/api/users", validateServletPath("/api/users"));
+ }
+
+ @Test void o04_testValidateServletPath_null() {
+ assertThrows(RuntimeException.class, () ->
validateServletPath(null));
+ }
+
+ @Test void o05_testValidateServletPath_root() {
+ assertThrows(RuntimeException.class, () ->
validateServletPath("/"));
+ }
+
+ @Test void o06_testValidateServletPath_noLeadingSlash() {
+ assertThrows(RuntimeException.class, () ->
validateServletPath("api"));
+ }
+
+ @Test void o07_testValidateServletPath_trailingSlash() {
+ assertThrows(RuntimeException.class, () ->
validateServletPath("/api/"));
+ }
+
+ @Test void o08_testValidateServletPath_noLeadingSlashTrailingSlash() {
+ assertThrows(RuntimeException.class, () ->
validateServletPath("api/"));
+ }
+
+ @Test void o09_testValidateServletPath_nestedTrailingSlash() {
+ assertThrows(RuntimeException.class, () ->
validateServletPath("/api/users/"));
+ }
}
\ No newline at end of file