How to use getClient method of com.testsigma.util.HttpClient class

Best Testsigma code snippet using com.testsigma.util.HttpClient.getClient

Source:HttpClient.java Github

copy

Full Screen

...70 HttpClientUtils.closeQuietly(client);71 }72 public RestStepResponseDTO execute(RestStepRequest restStepRequest) {73 log.info("Making a request to " + restStepRequest.toString());74 CloseableHttpClient client = getClient();75 RestStepResponseDTO restStepResponseDTO = null;76 try {77 HttpRequest request;78 StringEntity payload = new StringEntity("");79 if (restStepRequest.getPayload() != null) {80 payload = prepareBody(restStepRequest.getPayload());81 }82 if (restStepRequest.getRequestMethod().equals(HttpRequestMethod.POST)) {83 request = new HttpPost(restStepRequest.getUrl());84 ((HttpPost) request).setEntity(payload);85 } else if (restStepRequest.getRequestMethod().equals(HttpRequestMethod.PATCH)) {86 request = new HttpPatch(restStepRequest.getUrl());87 ((HttpPatch) request).setEntity(payload);88 } else if (restStepRequest.getRequestMethod().equals(HttpRequestMethod.DELETE)) {89 request = new HttpDelete(restStepRequest.getUrl());90 } else if (restStepRequest.getRequestMethod().equals(HttpRequestMethod.PUT)) {91 request = new HttpPut(restStepRequest.getUrl());92 ((HttpPut) request).setEntity(payload);93 } else if (restStepRequest.getRequestMethod().equals(HttpRequestMethod.HEAD)) {94 request = new HttpHead(restStepRequest.getUrl());95 } else {96 request = new HttpGet(restStepRequest.getUrl());97 }98 if (restStepRequest.getAuthorizationType() != null) {99 setAuthorizationHeaders(restStepRequest);100 }101 if (restStepRequest.getRequestHeaders() != null) {102 for (String key : restStepRequest.getRequestHeaders().keySet()) {103 String value = restStepRequest.getRequestHeaders().get(key);104 request.setHeader(key, value);105 }106 }107 HttpUriRequest httpUriRequest = HttpRequestWrapper.wrap(request);108 if (restStepRequest.getFollowRedirects() != null && !restStepRequest.getFollowRedirects()) {109 client = HttpClients.custom().disableRedirectHandling().build();110 }111 HttpResponse httpResponse = client.execute(httpUriRequest);112 HttpEntity responseEntity = httpResponse.getEntity();113 String responseStr = (responseEntity != null) ? EntityUtils.toString(httpResponse.getEntity()) : null;114 restStepResponseDTO = new RestStepResponseDTO(115 httpResponse.getStatusLine().getStatusCode(),116 responseStr,117 httpResponse.getAllHeaders()118 );119 } catch (IOException e) {120 log.error(e.getMessage(), e);121 ;122 } finally {123 if (client != null) {124 closeConnection(client);125 }126 }127 return restStepResponseDTO;128 }129 private StringEntity prepareBody(Object data) throws IOException {130 if (data.getClass().getName().equals("java.lang.String")) {131 return new StringEntity(data.toString(), "UTF-8");132 }133 String json = new ObjectMapperService().convertToJson(data);134 log.info("Request Data: " + json.substring(0, Math.min(json.length(), 1000)));135 return new StringEntity(json, "UTF-8");136 }137 private StringEntity prepareFormBody(Map<String, String> params) throws IOException {138 StringBuilder result = new StringBuilder();139 boolean first = true;140 for (Map.Entry<String, String> entry : params.entrySet()) {141 if (first)142 first = false;143 else144 result.append("&");145 result.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8));146 result.append("=");147 result.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));148 }149 return new StringEntity(result.toString(), "UTF-8");150 }151 private void setAuthorizationHeaders(RestStepRequest entity) {152 if (entity.getAuthorizationType() != null) {153 Map<String, String> headers = entity.getRequestHeaders();154 headers = (headers != null) ? headers : new HashMap<>();155 Map<String, String> info = new ObjectMapperService().parseJson(entity.getAuthorizationValue(),156 new TypeReference<>() {157 });158 if (entity.getAuthorizationType().equals(1)) {159 headers.put("Authorization",160 "Basic " + getBasicAuthString(info.get("username") + ":" + info.get("password")));161 } else if (entity.getAuthorizationType() == 2) {162 headers.put("Authorization", "Bearer " + info.get("Bearertoken"));163 }164 entity.setRequestHeaders(headers);165 }166 }167 public <T> com.testsigma.util.HttpResponse<T> post(String url, Object data,168 TypeReference<T> typeReference)169 throws TestsigmaException {170 CloseableHttpClient client = getClient();171 try {172 log.info("Making a post request to " + url + " | with data - " + data.toString());173 HttpPost postRequest = new HttpPost(url);174 setHeaders(postRequest);175 postRequest.setEntity(prepareBody(data));176 HttpResponse res = client.execute(postRequest);177 return new com.testsigma.util.HttpResponse<T>(res, typeReference);178 } catch (IOException e) {179 throw new TestsigmaException(e);180 } finally {181 if (client != null) {182 closeConnection(client);183 }184 }185 }186 public <T> com.testsigma.util.HttpResponse<T> patch(String url, List<Header> httpHeaders, Object data,187 TypeReference<T> typeReference)188 throws TestsigmaException {189 CloseableHttpClient client = getClient();190 try {191 log.info("Making a patch request to " + url + " | with data - " + data.toString());192 HttpPatch patchRequest = new HttpPatch(url);193 Header[] itemsArray = new Header[httpHeaders.size()];194 itemsArray = httpHeaders.toArray(itemsArray);195 patchRequest.setHeaders(itemsArray);196 patchRequest.setEntity(prepareBody(data));197 HttpResponse res = client.execute(patchRequest);198 return new com.testsigma.util.HttpResponse<T>(res, typeReference);199 } catch (IOException e) {200 throw new TestsigmaException(e);201 } finally {202 if (client != null) {203 closeConnection(client);204 }205 }206 }207 public <T> com.testsigma.util.HttpResponse<T> postAndStoreCookies(String url, List<Header> httpHeaders, Object data,208 TypeReference<T> typeReference)209 throws TestsigmaException {210 CloseableHttpClient client = getClient();211 try {212 log.info("Making a post request to " + url + " | with data - " + data.toString());213 HttpPost postRequest = new HttpPost(url);214 Header[] itemsArray = new Header[httpHeaders.size()];215 itemsArray = httpHeaders.toArray(itemsArray);216 postRequest.setHeaders(itemsArray);217 postRequest.setEntity(prepareBody(data));218 HttpClientContext context = HttpClientContext.create();219 HttpResponse res = client.execute(postRequest, context);220 CookieStore cookieStore = context.getCookieStore();221 List<Cookie> cookies = cookieStore.getCookies();222 return new com.testsigma.util.HttpResponse<>(res, typeReference, cookies);223 } catch (IOException e) {224 throw new TestsigmaException(e);225 } finally {226 if (client != null) {227 closeConnection(client);228 }229 }230 }231 public <T> com.testsigma.util.HttpResponse<T> post(String url, List<Header> httpHeaders, Object data,232 TypeReference<T> typeReference)233 throws TestsigmaException {234 return this.post(url, httpHeaders, data, typeReference, null);235 }236 public <T> com.testsigma.util.HttpResponse<T> formPost(String url, List<Header> httpHeaders, HashMap<String, String> data,237 TypeReference<T> typeReference)238 throws TestsigmaException {239 CloseableHttpClient client = getClient();240 try {241 log.info("Making a post request to " + url + " | with data - " + data.toString());242 HttpPost postRequest = new HttpPost(url);243 Header[] itemsArray = new Header[httpHeaders.size()];244 itemsArray = httpHeaders.toArray(itemsArray);245 postRequest.setHeaders(itemsArray);246 postRequest.setEntity(prepareFormBody(data));247 HttpResponse res = client.execute(postRequest);248 return new com.testsigma.util.HttpResponse<T>(res, typeReference);249 } catch (IOException e) {250 throw new TestsigmaException(e);251 } finally {252 if (client != null) {253 closeConnection(client);254 }255 }256 }257 public <T> com.testsigma.util.HttpResponse<T> put(String url, List<Header> httpHeaders, Object data,258 TypeReference<T> typeReference)259 throws TestsigmaException {260 CloseableHttpClient client = getClient();261 try {262 log.info("Making a put request to " + url + " | with data - " + data.toString());263 HttpPut putRequest = new HttpPut(url);264 Header[] itemsArray = new Header[httpHeaders.size()];265 itemsArray = httpHeaders.toArray(itemsArray);266 putRequest.setHeaders(itemsArray);267 putRequest.setEntity(prepareBody(data));268 HttpResponse res = client.execute(putRequest);269 return new com.testsigma.util.HttpResponse<T>(res, typeReference);270 } catch (IOException e) {271 throw new TestsigmaException(e);272 } finally {273 if (client != null) {274 closeConnection(client);275 }276 }277 }278 public <T> com.testsigma.util.HttpResponse<T> get(List<Header> httpHeaders, URIBuilder builder,279 TypeReference<T> typeReference)280 throws TestsigmaException {281 CloseableHttpClient client = getClient();282 try {283 Header[] itemsArray = new Header[httpHeaders.size()];284 itemsArray = httpHeaders.toArray(itemsArray);285 HttpGet getRequest = new HttpGet(builder.build());286 getRequest.setHeaders(itemsArray);287 HttpResponse res = client.execute(getRequest);288 return new com.testsigma.util.HttpResponse<T>(res, typeReference);289 } catch (IOException | URISyntaxException e) {290 throw new TestsigmaException(e);291 } finally {292 if (client != null) {293 closeConnection(client);294 }295 }296 }297 public <T> com.testsigma.util.HttpResponse<T> get(String url, List<Header> httpHeaders,298 TypeReference<T> typeReference)299 throws TestsigmaException {300 return this.get(url, httpHeaders, typeReference, null);301 }302 public <T> com.testsigma.util.HttpResponse<T> delete(String url, List<Header> httpHeaders,303 TypeReference<T> typeReference)304 throws TestsigmaException {305 return this.delete(url, httpHeaders, typeReference, null);306 }307 public <T> com.testsigma.util.HttpResponse<T> delete(String url, List<Header> httpHeaders,308 TypeReference<T> typeReference, BasicCookieStore cookieStore)309 throws TestsigmaException {310 CloseableHttpClient client = getClient();311 try {312 log.info("Making a delete request to " + url + " | with headers - " + httpHeaders);313 HttpDelete delRequest = new HttpDelete(url);314 Header[] itemsArray = new Header[httpHeaders.size()];315 itemsArray = httpHeaders.toArray(itemsArray);316 delRequest.setHeaders(itemsArray);317 HttpResponse res;318 if (cookieStore != null) {319 HttpContext localContext = new BasicHttpContext();320 localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);321 res = client.execute(delRequest, localContext);322 } else323 res = client.execute(delRequest);324 return new com.testsigma.util.HttpResponse<T>(res, typeReference);325 } catch (IOException e) {326 throw new TestsigmaException(e);327 } finally {328 if (client != null) {329 closeConnection(client);330 }331 }332 }333 public <T> com.testsigma.util.HttpResponse<T> post(String url, List<Header> httpHeaders, Object data,334 TypeReference<T> typeReference, BasicCookieStore cookieStore)335 throws TestsigmaException {336 CloseableHttpClient client = getClient();337 try {338 log.info("Making a post request to " + url + " | with data - " + data.toString());339 HttpPost postRequest = new HttpPost(url);340 Header[] itemsArray = new Header[httpHeaders.size()];341 itemsArray = httpHeaders.toArray(itemsArray);342 postRequest.setHeaders(itemsArray);343 postRequest.setEntity(prepareBody(data));344 HttpResponse res;345 if (cookieStore != null) {346 HttpContext localContext = new BasicHttpContext();347 localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);348 res = client.execute(postRequest, localContext);349 } else350 res = client.execute(postRequest);351 return new com.testsigma.util.HttpResponse<T>(res, typeReference);352 } catch (IOException e) {353 throw new TestsigmaException(e);354 } finally {355 if (client != null) {356 closeConnection(client);357 }358 }359 }360 public <T> com.testsigma.util.HttpResponse<T> get(String url, List<Header> httpHeaders,361 TypeReference<T> typeReference, BasicCookieStore cookieStore)362 throws TestsigmaException {363 CloseableHttpClient client = getClient();364 try {365 log.info("Making a Get request to " + url + " | with headers - " + httpHeaders);366 HttpGet getRequest = new HttpGet(url);367 Header[] itemsArray = new Header[httpHeaders.size()];368 itemsArray = httpHeaders.toArray(itemsArray);369 getRequest.setHeaders(itemsArray);370 HttpResponse res;371 if (cookieStore != null) {372 HttpContext localContext = new BasicHttpContext();373 localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);374 res = client.execute(getRequest, localContext);375 } else376 res = client.execute(getRequest);377 return new com.testsigma.util.HttpResponse<T>(res, typeReference);378 } catch (IOException e) {379 throw new TestsigmaException(e);380 } finally {381 if (client != null) {382 closeConnection(client);383 }384 }385 }386 public <T> com.testsigma.util.HttpResponse<T> post(String url, List<Header> httpHeaders,387 String fileName, InputStream is,388 TypeReference<T> typeReference, BasicCookieStore cookieStore) throws TestsigmaException {389 log.info("Making a Post request to " + url + " | with file - " + fileName);390 CloseableHttpClient client = getClient();391 try {392 HttpEntity entity = MultipartEntityBuilder393 .create()394 .addBinaryBody("file", is, ContentType.create("application/octet-stream"), fileName)395 .build();396 HttpPost httpPost = new HttpPost(url);397 httpPost.setEntity(entity);398 Header[] itemsArray = new Header[httpHeaders.size()];399 itemsArray = httpHeaders.toArray(itemsArray);400 httpPost.setHeaders(itemsArray);401 if (cookieStore != null) {402 HttpContext localContext = new BasicHttpContext();403 localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);404 }405 HttpResponse res = client.execute(httpPost);406 return new com.testsigma.util.HttpResponse<T>(res, typeReference);407 } catch (IOException e) {408 throw new TestsigmaException(e);409 } finally {410 if (client != null) {411 closeConnection(client);412 }413 }414 }415 public HttpResponse downloadFile(String url, String filePath, Map<String, String> headers) throws IOException {416 log.info("Making a download file request to " + url);417 CloseableHttpClient client = getClient();418 try {419 HttpGet getRequest = new HttpGet(url);420 for (String key : headers.keySet()) {421 getRequest.addHeader(key, headers.get(key));422 }423 getRequest.setHeader("Accept", "*/*");424 HttpResponse res = client.execute(getRequest);425 Integer status = res.getStatusLine().getStatusCode();426 log.info("Download file request response code - " + status);427 if (status.equals(HttpStatus.OK.value())) {428 BufferedInputStream bis = new BufferedInputStream(res.getEntity().getContent());429 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));430 int inByte;431 while ((inByte = bis.read()) != -1) bos.write(inByte);432 bis.close();433 bos.close();434 }435 return res;436 } finally {437 if (client != null) {438 closeConnection(client);439 }440 }441 }442 private void setHeaders(HttpRequestBase getRequest) {443 if (this.jwtApiKey != null) {444 getRequest.setHeader(org.apache.http.HttpHeaders.AUTHORIZATION, "Bearer " + jwtApiKey);445 }446 getRequest.setHeader(org.apache.http.HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");447 }448 public synchronized CloseableHttpClient getClient() {449 RequestConfig config = RequestConfig.custom()450 .setCookieSpec(CookieSpecs.STANDARD)451 .setSocketTimeout(10 * 60 * 1000)452 .setConnectionRequestTimeout(60 * 1000)453 .setConnectTimeout(2 * 60 * 1000)454 .build();455 return HttpClients.custom().setDefaultRequestConfig(config).build();456 }457}...

Full Screen

Full Screen

getClient

Using AI Code Generation

copy

Full Screen

1org.apache.http.client.HttpClient client = com.testsigma.util.HttpClient.getClient();2org.apache.http.client.HttpClient client = com.testsigma.util.HttpClient.getClient("proxyHost", "proxyPort");3org.apache.http.client.HttpClient client = com.testsigma.util.HttpClient.getClient("proxyHost", "proxyPort", "proxyUser", "proxyPassword");4org.apache.http.client.HttpClient client = com.testsigma.util.HttpClient.getClient("proxyHost", "proxyPort", "proxyUser", "proxyPassword", "proxyDomain");5org.apache.http.client.HttpClient client = com.testsigma.util.HttpClient.getHttpClient();6org.apache.http.client.HttpClient client = com.testsigma.util.HttpClient.getHttpClient("proxyHost", "proxyPort");7org.apache.http.client.HttpClient client = com.testsigma.util.HttpClient.getHttpClient("proxyHost", "proxyPort", "proxyUser", "proxyPassword");8org.apache.http.client.HttpClient client = com.testsigma.util.HttpClient.getHttpClient("proxyHost", "proxyPort", "proxyUser", "proxyPassword", "proxyDomain");

Full Screen

Full Screen

getClient

Using AI Code Generation

copy

Full Screen

1HttpClient client = HttpClient.getClient();2HttpResponse response = client.get("/");3int statusCode = response.getStatusCode();4String statusMessage = response.getStatusMessage();5Map<String, String> headers = response.getHeaders();6String body = response.getBody();7Map<String, String> cookies = response.getCookies();8String responseString = response.getResponse();9JSONObject json = response.getJSON();10Document xml = response.getXML();11byte[] bytes = response.getBytes();12File file = response.getFile();13InputStream inputStream = response.getInputStream();14Reader reader = response.getReader();15JSONArray jsonArray = response.getJSONArray();16JSONArray jsonObjects = response.getJSONObjects();17JSONArray jsonValues = response.getJSONValues();18JSONArray jsonKeys = response.getJSONKeys();19JSONArray jsonNames = response.getJSONNames();20JSONArray jsonStrings = response.getJSONStrings();21JSONArray jsonNumbers = response.getJSONNumbers();22JSONArray jsonBooleans = response.getJSONBooleans();23JSONArray jsonNulls = response.getJSONNulls();24JSONArray jsonArrays = response.getJSONArrays();25JSONArray jsonObjects = response.getJSONObjects();

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.

Run Testsigma automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful