Files
spring-boot-rest/testing-modules/test-containers/src/test/java/com/baeldung/testconainers/GenericContainerLiveTest.java
Dhawal Kapil 8270c6d61a BAEL-1987 Fix Tests and Compilation In All Projects (#4693)
* BAEL-1987 Fix Tests and Compilation In All Projects

-Fixed various compilation issues, test assertions throughout the project.

* Update pom.xml
2018-07-13 00:07:27 +02:00

52 lines
1.6 KiB
Java

package com.baeldung.testconainers;
import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.platform.commons.annotation.Testable;
import org.testcontainers.containers.GenericContainer;
@Testable
public class GenericContainerLiveTest {
@ClassRule
public static GenericContainer simpleWebServer =
new GenericContainer("alpine:3.2")
.withExposedPorts(80)
.withCommand("/bin/sh", "-c", "while true; do echo "
+ "\"HTTP/1.1 200 OK\n\nHello World!\" | nc -l -p 80; done");
@Test
public void givenSimpleWebServerContainer_whenGetReuqest_thenReturnsResponse()
throws Exception {
String address = "http://"
+ simpleWebServer.getContainerIpAddress()
+ ":" + simpleWebServer.getMappedPort(80);
String response = simpleGetRequest(address);
assertEquals(response, "Hello World!");
}
private String simpleGetRequest(String address) throws Exception {
URL url = new URL(address);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
return content.toString();
}
}