How to use forStatusCodeMatching method of org.testcontainers.containers.wait.strategy.HttpWaitStrategy class

Best Testcontainers-java code snippet using org.testcontainers.containers.wait.strategy.HttpWaitStrategy.forStatusCodeMatching

Source:HttpWaitStrategyTest.java Github

copy

Full Screen

...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 @Test...

Full Screen

Full Screen

Source:JaegerAllInOneContainer.java Github

copy

Full Screen

...34 );35 waitingFor(new WaitAllStrategy(WITH_INDIVIDUAL_TIMEOUTS_ONLY)36 .withStrategy(new HttpWaitStrategy()37 .forPort(JAEGER_QUERY_PORT)38 .forStatusCodeMatching(status -> 200 <= status && status < 500)39 )40 .withStrategy(new HttpWaitStrategy()41 .forPort(JAEGER_COLLECTOR_THRIFT_PORT)42 .forStatusCodeMatching(status -> 200 <= status && status < 500)43 )44 );45 }46 public int getQueryPort() {47 return getMappedPort(JAEGER_QUERY_PORT);48 }49 public int getCollectorThriftPort() {50 return getMappedPort(JAEGER_COLLECTOR_THRIFT_PORT);51 }52 @Override53 public int getZipkinPort() {54 return getMappedPort(ZIPKIN_PORT);55 }56 @Override...

Full Screen

Full Screen

Source:CustomElasticSearchContainer.java Github

copy

Full Screen

...15 addFixedExposedPort(9300, 9300);16 this.addExposedPorts(9200, 9300);17 withEnv("discovery.type", "single-node");18 setWaitStrategy((new HttpWaitStrategy()).forPort(9200)19 .forStatusCodeMatching((response) -> response == 200 || response == 401)20 .withStartupTimeout(Duration.ofMinutes(2L)));21 }22 public String getHttpHostAddress() {23 return getHost() + ":" + getMappedPort(9200);24 }25}...

Full Screen

Full Screen

forStatusCodeMatching

Using AI Code Generation

copy

Full Screen

1HttpWaitStrategy httpWaitStrategy = new HttpWaitStrategy();2httpWaitStrategy.forStatusCodeMatching(statusCode -> statusCode == 200);3HttpWaitStrategy httpWaitStrategy = new HttpWaitStrategy();4httpWaitStrategy.forPath("/myPath");5HttpWaitStrategy httpWaitStrategy = new HttpWaitStrategy();6httpWaitStrategy.forStatusCodeMatching(statusCode -> statusCode == 200);7HttpWaitStrategy httpWaitStrategy = new HttpWaitStrategy();8httpWaitStrategy.forPath("/myPath");9HttpWaitStrategy httpWaitStrategy = new HttpWaitStrategy();10httpWaitStrategy.forStatusCodeMatching(statusCode -> statusCode == 200);11HttpWaitStrategy httpWaitStrategy = new HttpWaitStrategy();12httpWaitStrategy.forPath("/myPath");13HttpWaitStrategy httpWaitStrategy = new HttpWaitStrategy();14httpWaitStrategy.forStatusCodeMatching(statusCode -> statusCode == 200);15HttpWaitStrategy httpWaitStrategy = new HttpWaitStrategy();16httpWaitStrategy.forPath("/myPath");17HttpWaitStrategy httpWaitStrategy = new HttpWaitStrategy();18httpWaitStrategy.forStatusCodeMatching(statusCode -> statusCode == 200);19HttpWaitStrategy httpWaitStrategy = new HttpWaitStrategy();20httpWaitStrategy.forPath("/myPath");21HttpWaitStrategy httpWaitStrategy = new HttpWaitStrategy();22httpWaitStrategy.forStatusCodeMatching(statusCode -> statusCode ==

Full Screen

Full Screen

forStatusCodeMatching

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers.wait.strategy;2import com.github.dockerjava.api.command.InspectContainerResponse;3import com.github.dockerjava.api.model.ContainerNetwork;4import org.testcontainers.containers.ContainerLaunchException;5import org.testcontainers.containers.GenericContainer;6import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;7import java.io.IOException;8import java.net.HttpURLConnection;9import java.net.URL;10import java.time.Duration;11import java.util.Map;12import java.util.concurrent.TimeUnit;13public class HttpWaitStrategy extends AbstractWaitStrategy {14 private String path = "/";15 private int port = -1;16 private int statusCode = 200;17 public HttpWaitStrategy() {18 withStartupTimeout(Duration.ofSeconds(60));19 }20 public HttpWaitStrategy withPath(String path) {21 this.path = path;22 return this;23 }24 public HttpWaitStrategy withPort(int port) {25 this.port = port;26 return this;27 }28 public HttpWaitStrategy withStatusCodeMatching(int statusCode) {29 this.statusCode = statusCode;30 return this;31 }32 protected void waitUntilReady() {33 WaitStrategyTarget waitStrategyTarget = getWaitStrategyTarget();34 InspectContainerResponse containerInfo = waitStrategyTarget.getContainerInfo();35 Map<String, ContainerNetwork> networks = containerInfo.getNetworkSettings().getNetworks();36 String hostIpAddress = networks.values().stream().findFirst().map(ContainerNetwork::getIpAddress).orElseThrow(() -> new ContainerLaunchException("Could not find an IP address for container " + containerInfo.getId()));37 log.info("Waiting for {} seconds for URL: {}", startupTimeout.getSeconds(), target);38 long end = System.currentTimeMillis() + startupTimeout.toMillis();39 boolean ready = false;40 while (System.currentTimeMillis() < end && !ready) {41 try {42 URL url = new URL(target);43 HttpURLConnection connection = (HttpURLConnection) url.openConnection();44 connection.setRequestMethod("HEAD");45 int responseCode = connection.getResponseCode();46 ready = responseCode == statusCode;47 } catch (IOException e) {48 log.debug("Caught an exception while waiting for URL to become available (ignoring)", e);49 }50 if (!ready) {51 try {52 TimeUnit.MILLISECONDS.sleep(100);53 } catch

Full Screen

Full Screen

forStatusCodeMatching

Using AI Code Generation

copy

Full Screen

1public class HttpWaitStrategy extends WaitStrategy {2 private String path;3 private int expectedStatusCode;4 public HttpWaitStrategy path(String path) {5 this.path = path;6 return this;7 }8 public HttpWaitStrategy forStatusCodeMatching(Predicate<Integer> statusCodePredicate) {9 this.statusCodePredicate = statusCodePredicate;10 return this;11 }12 public HttpWaitStrategy forStatusCode(int expectedStatusCode) {13 this.expectedStatusCode = expectedStatusCode;14 return this;15 }16 public HttpWaitStrategy forPort(int port) {17 this.port = port;18 return this;19 }20 public HttpWaitStrategy forResponsePredicate(Predicate<String> responsePredicate) {21 this.responsePredicate = responsePredicate;22 return this;23 }24 public HttpWaitStrategy forPath(String path) {25 this.path = path;26 return this;27 }28 public HttpWaitStrategy forHost(String host) {29 this.host = host;30 return this;31 }32 protected void waitUntilReady() {33 WaitAllStrategy waitAllStrategy = new WaitAllStrategy();34 waitAllStrategy.withStrategy(new HttpWaitStrategy()35 .forPath(path)36 .forStatusCodeMatching(statusCode -> statusCode == expectedStatusCode));37 waitAllStrategy.waitUntilReady(container);38 }39}40public class HttpWaitStrategy extends WaitStrategy {41 private String path;42 private int expectedStatusCode;43 public HttpWaitStrategy path(String path) {44 this.path = path;45 return this;46 }47 public HttpWaitStrategy forStatusCodeMatching(Predicate<Integer> statusCodePredicate) {48 this.statusCodePredicate = statusCodePredicate;49 return this;50 }51 public HttpWaitStrategy forStatusCode(int expectedStatusCode) {52 this.expectedStatusCode = expectedStatusCode;53 return this;54 }55 public HttpWaitStrategy forPort(int port) {56 this.port = port;57 return this;58 }59 public HttpWaitStrategy forResponsePredicate(Predicate<String> responsePredicate) {60 this.responsePredicate = responsePredicate;61 return this;62 }63 public HttpWaitStrategy forPath(String path) {64 this.path = path;65 return this;66 }67 public HttpWaitStrategy forHost(String host) {68 this.host = host;69 return this;70 }

Full Screen

Full Screen

forStatusCodeMatching

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;3public class HttpWaitStrategy1 {4 public static void main(String[] args) {5 GenericContainer container = new GenericContainer("nginx:latest")6 .withExposedPorts(80)7 .withCommand("nginx", "-g", "daemon off;");8 container.waitingFor(new HttpWaitStrategy()9 .forStatusCodeMatching(response -> response == 200));10 container.start();11 System.out.println("Container Id: " + container.getContainerId());12 System.out.println("Container Name: " + container.getContainerName());13 System.out.println("Container Image: " + container.getDockerImageName());14 System.out.println("Container Host: " + container.getHost());

Full Screen

Full Screen

forStatusCodeMatching

Using AI Code Generation

copy

Full Screen

1public class HttpWaitStrategyTest {2 private static final Logger LOGGER = LoggerFactory.getLogger(HttpWaitStrategyTest.class);3 public void testHttpWaitStrategy() {4 GenericContainer genericContainer = new GenericContainer("httpd:2.4")5 .withExposedPorts(80)6 .waitingFor(Wait.forHttp("/").forStatusCodeMatching(response -> response == 200));7 genericContainer.start();8 String containerIpAddress = genericContainer.getContainerIpAddress();9 Integer mappedPort = genericContainer.getMappedPort(80);10 LOGGER.info("Container IP address: {} and mapped port: {}", containerIpAddress, mappedPort);11 genericContainer.stop();12 }13}

Full Screen

Full Screen

forStatusCodeMatching

Using AI Code Generation

copy

Full Screen

1public class Test {2 public static void main(String[] args) {3 GenericContainer container = new GenericContainer("tomcat:8.5.5-jre8");4 container.waitingFor(new HttpWaitStrategy().forPath("/").forStatusCodeMatching(response -> response == 200));5 container.start();6 }7}8forPort(int port)9public class Test {10 public static void main(String[] args) {11 GenericContainer container = new GenericContainer("tomcat:8.5.5-jre8");12 container.waitingFor(new HttpWaitStrategy().forPath("/").forPort(8080));13 container.start();14 }15}16forStatusCode(int statusCode)17public class Test {18 public static void main(String[] args) {19 GenericContainer container = new GenericContainer("tomcat:8.5.5-jre8");

Full Screen

Full Screen

forStatusCodeMatching

Using AI Code Generation

copy

Full Screen

1public class HttpWaitStrategyTest {2 public void testHttpWaitStrategy() throws Exception {3 GenericContainer container = new GenericContainer("httpd:2.4.41");4 container.waitingFor(5 new HttpWaitStrategy()6 .forPath("/")7 .forStatusCodeMatching(response -> response == 200));8 container.start();9 container.stop();10 }11}12public class HttpWaitStrategyTest {13 public void testHttpWaitStrategy() throws Exception {14 GenericContainer container = new GenericContainer("httpd:2.4.41");15 container.waitingFor(16 new HttpWaitStrategy()17 .forPath("/")18 .forStatusCodeMatching(response -> response == 200));19 container.start();20 container.stop();21 }22}23public class HttpWaitStrategyTest {24 public void testHttpWaitStrategy() throws Exception {25 GenericContainer container = new GenericContainer("httpd:2.4.41");26 container.waitingFor(27 new HttpWaitStrategy()28 .forPath("/")29 .forStatusCodeMatching(response -> response == 200));30 container.start();31 container.stop();32 }33}34public class HttpWaitStrategyTest {35 public void testHttpWaitStrategy() throws Exception {36 GenericContainer container = new GenericContainer("httpd:2.4.41");37 container.waitingFor(38 new HttpWaitStrategy()39 .forPath("/")40 .forStatusCodeMatching(response -> response == 200));41 container.start();42 container.stop();43 }44}45public class HttpWaitStrategyTest {46 public void testHttpWaitStrategy() throws Exception {47 GenericContainer container = new GenericContainer("httpd:2.4.41");48 container.waitingFor(49 new HttpWaitStrategy()50 .forPath("/")51 .forStatusCodeMatching(response -> response ==

Full Screen

Full Screen

forStatusCodeMatching

Using AI Code Generation

copy

Full Screen

1public class HttpWaitStrategyExample {2 public static void main(String[] args) {3 GenericContainer container = new GenericContainer("nginx:latest")4 .waitingFor(new HttpWaitStrategy()5 .forStatusCodeMatching(response -> response == 200));6 container.start();7 System.out.println(container.getLogs());8 container.stop();9 }10}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful