Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for nginx module #2027

Merged
merged 5 commits into from Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions 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.

<!--codeinclude-->
[Creating a Nginx container](../../modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxTest.java) inside_block:creatingContainer
<!--/codeinclude-->

How to add custom content to the Nginx server.

<!--codeinclude-->
[Creating the static content to serve](../../modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxTest.java) inside_block:addCustomContent
<!--/codeinclude-->

And how to query the Nginx server for the custom content added.

<!--codeinclude-->
[Creating the static content to serve](../../modules/nginx/src/test/java/org/testcontainers/junit/SimpleNginxTest.java) inside_block:getFromNginxServer
<!--/codeinclude-->

## Adding this module to your project dependencies

Add the following dependency to your `pom.xml`/`build.gradle` file:
Expand Down
Expand Up @@ -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";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to shift some things a bit to be able to include the test in the docs.


// 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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a reason why these were being set. I'm happy to put them back but for the purposes of a test/demo I don't think they are required.

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("<html><body>This worked</body></html>");
printStream.println("<html><body>Hello World!</body></html>");
// }
}

@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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be done inline but I moved to a separate method to make the example more readable.

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();
}
}