diff --git a/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/SendErrorTest.java b/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/SendErrorTest.java new file mode 100644 index 000000000..4d20181b5 --- /dev/null +++ b/runtime/test/src/test/java/com/google/apphosting/runtime/jetty9/SendErrorTest.java @@ -0,0 +1,85 @@ +package com.google.apphosting.runtime.jetty9; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.Collection; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import org.eclipse.jetty.client.api.ContentResponse; +import org.eclipse.jetty.client.api.Result; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.eclipse.jetty.client.HttpClient; + + +@RunWith(Parameterized.class) +public class SendErrorTest extends JavaRuntimeViaHttpBase { + + @Parameterized.Parameters + public static Collection parameters() { + return Arrays.asList( + new Object[][] { + {"jetty94", false}, + {"jetty94", true}, + {"ee8", false}, + {"ee8", true}, + {"ee10", false}, + {"ee10", true}, + }); + } + + @Rule public TemporaryFolder temp = new TemporaryFolder(); + private final HttpClient httpClient = new HttpClient(); + private final boolean httpMode; + private final String environment; + private RuntimeContext runtime; + + + public SendErrorTest(String environment, boolean httpMode) { + this.environment = environment; + this.httpMode = httpMode; + System.setProperty("appengine.use.HttpConnector", Boolean.toString(httpMode)); + } + + @Before + public void start() throws Exception { + String app = "senderror" + environment; + copyAppToDir(app, temp.getRoot().toPath()); + runtime = runtimeContext(); + System.err.println("==== Using Environment: " + environment + " " + httpMode + " ===="); + } + + @After + public void after() throws Exception { + if (runtime != null) { + runtime.close(); + } + } + + @Test + public void testSendError() throws Exception { + String url = runtime.jettyUrl("/senderror"); + CompletableFuture completionListener = new CompletableFuture<>(); + httpClient.newRequest(url).send(completionListener::complete); + + Result result = completionListener.get(5, TimeUnit.SECONDS); + ContentResponse response = result.getRequest().send(); + assertEquals(500, response.getStatus()); + assertThat(response.getContentAsString(), containsString("Something went wrong.")); + } + + private RuntimeContext runtimeContext() throws Exception { + RuntimeContext.Config config = + RuntimeContext.Config.builder().setApplicationPath(temp.getRoot().toString()).build(); + return RuntimeContext.create(config); + } + +} \ No newline at end of file diff --git a/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/senderrorapp/404.html b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/senderrorapp/404.html new file mode 100644 index 000000000..3ecda5699 --- /dev/null +++ b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/senderrorapp/404.html @@ -0,0 +1,10 @@ + + + + + 404 + + +

You look lost.

+ + \ No newline at end of file diff --git a/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/senderrorapp/500.html b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/senderrorapp/500.html new file mode 100644 index 000000000..aa52d6585 --- /dev/null +++ b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/senderrorapp/500.html @@ -0,0 +1,10 @@ + + + + + 500 + + +

We encountered an error on our end. Sorry.

+ + \ No newline at end of file diff --git a/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/senderrorapp/SendErrorServlet.java b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/senderrorapp/SendErrorServlet.java new file mode 100644 index 000000000..e1244568c --- /dev/null +++ b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/senderrorapp/SendErrorServlet.java @@ -0,0 +1,35 @@ +package com.google.apphosting.runtime.jetty9.senderrorapp; + +import java.io.IOException; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +@WebServlet("/send-error") +public class SendErrorServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + int errorCode; + if (req.getParameter("errorCode") == null) { + errorCode = 0; + } else { + try { + errorCode = Integer.parseInt(req.getParameter("errorCode")); + } catch (NumberFormatException e) { + errorCode = -1; + } + } + switch (errorCode) { + case -1: + throw new RuntimeException("try to handle me"); + case 0: + req.getRequestDispatcher("/hello.html").forward(req, resp); + break; + default: + resp.sendError(errorCode); + break; + } + } +} \ No newline at end of file diff --git a/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/senderrorapp/hello.html b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/senderrorapp/hello.html new file mode 100644 index 000000000..9598275e7 --- /dev/null +++ b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/senderrorapp/hello.html @@ -0,0 +1,10 @@ + + + + + Hello + + +

Hello.

+ + \ No newline at end of file diff --git a/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/senderrorapp/unhandled-error.html b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/senderrorapp/unhandled-error.html new file mode 100644 index 000000000..e661da765 --- /dev/null +++ b/runtime/testapps/src/main/java/com/google/apphosting/runtime/jetty9/senderrorapp/unhandled-error.html @@ -0,0 +1,10 @@ + + + + + Unexpected Error + + +

This is embarrassing—we don't know what happened :(

+ + \ No newline at end of file diff --git a/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderroree10.WEB-INF/appengine-web.xml b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderroree10.WEB-INF/appengine-web.xml new file mode 100644 index 000000000..f68cf9716 --- /dev/null +++ b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderroree10.WEB-INF/appengine-web.xml @@ -0,0 +1,16 @@ + + + default + java21 + B1 + + 1 + 10m + + + + + + + + \ No newline at end of file diff --git a/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderroree10.WEB-INF/web.xml b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderroree10.WEB-INF/web.xml new file mode 100644 index 000000000..c0eed1f2d --- /dev/null +++ b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderroree10.WEB-INF/web.xml @@ -0,0 +1,32 @@ + + + + SendErrorServlet + com.google.apphosting.runtime.jetty9.senderrorapp + + + SendErrorServlet + /senderror + + + + 404 + /404.html + + + 500 + /500.html + + + java.lang.Throwable + /exception + + + + /unhandled-error.html + + \ No newline at end of file diff --git a/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderroree8.WEB-INF/appengine-web.xml b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderroree8.WEB-INF/appengine-web.xml new file mode 100644 index 000000000..f68cf9716 --- /dev/null +++ b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderroree8.WEB-INF/appengine-web.xml @@ -0,0 +1,16 @@ + + + default + java21 + B1 + + 1 + 10m + + + + + + + + \ No newline at end of file diff --git a/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderroree8.WEB-INF/web.xml b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderroree8.WEB-INF/web.xml new file mode 100644 index 000000000..c0eed1f2d --- /dev/null +++ b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderroree8.WEB-INF/web.xml @@ -0,0 +1,32 @@ + + + + SendErrorServlet + com.google.apphosting.runtime.jetty9.senderrorapp + + + SendErrorServlet + /senderror + + + + 404 + /404.html + + + 500 + /500.html + + + java.lang.Throwable + /exception + + + + /unhandled-error.html + + \ No newline at end of file diff --git a/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderrorjetty94.WEB-INF/appengine-web.xml b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderrorjetty94.WEB-INF/appengine-web.xml new file mode 100644 index 000000000..76fb066b4 --- /dev/null +++ b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderrorjetty94.WEB-INF/appengine-web.xml @@ -0,0 +1,16 @@ + + + default + java21 + B1 + + 1 + 10m + + + + + + + + \ No newline at end of file diff --git a/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderrorjetty94.WEB-INF/web.xml b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderrorjetty94.WEB-INF/web.xml new file mode 100644 index 000000000..c0eed1f2d --- /dev/null +++ b/runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/senderrorjetty94.WEB-INF/web.xml @@ -0,0 +1,32 @@ + + + + SendErrorServlet + com.google.apphosting.runtime.jetty9.senderrorapp + + + SendErrorServlet + /senderror + + + + 404 + /404.html + + + 500 + /500.html + + + java.lang.Throwable + /exception + + + + /unhandled-error.html + + \ No newline at end of file