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
Changes from all commits
79127be
46cde78
57681ce
d59fa26
e3e869b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
There was a problem hiding this comment.
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.