Skip to content

Commit

Permalink
HttpKeepAlive test + add http.keepAlive to externalProperties
Browse files Browse the repository at this point in the history
Signed-off-by: tvallin <thibault.vallin@oracle.com>
  • Loading branch information
tvallin committed Nov 12, 2020
1 parent c1263e0 commit ec06918
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public final class ExternalProperties {
*/
public static final String HTTP_NON_PROXY_HOSTS = "http.nonProxyHosts";

/**
* Property used to indicates if persistent connections should be supported.
*/
public static final String HTTP_KEEPALIVE = "http.keepAlive";

/**
* Prevent instantiation.
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/externalproperties/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dhttp.keepAlive=false</argLine>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.glassfish.jersey.tests.externalproperties;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.glassfish.jersey.ExternalProperties;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

public class HttpKeepAliveTest {

private final String SERVER_URI = "http://localhost:9997/";
private final String CONNECTION_HEADER = "Connection";
private final String RESPONSE_HEADER = "result";
private final String FALSE = "false";
private final int PORT = 9997;
private Server server;

@Test
public void testHttpKeepAlive() {
System.setProperty(ExternalProperties.HTTP_KEEPALIVE, FALSE);

Response response = ClientBuilder.newClient().target(SERVER_URI).request().get();

Object result = response.getHeaders().getFirst(RESPONSE_HEADER);

Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("close", result.toString());
}

@Before
public void startHandler() {
server = new Server(PORT);
server.setHandler(new CustomHandler());
try {
server.start();
} catch (Exception e) {
}
}

@After
public void stopHandler() {
try {
server.stop();
} catch (Exception e) {

}
}

class CustomHandler extends AbstractHandler {
@Override
public void handle(String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response) {

response.setStatus(200);
response.addHeader(RESPONSE_HEADER, request.getHeader(CONNECTION_HEADER));
baseRequest.setHandled(true);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.glassfish.jersey.ExternalProperties;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -38,6 +39,7 @@ public class HttpProxyTest extends JerseyTest {
private static final String PROXY_HOST = "localhost";
private static final String PROXY_PORT = "9997";
private static boolean proxyHit = false;
private Server server;

@Path("resource")
public static class ProxyTestResource {
Expand All @@ -58,7 +60,7 @@ protected Application configure() {
public void startFakeProxy() {
System.setProperty(ExternalProperties.HTTP_PROXY_HOST, PROXY_HOST);
System.setProperty(ExternalProperties.HTTP_PROXY_PORT, PROXY_PORT);
Server server = new Server(Integer.parseInt(PROXY_PORT));
server = new Server(Integer.parseInt(PROXY_PORT));
server.setHandler(new ProxyHandler(false));
try {
server.start();
Expand All @@ -67,6 +69,15 @@ public void startFakeProxy() {
}
}

@After
public void stopFakeProxy() {
try {
server.stop();
} catch (Exception e) {

}
}

@Test
public void testProxy() {
System.setProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS, "");
Expand Down

0 comments on commit ec06918

Please sign in to comment.