Skip to content

Commit

Permalink
Merge pull request #324 from GoogleCloudPlatform:error-page-test
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 707557613
Change-Id: I9e7f4ec95af64183890458da5fb2000bbf2295d1
  • Loading branch information
gae-java-bot committed Dec 18, 2024
2 parents 2a33143 + 61fedeb commit 3ad247f
Show file tree
Hide file tree
Showing 21 changed files with 610 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2021 Google LLC
*
* Licensed 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
*
* https://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.
*/

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 org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http.HttpStatus;
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 = "com/google/apphosting/runtime/jetty9/senderrorapp/" + environment;
copyAppToDir(app, temp.getRoot().toPath());
httpClient.start();
runtime = runtimeContext();
System.err.println("==== Using Environment: " + environment + " " + httpMode + " ====");
}

@After
public void after() throws Exception {
httpClient.stop();
runtime.close();
}

@Test
public void testSendError() throws Exception {
String url = runtime.jettyUrl("/send-error");
ContentResponse response = httpClient.GET(url);
assertEquals(HttpStatus.OK_200, response.getStatus());
assertThat(response.getContentAsString(), containsString("<h1>Hello, welcome to App Engine Java Standard!</h1>"));

url = runtime.jettyUrl("/send-error?errorCode=404");
response = httpClient.GET(url);
assertEquals(HttpStatus.NOT_FOUND_404, response.getStatus());
assertThat(response.getContentAsString(), containsString("<h1>404 - Page Not Found (App Engine Java Standard)</h1>"));

url = runtime.jettyUrl("/send-error?errorCode=500");
response = httpClient.GET(url);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
assertThat(response.getContentAsString(), containsString("<h1>500 - Internal Server Error (App Engine Java Standard)</h1>"));

url = runtime.jettyUrl("/send-error?errorCode=503");
response = httpClient.GET(url);
assertEquals(HttpStatus.SERVICE_UNAVAILABLE_503, response.getStatus());
assertThat(response.getContentAsString(), containsString("<h1>Unhandled Error - Service Temporarily Unavailable (App Engine Java Standard)</h1>"));

}

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,50 @@
/*
* Copyright 2021 Google LLC
*
* Licensed 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
*
* https://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.
*/

package com.google.apphosting.runtime.jetty9.senderrorapp;

import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class SendErrorServletEE10 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,49 @@
/*
* Copyright 2021 Google LLC
*
* Licensed 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
*
* https://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.
*/

package com.google.apphosting.runtime.jetty9.senderrorapp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SendErrorServletEE8 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,16 @@
<!--
Copyright 2021 Google LLC
Licensed 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
https://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.
-->
<h1>404 - Page Not Found (App Engine Java Standard)</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--
Copyright 2021 Google LLC
Licensed 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
https://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.
-->
<h1>500 - Internal Server Error (App Engine Java Standard)</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2021 Google LLC
Licensed 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
https://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.
-->

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<runtime>java21</runtime>
<application>senderror</application>
<version>1</version>
<system-properties>
<property name="appengine.use.EE10" value="true"/>
</system-properties>
</appengine-web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2021 Google LLC
Licensed 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
https://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.
-->

<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>Main</servlet-name>
<servlet-class>com.google.apphosting.runtime.jetty9.senderrorapp.SendErrorServletEE10</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Main</servlet-name>
<url-pattern>/send-error</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>
<!-- 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 @@
<!--
Copyright 2021 Google LLC
Licensed 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
https://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.
-->
<h1>Hello, welcome to App Engine Java Standard!</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--
Copyright 2021 Google LLC
Licensed 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
https://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.
-->
<h1>Unhandled Error - Service Temporarily Unavailable (App Engine Java Standard)</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--
Copyright 2021 Google LLC
Licensed 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
https://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.
-->
<h1>404 - Page Not Found (App Engine Java Standard)</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--
Copyright 2021 Google LLC
Licensed 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
https://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.
-->
<h1>500 - Internal Server Error (App Engine Java Standard)</h1>
Loading

0 comments on commit 3ad247f

Please sign in to comment.