Created
July 19, 2024 06:16
-
-
Save kiview/007d80374371ede5f349cafc344562e9 to your computer and use it in GitHub Desktop.
TC Cloudflare Tunnel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.testcontainers.containers; | |
import org.testcontainers.Testcontainers; | |
import org.testcontainers.containers.GenericContainer; | |
import org.testcontainers.containers.wait.strategy.Wait; | |
import org.testcontainers.utility.DockerImageName; | |
public class CloudflaredContainer extends GenericContainer<CloudflaredContainer> { | |
private String publicUrl; | |
public CloudflaredContainer(DockerImageName dockerImageName, int port) { | |
super(dockerImageName); | |
dockerImageName.assertCompatibleWith(DockerImageName.parse("cloudflare/cloudflared")); | |
withAccessToHost(true); | |
Testcontainers.exposeHostPorts(port); | |
withCommand("tunnel", "--url", String.format("http://host.testcontainers.internal:%d", port)); | |
waitingFor(Wait.forLogMessage(".*Registered tunnel connection.*", 1)); | |
} | |
public String getPublicUrl() { | |
if (null != publicUrl) { | |
return publicUrl; | |
} | |
String logs = getLogs(); | |
String[] split = logs.split(String.format("\\n")); | |
boolean found = false; | |
for (int i = 0; i < split.length; i++) { | |
String currentLine = split[i]; | |
if (currentLine.contains("Your quick Tunnel has been created")) { | |
found = true; | |
continue; | |
} | |
if (found) { | |
return publicUrl = currentLine.substring(currentLine.indexOf("http"), currentLine.indexOf(".com") + 4); | |
} | |
} | |
throw new IllegalStateException("Didn't find public url in logs. Has container started?"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.testcontainers.containers; | |
import io.restassured.RestAssured; | |
import org.junit.jupiter.api.Test; | |
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; | |
import org.testcontainers.shaded.org.awaitility.Awaitility; | |
import org.testcontainers.utility.DockerImageName; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.URL; | |
import java.util.concurrent.TimeUnit; | |
import static org.assertj.core.api.Assertions.assertThat; | |
import static org.junit.jupiter.api.Assertions.*; | |
class CloudflaredContainerTest { | |
@Test | |
public void shouldStartAndTunnelToHelloWorld() throws IOException { | |
try ( | |
GenericContainer<?> helloworld = new GenericContainer<>( | |
DockerImageName.parse("testcontainers/helloworld:1.1.0") | |
) | |
.withNetworkAliases("helloworld") | |
.withExposedPorts(8080, 8081) | |
.waitingFor(new HttpWaitStrategy()) | |
) { | |
helloworld.start(); | |
try ( | |
// starting { | |
CloudflaredContainer cloudflare = new CloudflaredContainer( | |
DockerImageName.parse("cloudflare/cloudflared:2024.5.0"), | |
helloworld.getFirstMappedPort() | |
); | |
// | |
) { | |
cloudflare.start(); | |
String url = cloudflare.getPublicUrl(); | |
assertThat(url).as("Public url contains 'cloudflare'").contains("cloudflare"); | |
System.setProperty("networkaddress.cache.ttl", "0"); | |
Awaitility.await().pollDelay(10, TimeUnit.SECONDS).atMost(30, TimeUnit.SECONDS).ignoreExceptions().untilAsserted(()-> { | |
String body = RestAssured.given().baseUri(url) | |
.get() | |
.body() | |
.print(); | |
assertThat(body.trim()).as("the index page contains the title 'Hello world'").contains("Hello world"); | |
}); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment