SLING-7836 unit test to confirm that the error handling scripts consider
the request file extension
diff --git a/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolver/errorhandler/ErrorHandlingTest.java b/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolver/errorhandler/ErrorHandlingTest.java
index 1a26cd7..ffd63a2 100644
--- a/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolver/errorhandler/ErrorHandlingTest.java
+++ b/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/resolver/errorhandler/ErrorHandlingTest.java
@@ -16,14 +16,19 @@
*/
package org.apache.sling.launchpad.webapp.integrationtest.servlets.resolver.errorhandler;
+import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import javax.json.Json;
+import javax.json.JsonObject;
+import javax.json.JsonReader;
+
import org.apache.commons.httpclient.NameValuePair;
-import org.apache.sling.servlets.post.SlingPostConstants;
import org.apache.sling.launchpad.webapp.integrationtest.RenderingTestBase;
+import org.apache.sling.servlets.post.SlingPostConstants;
/** Test the sling error handling mechanism http://sling.apache.org/site/errorhandling.html*/
public class ErrorHandlingTest extends RenderingTestBase {
@@ -58,6 +63,7 @@
testClient.mkdirs(HTTP_BASE_URL, ERROR_HANDLER_PATH);
testClient.mkdirs(HTTP_BASE_URL, TEST_ROOT+"/"+THROW_ERROR_PATH);
uploadTestScript("servlets/errorhandler/404.jsp", "sling/servlet/errorhandler/404.jsp");
+ uploadTestScript("servlets/errorhandler/json.404.jsp", "sling/servlet/errorhandler/json.404.jsp");
uploadTestScript("servlets/errorhandler/Throwable.jsp", "sling/servlet/errorhandler/Throwable.jsp");
uploadTestScript("servlets/errorhandler/500.jsp", "sling/servlet/errorhandler/500.jsp");
uploadTestScript("servlets/errorhandler/401.jsp", "sling/servlet/errorhandler/401.jsp");
@@ -171,4 +177,23 @@
assertWithRetries(url, 500, expected, HTTP_METHOD_POST, params);
}
-}
+ /**
+ * SLING-7836 - Test an error handling script registered for a json extension
+ */
+ public void test_404_errorhandling_JSON_extension() throws Throwable {
+ final String url = testNodePath + NOT_EXISTING_NODE_PATH + ".json";
+ List <NameValuePair> params=new ArrayList<NameValuePair>();
+ String json = getContent(url, CONTENT_TYPE_JSON, params, 404, HTTP_METHOD_GET);
+
+ // assert the error content is right.
+ JsonObject jsonObj = null;
+ try (JsonReader reader = Json.createReader(new StringReader(json))) {
+ jsonObj = reader.readObject();
+ }
+ assertNotNull(jsonObj);
+ assertEquals(404, jsonObj.getInt("status"));
+ assertEquals("Resource at '/apps/testNode/notExisting.json' not found: No resource found", jsonObj.getString("message"));
+ assertEquals("/apps/testNode/notExisting.json", jsonObj.getString("request_uri"));
+ }
+
+}
\ No newline at end of file
diff --git a/src/main/resources/integration-test/servlets/errorhandler/json.404.jsp b/src/main/resources/integration-test/servlets/errorhandler/json.404.jsp
new file mode 100644
index 0000000..b15ae57
--- /dev/null
+++ b/src/main/resources/integration-test/servlets/errorhandler/json.404.jsp
@@ -0,0 +1,52 @@
+<%@page import="javax.json.JsonWriter"%>
+<%@page import="javax.json.JsonObject"%>
+<%@page import="javax.json.JsonObjectBuilder"%>
+<%@page import="javax.json.JsonBuilderFactory"%>
+<%@page import="javax.json.Json"%>
+<%@page import="java.util.Collections"%>
+<%@page import="org.apache.sling.api.SlingConstants"%>
+<%
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+int status = (Integer)request.getAttribute(SlingConstants.ERROR_STATUS);
+response.setStatus(status);
+response.setCharacterEncoding("UTF-8");
+response.setContentType("application/json");
+
+JsonBuilderFactory factory = Json.createBuilderFactory(Collections.emptyMap());
+JsonObjectBuilder jsonObjBuilder = factory.createObjectBuilder();
+
+jsonObjBuilder.add("status", status);
+
+String msg = (String)request.getAttribute(SlingConstants.ERROR_MESSAGE);
+if (msg != null && !msg.isEmpty()) {
+ jsonObjBuilder.add("message", msg);
+}
+
+String requestUri = (String)request.getAttribute(SlingConstants.ERROR_REQUEST_URI);
+if (requestUri != null && !requestUri.isEmpty()) {
+ jsonObjBuilder.add("request_uri", requestUri);
+}
+
+JsonObject jsonObj = jsonObjBuilder.build();
+JsonWriter jsonWriter = Json.createWriter(response.getWriter());
+jsonWriter.writeObject(jsonObj);
+jsonWriter.close();
+%>
\ No newline at end of file