Issue #4334 - Enhancing JSON Test

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2019-11-20 15:47:53 -06:00
parent ba1fe2826d
commit 217602e97f
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
1 changed files with 24 additions and 1 deletions

View File

@ -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<String> 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));