From 79127be728bec269ee9c77641f6149adac00797e Mon Sep 17 00:00:00 2001 From: dmartinsalbuqu Date: Sat, 2 Nov 2019 17:35:12 +0000 Subject: [PATCH 1/3] Add example for nginx module --- docs/modules/nginx.md | 22 +++++++++ .../testcontainers/junit/SimpleNginxTest.java | 48 +++++++++++-------- 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/docs/modules/nginx.md b/docs/modules/nginx.md index 32d668e150a..57f376f4ef8 100644 --- a/docs/modules/nginx.md +++ b/docs/modules/nginx.md @@ -1,5 +1,27 @@ # Nginx Module +Nginx is a web server, reverse proxy and mail proxy and http cache. + +## Usage example + +The following example shows how to start Nginx. + + +[Creating a Nginx container](../../modules/nginx/src/test/java/org/testcontainers/containers/SimpleNginxText.java) inside_block:creatingContainer + + +How to add custom content to the Nginx server. + + +[Creating the static content to serve](../../modules/nginx/src/test/java/org/testcontainers/containers/SimpleNginxText.java) inside_block:addCustomContent + + +And how to query the Nginx server for the custom content added. + + +[Creating the static content to serve](../../modules/nginx/src/test/java/org/testcontainers/containers/SimpleNginxText.java) inside_block:getFromNginxServer + + ## Adding this module to your project dependencies Add the following dependency to your `pom.xml`/`build.gradle` file: diff --git a/modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxTest.java b/modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxTest.java index ad9026b6e4c..b41a0d8361e 100644 --- a/modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxTest.java +++ b/modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxTest.java @@ -8,52 +8,58 @@ import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; import java.io.*; +import java.net.URL; import java.net.URLConnection; -import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue; -import static org.rnorth.visibleassertions.VisibleAssertions.info; - +import static org.hamcrest.CoreMatchers.containsString; +import static org.rnorth.visibleassertions.VisibleAssertions.*; /** * @author richardnorth */ public class SimpleNginxTest { - private static File contentFolder = new File(System.getProperty("user.home") + "/.tmp-test-container"); + private static String tmpDirectory = System.getProperty("user.home") + "/.tmp-test-container"; + // creatingContainer { @Rule public NginxContainer nginx = new NginxContainer<>() - .withCustomContent(contentFolder.toString()) + .withCustomContent(tmpDirectory) .waitingFor(new HttpWaitStrategy()); + // } @SuppressWarnings({"Duplicates", "ResultOfMethodCallIgnored"}) @BeforeClass - public static void setupContent() throws FileNotFoundException { + public static void setupContent() throws Exception { + // addCustomContent { + // Create a temporary dir + File contentFolder = new File(tmpDirectory); contentFolder.mkdir(); - contentFolder.setReadable(true, false); - contentFolder.setWritable(true, false); - contentFolder.setExecutable(true, false); - + contentFolder.deleteOnExit(); + // And "hello world" HTTP file File indexFile = new File(contentFolder, "index.html"); - indexFile.setReadable(true, false); - indexFile.setWritable(true, false); - indexFile.setExecutable(true, false); - + indexFile.deleteOnExit(); @Cleanup PrintStream printStream = new PrintStream(new FileOutputStream(indexFile)); - printStream.println("This worked"); + printStream.println("Hello World!"); + // } } @Test public void testSimple() throws Exception { + // getFromNginxServer { + URL baseUrl = nginx.getBaseUrl("http", 80); - info("Base URL is " + nginx.getBaseUrl("http", 80)); + assertThat("An HTTP GET from the Nginx server returns the index.html from the custom content directory", + responseFromNginx(baseUrl), + containsString("Hello World!") + ); + // } + } - URLConnection urlConnection = nginx.getBaseUrl("http", 80).openConnection(); + private static String responseFromNginx(URL baseUrl) throws IOException { + URLConnection urlConnection = baseUrl.openConnection(); @Cleanup BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); - String line = reader.readLine(); - System.out.println(line); - - assertTrue("Using URLConnection, an HTTP GET from the nginx server returns the index.html from the custom content directory", line.contains("This worked")); + return reader.readLine(); } } From d59fa26afcbb4a500daa61aae93372e382aa85ab Mon Sep 17 00:00:00 2001 From: dmartinsalbuqu Date: Wed, 13 Nov 2019 23:46:38 +0530 Subject: [PATCH 2/3] Add example for nginx module (fix dir of nginx test) --- docs/modules/nginx.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/modules/nginx.md b/docs/modules/nginx.md index 57f376f4ef8..6be8bc35dfc 100644 --- a/docs/modules/nginx.md +++ b/docs/modules/nginx.md @@ -7,19 +7,19 @@ Nginx is a web server, reverse proxy and mail proxy and http cache. The following example shows how to start Nginx. -[Creating a Nginx container](../../modules/nginx/src/test/java/org/testcontainers/containers/SimpleNginxText.java) inside_block:creatingContainer +[Creating a Nginx container](../../modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxText.java) inside_block:creatingContainer How to add custom content to the Nginx server. -[Creating the static content to serve](../../modules/nginx/src/test/java/org/testcontainers/containers/SimpleNginxText.java) inside_block:addCustomContent +[Creating the static content to serve](../../modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxText.java) inside_block:addCustomContent And how to query the Nginx server for the custom content added. -[Creating the static content to serve](../../modules/nginx/src/test/java/org/testcontainers/containers/SimpleNginxText.java) inside_block:getFromNginxServer +[Creating the static content to serve](../../modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxText.java) inside_block:getFromNginxServer ## Adding this module to your project dependencies From e3e869b70d2c0ee8dc7bf4600e89bfe74fd8350c Mon Sep 17 00:00:00 2001 From: dmartinsalbuqu Date: Thu, 14 Nov 2019 00:01:47 +0530 Subject: [PATCH 3/3] Add example for nginx module (typo in text/test) --- docs/modules/nginx.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/modules/nginx.md b/docs/modules/nginx.md index 6be8bc35dfc..d195b5451a2 100644 --- a/docs/modules/nginx.md +++ b/docs/modules/nginx.md @@ -7,19 +7,19 @@ Nginx is a web server, reverse proxy and mail proxy and http cache. The following example shows how to start Nginx. -[Creating a Nginx container](../../modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxText.java) inside_block:creatingContainer +[Creating a Nginx container](../../modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxTest.java) inside_block:creatingContainer How to add custom content to the Nginx server. -[Creating the static content to serve](../../modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxText.java) inside_block:addCustomContent +[Creating the static content to serve](../../modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxTest.java) inside_block:addCustomContent And how to query the Nginx server for the custom content added. -[Creating the static content to serve](../../modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxText.java) inside_block:getFromNginxServer +[Creating the static content to serve](../../modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxTest.java) inside_block:getFromNginxServer ## Adding this module to your project dependencies