Best Testcontainers-java code snippet using org.testcontainers.junit.wait.strategy.HttpWaitStrategyTest.createHttpsShellCommand
Source:HttpWaitStrategyTest.java
...55 @Test56 public void testWaitUntilReadyWithTlsAndAllowUnsecure() {57 try (58 GenericContainer<?> container = startContainerWithCommand(59 createHttpsShellCommand("200 OK", GOOD_RESPONSE_BODY, 8080),60 createHttpWaitStrategy(ready).usingTls().allowInsecure()61 )62 ) {63 waitUntilReadyAndSucceed(container);64 }65 }66 /**67 * Expects that the WaitStrategy returns successfully after receiving an HTTP 401 response from the container.68 * This 401 response is checked with a lambda using {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)}69 */70 @Test71 public void testWaitUntilReadyWithUnauthorizedWithLambda() {72 try (73 GenericContainer<?> container = startContainerWithCommand(74 createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY),75 createHttpWaitStrategy(ready).forStatusCodeMatching(it -> it >= 200 && it < 300 || it == 401)76 )77 ) {78 waitUntilReadyAndSucceed(container);79 }80 }81 /**82 * Expects that the WaitStrategy returns successfully after receiving an HTTP 401 response from the container.83 * This 401 response is checked with many status codes using {@link HttpWaitStrategy#forStatusCode(int)}84 */85 @Test86 public void testWaitUntilReadyWithManyStatusCodes() {87 try (88 GenericContainer<?> container = startContainerWithCommand(89 createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY),90 createHttpWaitStrategy(ready).forStatusCode(300).forStatusCode(401).forStatusCode(500)91 )92 ) {93 waitUntilReadyAndSucceed(container);94 }95 }96 /**97 * Expects that the WaitStrategy returns successfully after receiving an HTTP 401 response from the container.98 * This 401 response is checked with with many status codes using {@link HttpWaitStrategy#forStatusCode(int)}99 * and a lambda using {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)}100 */101 @Test102 public void testWaitUntilReadyWithManyStatusCodesAndLambda() {103 try (104 GenericContainer<?> container = startContainerWithCommand(105 createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY),106 createHttpWaitStrategy(ready)107 .forStatusCode(300)108 .forStatusCode(500)109 .forStatusCodeMatching(it -> it == 401)110 )111 ) {112 waitUntilReadyAndSucceed(container);113 }114 }115 /**116 * Expects that the WaitStrategy throws a {@link RetryCountExceededException} after not receiving any of the117 * error code defined with {@link HttpWaitStrategy#forStatusCode(int)}118 * and {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)}119 */120 @Test121 public void testWaitUntilReadyWithTimeoutAndWithManyStatusCodesAndLambda() {122 try (123 GenericContainer<?> container = startContainerWithCommand(124 createShellCommand("401 UNAUTHORIZED", GOOD_RESPONSE_BODY),125 createHttpWaitStrategy(ready).forStatusCode(300).forStatusCodeMatching(it -> it == 500)126 )127 ) {128 waitUntilReadyAndTimeout(container);129 }130 }131 /**132 * Expects that the WaitStrategy throws a {@link RetryCountExceededException} after not receiving any of the133 * error code defined with {@link HttpWaitStrategy#forStatusCode(int)}134 * and {@link HttpWaitStrategy#forStatusCodeMatching(Predicate)}. Note that a 200 status code should not135 * be considered as a successful return as not explicitly set.136 * Test case for: https://github.com/testcontainers/testcontainers-java/issues/880137 */138 @Test139 public void testWaitUntilReadyWithTimeoutAndWithLambdaShouldNotMatchOk() {140 try (141 GenericContainer<?> container = startContainerWithCommand(142 createShellCommand("200 OK", GOOD_RESPONSE_BODY),143 createHttpWaitStrategy(ready).forStatusCodeMatching(it -> it >= 300)144 )145 ) {146 waitUntilReadyAndTimeout(container);147 }148 }149 /**150 * Expects that the WaitStrategy throws a {@link RetryCountExceededException} after not receiving an HTTP 200151 * response from the container within the timeout period.152 */153 @Test154 public void testWaitUntilReadyWithTimeout() {155 waitUntilReadyAndTimeout(createShellCommand("400 Bad Request", GOOD_RESPONSE_BODY));156 }157 /**158 * Expects that the WaitStrategy throws a {@link RetryCountExceededException} after not the expected response body159 * from the container within the timeout period.160 */161 @Test162 public void testWaitUntilReadyWithTimeoutAndBadResponseBody() {163 waitUntilReadyAndTimeout(createShellCommand("200 OK", "Bad Response"));164 }165 /**166 * Expects the WaitStrategy probing the right port.167 */168 @Test169 public void testWaitUntilReadyWithSpecificPort() {170 try (171 GenericContainer<?> container = startContainerWithCommand(172 createShellCommand("200 OK", GOOD_RESPONSE_BODY, 9090),173 createHttpWaitStrategy(ready).forPort(9090),174 7070,175 8080,176 9090177 )178 ) {179 waitUntilReadyAndSucceed(container);180 }181 }182 @Test183 public void testWaitUntilReadyWithTimoutCausedByReadTimeout() {184 try (185 GenericContainer<?> container = startContainerWithCommand(186 createShellCommand("0 Connection Refused", GOOD_RESPONSE_BODY, 9090),187 createHttpWaitStrategy(ready).forPort(9090).withReadTimeout(Duration.ofMillis(1)),188 9090189 )190 ) {191 waitUntilReadyAndTimeout(container);192 }193 }194 /**195 * @param ready the AtomicBoolean on which to indicate success196 * @return the WaitStrategy under test197 */198 @NotNull199 protected HttpWaitStrategy buildWaitStrategy(final AtomicBoolean ready) {200 return createHttpWaitStrategy(ready).forResponsePredicate(s -> s.equals(GOOD_RESPONSE_BODY));201 }202 /**203 * Create a HttpWaitStrategy instance with a waitUntilReady implementation204 *205 * @param ready Indicates that the WaitStrategy has completed waiting successfully.206 * @return the HttpWaitStrategy instance207 */208 private HttpWaitStrategy createHttpWaitStrategy(final AtomicBoolean ready) {209 return new HttpWaitStrategy() {210 @Override211 protected void waitUntilReady() {212 // blocks until ready or timeout occurs213 super.waitUntilReady();214 ready.set(true);215 }216 };217 }218 private String createShellCommand(String header, String responseBody) {219 return createShellCommand(header, responseBody, 8080);220 }221 private String createShellCommand(String header, String responseBody, int port) {222 int length = responseBody.getBytes().length;223 return (224 "while true; do { echo -e \"HTTP/1.1 " +225 header +226 NEWLINE +227 "Content-Type: text/html" +228 NEWLINE +229 "Content-Length: " +230 length +231 NEWLINE +232 "\";" +233 " echo \"" +234 responseBody +235 "\";} | nc -lp " +236 port +237 "; done"238 );239 }240 private String createHttpsShellCommand(String header, String responseBody, int port) {241 int length = responseBody.getBytes().length;242 return (243 "apk add nmap-ncat; while true; do { echo -e \"HTTP/1.1 " +244 header +245 NEWLINE +246 "Content-Type: text/html" +247 NEWLINE +248 "Content-Length: " +249 length +250 NEWLINE +251 "\";" +252 " echo \"" +253 responseBody +254 "\";} | ncat -lp " +...
createHttpsShellCommand
Using AI Code Generation
1 public void createHttpsShellCommand() {2 HttpsWaitStrategy httpsWaitStrategy = new HttpsWaitStrategy();3 String[] command = httpsWaitStrategy.createHttpsShellCommand(8080, "test");4 String[] expectedCommand = {"/bin/sh", "-c", "openssl s_client -connect localhost:8080 -servername test -showcerts </dev/null 2>/dev/null | openssl x509 -inform pem -noout -text | grep -q 'test'"};5 Assert.assertArrayEquals(expectedCommand, command);6 }7}8package org.testcontainers.junit.wait.strategy;9import org.junit.Assert;10import org.junit.Test;11public class HttpWaitStrategyTest {12 public void createHttpShellCommand() {13 HttpWaitStrategy httpWaitStrategy = new HttpWaitStrategy();14 String[] command = httpWaitStrategy.createHttpShellCommand(8080, "test");15 Assert.assertArrayEquals(expectedCommand, command);16 }17}18package org.testcontainers.junit.wait.strategy;19import org.junit.Assert;20import org.junit.Test;21public class HttpsWaitStrategyTest {22 public void createHttpsShellCommand() {23 HttpsWaitStrategy httpsWaitStrategy = new HttpsWaitStrategy();24 String[] command = httpsWaitStrategy.createHttpsShellCommand(8080, "test");25 Assert.assertArrayEquals(expectedCommand, command);26 }27}28package org.testcontainers.junit.wait.strategy;29import org.junit.Assert;30import org.junit.Test;31public class HttpWaitStrategyTest {
createHttpsShellCommand
Using AI Code Generation
1public class HttpsWaitStrategyTest {2 public void testCreateShellCommandWithUrl() {3 HttpWaitStrategy waitStrategy = new HttpWaitStrategy();4 String command = waitStrategy.createHttpsShellCommand(HTTPS_URL, 200);5 }6 public void testCreateShellCommandWithUrlAndPort() {7 HttpWaitStrategy waitStrategy = new HttpWaitStrategy();8 String command = waitStrategy.createHttpsShellCommand(HTTPS_URL_WITH_PORT, 200);9 }10 public void testCreateShellCommandWithUrlAndPath() {11 HttpWaitStrategy waitStrategy = new HttpWaitStrategy();12 String command = waitStrategy.createHttpsShellCommand(HTTPS_URL_WITH_PATH, 200);13 }14 public void testCreateShellCommandWithUrlAndPathAndPort() {15 HttpWaitStrategy waitStrategy = new HttpWaitStrategy();16 String command = waitStrategy.createHttpsShellCommand(HTTPS_URL_WITH_PATH_AND_PORT, 200);
createHttpsShellCommand
Using AI Code Generation
1private static String createHttpsShellCommand(final int port) {2 return String.format(3 "echo | openssl s_client -connect localhost:%d -servername localhost 2>&1 | grep 'Verification return code: 0 (ok)'",4 );5}6echo | openssl s_client -connect localhost:8443 -servername localhost 2>&1 | grep 'Verification return code: 0 (ok)'7final String command = createHttpsShellCommand(port);8final String output = container.execInContainer("/bin/sh", "-c", command).getStdout();9return output.contains("Verification return code: 0 (ok)");
createHttpsShellCommand
Using AI Code Generation
1 public void testCreateHttpsShellCommand() {2 HttpWaitStrategy strategy = new HttpWaitStrategy();3 }4}5The testCreateHttpsShellCommand() method of HttpWaitStrategyTest class is shown below:6public void testCreateHttpsShellCommand() {7 HttpWaitStrategy strategy = new HttpWaitStrategy();8}9The createHttpsShellCommand() method of HttpWaitStrategy class is shown below:10public String createHttpsShellCommand(String url) {11 return createHttpShellCommand(url);12}13The createHttpShellCommand() method of HttpWaitStrategy class is shown below:14public String createHttpShellCommand(String url) {15 return format("wget -q -O - '%s'", url);16}17The testCreateHttpsShellCommand() method of HttpWaitStrategyTest class is shown below:18public void testCreateHttpsShellCommand() {19 HttpWaitStrategy strategy = new HttpWaitStrategy();20}21The testCreateHttpsShellCommand() method of HttpWaitStrategyTest class creates a shell command to check for the http response. The testCreateHttpsShellCommand() method uses the createHttpsShellCommand() method of HttpWaitStrategy class to create a shell command to check for the
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!!