Add Tomcat X-Forwarded-For header tests

Update Abstract & Tomcat EmbeddedServletContainerFactoryTests to check
that X-Forwarded-For headers work as expected.

See gh-4018
pull/4057/merge
Phillip Webb 9 years ago
parent 5cbb81c64f
commit 50430a20c6

@ -638,8 +638,9 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
return "http://localhost:" + port + resourcePath; return "http://localhost:" + port + resourcePath;
} }
protected String getResponse(String url) throws IOException, URISyntaxException { protected String getResponse(String url, String... headers) throws IOException,
ClientHttpResponse response = getClientResponse(url); URISyntaxException {
ClientHttpResponse response = getClientResponse(url, headers);
try { try {
return StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8")); return StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8"));
} }
@ -649,9 +650,9 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
} }
protected String getResponse(String url, protected String getResponse(String url,
HttpComponentsClientHttpRequestFactory requestFactory) throws IOException, HttpComponentsClientHttpRequestFactory requestFactory, String... headers)
URISyntaxException { throws IOException, URISyntaxException {
ClientHttpResponse response = getClientResponse(url, requestFactory); ClientHttpResponse response = getClientResponse(url, requestFactory, headers);
try { try {
return StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8")); return StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8"));
} }
@ -660,8 +661,8 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
} }
} }
protected ClientHttpResponse getClientResponse(String url) throws IOException, protected ClientHttpResponse getClientResponse(String url, String... headers)
URISyntaxException { throws IOException, URISyntaxException {
return getClientResponse(url, new HttpComponentsClientHttpRequestFactory() { return getClientResponse(url, new HttpComponentsClientHttpRequestFactory() {
@Override @Override
@ -669,19 +670,32 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
return AbstractEmbeddedServletContainerFactoryTests.this.httpClientContext; return AbstractEmbeddedServletContainerFactoryTests.this.httpClientContext;
} }
}); }, headers);
} }
protected ClientHttpResponse getClientResponse(String url, protected ClientHttpResponse getClientResponse(String url,
HttpComponentsClientHttpRequestFactory requestFactory) throws IOException, HttpComponentsClientHttpRequestFactory requestFactory, String... headers)
URISyntaxException { throws IOException, URISyntaxException {
ClientHttpRequest request = requestFactory.createRequest(new URI(url), ClientHttpRequest request = requestFactory.createRequest(new URI(url),
HttpMethod.GET); HttpMethod.GET);
request.getHeaders().add("Cookie", "JSESSIONID=" + "123"); request.getHeaders().add("Cookie", "JSESSIONID=" + "123");
for (String header : headers) {
String[] parts = header.split(":");
request.getHeaders().add(parts[0], parts[1]);
}
ClientHttpResponse response = request.execute(); ClientHttpResponse response = request.execute();
return response; return response;
} }
protected void assertForwardHeaderIsUsed(EmbeddedServletContainerFactory factory)
throws IOException, URISyntaxException {
this.container = factory.getEmbeddedServletContainer(new ServletRegistrationBean(
new ExampleServlet(true), "/hello"));
this.container.start();
assertThat(getResponse(getLocalUrl("/hello"), "X-Forwarded-For:140.211.11.130"),
containsString("remoteaddr=140.211.11.130"));
}
protected abstract AbstractEmbeddedServletContainerFactory getFactory(); protected abstract AbstractEmbeddedServletContainerFactory getFactory();
protected abstract Object getJspServlet(); protected abstract Object getJspServlet();

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2013 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -47,6 +47,7 @@ public class ExampleServlet extends GenericServlet {
String content = "Hello World"; String content = "Hello World";
if (this.echoRequestInfo) { if (this.echoRequestInfo) {
content += " scheme=" + request.getScheme(); content += " scheme=" + request.getScheme();
content += " remoteaddr=" + request.getRemoteAddr();
} }
response.getWriter().write(content); response.getWriter().write(content);
} }

@ -35,6 +35,7 @@ import org.apache.catalina.Valve;
import org.apache.catalina.Wrapper; import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.Connector; import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.valves.RemoteIpValve;
import org.apache.coyote.http11.AbstractHttp11JsseProtocol; import org.apache.coyote.http11.AbstractHttp11JsseProtocol;
import org.junit.Test; import org.junit.Test;
import org.mockito.InOrder; import org.mockito.InOrder;
@ -333,6 +334,13 @@ public class TomcatEmbeddedServletContainerFactoryTests extends
assertThat(jspServlet.findInitParameter("a"), is(equalTo("alpha"))); assertThat(jspServlet.findInitParameter("a"), is(equalTo("alpha")));
} }
@Test
public void useForwardHeaders() throws Exception {
TomcatEmbeddedServletContainerFactory factory = getFactory();
factory.addContextValves(new RemoteIpValve());
assertForwardHeaderIsUsed(factory);
}
@Override @Override
protected Wrapper getJspServlet() { protected Wrapper getJspServlet() {
Container context = ((TomcatEmbeddedServletContainer) this.container).getTomcat() Container context = ((TomcatEmbeddedServletContainer) this.container).getTomcat()

Loading…
Cancel
Save