How to use httpInvokeOnce method of com.intuit.karate.core.ScenarioEngine class

Best Karate code snippet using com.intuit.karate.core.ScenarioEngine.httpInvokeOnce

Source:ScenarioEngine.java Github

copy

Full Screen

...571 public Response httpInvoke() {572 if (requestBuilder.isRetry()) {573 httpInvokeWithRetries();574 } else {575 httpInvokeOnce();576 }577 requestBuilder.reset();578 return response;579 }580 private void httpInvokeOnce() {581 Map<String, Map> cookies = getOrEvalAsMap(config.getCookies());582 if (cookies != null) {583 requestBuilder.cookies(cookies.values());584 }585 Map<String, Object> headers;586 if (config.getHeaders().isJsOrJavaFunction()) {587 headers = getOrEvalAsMap(config.getHeaders(), requestBuilder.build());588 } else {589 headers = getOrEvalAsMap(config.getHeaders()); // avoid an extra http request build590 }591 if (headers != null) {592 requestBuilder.headers(headers);593 }594 request = requestBuilder.build();595 String perfEventName = null; // acts as a flag to report perf if not null596 if (runtime.perfMode) {597 perfEventName = runtime.featureRuntime.perfHook.getPerfEventName(request, runtime);598 }599 long startTime = System.currentTimeMillis();600 request.setStartTimeMillis(startTime); // this may be fine-adjusted by actual http client601 if (hooks != null) {602 hooks.forEach(h -> h.beforeHttpCall(request, runtime));603 }604 try {605 response = requestBuilder.client.invoke(request);606 } catch (Exception e) {607 long endTime = System.currentTimeMillis();608 long responseTime = endTime - startTime;609 String message = "http call failed after " + responseTime + " milliseconds for url: " + request.getUrl();610 logger.error(e.getMessage() + ", " + message);611 if (perfEventName != null) {612 PerfEvent pe = new PerfEvent(startTime, endTime, perfEventName, 0);613 capturePerfEvent(pe); // failure flag and message should be set by logLastPerfEvent()614 }615 throw new KarateException(message, e);616 }617 if (hooks != null) {618 hooks.forEach(h -> h.afterHttpCall(request, response, runtime));619 }620 byte[] bytes = response.getBody();621 Object body;622 String responseType;623 ResourceType resourceType = response.getResourceType();624 if (resourceType != null && resourceType.isBinary()) {625 responseType = "binary";626 body = bytes;627 } else {628 try {629 body = JsValue.fromBytes(bytes, true, resourceType);630 } catch (Exception e) {631 body = FileUtils.toString(bytes);632 logger.warn("auto-conversion of response failed: {}", e.getMessage());633 }634 if (body instanceof Map || body instanceof List) {635 responseType = "json";636 } else if (body instanceof Node) {637 responseType = "xml";638 } else {639 responseType = "string";640 }641 }642 setVariable(RESPONSE_STATUS, response.getStatus());643 setVariable(RESPONSE, body);644 if (config.isLowerCaseResponseHeaders()) {645 setVariable(RESPONSE_HEADERS, response.getHeadersWithLowerCaseNames());646 } else {647 setVariable(RESPONSE_HEADERS, response.getHeaders());648 }649 setHiddenVariable(RESPONSE_BYTES, bytes);650 setHiddenVariable(RESPONSE_TYPE, responseType);651 cookies = response.getCookies();652 updateConfigCookies(cookies);653 setHiddenVariable(RESPONSE_COOKIES, cookies);654 startTime = request.getStartTimeMillis(); // in case it was re-adjusted by http client655 long endTime = request.getEndTimeMillis();656 setHiddenVariable(REQUEST_TIME_STAMP, startTime);657 setHiddenVariable(RESPONSE_TIME, endTime - startTime);658 if (perfEventName != null) {659 PerfEvent pe = new PerfEvent(startTime, endTime, perfEventName, response.getStatus());660 capturePerfEvent(pe);661 }662 }663 private void httpInvokeWithRetries() {664 int maxRetries = config.getRetryCount();665 int sleep = config.getRetryInterval();666 int retryCount = 0;667 while (true) {668 if (retryCount == maxRetries) {669 throw new KarateException("too many retry attempts: " + maxRetries);670 }671 if (retryCount > 0) {672 try {673 logger.debug("sleeping before retry #{}", retryCount);674 Thread.sleep(sleep);675 } catch (Exception e) {676 throw new RuntimeException(e);677 }678 }679 httpInvokeOnce();680 Variable v;681 try {682 v = evalKarateExpression(requestBuilder.getRetryUntil());683 } catch (Exception e) {684 logger.warn("retry condition evaluation failed: {}", e.getMessage());685 v = Variable.NULL;686 }687 if (v.isTrue()) {688 if (retryCount > 0) {689 logger.debug("retry condition satisfied");690 }691 break;692 } else {693 logger.debug("retry condition not satisfied: {}", requestBuilder.getRetryUntil());...

Full Screen

Full Screen

httpInvokeOnce

Using AI Code Generation

copy

Full Screen

1import com.intuit.karate.core.ScenarioEngine2import com.intuit.karate.http.HttpRequest3import com.intuit.karate.http.HttpResponse4import com.intuit.karate.http.HttpClient5import com.intuit.karate.http.HttpConfig6import java.util.concurrent.TimeUnit7def config = new HttpConfig()8def client = new HttpClient(config)9def engine = new ScenarioEngine(null, null, null, null)10def response = engine.httpInvokeOnce(request, client)11import com.intuit.karate.core.FeatureRuntime12import com.intuit.karate.http.HttpRequest13import com.intuit.karate.http.HttpResponse14import com.intuit.karate.http.HttpClient15import com.intuit.karate.http.HttpConfig16import java.util.concurrent.TimeUnit17def config = new HttpConfig()18def client = new HttpClient(config)

Full Screen

Full Screen

httpInvokeOnce

Using AI Code Generation

copy

Full Screen

1def httpInvokeOnce = { String method, String url, Object request, Map headers, Map params ->2 if (!headers) headers = [:]3 if (!params) params = [:]4 if (headers['Content-Type'] == 'application/json' && request) {5 request = request.toString()6 }7 if (headers['Content-Type'] == 'application/xml' && request) {8 request = request.toXmlString()9 }10 if (headers['Content-Type'] == 'application/x-www-form-urlencoded' && request) {11 request = request.toUrlEncodedString()12 }13 def engine = karate.getEngine()14 def response = engine.httpInvokeOnce(method, url, request, headers, params)15}16def httpInvoke = { String method, String url, Object request, Map headers, Map params ->17 if (!headers) headers = [:]18 if (!params) params = [:]19 if (headers['Content-Type'] == 'application/json' && request) {20 request = request.toString()21 }22 if (headers['Content-Type'] == 'application/xml' && request) {23 request = request.toXmlString()24 }25 if (headers['Content-Type'] == 'application/x-www-form-urlencoded' && request) {26 request = request.toUrlEncodedString()27 }28 def engine = karate.getEngine()29 def response = engine.httpInvoke(method, url, request, headers, params)30}31def httpInvokeStream = { String method, String url, Object request, Map headers, Map params ->32 if (!headers) headers = [:]33 if (!params) params = [:]34 if (headers['Content-Type'] == 'application/json' && request) {35 request = request.toString()36 }37 if (headers['Content-Type'] == 'application/xml' && request) {38 request = request.toXmlString()39 }40 if (headers['Content-Type'] == 'application/x-www-form-urlencoded' && request) {41 request = request.toUrlEncodedString()42 }43 def engine = karate.getEngine()44 def response = engine.httpInvokeStream(method, url, request, headers, params)

Full Screen

Full Screen

httpInvokeOnce

Using AI Code Generation

copy

Full Screen

1* def response = engine.httpInvokeOnce('get', httpUrl + '/posts/1')2* match response.json().id == 13* match response.json().title == "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"4* def response2 = engine.httpInvokeOnce('get', httpUrl2 + '/anything', { headers: { 'Content-Type': 'application/json' } })5* def response3 = engine.httpInvokeOnce('get', httpUrl + '/posts/1')6* match response3.json().id == 17* match response3.json().title == "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"8* def response4 = engine.httpInvokeOnce('get', httpUrl2 + '/anything', { headers: { 'Content-Type': 'application/json' } })9* def response5 = engine.httpInvokeOnce('get', httpUrl + '/posts/1')10* match response5.json().id == 111* match response5.json().title == "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"12* def response6 = engine.httpInvokeOnce('get', httpUrl2 + '/anything', { headers: { 'Content-Type': 'application/json' } })

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 Karate automation tests on LambdaTest cloud grid

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

Most used method in ScenarioEngine

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful