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

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

Source:HttpWaitStrategy.java Github

copy

Full Screen

...126 try {127 final HttpURLConnection connection = (HttpURLConnection) new URL(uri).openConnection();128 // authenticate129 if (!Strings.isNullOrEmpty(username)) {130 connection.setRequestProperty(HEADER_AUTHORIZATION, buildAuthString(username, password));131 connection.setUseCaches(false);132 }133 connection.setRequestMethod("GET");134 connection.connect();135 if (statusCode != connection.getResponseCode()) {136 throw new RuntimeException(String.format("HTTP response code was: %s",137 connection.getResponseCode()));138 }139 if (responsePredicate != null) {140 String responseBody = getResponseBody(connection);141 if (!responsePredicate.test(responseBody)) {142 throw new RuntimeException(String.format("Response: %s did not match predicate",143 responseBody));144 }145 }146 } catch (IOException e) {147 throw new RuntimeException(e);148 }149 });150 return true;151 });152 } catch (TimeoutException e) {153 throw new ContainerLaunchException(String.format(154 "Timed out waiting for URL to be accessible (%s should return HTTP %s)", uri, statusCode));155 }156 }157 /**158 * Build the URI on which to check if the container is ready.159 *160 * @param livenessCheckPort the liveness port161 * @return the liveness URI162 */163 private URI buildLivenessUri(int livenessCheckPort) {164 final String scheme = (tlsEnabled ? "https" : "http") + "://";165 final String host = waitStrategyTarget.getContainerIpAddress();166 final String portSuffix;167 if ((tlsEnabled && 443 == livenessCheckPort) || (!tlsEnabled && 80 == livenessCheckPort)) {168 portSuffix = "";169 } else {170 portSuffix = ":" + String.valueOf(livenessCheckPort);171 }172 return URI.create(scheme + host + portSuffix + path);173 }174 /**175 * @param username the username176 * @param password the password177 * @return a basic authentication string for the given credentials178 */179 private String buildAuthString(String username, String password) {180 return AUTH_BASIC + BaseEncoding.base64().encode((username + ":" + password).getBytes(UTF_8));181 }182 private String getResponseBody(HttpURLConnection connection) throws IOException {183 BufferedReader reader = null;184 try {185 if (200 <= connection.getResponseCode() && connection.getResponseCode() <= 299) {186 reader = new BufferedReader(new InputStreamReader((connection.getInputStream()), UTF_8));187 } else {188 reader = new BufferedReader(new InputStreamReader((connection.getErrorStream()), UTF_8));189 }190 StringBuilder builder = new StringBuilder();191 String line;192 while ((line = reader.readLine()) != null) {193 builder.append(line);...

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