Best Testcontainers-java code snippet using org.testcontainers.junit.wait.strategy.HttpWaitStrategyTest
Source:HttpWaitStrategyTest.java
...5 * Tests for {@link HttpWaitStrategy}.6 *7 * @author Pete Cornish {@literal <outofcoffee@gmail.com>}8 */9public class HttpWaitStrategyTest extends AbstractWaitStrategyTest<HttpWaitStrategy> {10 /**11 * newline sequence indicating end of the HTTP header.12 */13 private static final String NEWLINE = "\r\n";14 private static final String GOOD_RESPONSE_BODY = "Good Response Body";15 /**16 * Expects that the WaitStrategy returns successfully after receiving an HTTP 200 response from the container.17 */18 @Test19 public void testWaitUntilReadyWithSuccess() {20 waitUntilReadyAndSucceed(createShellCommand("200 OK", HttpWaitStrategyTest.GOOD_RESPONSE_BODY));21 }22 /**23 * Expects that the WaitStrategy returns successfully after receiving an HTTP 401 response from the container.24 * This 401 response is checked with a lambda using {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)}25 */26 @Test27 public void testWaitUntilReadyWithUnauthorizedWithLambda() {28 waitUntilReadyAndSucceed(startContainerWithCommand(createShellCommand("401 UNAUTHORIZED", HttpWaitStrategyTest.GOOD_RESPONSE_BODY), createHttpWaitStrategy(ready).forStatusCodeMatching(( it) -> ((it >= 200) && (it < 300)) || (it == 401))));29 }30 /**31 * Expects that the WaitStrategy returns successfully after receiving an HTTP 401 response from the container.32 * This 401 response is checked with many status codes using {@link HttpWaitStrategy#forStatusCode(int)}33 */34 @Test35 public void testWaitUntilReadyWithManyStatusCodes() {36 waitUntilReadyAndSucceed(startContainerWithCommand(createShellCommand("401 UNAUTHORIZED", HttpWaitStrategyTest.GOOD_RESPONSE_BODY), createHttpWaitStrategy(ready).forStatusCode(300).forStatusCode(401).forStatusCode(500)));37 }38 /**39 * Expects that the WaitStrategy returns successfully after receiving an HTTP 401 response from the container.40 * This 401 response is checked with with many status codes using {@link HttpWaitStrategy#forStatusCode(int)}41 * and a lambda using {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)}42 */43 @Test44 public void testWaitUntilReadyWithManyStatusCodesAndLambda() {45 waitUntilReadyAndSucceed(startContainerWithCommand(createShellCommand("401 UNAUTHORIZED", HttpWaitStrategyTest.GOOD_RESPONSE_BODY), createHttpWaitStrategy(ready).forStatusCode(300).forStatusCode(500).forStatusCodeMatching(( it) -> it == 401)));46 }47 /**48 * Expects that the WaitStrategy throws a {@link RetryCountExceededException} after not receiving any of the49 * error code defined with {@link HttpWaitStrategy#forStatusCode(int)}50 * and {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)}51 */52 @Test53 public void testWaitUntilReadyWithTimeoutAndWithManyStatusCodesAndLambda() {54 waitUntilReadyAndTimeout(startContainerWithCommand(createShellCommand("401 UNAUTHORIZED", HttpWaitStrategyTest.GOOD_RESPONSE_BODY), createHttpWaitStrategy(ready).forStatusCode(300).forStatusCodeMatching(( it) -> it == 500)));55 }56 /**57 * Expects that the WaitStrategy throws a {@link RetryCountExceededException} after not receiving any of the58 * error code defined with {@link HttpWaitStrategy#forStatusCode(int)}59 * and {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)}. Note that a 200 status code should not60 * be considered as a successful return as not explicitly set.61 * Test case for: https://github.com/testcontainers/testcontainers-java/issues/88062 */63 @Test64 public void testWaitUntilReadyWithTimeoutAndWithLambdaShouldNotMatchOk() {65 waitUntilReadyAndTimeout(startContainerWithCommand(createShellCommand("200 OK", HttpWaitStrategyTest.GOOD_RESPONSE_BODY), createHttpWaitStrategy(ready).forStatusCodeMatching(( it) -> it >= 300)));66 }67 /**68 * Expects that the WaitStrategy throws a {@link RetryCountExceededException} after not receiving an HTTP 20069 * response from the container within the timeout period.70 */71 @Test72 public void testWaitUntilReadyWithTimeout() {73 waitUntilReadyAndTimeout(createShellCommand("400 Bad Request", HttpWaitStrategyTest.GOOD_RESPONSE_BODY));74 }75 /**76 * Expects that the WaitStrategy throws a {@link RetryCountExceededException} after not the expected response body77 * from the container within the timeout period.78 */79 @Test80 public void testWaitUntilReadyWithTimeoutAndBadResponseBody() {81 waitUntilReadyAndTimeout(createShellCommand("200 OK", "Bad Response"));82 }83 /**84 * Expects the WaitStrategy probing the right port.85 */86 @Test87 public void testWaitUntilReadyWithSpecificPort() {88 waitUntilReadyAndSucceed(startContainerWithCommand(createShellCommand("200 OK", HttpWaitStrategyTest.GOOD_RESPONSE_BODY, 9090), createHttpWaitStrategy(ready).forPort(9090), 7070, 8080, 9090));89 }90}...
HttpWaitStrategyTest
Using AI Code Generation
1package com.testcontainers.junit.wait.strategy;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;4import org.testcontainers.containers.wait.strategy.Wait;5import java.time.Duration;6public class HttpWaitStrategyTest {7 public static void main(String[] args) {8 GenericContainer container = new GenericContainer("nginx:latest")9 .withExposedPorts(80)10 .waitingFor(Wait.forHttp("/").forStatusCode(200)11 .withStartupTimeout(Duration.ofSeconds(60)));12 container.start();13 }14}15[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ testcontainers-junit ---16[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ testcontainers-junit ---17[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ testcontainers-junit ---18[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ testcontainers-junit ---
HttpWaitStrategyTest
Using AI Code Generation
1public class HttpWaitStrategyTest {2 public GenericContainer container = new GenericContainer()3 .withImage("nginx")4 .withExposedPorts(80)5 .waitingFor(new HttpWaitStrategy().forPath("/"));6}7public void testHttpWaitStrategy() {8 assertThat(container.isRunning(), is(true));9}10org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (localhost ports: [32780] should be listening)11 at org.testcontainers.containers.wait.strategy.AbstractWaitStrategy.waitUntilReady(AbstractWaitStrategy.java:35)12 at org.testcontainers.containers.GenericContainer.waitUntilContainerStarted(GenericContainer.java:563)13 at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:267)14 at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:235)15 at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:43)16 at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:233)17 at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:221)18 at org.testcontainers.containers.GenericContainer.starting(GenericContainer.java:625)19 at org.testcontainers.containers.FailureDetectingExternalResource$1.evaluate(FailureDetectingExternalResource.java:29)20 at org.junit.rules.RunRules.evaluate(RunRules.java:20)21 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)22 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)23 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)24 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)25 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)26 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)27 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)28 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)29 at org.junit.runners.ParentRunner.run(ParentRunner.java:363)30 at org.junit.runner.JUnitCore.run(JUnitCore.java:137)31 at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
HttpWaitStrategyTest
Using AI Code Generation
1import org.testcontainers.containers.wait.strategy.HttpWaitStrategyTest;2import org.testcontainers.containers.wait.strategy.HttpWaitStrategyTest$HttpWaitStrategyTestContainer;3import org.testcontainers.containers.wait.strategy.HttpWaitStrategyTest$HttpWaitStrategyTestContainer$HttpWaitStrategyTestContainerBuilder;4import org.testcontainers.containers.wait.strategy.HttpWaitStrategyTest$HttpWaitStrategyTestContainer$HttpWaitStrategyTestContainerBuilder$HttpWaitStrategyTestContainerBuilderBuild;5import org.testcontainers.containers.wait.strategy.HttpWaitStrategyTest$HttpWaitStrategyTestContainer$HttpWaitStrategyTestContainerBuilder$HttpWaitStrategyTestContainerBuilderBuild$HttpWaitStrategyTestContainerBuilderBuildStart;6import org.testcontainers.containers.wait.strategy.HttpWaitStrategyTest$HttpWaitStrategyTestContainer$HttpWaitStrategyTestContainerBuilder$HttpWaitStrategyTestContainerBuilderBuild$HttpWaitStrategyTestContainerBuilderBuildStart$HttpWaitStrategyTestContainerBuilderBuildStartStop;7import org.testcontainers.containers.wait.strategy.HttpWaitStrategyTest$HttpWaitStrategyTestContainer$HttpWaitStrategyTestContainerBuilder$HttpWaitStrategyTestContainerBuilderBuild$HttpWaitStrategyTestContainerBuilderBuildStart$HttpWaitStrategyTestContainerBuilderBuildStartStop$HttpWaitStrategyTestContainerBuilderBuildStartStopClose;8import org.testcontainers.containers.wait.strategy.HttpWaitStrategyTest$HttpWaitStrategyTestContainer$HttpWaitStrategyTestContainerBuilder$HttpWaitStrategyTestContainerBuilderBuild$HttpWaitStrategyTestContainerBuilderBuildStart$HttpWaitStrategyTestContainerBuilderBuildStartStop$HttpWaitStrategyTestContainerBuilderBuildStartStopClose$HttpWaitStrategyTestContainerBuilderBuildStartStopCloseWithLogConsumer;9import org.testcontainers.containers.wait.strategy.HttpWaitStrategyTest$HttpWaitStrategyTestContainer$HttpWaitStrategyTestContainerBuilder$HttpWaitStrategyTestContainerBuilderBuild$HttpWaitStrategyTestContainerBuilderBuildStart$HttpWaitStrategyTestContainerBuilderBuildStartStop$HttpWaitStrategyTestContainerBuilderBuildStartStopClose$HttpWaitStrategyTestContainerBuilderBuildStartStopCloseWithLogConsumer$HttpWaitStrategyTestContainerBuilderBuildStartStopCloseWithLogConsumerWithExposedPorts;10import org.testcontainers.containers.wait.strategy.HttpWaitStrategyTest$HttpWaitStrategyTestContainer$HttpWaitStrategyTestContainerBuilder$HttpWaitStrategyTestContainerBuilderBuild$HttpWaitStrategyTestContainerBuilderBuildStart$HttpWaitStrategyTestContainerBuilderBuildStartStop$HttpWaitStrategyTestContainerBuilderBuildStartStopClose$HttpWaitStrategyTestContainerBuilderBuildStartStop
HttpWaitStrategyTest
Using AI Code Generation
1import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;2public class HttpWaitStrategyTest {3 public static void main(String[] args) {4 HttpWaitStrategy httpWaitStrategy = new HttpWaitStrategy();5 httpWaitStrategy = new HttpWaitStrategy("/health");6 httpWaitStrategy = new HttpWaitStrategy("/health", 8080);7 httpWaitStrategy = new HttpWaitStrategy("/health", 8080, "GET");8 httpWaitStrategy = new HttpWaitStrategy("/health", 8080, "GET", "200");9 httpWaitStrategy = new HttpWaitStrategy("/health", 8080, "GET", "200", "401");10 httpWaitStrategy = new HttpWaitStrategy("/health", 8080, "GET", "200", "401", "500");11 httpWaitStrategy = new HttpWaitStrategy("/health", 8080, "GET", "200", "401", "500", "503");12 httpWaitStrategy = new HttpWaitStrategy("/health", 8080, "GET", "200", "401", "500", "503", "504");13 httpWaitStrategy = new HttpWaitStrategy("/health", 8080, "GET", "200", "401", "500", "503", "504", "999");14 httpWaitStrategy = new HttpWaitStrategy("/health", 8080, "GET", "200", "401", "500", "503", "504", "999", "2000");15 httpWaitStrategy = new HttpWaitStrategy("/health", 8080, "GET", "200", "401", "500", "503", "504", "999", "2000", "3000");16 httpWaitStrategy = new HttpWaitStrategy("/health", 8080, "GET", "200", "401", "500", "503", "504", "999", "2000", "3000", "4000");17 httpWaitStrategy = new HttpWaitStrategy("/health", 8080, "GET", "200", "401", "500", "503", "504", "999", "2000", "3000", "4000", "5000");
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!