Skip to content

Commit

Permalink
added send error test
Browse files Browse the repository at this point in the history
  • Loading branch information
srinjoyray committed Dec 16, 2024
1 parent a066473 commit b11dad5
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Object[]> 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<Result> 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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>404</title>
</head>
<body>
<p>You look lost.</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>500</title>
</head>
<body>
<p>We encountered an error on our end. Sorry.</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<p>Hello.</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Unexpected Error</title>
</head>
<body>
<p>This is embarrassing&mdash;we don't know what happened :(</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<module>default</module>
<runtime>java21</runtime>
<instance-class>B1</instance-class>
<basic-scaling>
<max-instances>1</max-instances>
<idle-timeout>10m</idle-timeout>
</basic-scaling>
<static-files>
<include path="/404.html"/>
<include path="/500.html"/>
<include path="/unhandled-error.html"/>
<include path="/hello.html"/>
</static-files>
</appengine-web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>SendErrorServlet</servlet-name>
<servlet-class>com.google.apphosting.runtime.jetty9.senderrorapp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SendErrorServlet</servlet-name>
<url-pattern>/senderror</url-pattern>
</servlet-mapping>

<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.html</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/exception</location>
</error-page>
<error-page>
<!-- No error-code or exception-type, i.e. this will match any other HTTP status than defined above -->
<location>/unhandled-error.html</location>
</error-page>
</web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<module>default</module>
<runtime>java21</runtime>
<instance-class>B1</instance-class>
<basic-scaling>
<max-instances>1</max-instances>
<idle-timeout>10m</idle-timeout>
</basic-scaling>
<static-files>
<include path="/404.html"/>
<include path="/500.html"/>
<include path="/unhandled-error.html"/>
<include path="/hello.html"/>
</static-files>
</appengine-web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>SendErrorServlet</servlet-name>
<servlet-class>com.google.apphosting.runtime.jetty9.senderrorapp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SendErrorServlet</servlet-name>
<url-pattern>/senderror</url-pattern>
</servlet-mapping>

<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.html</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/exception</location>
</error-page>
<error-page>
<!-- No error-code or exception-type, i.e. this will match any other HTTP status than defined above -->
<location>/unhandled-error.html</location>
</error-page>
</web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<module>default</module>
<runtime>java21</runtime>
<instance-class>B1</instance-class>
<basic-scaling>
<max-instances>1</max-instances>
<idle-timeout>10m</idle-timeout>
</basic-scaling>
<static-files>
<include path="/404.html"/>
<include path="/500.html"/>
<include path="/unhandled-error.html"/>
<include path="/hello.html"/>
</static-files>
</appengine-web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>SendErrorServlet</servlet-name>
<servlet-class>com.google.apphosting.runtime.jetty9.senderrorapp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SendErrorServlet</servlet-name>
<url-pattern>/senderror</url-pattern>
</servlet-mapping>

<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.html</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/exception</location>
</error-page>
<error-page>
<!-- No error-code or exception-type, i.e. this will match any other HTTP status than defined above -->
<location>/unhandled-error.html</location>
</error-page>
</web-app>

0 comments on commit b11dad5

Please sign in to comment.