From 8e379ee43892d0806ddf50ef196a5daf7510a864 Mon Sep 17 00:00:00 2001 From: Tal Goldrat Date: Mon, 26 Oct 2020 11:46:24 +0200 Subject: [PATCH 1/4] Fixed proxy with credentials --- .../ApplitoolsTestResultsHandler.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/ApplitoolsTestResultHandler/ApplitoolsTestResultsHandler.java b/src/main/java/ApplitoolsTestResultHandler/ApplitoolsTestResultsHandler.java index 580c7cd..772faa3 100644 --- a/src/main/java/ApplitoolsTestResultHandler/ApplitoolsTestResultsHandler.java +++ b/src/main/java/ApplitoolsTestResultHandler/ApplitoolsTestResultsHandler.java @@ -92,6 +92,7 @@ private String preparePath(String Path) { public ApplitoolsTestResultsHandler(TestResults testResults, String viewKey, String proxyServer, String proxyPort, String proxyUser, String proxyPassword) throws Exception { if ((proxyServer != null) && (proxyPort != null)) { + proxy = new HttpHost(proxyServer, Integer.parseInt(proxyPort)); if ((proxyPassword != null) && (proxyUser != null)) { Credentials credentials = new UsernamePasswordCredentials(proxyUser, proxyPassword); AuthScope authScope = new AuthScope(proxyServer, Integer.parseInt(proxyPort)); @@ -99,9 +100,6 @@ public ApplitoolsTestResultsHandler(TestResults testResults, String viewKey, Str credsProvider.setCredentials(authScope, credentials); } - else { - proxy = new HttpHost(proxyServer, Integer.parseInt(proxyPort)); - } } this.applitoolsViewKey = viewKey; this.testResults = testResults; @@ -264,13 +262,14 @@ private String[] calculateStepsNames() throws Exception { } private CloseableHttpClient getCloseableHttpClient() { - CloseableHttpClient client = null; - if (proxy != null) + CloseableHttpClient client; + if (proxy == null) { + client = HttpClientBuilder.create().build(); + } else if (credsProvider == null) { client = HttpClientBuilder.create().setProxy(proxy).build(); - else if (credsProvider != null) + } else { client = HttpClientBuilder.create().setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build(); - else - client = HttpClientBuilder.create().build(); + } return client; } From a3d9cc3067d80bd40cecba8e912bd9c6e5111782 Mon Sep 17 00:00:00 2001 From: Tal Goldrat Date: Mon, 26 Oct 2020 11:47:43 +0200 Subject: [PATCH 2/4] Updated git ignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index da4867f..7c45d64 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +.idea +*.iml \ No newline at end of file From a6580ac1c2deb7b3baf83d40a07cf7070f399b75 Mon Sep 17 00:00:00 2001 From: Tal Goldrat Date: Mon, 26 Oct 2020 11:48:49 +0200 Subject: [PATCH 3/4] Updated version --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7b44144..f825e59 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # ApplitoolsTestResultsHandler - Java -### v2.0.3 +### v2.0.4 The Applitools Test Results Handler extends the capabilities of TestResults with additional API calls. With these additional API calls you will be able to retrive additional details at the end of the test. From bc1042533500520acebe0abf4d2c7e00310c64f4 Mon Sep 17 00:00:00 2001 From: Tal Goldrat Date: Tue, 27 Oct 2020 11:20:28 +0200 Subject: [PATCH 4/4] Fixed proxy --- .../ApplitoolsTestResultsHandler.java | 49 +++++++++---------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/src/main/java/ApplitoolsTestResultHandler/ApplitoolsTestResultsHandler.java b/src/main/java/ApplitoolsTestResultHandler/ApplitoolsTestResultsHandler.java index 772faa3..cbb1204 100644 --- a/src/main/java/ApplitoolsTestResultHandler/ApplitoolsTestResultsHandler.java +++ b/src/main/java/ApplitoolsTestResultHandler/ApplitoolsTestResultsHandler.java @@ -261,18 +261,6 @@ private String[] calculateStepsNames() throws Exception { return StepsNames; } - private CloseableHttpClient getCloseableHttpClient() { - CloseableHttpClient client; - if (proxy == null) { - client = HttpClientBuilder.create().build(); - } else if (credsProvider == null) { - client = HttpClientBuilder.create().setProxy(proxy).build(); - } else { - client = HttpClientBuilder.create().setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build(); - } - return client; - } - private String readJsonStringFromUrl(String url) throws Exception { HttpsURLConnection.setDefaultSSLSocketFactory(new sun.security.ssl.SSLSocketFactoryImpl()); @@ -280,7 +268,7 @@ private String readJsonStringFromUrl(String url) throws Exception { HttpGet get = new HttpGet(url); CloseableHttpClient client = getCloseableHttpClient(); - response = runLongRequest(get); + response = runLongRequest(client, get); InputStream is = response.getEntity().getContent(); try { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); @@ -309,7 +297,7 @@ private String postJsonToURL(String url, String payload) throws Exception { CloseableHttpClient client = getCloseableHttpClient(); - response = runLongRequest(post); + response = runLongRequest(client, post); InputStream is = response.getEntity().getContent(); try { @@ -354,7 +342,7 @@ else if (type == "Diff") CloseableHttpResponse response = null; HttpGet get = new HttpGet(urls[i].toString()); CloseableHttpClient client = getCloseableHttpClient(); - response = runLongRequest(get); + response = runLongRequest(client, get); InputStream is = response.getEntity().getContent(); try { BufferedImage image = ImageIO.read(is); @@ -453,7 +441,7 @@ private void saveImagesInFolder(String path, String imageType, URL[] imageURLS) HttpGet get = new HttpGet(imageURLS[i].toString()); CloseableHttpClient client = getCloseableHttpClient(); - response = runLongRequest(get); + response = runLongRequest(client, get); InputStream is = response.getEntity().getContent(); try { BufferedImage bi = ImageIO.read(is); @@ -585,7 +573,7 @@ private String getSessionInfo(String sessionId, String batchId) throws IOExcepti CloseableHttpClient client = getCloseableHttpClient(); CloseableHttpResponse response = null; - response = runLongRequest(get); + response = runLongRequest(client, get); InputStream stream = response.getEntity().getContent(); try { @@ -829,17 +817,16 @@ public String getHostingApp() throws JSONException { return this.testData.getJSONObject("startInfo").getJSONObject("environment").optString("hostingApp"); } - public CloseableHttpResponse runLongRequest(HttpRequestBase apiCall) throws InterruptedException { + public CloseableHttpResponse runLongRequest(CloseableHttpClient client, HttpRequestBase apiCall) throws InterruptedException { HttpRequestBase requestBase = createHttpRequest(apiCall); - CloseableHttpResponse response = sendRequest(requestBase, 1, false); + CloseableHttpResponse response = sendRequest(client, requestBase, 1, false); return longRequestCheckStatus(response); } - public CloseableHttpResponse sendRequest(HttpRequestBase apiCall, int retry, boolean delayBeforeRetry) throws InterruptedException { + public CloseableHttpResponse sendRequest(CloseableHttpClient client, HttpRequestBase apiCall, int retry, boolean delayBeforeRetry) throws InterruptedException { counter += 1; String requestId = counter + "--" + UUID.randomUUID(); apiCall.addHeader("x-applitools-eyes-client-request-id", requestId); - CloseableHttpClient client = HttpClientBuilder.create().build(); CloseableHttpResponse response; try { @@ -852,9 +839,9 @@ public CloseableHttpResponse sendRequest(HttpRequestBase apiCall, int retry, boo if (retry > 0) { if (delayBeforeRetry) { Thread.sleep(RETRY_REQUEST_INTERVAL); - return sendRequest(apiCall, retry - 1, delayBeforeRetry); + return sendRequest(client, apiCall, retry - 1, delayBeforeRetry); } - return sendRequest(apiCall, retry - 1, delayBeforeRetry); + return sendRequest(client, apiCall, retry - 1, delayBeforeRetry); } throw new Error(errorMessage); } @@ -882,7 +869,7 @@ public CloseableHttpResponse longRequestCheckStatus(CloseableHttpResponse respon URI = responseReceived.getFirstHeader("Location").getValue() + "?apiKey=" + this.applitoolsViewKey; request = new HttpDelete(URI); request = createHttpRequest(request); - return sendRequest(request, 1, false); + return sendRequest(getCloseableHttpClient(), request, 1, false); case HttpStatus.SC_GONE: throw new Error("The server task is gone"); default: @@ -897,14 +884,24 @@ public CloseableHttpResponse longRequestLoop(HttpRequestBase options, int delay) System.out.println("Still running... Retrying in " + delay); Thread.sleep(delay); - CloseableHttpResponse response = sendRequest(options, 1, false); + CloseableHttpResponse response = sendRequest(getCloseableHttpClient(), options, 1, false); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { return longRequestLoop(options, delay); } return response; } - + private CloseableHttpClient getCloseableHttpClient() { + CloseableHttpClient client; + if (proxy == null) { + client = HttpClientBuilder.create().build(); + } else if (credsProvider == null) { + client = HttpClientBuilder.create().setProxy(proxy).build(); + } else { + client = HttpClientBuilder.create().setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build(); + } + return client; + } public HttpRequestBase createHttpRequest(HttpRequestBase apiCall) {