How to use HttpDeleteWithBody method of org.cerberus.service.rest.impl.RestService class

Best Cerberus-source code snippet using org.cerberus.service.rest.impl.RestService.HttpDeleteWithBody

Source:RestService.java Github

copy

Full Screen

...110 private static final String DEFAULT_PROXYAUTHENT_USER = "squid";111 private static final String DEFAULT_PROXYAUTHENT_PASSWORD = "squid";112 private static final Logger LOG = LogManager.getLogger(RestService.class);113 @NotThreadSafe114 class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {115 public static final String METHOD_NAME = "DELETE";116 public String getMethod() {117 return METHOD_NAME;118 }119 public HttpDeleteWithBody(final String uri) {120 super();121 setURI(URI.create(uri));122 }123 public HttpDeleteWithBody(final URI uri) {124 super();125 setURI(uri);126 }127 public HttpDeleteWithBody() {128 super();129 }130 }131 private AppService executeHTTPCall(CloseableHttpClient httpclient, HttpRequestBase httpget) throws Exception {132 try {133 // Create a custom response handler134 ResponseHandler<AppService> responseHandler = new ResponseHandler<AppService>() {135 @Override136 public AppService handleResponse(final HttpResponse response)137 throws ClientProtocolException, IOException {138 AppService myResponse = factoryAppService.create("", AppService.TYPE_REST,139 AppService.METHOD_HTTPGET, "", "", "", "", "", "", "", "", "", "", "", "", null, "", null, null);140 int responseCode = response.getStatusLine().getStatusCode();141 myResponse.setResponseHTTPCode(responseCode);142 myResponse.setResponseHTTPVersion(response.getProtocolVersion().toString());143 LOG.info(String.valueOf(responseCode) + " " + response.getProtocolVersion().toString());144 Header[] allHeaderList = response.getAllHeaders();145 for (Header header : allHeaderList) {146 myResponse.addResponseHeaderList(factoryAppServiceHeader.create(null, header.getName(),147 header.getValue(), "Y", 0, "", "", null, "", null));148 }149 HttpEntity entity = response.getEntity();150 myResponse.setResponseHTTPBody(entity != null ? EntityUtils.toString(entity) : null);151 return myResponse;152 }153 };154 return httpclient.execute(httpget, responseHandler);155 } catch (Exception ex) {156 LOG.error(ex.toString(), ex);157 throw ex;158 } finally {159 httpclient.close();160 }161 }162 @Override163 public AnswerItem<AppService> callREST(String servicePath, String requestString, String method,164 List<AppServiceHeader> headerList, List<AppServiceContent> contentList, String token, int timeOutMs,165 String system, TestCaseExecution tcexecution) {166 AnswerItem<AppService> result = new AnswerItem<>();167 AppService serviceREST = factoryAppService.create("", AppService.TYPE_REST, method, "", "", "", "", "", "", "", "", "", "", "",168 "", null, "", null, null);169 serviceREST.setProxy(false);170 serviceREST.setProxyHost(null);171 serviceREST.setProxyPort(0);172 serviceREST.setProxyWithCredential(false);173 serviceREST.setProxyUser(null);174 serviceREST.setTimeoutms(timeOutMs);175 MessageEvent message = null;176 if (StringUtil.isNullOrEmpty(servicePath)) {177 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSERVICE_SERVICEPATHMISSING);178 result.setResultMessage(message);179 return result;180 }181 if (StringUtil.isNullOrEmpty(method)) {182 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSERVICE_METHODMISSING);183 result.setResultMessage(message);184 return result;185 }186 // If token is defined, we add 'cerberus-token' on the http header.187 if (!StringUtil.isNullOrEmpty(token)) {188 headerList.add(factoryAppServiceHeader.create(null, "cerberus-token", token, "Y", 0, "", "", null, "", null));189 }190 CloseableHttpClient httpclient = null;191 HttpClientBuilder httpclientBuilder;192 if (proxyService.useProxy(servicePath, system)) {193 String proxyHost = parameterService.getParameterStringByKey("cerberus_proxy_host", system, DEFAULT_PROXY_HOST);194 int proxyPort = parameterService.getParameterIntegerByKey("cerberus_proxy_port", system, DEFAULT_PROXY_PORT);195 serviceREST.setProxy(true);196 serviceREST.setProxyHost(proxyHost);197 serviceREST.setProxyPort(proxyPort);198 HttpHost proxyHostObject = new HttpHost(proxyHost, proxyPort);199 if (parameterService.getParameterBooleanByKey("cerberus_proxyauthentification_active", system,200 DEFAULT_PROXYAUTHENT_ACTIVATE)) {201 String proxyUser = parameterService.getParameterStringByKey("cerberus_proxyauthentification_user", system, DEFAULT_PROXYAUTHENT_USER);202 String proxyPassword = parameterService.getParameterStringByKey("cerberus_proxyauthentification_password", system, DEFAULT_PROXYAUTHENT_PASSWORD);203 serviceREST.setProxyWithCredential(true);204 serviceREST.setProxyUser(proxyUser);205 CredentialsProvider credsProvider = new BasicCredentialsProvider();206 credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword));207 LOG.debug("Activating Proxy With Authentification.");208 httpclientBuilder = HttpClientBuilder.create().setProxy(proxyHostObject)209 .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())210 .setDefaultCredentialsProvider(credsProvider);211 } else {212 LOG.debug("Activating Proxy (No Authentification).");213 httpclientBuilder = HttpClientBuilder.create().setProxy(proxyHostObject);214 }215 } else {216 httpclientBuilder = HttpClientBuilder.create();217 }218 // if it is an GUI REST, share the GUI context with api call219 if ((tcexecution != null) && (tcexecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_GUI))) {220 WebDriver driver = tcexecution.getSession().getDriver();221 BasicCookieStore cookieStore = new BasicCookieStore();222 driver.manage().getCookies().forEach(cookieSelenium -> {223 BasicClientCookie cookie = new BasicClientCookie(cookieSelenium.getName(), cookieSelenium.getValue());224 cookie.setDomain(cookieSelenium.getDomain());225 cookie.setPath(cookieSelenium.getPath());226 cookie.setExpiryDate(cookieSelenium.getExpiry());227 cookieStore.addCookie(cookie);228 });229 httpclientBuilder.setDefaultCookieStore(cookieStore);230 }231 try {232 boolean acceptUnsignedSsl = parameterService.getParameterBooleanByKey("cerberus_accept_unsigned_ssl_certificate", system, true);233 if (acceptUnsignedSsl) {234 LOG.debug("Trusting all SSL Certificates.");235 // authorize non valide certificat ssl236 SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy() {237 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {238 return true;239 }240 }).build();241 httpclientBuilder242 .setSSLContext(sslContext)243 .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);244 }245 httpclient = httpclientBuilder.build();246 RequestConfig requestConfig;247 // Timeout setup.248 requestConfig = RequestConfig.custom().setConnectTimeout(timeOutMs).setConnectionRequestTimeout(timeOutMs)249 .setSocketTimeout(timeOutMs).build();250 AppService responseHttp = null;251 switch (method) {252 case AppService.METHOD_HTTPGET:253 LOG.info("Start preparing the REST Call (GET). " + servicePath + " - " + requestString);254 // Adding query string from requestString255 servicePath = StringUtil.addQueryString(servicePath, requestString);256 // Adding query string from contentList257 String newRequestString = AppServiceService.convertContentListToQueryString(contentList);258 servicePath = StringUtil.addQueryString(servicePath, newRequestString);259 serviceREST.setServicePath(servicePath);260 HttpGet httpGet = new HttpGet(servicePath);261 // Timeout setup.262 httpGet.setConfig(requestConfig);263 // Header.264 if (headerList != null) {265 for (AppServiceHeader contentHeader : headerList) {266 httpGet.addHeader(contentHeader.getKey(), contentHeader.getValue());267 }268 }269 serviceREST.setHeaderList(headerList);270 // Saving the service before the call Just in case it goes wrong (ex : timeout).271 result.setItem(serviceREST);272 LOG.info("Executing request " + httpGet.getRequestLine());273 responseHttp = executeHTTPCall(httpclient, httpGet);274 if (responseHttp != null) {275 serviceREST.setResponseHTTPBody(responseHttp.getResponseHTTPBody());276 serviceREST.setResponseHTTPCode(responseHttp.getResponseHTTPCode());277 serviceREST.setResponseHTTPVersion(responseHttp.getResponseHTTPVersion());278 serviceREST.setResponseHeaderList(responseHttp.getResponseHeaderList());279 }280 break;281 case AppService.METHOD_HTTPPOST:282 LOG.info("Start preparing the REST Call (POST). " + servicePath);283 serviceREST.setServicePath(servicePath);284 HttpPost httpPost = new HttpPost(servicePath);285 // Timeout setup.286 httpPost.setConfig(requestConfig);287 // Content288 if (!(StringUtil.isNullOrEmpty(requestString))) {289 // If requestString is defined, we POST it.290 InputStream stream = new ByteArrayInputStream(requestString.getBytes(StandardCharsets.UTF_8));291 InputStreamEntity reqEntity = new InputStreamEntity(stream);292 reqEntity.setChunked(true);293 httpPost.setEntity(reqEntity);294 serviceREST.setServiceRequest(requestString);295 } else {296 // If requestString is not defined, we POST the list of key/value request.297 List<NameValuePair> nvps = new ArrayList<>();298 for (AppServiceContent contentVal : contentList) {299 nvps.add(new BasicNameValuePair(contentVal.getKey(), contentVal.getValue()));300 }301 httpPost.setEntity(new UrlEncodedFormEntity(nvps));302 serviceREST.setContentList(contentList);303 }304 // Header.305 for (AppServiceHeader contentHeader : headerList) {306 httpPost.addHeader(contentHeader.getKey(), contentHeader.getValue());307 }308 serviceREST.setHeaderList(headerList);309 // Saving the service before the call Just in case it goes wrong (ex : timeout).310 result.setItem(serviceREST);311 LOG.info("Executing request " + httpPost.getRequestLine());312 responseHttp = executeHTTPCall(httpclient, httpPost);313 if (responseHttp != null) {314 serviceREST.setResponseHTTPBody(responseHttp.getResponseHTTPBody());315 serviceREST.setResponseHTTPCode(responseHttp.getResponseHTTPCode());316 serviceREST.setResponseHTTPVersion(responseHttp.getResponseHTTPVersion());317 serviceREST.setResponseHeaderList(responseHttp.getResponseHeaderList());318 } else {319 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_CALLSERVICE);320 message.setDescription(message.getDescription().replace("%SERVICE%", servicePath));321 message.setDescription(message.getDescription().replace("%DESCRIPTION%",322 "Any issue was found when calling the service. Coud be a reached timeout during the call (."323 + timeOutMs + ")"));324 result.setResultMessage(message);325 return result;326 }327 break;328 case AppService.METHOD_HTTPDELETE:329 LOG.info("Start preparing the REST Call (DELETE). " + servicePath);330 serviceREST.setServicePath(servicePath);331 HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(servicePath);332 // Timeout setup.333 httpDelete.setConfig(requestConfig);334 // Content335 if (!(StringUtil.isNullOrEmpty(requestString))) {336 // If requestString is defined, we POST it.337 InputStream stream = new ByteArrayInputStream(requestString.getBytes(StandardCharsets.UTF_8));338 InputStreamEntity reqEntity = new InputStreamEntity(stream);339 reqEntity.setChunked(true);340 httpDelete.setEntity(reqEntity);341 serviceREST.setServiceRequest(requestString);342 } else {343 // If requestString is not defined, we POST the list of key/value request.344 List<NameValuePair> nvps = new ArrayList<>();345 for (AppServiceContent contentVal : contentList) {...

Full Screen

Full Screen

HttpDeleteWithBody

Using AI Code Generation

copy

Full Screen

1import groovy.json.JsonSlurper2import groovy.json.JsonOutput3import org.cerberus.service.rest.impl.RestService4import org.cerberus.service.rest.impl.HttpDeleteWithBody5import org.apache.http.impl.client.CloseableHttpClient6import org.apache.http.impl.client.HttpClients7import org.apache.http.impl.client.HttpClientBuilder8def httpClient = HttpClients.createDefault()9def delete = new HttpDeleteWithBody(url)10delete.setHeader("Content-Type", "application/json")11delete.setHeader("Accept", "application/json")12def request = new JsonSlurper().parseText("""{13}""")14delete.setEntity(request)15def httpResponse = httpClient.execute(delete)16def response = new JsonSlurper().parseText(httpResponse.getEntity().getContent())17import groovy.json.JsonSlurper18import org.cerberus.service.rest.impl.RestService19import org.cerberus.service.rest.impl.HttpDeleteWithBody20def request = """{21}"""22def response = new RestService().doHttpDeleteWithBody(url, request)23import groovy.json.JsonSlurper24import org.cerberus.service.rest.impl.RestService25import org.cerberus.service.rest.impl.HttpDeleteWithBody26def request = """{27}"""28def response = new RestService().doHttpDeleteWithBody(url, request, "application/json", "application/json")29import groovy.json.JsonSlurper30import org.cerberus.service.rest.impl.RestService31import org.cerberus.service.rest.impl.HttpDeleteWithBody

Full Screen

Full Screen

HttpDeleteWithBody

Using AI Code Generation

copy

Full Screen

1 import org.cerberus.service.rest.impl.RestService;2 import org.cerberus.service.rest.impl.HttpDeleteWithBody;3 import org.apache.http.client.methods.HttpDelete;4 import org.apache.http.client.methods.HttpRequestBase;5 import org.apache.http.entity.StringEntity;6 import org.apache.http.entity.ContentType;7 import java.io.IOException;8 public class RestService extends RestService {9 public HttpRequestBase executeDelete(String url, String body, String contentType) throws IOException {10 HttpDeleteWithBody request = new HttpDeleteWithBody(url);11 request.setEntity(new StringEntity(body, ContentType.create(contentType)));12 return request;13 }14 }15 import org.cerberus.service.rest.impl.RestService;16 import org.cerberus.service.rest.impl.HttpDeleteWithBody;17 import org.apache.http.client.methods.HttpDelete;18 import org.apache.http.client.methods.HttpRequestBase;19 import org.apache.http.entity.StringEntity;20 import org.apache.http.entity.ContentType;21 import java.io.IOException;22 public class RestService extends RestService {23 public HttpRequestBase executeDelete(String url, String body, String contentType) throws IOException {24 HttpDeleteWithBody request = new HttpDeleteWithBody(url);25 request.setEntity(new StringEntity(body, ContentType.create(contentType)));26 return request;27 }28 }29 import org.cerberus.service.rest.impl.RestService;30 import org.cerberus.service.rest.impl.HttpDeleteWithBody;31 import org.apache.http.client.methods.HttpDelete;32 import org.apache.http.client.methods.HttpRequestBase;33 import org.apache.http.entity.StringEntity;34 import org.apache.http.entity.ContentType;35 import java.io.IOException;36 public class RestService extends RestService {37 public HttpRequestBase executeDelete(String url, String body, String contentType) throws IOException {38 HttpDeleteWithBody request = new HttpDeleteWithBody(url);39 request.setEntity(new StringEntity(body, ContentType.create(contentType)));40 return request;41 }42 }43 import org.cerberus.service.rest.impl.RestService;44 import org.cerberus.service.rest.impl.HttpDeleteWithBody

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 Cerberus-source 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