forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleNginxTest.java
65 lines (54 loc) · 2.07 KB
/
SimpleNginxTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package org.testcontainers.junit;
import lombok.Cleanup;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.testcontainers.containers.NginxContainer;
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import static org.hamcrest.CoreMatchers.containsString;
import static org.rnorth.visibleassertions.VisibleAssertions.*;
/**
* @author richardnorth
*/
public class SimpleNginxTest {
private static String tmpDirectory = System.getProperty("user.home") + "/.tmp-test-container";
// creatingContainer {
@Rule
public NginxContainer nginx = new NginxContainer<>()
.withCustomContent(tmpDirectory)
.waitingFor(new HttpWaitStrategy());
// }
@SuppressWarnings({"Duplicates", "ResultOfMethodCallIgnored"})
@BeforeClass
public static void setupContent() throws Exception {
// addCustomContent {
// Create a temporary dir
File contentFolder = new File(tmpDirectory);
contentFolder.mkdir();
contentFolder.deleteOnExit();
// And "hello world" HTTP file
File indexFile = new File(contentFolder, "index.html");
indexFile.deleteOnExit();
@Cleanup PrintStream printStream = new PrintStream(new FileOutputStream(indexFile));
printStream.println("<html><body>Hello World!</body></html>");
// }
}
@Test
public void testSimple() throws Exception {
// getFromNginxServer {
URL baseUrl = 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!")
);
// }
}
private static String responseFromNginx(URL baseUrl) throws IOException {
URLConnection urlConnection = baseUrl.openConnection();
@Cleanup BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
return reader.readLine();
}
}