Skip to content

Commit

Permalink
Adding error-page to location that is a static html examples
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Feb 12, 2024
1 parent ab92f98 commit 8ff124a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions webapps/error-handling-4/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@
<error-code>404</error-code>
<location>/errors/400</location>
</error-page>

<error-page>
<error-code>403</error-code>
<location>/forbidden.html</location>
</error-page>
</web-app>
12 changes: 12 additions & 0 deletions webapps/error-handling-4/src/main/webapp/forbidden.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<head>
<title>Error Handling WebApp - FORBIDDEN</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<h1>Error Handling WebApp - FORBIDDEN</h1>
<p>
This endpoint is FORBIDDEN
</p>
</body>
</html>
8 changes: 8 additions & 0 deletions webapps/error-handling-4/src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ <h1>Error Handling WebApp</h1>
<td><a href="hello"><code>hello</code></a></td>
<td>Servlet replies with Hello World</td>
</tr>
<tr>
<td><a href="triggers/403"><code>triggers/403</code></a></td>
<td>Servlet calls <code>response.sendError(403)</code> - should see <code>forbidden.html</code> contents</td>
</tr>
<tr>
<td><a href="triggers/404"><code>triggers/404</code></a></td>
<td>Servlet calls <code>response.sendError(404)</code></td>
Expand All @@ -36,6 +40,10 @@ <h1>Error Handling WebApp</h1>
<td><a href="triggers/Example%20Message%20from%20sendError"><code>triggers/Example%20Message%20from%20sendError</code></a></td>
<td>Servlet calls a <code>response.sendError(500, message)</code></td>
</tr>
<tr>
<td><a href="hello?trigger=403"><code>hello?trigger=403</code></a></td>
<td>Filter calls <code>response.sendError(403)</code> - should see <code>forbidden.html</code> contents</td>
</tr>
<tr>
<td><a href="hello?trigger=404"><code>hello?trigger=404</code></a></td>
<td>Filter calls <code>response.sendError(404)</code></td>
Expand Down
34 changes: 34 additions & 0 deletions webapps/error-handling-4/src/test/java/examples/ErrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.ee8.webapp.WebAppContext;
import org.eclipse.jetty.server.Server;
Expand Down Expand Up @@ -106,6 +107,24 @@ public void testTriggers404InContext() throws IOException, InterruptedException
assertThat(body, containsString("ERROR_EXCEPTION: null"));
}

/**
* Demo of an error-page setup to respond to 403 (Forbidden) sendError with a static HTML page.
*/
@Test
public void testTriggers403InContext() throws IOException, InterruptedException
{
HttpClient httpClient = HttpClient.newBuilder().build();
HttpRequest httpRequest = HttpRequest.newBuilder()
.uri(server.getURI().resolve("/app/triggers/" + HttpServletResponse.SC_FORBIDDEN))
.GET().build();
HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));

assertThat(httpResponse.statusCode(), is(403));
String body = httpResponse.body();
assertThat(body, containsString("<title>Error Handling WebApp - FORBIDDEN</title>"));
assertThat(body, containsString("<h1>Error Handling WebApp - FORBIDDEN</h1>"));
}

@Test
public void testTriggers500InContext() throws IOException, InterruptedException
{
Expand Down Expand Up @@ -233,6 +252,21 @@ public void testTriggersExceptionInFilterUsingHeader() throws IOException, Inter
assertThat(body, containsString("at examples.TriggersFilter.doFilter("));
}

@Test
public void testTriggers403InFilterUsingQuery() throws IOException, InterruptedException
{
HttpClient httpClient = HttpClient.newBuilder().build();
HttpRequest httpRequest = HttpRequest.newBuilder()
.uri(server.getURI().resolve("/app/hello?trigger=403"))
.GET().build();
HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));

assertThat(httpResponse.statusCode(), is(403));
String body = httpResponse.body();
assertThat(body, containsString("<title>Error Handling WebApp - FORBIDDEN</title>"));
assertThat(body, containsString("<h1>Error Handling WebApp - FORBIDDEN</h1>"));
}

@Test
public void testTriggers404InFilterUsingQuery() throws IOException, InterruptedException
{
Expand Down

0 comments on commit 8ff124a

Please sign in to comment.