This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-hapi-client.git
commit 72aff57fff72e9d50916c4e55024cc9bd3de0241 Author: Bertrand Delacretaz <[email protected]> AuthorDate: Tue Oct 13 11:50:44 2015 +0000 SLING-5136 - use a JUnit Rule to setup the HTTP server git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1708357 13f79535-47bb-0310-9956-ffa450edef68 --- src/test/java/FormTest.java | 113 ++++++++++----------- src/test/java/GetPostTest.java | 109 +++++++++----------- src/test/java/ItemsTest.java | 100 ++++++++---------- .../util/{TestBase.java => HttpServerRule.java} | 75 ++++++++------ 4 files changed, 190 insertions(+), 207 deletions(-) diff --git a/src/test/java/FormTest.java b/src/test/java/FormTest.java index c179b9b..e13c20f 100644 --- a/src/test/java/FormTest.java +++ b/src/test/java/FormTest.java @@ -17,9 +17,19 @@ * under the License. ******************************************************************************/ +import static org.hamcrest.core.IsEqual.equalTo; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; + import org.apache.commons.io.IOUtils; -import org.apache.http.*; -import org.apache.http.client.utils.URIUtils; +import org.apache.http.HttpEntity; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HttpContext; @@ -29,67 +39,49 @@ import org.apache.sling.hapi.client.Document; import org.apache.sling.hapi.client.Items; import org.apache.sling.hapi.client.microdata.MicrodataHtmlClient; import org.junit.Assert; -import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; -import util.TestBase; - -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.List; - -import static org.hamcrest.core.IsEqual.equalTo; - -public class FormTest extends TestBase { - public static final String GET_URL = "/test1"; - public static final String POST_URL = "/testpost1"; - public static final String OK_RESPONSE = "TEST_OK"; - public static final String FAIL_RESPONSE = "TEST_FAIL"; - - public static String html; - - private static HttpHost host; - private static URI uri; - - @BeforeClass - public static void setUp() throws Exception { - FormTest.html = IOUtils.toString(ItemsTest.class.getResourceAsStream("items_forms.html"), "UTF-8"); - setupServer(); - } - - public static void setupServer() throws Exception { - TestBase.setUp(); - serverBootstrap.registerHandler(GET_URL, new HttpRequestHandler() { - @Override - public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) - throws HttpException, IOException { - HttpEntity entity = new StringEntity(html, "UTF-8"); - httpResponse.setEntity(entity); - } - }).registerHandler(POST_URL, new HttpRequestHandler() { - @Override - public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) - throws HttpException, IOException { - if (!httpRequest.getRequestLine().getMethod().equals("POST")) { - httpResponse.setEntity(new StringEntity(FAIL_RESPONSE)); - } else { - httpResponse.setEntity(new StringEntity(OK_RESPONSE)); +import util.HttpServerRule; + +public class FormTest { + private static final String GET_URL = "/test1"; + private static final String POST_URL = "/testpost1"; + private static final String OK_RESPONSE = "TEST_OK"; + private static final String FAIL_RESPONSE = "TEST_FAIL"; + + @ClassRule + public static final HttpServerRule httpServer = new HttpServerRule() { + + @Override + protected void registerHandlers() { + serverBootstrap.registerHandler(GET_URL, new HttpRequestHandler() { + @Override + public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) + throws HttpException, IOException { + final String html = IOUtils.toString(ItemsTest.class.getResourceAsStream("items_forms.html"), "UTF-8"); + HttpEntity entity = new StringEntity(html, "UTF-8"); + httpResponse.setEntity(entity); } - httpResponse.setStatusCode(302); - httpResponse.setHeader("Location", GET_URL); - } - }); - - // start server - host = TestBase.start(); - uri = URIUtils.rewriteURI(new URI("/"), host); - } - + }).registerHandler(POST_URL, new HttpRequestHandler() { + @Override + public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) + throws HttpException, IOException { + if (!httpRequest.getRequestLine().getMethod().equals("POST")) { + httpResponse.setEntity(new StringEntity(FAIL_RESPONSE)); + } else { + httpResponse.setEntity(new StringEntity(OK_RESPONSE)); + } + httpResponse.setStatusCode(302); + httpResponse.setHeader("Location", GET_URL); + } + }); + } + }; + @Test public void testForm() throws ClientException, URISyntaxException { - MicrodataHtmlClient client = new MicrodataHtmlClient(uri.toString()); + MicrodataHtmlClient client = new MicrodataHtmlClient(httpServer.getURI().toString()); Document doc = client.enter(GET_URL); Items items = doc.items(); Assert.assertThat(items.length(), equalTo(1)); @@ -106,8 +98,5 @@ public class FormTest extends TestBase { // the multipart enctype Document doc3 = form.at(1).submit(data); Assert.assertThat(doc3.items().length(), equalTo(1)); - - - } -} +} \ No newline at end of file diff --git a/src/test/java/GetPostTest.java b/src/test/java/GetPostTest.java index 23589b6..5b13112 100644 --- a/src/test/java/GetPostTest.java +++ b/src/test/java/GetPostTest.java @@ -18,8 +18,16 @@ * under the License. ******************************************************************************/ -import org.apache.http.*; -import org.apache.http.client.utils.URIUtils; +import static org.hamcrest.core.StringContains.containsString; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URISyntaxException; + +import org.apache.http.Header; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; import org.apache.http.entity.StringEntity; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpRequestHandler; @@ -28,88 +36,71 @@ import org.apache.sling.hapi.client.Document; import org.apache.sling.hapi.client.microdata.MicrodataHtmlClient; import org.hamcrest.core.StringContains; import org.junit.Assert; -import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; -import util.TestBase; - -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.net.URISyntaxException; -import static org.hamcrest.core.StringContains.containsString; +import util.HttpServerRule; -public class GetPostTest extends TestBase { - public static final String GET_URL = "/test"; - public static final String GET_AUTH_URL = "/testauth"; - public static final String OK_RESPONSE = "TEST_OK"; - public static final String FAIL_RESPONSE = "TEST_FAIL"; - public static final String USER = "admin"; +public class GetPostTest { + private static final String GET_URL = "/test"; + private static final String GET_AUTH_URL = "/testauth"; + private static final String OK_RESPONSE = "TEST_OK"; + private static final String USER = "admin"; private static final String PASSWORD = "admin"; private static final String AUTH_STRING = "Basic YWRtaW46YWRtaW4="; private static final String REDIRECT_URL = "/test_redirect"; + @ClassRule + public static final HttpServerRule httpServer = new HttpServerRule() { - private static HttpHost host; - private static URI uri; - - @BeforeClass - public static void setUp() throws Exception { - setupServer(); - } - - public static void setupServer() throws Exception { - TestBase.setUp(); - serverBootstrap.registerHandler(GET_URL, new HttpRequestHandler() { - @Override - public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) - throws HttpException, IOException { - httpResponse.setEntity(new StringEntity(OK_RESPONSE)); - } - }).registerHandler(GET_AUTH_URL, new HttpRequestHandler() { - @Override - public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) - throws HttpException, IOException { - Header[] headers = httpRequest.getHeaders("Authorization"); - if (null == headers || headers.length == 0 || !headers[0].getValue().equals(AUTH_STRING)) { - httpResponse.setStatusCode(401); - httpResponse.setHeader("WWW-Authenticate", "Basic realm=\"TEST\""); - } else { + @Override + protected void registerHandlers() { + serverBootstrap.registerHandler(GET_URL, new HttpRequestHandler() { + @Override + public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) + throws HttpException, IOException { httpResponse.setEntity(new StringEntity(OK_RESPONSE)); } - } - }).registerHandler(REDIRECT_URL, new HttpRequestHandler() { - @Override - public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { - response.setStatusCode(307); - response.setHeader("Location", GET_URL); - } - }); - - // start server - host = TestBase.start(); - uri = URIUtils.rewriteURI(new URI("/"), host); - } - + }).registerHandler(GET_AUTH_URL, new HttpRequestHandler() { + @Override + public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) + throws HttpException, IOException { + Header[] headers = httpRequest.getHeaders("Authorization"); + if (null == headers || headers.length == 0 || !headers[0].getValue().equals(AUTH_STRING)) { + httpResponse.setStatusCode(401); + httpResponse.setHeader("WWW-Authenticate", "Basic realm=\"TEST\""); + } else { + httpResponse.setEntity(new StringEntity(OK_RESPONSE)); + } + } + }).registerHandler(REDIRECT_URL, new HttpRequestHandler() { + @Override + public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { + response.setStatusCode(307); + response.setHeader("Location", GET_URL); + } + }); + } + }; + @Test public void testValidGet() throws ClientException, URISyntaxException { - MicrodataHtmlClient client = new MicrodataHtmlClient(uri.toString()); + MicrodataHtmlClient client = new MicrodataHtmlClient(httpServer.getURI().toString()); Document doc = client.get(GET_URL); Assert.assertThat("GET request failed", doc.toString(), new StringContains(OK_RESPONSE)); } @Test public void testValidAuthGet() throws ClientException, URISyntaxException { - MicrodataHtmlClient client = new MicrodataHtmlClient(uri.toString(), USER, PASSWORD); + MicrodataHtmlClient client = new MicrodataHtmlClient(httpServer.getURI().toString(), USER, PASSWORD); Document doc = client.get(GET_AUTH_URL); Assert.assertThat("GET request failed with basic auth", doc.toString(), containsString(OK_RESPONSE)); } @Test public void testRedirect() throws ClientException, URISyntaxException, UnsupportedEncodingException { - MicrodataHtmlClient client = new MicrodataHtmlClient(uri.toString(), USER, PASSWORD); + MicrodataHtmlClient client = new MicrodataHtmlClient(httpServer.getURI().toString()); Document doc = client.post(REDIRECT_URL, new StringEntity("test")); Assert.assertThat("POST request failed to redirect", doc.toString(), containsString(OK_RESPONSE)); } - } diff --git a/src/test/java/ItemsTest.java b/src/test/java/ItemsTest.java index 7c09f45..8ab5453 100644 --- a/src/test/java/ItemsTest.java +++ b/src/test/java/ItemsTest.java @@ -17,9 +17,16 @@ * under the License. ******************************************************************************/ +import static org.hamcrest.core.IsEqual.equalTo; + +import java.io.IOException; +import java.net.URISyntaxException; + import org.apache.commons.io.IOUtils; -import org.apache.http.*; -import org.apache.http.client.utils.URIUtils; +import org.apache.http.HttpEntity; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; import org.apache.http.entity.StringEntity; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpRequestHandler; @@ -28,62 +35,42 @@ import org.apache.sling.hapi.client.Document; import org.apache.sling.hapi.client.Items; import org.apache.sling.hapi.client.microdata.MicrodataHtmlClient; import org.junit.Assert; -import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; -import util.TestBase; - -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; - -import static org.hamcrest.core.IsEqual.equalTo; - - -public class ItemsTest extends TestBase { - public static final String GET_URL = "/test"; - public static final String GET_LINKS_URL = "/testlinks"; - public static final String OK_RESPONSE = "TEST_OK"; - public static final String FAIL_RESPONSE = "TEST_FAIL"; - - public static String html; - public static String htmlLinks; - - private static HttpHost host; - private static URI uri; - - @BeforeClass - public static void setUp() throws Exception { - ItemsTest.html = IOUtils.toString(ItemsTest.class.getResourceAsStream("items.html"), "UTF-8"); - ItemsTest.htmlLinks = IOUtils.toString(ItemsTest.class.getResourceAsStream("items_links.html"), "UTF-8"); - setupServer(); - } - - public static void setupServer() throws Exception { - TestBase.setUp(); - serverBootstrap.registerHandler(GET_URL, new HttpRequestHandler() { - @Override - public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) - throws HttpException, IOException { - HttpEntity entity = new StringEntity(html, "UTF-8"); - httpResponse.setEntity(entity); - } - }).registerHandler(GET_LINKS_URL, new HttpRequestHandler() { - @Override - public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) - throws HttpException, IOException { - HttpEntity entity = new StringEntity(htmlLinks, "UTF-8"); - httpResponse.setEntity(entity); - } - }); - - // start server - host = TestBase.start(); - uri = URIUtils.rewriteURI(new URI("/"), host); - } +import util.HttpServerRule; + +public class ItemsTest { + private static final String GET_URL = "/test"; + private static final String GET_LINKS_URL = "/testlinks"; + + @ClassRule + public static final HttpServerRule httpServer = new HttpServerRule() { + @Override + protected void registerHandlers() throws IOException { + final String html = IOUtils.toString(ItemsTest.class.getResourceAsStream("items.html"), "UTF-8"); + final String htmlLinks = IOUtils.toString(ItemsTest.class.getResourceAsStream("items_links.html"), "UTF-8"); + serverBootstrap.registerHandler(GET_URL, new HttpRequestHandler() { + @Override + public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) + throws HttpException, IOException { + HttpEntity entity = new StringEntity(html, "UTF-8"); + httpResponse.setEntity(entity); + } + }).registerHandler(GET_LINKS_URL, new HttpRequestHandler() { + @Override + public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) + throws HttpException, IOException { + HttpEntity entity = new StringEntity(htmlLinks, "UTF-8"); + httpResponse.setEntity(entity); + } + }); + } + }; + @Test public void testItems() throws ClientException, URISyntaxException { - MicrodataHtmlClient client = new MicrodataHtmlClient(uri.toString()); + MicrodataHtmlClient client = new MicrodataHtmlClient(httpServer.getURI().toString()); Document doc = client.enter(GET_URL); Items items = doc.items(); Assert.assertThat(items.length(), equalTo(2)); @@ -98,7 +85,7 @@ public class ItemsTest extends TestBase { @Test public void testItemsLinks() throws ClientException, URISyntaxException { - MicrodataHtmlClient client = new MicrodataHtmlClient(uri.toString()); + MicrodataHtmlClient client = new MicrodataHtmlClient(httpServer.getURI().toString()); Document doc = client.enter(GET_LINKS_URL); Items items = doc.items(); Assert.assertThat(items.length(), equalTo(1)); @@ -116,5 +103,4 @@ public class ItemsTest extends TestBase { Assert.assertThat(otherMovies.length(), equalTo(2)); } - -} +} \ No newline at end of file diff --git a/src/test/java/util/TestBase.java b/src/test/java/util/HttpServerRule.java similarity index 57% rename from src/test/java/util/TestBase.java rename to src/test/java/util/HttpServerRule.java index d1dbc25..0822ae4 100644 --- a/src/test/java/util/TestBase.java +++ b/src/test/java/util/HttpServerRule.java @@ -18,50 +18,67 @@ ******************************************************************************/ package util; +import java.io.IOException; +import java.net.URI; +import java.util.concurrent.TimeUnit; + import org.apache.http.HttpHost; +import org.apache.http.client.utils.URIUtils; import org.apache.http.config.SocketConfig; import org.apache.http.impl.bootstrap.HttpServer; import org.apache.http.impl.bootstrap.ServerBootstrap; import org.apache.http.localserver.SSLTestContexts; -import org.junit.AfterClass; -import org.junit.BeforeClass; - -import java.util.concurrent.TimeUnit; +import org.junit.rules.ExternalResource; -public abstract class TestBase { +/** JUnit Rule that starts an HTTP server */ +public class HttpServerRule extends ExternalResource { public static final String ORIGIN = "TEST/1.1"; - protected static TestBase.ProtocolScheme scheme = TestBase.ProtocolScheme.http; - protected static ServerBootstrap serverBootstrap; - protected static HttpServer server; + private HttpServer server; + private HttpHost host; + private URI uri; - @BeforeClass - public static void setUp() throws Exception { - SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(5000).build(); - serverBootstrap = ServerBootstrap.bootstrap().setSocketConfig(socketConfig).setServerInfo("TEST/1.1"); - if(scheme.equals(TestBase.ProtocolScheme.https)) { - serverBootstrap.setSslContext(SSLTestContexts.createServerSSLContext()); + protected ServerBootstrap serverBootstrap; + + public static enum ProtocolScheme { + http, + https; + private ProtocolScheme() { } } + + protected final ProtocolScheme protocolScheme; - @AfterClass - public static void shutDown() throws Exception { - if(server != null) { - server.shutdown(-1, TimeUnit.SECONDS); - } - + public HttpServerRule() { + this(ProtocolScheme.http); + } + + public HttpServerRule(ProtocolScheme protocolScheme) { + this.protocolScheme = protocolScheme; + } + + @Override + protected void after() { + server.shutdown(-1, TimeUnit.SECONDS); } - public static HttpHost start() throws Exception { + @Override + protected void before() throws Throwable { + final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(5000).build(); + serverBootstrap = ServerBootstrap.bootstrap().setSocketConfig(socketConfig).setServerInfo(ORIGIN); + if(ProtocolScheme.https.equals(protocolScheme)) { + serverBootstrap.setSslContext(SSLTestContexts.createServerSSLContext()); + } + registerHandlers(); server = serverBootstrap.create(); server.start(); - return new HttpHost("localhost", server.getLocalPort(), scheme.name()); + host = new HttpHost("localhost", server.getLocalPort(), protocolScheme.name()); + uri = URIUtils.rewriteURI(new URI("/"), host); } - - public static enum ProtocolScheme { - http, - https; - - private ProtocolScheme() { - } + + protected void registerHandlers() throws IOException { + } + + public URI getURI() { + return uri; } } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
