From 217602e97f2b9de41e042620148f910a82626cd0 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 20 Nov 2019 15:47:53 -0600 Subject: [PATCH] Issue #4334 - Enhancing JSON Test Signed-off-by: Joakim Erdfelt --- .../jetty/server/ErrorHandlerTest.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/ErrorHandlerTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/ErrorHandlerTest.java index 87184c19e32..8fc405a163d 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/ErrorHandlerTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/ErrorHandlerTest.java @@ -19,7 +19,9 @@ package org.eclipse.jetty.server; import java.io.IOException; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import javax.servlet.DispatcherType; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; @@ -42,10 +44,12 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertTrue; public class ErrorHandlerTest { @@ -91,7 +95,7 @@ public class ErrorHandlerTest // produce an exception with an JSON formatted cause message if (target.startsWith("/jsonmessage/")) { - String message = "{\n \"glossary\": {\n \"title\": \"example\"\n }\n }"; + String message = "\"}, \"glossary\": {\n \"title\": \"example\"\n }\n {\""; throw new ServletException(new RuntimeException(message)); } @@ -473,6 +477,25 @@ public class ErrorHandlerTest { Map jo = (Map)JSON.parse(response.getContent()); + Set acceptableKeyNames = new HashSet<>(); + acceptableKeyNames.add("url"); + acceptableKeyNames.add("status"); + acceptableKeyNames.add("message"); + acceptableKeyNames.add("servlet"); + acceptableKeyNames.add("cause0"); + acceptableKeyNames.add("cause1"); + acceptableKeyNames.add("cause2"); + + for (Object key : jo.keySet()) + { + String keyStr = (String)key; + assertTrue(acceptableKeyNames.contains(keyStr), "Unexpected Key [" + keyStr + "]"); + + Object value = jo.get(key); + assertThat("Unexpected value type (" + value.getClass().getName() + ")", + value, instanceOf(String.class)); + } + assertThat("url field", jo.get("url"), is(notNullValue())); String expectedStatus = String.valueOf(response.getStatus()); assertThat("status field", jo.get("status"), is(expectedStatus));