diff --git a/docs/modules/nginx.md b/docs/modules/nginx.md index 32d668e150a..d195b5451a2 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/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/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/SimpleNginxTest.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(); } }