Skip to content

Commit

Permalink
PR #327 - formatting fixes from review
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <[email protected]>
  • Loading branch information
lachlan-roberts committed Jan 2, 2025
1 parent b3de558 commit 39dc724
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public JettyRequestAPIData(

HttpFields.Mutable fields = HttpFields.build();
for (HttpField field : request.getHeaders()) {
// If it has a HttpHeader it is one of the standard headers so won't match any appengine specific header.
// If it has a HttpHeader it is one of the standard headers so won't match any appengine
// specific header.
if (field.getHeader() != null) {
fields.add(field);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ public JettyRequestAPIData(

HttpFields fields = new HttpFields();
for (HttpField field : request.getHttpFields()) {
// If it has a HttpHeader it is one of the standard headers so won't match any appengine specific header.
// If it has a HttpHeader it is one of the standard headers so won't match any appengine
// specific header.
if (field.getHeader() != null) {
fields.add(field);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public void after() throws Exception {

@Test
public void testWithHostHeader() throws Exception {
ContentResponse response = httpClient.newRequest(url)
ContentResponse response =
httpClient
.newRequest(url)
.header("Host", "foobar:1234")
.header("X-AppEngine-User-IP", "203.0.113.1")
.send();
Expand All @@ -101,21 +103,24 @@ public void testWithHostHeader() throws Exception {

@Test
public void testWithIPv6() throws Exception {
// Test the host header to be IPv6 with a port.
ContentResponse response = httpClient.newRequest(url)
.header("Host", "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:1234")
.header("X-AppEngine-User-IP", "203.0.113.1")
.send();
assertThat(response.getStatus(), equalTo(HttpStatus.OK_200));
String contentReceived = response.getContentAsString();
assertThat(contentReceived, containsString("getRemoteAddr: 203.0.113.1"));
assertThat(contentReceived, containsString("getRemoteHost: 203.0.113.1"));
assertThat(contentReceived, containsString("getRemotePort: 0"));
assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0"));
assertThat(contentReceived, containsString("getLocalName: 0.0.0.0"));
assertThat(contentReceived, containsString("getLocalPort: 0"));
assertThat(contentReceived, containsString("getServerName: [2001:db8:85a3:8d3:1319:8a2e:370:7348]"));
assertThat(contentReceived, containsString("getServerPort: 1234"));
// Test the host header to be IPv6 with a port.
ContentResponse response =
httpClient
.newRequest(url)
.header("Host", "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:1234")
.header("X-AppEngine-User-IP", "203.0.113.1")
.send();
assertThat(response.getStatus(), equalTo(HttpStatus.OK_200));
String contentReceived = response.getContentAsString();
assertThat(contentReceived, containsString("getRemoteAddr: 203.0.113.1"));
assertThat(contentReceived, containsString("getRemoteHost: 203.0.113.1"));
assertThat(contentReceived, containsString("getRemotePort: 0"));
assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0"));
assertThat(contentReceived, containsString("getLocalName: 0.0.0.0"));
assertThat(contentReceived, containsString("getLocalPort: 0"));
assertThat(
contentReceived, containsString("getServerName: [2001:db8:85a3:8d3:1319:8a2e:370:7348]"));
assertThat(contentReceived, containsString("getServerPort: 1234"));

// Test the user IP to be IPv6 with a port.
response =
Expand All @@ -127,13 +132,17 @@ public void testWithIPv6() throws Exception {
assertThat(response.getStatus(), equalTo(HttpStatus.OK_200));
contentReceived = response.getContentAsString();
if ("jetty94".equals(environment)) {
assertThat(contentReceived, containsString("getRemoteAddr: [2001:db8:85a3:8d3:1319:8a2e:370:7348]"));
assertThat(contentReceived, containsString("getRemoteHost: [2001:db8:85a3:8d3:1319:8a2e:370:7348]"));
assertThat(
contentReceived, containsString("getRemoteAddr: [2001:db8:85a3:8d3:1319:8a2e:370:7348]"));
assertThat(
contentReceived, containsString("getRemoteHost: [2001:db8:85a3:8d3:1319:8a2e:370:7348]"));
} else {
// The correct behaviour for getRemoteAddr and getRemoteHost is to not include []
// because they return raw IP/hostname and not URI-formatted addresses.
assertThat(contentReceived, containsString("getRemoteAddr: 2001:db8:85a3:8d3:1319:8a2e:370:7348"));
assertThat(contentReceived, containsString("getRemoteHost: 2001:db8:85a3:8d3:1319:8a2e:370:7348"));
assertThat(
contentReceived, containsString("getRemoteAddr: 2001:db8:85a3:8d3:1319:8a2e:370:7348"));
assertThat(
contentReceived, containsString("getRemoteHost: 2001:db8:85a3:8d3:1319:8a2e:370:7348"));
}
assertThat(contentReceived, containsString("getRemotePort: 0"));
assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0"));
Expand All @@ -145,9 +154,9 @@ public void testWithIPv6() throws Exception {

@Test
public void testWithoutHostHeader() throws Exception {
String url = runtime.jettyUrl("/");

ContentResponse response = httpClient.newRequest(url)
ContentResponse response =
httpClient
.newRequest(url)
.version(HttpVersion.HTTP_1_0)
.header("X-AppEngine-User-IP", "203.0.113.1")
.onRequestHeaders(request -> request.getHeaders().remove("Host"))
Expand Down Expand Up @@ -191,7 +200,6 @@ public void testForwardedHeadersIgnored() throws Exception {
assertThat(contentReceived, containsString("getServerPort: 1234"));
}


private RuntimeContext<?> runtimeContext() throws Exception {
RuntimeContext.Config<?> config =
RuntimeContext.Config.builder().setApplicationPath(temp.getRoot().toString()).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
writer.println("getLocalPort: " + req.getLocalPort());
writer.println("getServerName: " + req.getServerName());
writer.println("getServerPort: " + req.getServerPort());

}
}

0 comments on commit 39dc724

Please sign in to comment.