-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
SimpleNginxTest.java
75 lines (61 loc) · 2.54 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
66
67
68
69
70
71
72
73
74
75
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 org.testcontainers.utility.DockerImageName;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author richardnorth
*/
public class SimpleNginxTest {
private static final DockerImageName NGINX_IMAGE = DockerImageName.parse("nginx:1.9.4");
private static String tmpDirectory = System.getProperty("user.home") + "/.tmp-test-container";
// creatingContainer {
@Rule
public NginxContainer<?> nginx = new NginxContainer<>(NGINX_IMAGE)
.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(responseFromNginx(baseUrl))
.as("An HTTP GET from the Nginx server returns the index.html from the custom content directory")
.contains("Hello World!");
// }
assertHasCorrectExposedAndLivenessCheckPorts(nginx);
}
private void assertHasCorrectExposedAndLivenessCheckPorts(NginxContainer<?> nginxContainer) throws Exception {
assertThat(nginxContainer.getExposedPorts()).containsExactly(80);
assertThat(nginxContainer.getLivenessCheckPortNumbers()).containsExactly(nginxContainer.getMappedPort(80));
}
private static String responseFromNginx(URL baseUrl) throws IOException {
URLConnection urlConnection = baseUrl.openConnection();
@Cleanup
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
return reader.readLine();
}
}