How to use paramExists method of com.intuit.karate.core.MockHandler class

Best Karate code snippet using com.intuit.karate.core.MockHandler.paramExists

Source:MockHandler.java Github

copy

Full Screen

...53 private static final String TYPE_CONTAINS = "typeContains";54 private static final String ACCEPT_CONTAINS = "acceptContains";55 private static final String HEADER_CONTAINS = "headerContains";56 private static final String PARAM_VALUE = "paramValue";57 private static final String PARAM_EXISTS = "paramExists";58 private static final String PATH_PARAMS = "pathParams";59 private static final String BODY_PATH = "bodyPath";60 private final LinkedHashMap<Feature, ScenarioRuntime> features = new LinkedHashMap<>(); // feature + holds global config and vars61 private final Map<String, Variable> globals = new HashMap<>();62 private boolean corsEnabled;63 protected static final ThreadLocal<Request> LOCAL_REQUEST = new ThreadLocal<>();64 private String prefix = "";65 public MockHandler withPrefix(String prefix) {66 this.prefix = prefix;67 return this;68 }69 public MockHandler(Feature feature) {70 this(feature, null);71 }72 public MockHandler(Feature feature, Map<String, Object> args) {73 this(Collections.singletonList(feature), args);74 }75 public MockHandler(List<Feature> features) {76 this(features, null);77 }78 public MockHandler(List<Feature> features, Map<String, Object> args) {79 for (Feature feature : features) {80 FeatureRuntime featureRuntime = FeatureRuntime.of(Suite.forTempUse(), feature, args);81 FeatureSection section = new FeatureSection();82 section.setIndex(-1); // TODO util for creating dummy scenario83 Scenario dummy = new Scenario(feature, section, -1);84 section.setScenario(dummy);85 ScenarioRuntime runtime = new ScenarioRuntime(featureRuntime, dummy);86 initRuntime(runtime);87 if (feature.isBackgroundPresent()) {88 // if we are within a scenario already e.g. karate.start(), preserve context89 ScenarioEngine prevEngine = ScenarioEngine.get();90 try {91 ScenarioEngine.set(runtime.engine);92 for (Step step : feature.getBackground().getSteps()) {93 Result result = StepRuntime.execute(step, runtime.actions);94 if (result.isFailed()) {95 String message = "mock-server background failed - " + feature + ":" + step.getLine();96 runtime.logger.error(message);97 throw new KarateException(message, result.getError());98 }99 }100 } finally {101 ScenarioEngine.set(prevEngine);102 }103 }104 corsEnabled = corsEnabled || runtime.engine.getConfig().isCorsEnabled();105 globals.putAll(runtime.engine.detachVariables());106 runtime.logger.info("mock server initialized: {}", feature);107 this.features.put(feature, runtime);108 }109 }110 private void initRuntime(ScenarioRuntime runtime) {111 runtime.engine.setVariable(PATH_MATCHES, (Function<String, Boolean>) this::pathMatches);112 runtime.engine.setVariable(PARAM_EXISTS, (Function<String, Boolean>) this::paramExists);113 runtime.engine.setVariable(PARAM_VALUE, (Function<String, String>) this::paramValue);114 runtime.engine.setVariable(METHOD_IS, (Function<String, Boolean>) this::methodIs);115 runtime.engine.setVariable(TYPE_CONTAINS, (Function<String, Boolean>) this::typeContains);116 runtime.engine.setVariable(ACCEPT_CONTAINS, (Function<String, Boolean>) this::acceptContains);117 runtime.engine.setVariable(HEADER_CONTAINS, (BiFunction<String, String, Boolean>) this::headerContains);118 runtime.engine.setVariable(BODY_PATH, (Function<String, Object>) this::bodyPath);119 runtime.engine.init();120 }121 private static final Result PASSED = Result.passed(0);122 private static final String ALLOWED_METHODS = "GET, HEAD, POST, PUT, DELETE, PATCH";123 @Override124 public synchronized Response handle(Request req) { // note the [synchronized]125 if (corsEnabled && "OPTIONS".equals(req.getMethod())) {126 Response response = new Response(200);127 response.setHeader("Allow", ALLOWED_METHODS);128 response.setHeader("Access-Control-Allow-Origin", "*");129 response.setHeader("Access-Control-Allow-Methods", ALLOWED_METHODS);130 List<String> requestHeaders = req.getHeaderValues("Access-Control-Request-Headers");131 if (requestHeaders != null) {132 response.setHeader("Access-Control-Allow-Headers", requestHeaders);133 }134 return response;135 }136 String path = req.getPath();137 if (!path.isEmpty()) {138 req.setPath(path.substring(prefix.length()));139 }140 for (Map.Entry<Feature, ScenarioRuntime> entry : this.features.entrySet()) {141 Feature feature = entry.getKey();142 ScenarioRuntime runtime = entry.getValue();143 // important for graal to work properly144 Thread.currentThread().setContextClassLoader(runtime.featureRuntime.suite.classLoader);145 LOCAL_REQUEST.set(req);146 req.processBody();147 ScenarioEngine engine = createScenarioEngine(req, runtime);148 Map<String, List<Map<String, Object>>> parts = req.getMultiParts();149 if (parts != null) {150 engine.setHiddenVariable(REQUEST_PARTS, parts);151 }152 for (FeatureSection fs : feature.getSections()) {153 if (fs.isOutline()) {154 runtime.logger.warn("skipping scenario outline - {}:{}", feature, fs.getScenarioOutline().getLine());155 break;156 }157 Scenario scenario = fs.getScenario();158 if (isMatchingScenario(scenario, engine)) {159 Map<String, Object> configureHeaders;160 Variable response, responseStatus, responseHeaders, responseDelay;161 ScenarioActions actions = new ScenarioActions(engine);162 Result result = PASSED;163 result = executeScenarioSteps(feature, runtime, scenario, actions, result);164 engine.mockAfterScenario();165 configureHeaders = engine.mockConfigureHeaders();166 response = engine.vars.remove(ScenarioEngine.RESPONSE);167 responseStatus = engine.vars.remove(ScenarioEngine.RESPONSE_STATUS);168 responseHeaders = engine.vars.remove(ScenarioEngine.RESPONSE_HEADERS);169 responseDelay = engine.vars.remove(RESPONSE_DELAY);170 globals.putAll(engine.detachVariables());171 Response res = new Response(200);172 if (result.isFailed()) {173 response = new Variable(result.getError().getMessage());174 responseStatus = new Variable(500);175 } else {176 if (corsEnabled) {177 res.setHeader("Access-Control-Allow-Origin", "*");178 }179 res.setHeaders(configureHeaders);180 if (responseHeaders != null && responseHeaders.isMap()) {181 res.setHeaders(responseHeaders.getValue());182 }183 if (responseDelay != null) {184 res.setDelay(responseDelay.getAsInt());185 }186 }187 if (response != null && !response.isNull()) {188 res.setBody(response.getAsByteArray());189 if (res.getContentType() == null) {190 ResourceType rt = ResourceType.fromObject(response.getValue());191 if (rt != null) {192 res.setContentType(rt.contentType);193 }194 }195 }196 if (responseStatus != null) {197 res.setStatus(responseStatus.getAsInt());198 }199 return res;200 }201 }202 }203 logger.warn("no scenarios matched, returning 404: {}", req); // NOTE: not logging with engine.logger204 return new Response(404);205 }206 private Result executeScenarioSteps(Feature feature, ScenarioRuntime runtime, Scenario scenario, ScenarioActions actions, Result result) {207 for (Step step : scenario.getSteps()) {208 result = StepRuntime.execute(step, actions);209 if (result.isAborted()) {210 runtime.logger.debug("abort at {}:{}", feature, step.getLine());211 break;212 }213 if (result.isFailed()) {214 String message = "server-side scenario failed, " + feature + ":" + step.getLine()215 + "\n" + step.toString() + "\n" + result.getError().getMessage();216 runtime.logger.error(message);217 break;218 }219 }220 return result;221 }222 private ScenarioEngine createScenarioEngine(Request req, ScenarioRuntime runtime) {223 ScenarioEngine engine = new ScenarioEngine(runtime, new HashMap<>(globals));224 ScenarioEngine.set(engine);225 engine.init();226 engine.setVariable(ScenarioEngine.REQUEST_URL_BASE, req.getUrlBase());227 engine.setVariable(ScenarioEngine.REQUEST_URI, req.getPath());228 engine.setVariable(ScenarioEngine.REQUEST_METHOD, req.getMethod());229 engine.setVariable(ScenarioEngine.REQUEST_HEADERS, req.getHeaders());230 engine.setVariable(ScenarioEngine.REQUEST, req.getBodyConverted());231 engine.setVariable(REQUEST_PARAMS, req.getParams());232 engine.setVariable(REQUEST_BYTES, req.getBody());233 return engine;234 }235 private boolean isMatchingScenario(Scenario scenario, ScenarioEngine engine) {236 String expression = StringUtils.trimToNull(scenario.getName() + scenario.getDescription());237 if (expression == null) {238 engine.logger.debug("default scenario matched at line: {}", scenario.getLine());239 return true;240 }241 try {242 Variable v = engine.evalJs(expression);243 if (v.isTrue()) {244 engine.logger.debug("scenario matched at line {}: {}", scenario.getLine(), expression);245 return true;246 } else {247 engine.logger.trace("scenario skipped at line {}: {}", scenario.getLine(), expression);248 return false;249 }250 } catch (Exception e) {251 engine.logger.warn("scenario match evaluation failed at line {}: {} - {}", scenario.getLine(), expression, e + "");252 return false;253 }254 }255 public boolean pathMatches(String pattern) {256 String uri = LOCAL_REQUEST.get().getPath();257 Map<String, String> pathParams = HttpUtils.parseUriPattern(pattern, uri);258 if (pathParams == null) {259 return false;260 } else {261 ScenarioEngine.get().setVariable(PATH_PARAMS, pathParams);262 return true;263 }264 }265 public boolean paramExists(String name) {266 Map<String, List<String>> params = LOCAL_REQUEST.get().getParams();267 return params != null && params.containsKey(name);268 }269 public String paramValue(String name) {270 return LOCAL_REQUEST.get().getParam(name);271 }272 public boolean methodIs(String name) { // TODO no more supporting array arg273 return LOCAL_REQUEST.get().getMethod().equalsIgnoreCase(name);274 }275 public boolean typeContains(String text) {276 String contentType = LOCAL_REQUEST.get().getContentType();277 return contentType != null && contentType.contains(text);278 }279 public boolean acceptContains(String text) {...

Full Screen

Full Screen

paramExists

Using AI Code Generation

copy

Full Screen

1#(paramExists('name'))2#(paramValue('name'))3#(paramValues('name'))4#(paramNames())5#(paramNamesAsList())6#(paramValuesAsList())7#(paramValuesAsMap())8#(headerExists('name'))9#(headerValue('name'))10#(headerValues('name'))11#(headerNames())12#(headerNamesAsList())13#(headerValuesAsList())14#(headerValuesAsMap())15#(body())16#(bodyAs(String.class))17#(bodyAsList())18#(bodyAsMap())19#(bodyAsPojo(String.class))

Full Screen

Full Screen

paramExists

Using AI Code Generation

copy

Full Screen

1* def mockHandler = new com.intuit.karate.core.MockHandler()2* def paramExists = mockHandler.paramExists('param1')3* def mockHandler = new com.intuit.karate.core.MockHandler()4* def paramExists = mockHandler.paramExists('param1')5* def mockHandler = new com.intuit.karate.core.MockHandler()6* def paramExists = mockHandler.paramExists('param1')7* def mockHandler = new com.intuit.karate.core.MockHandler()8* def paramExists = mockHandler.paramExists('param1')9* def mockHandler = new com.intuit.karate.core.MockHandler()10* def paramExists = mockHandler.paramExists('param1')11* def mockHandler = new com.intuit.karate.core.MockHandler()12* def paramExists = mockHandler.paramExists('param1')13* def mockHandler = new com.intuit.karate.core.MockHandler()14* def paramExists = mockHandler.paramExists('param1')15* def mockHandler = new com.intuit.karate.core.MockHandler()16* def paramExists = mockHandler.paramExists('param1')17* def mockHandler = new com.intuit.karate.core.MockHandler()18* def paramExists = mockHandler.paramExists('param1')19* def mockHandler = new com.intuit.karate.core.MockHandler()

Full Screen

Full Screen

paramExists

Using AI Code Generation

copy

Full Screen

1* def mockHandler = new com.intuit.karate.core.MockHandler()2* def paramExists = mockHandler.paramExists('param1')3* def paramExists = mockHandler.paramExists('param2')4* def mockHandler = new com.intuit.karate.core.MockHandler()5* def paramExists = mockHandler.paramExists('param1')6* def paramExists = mockHandler.paramExists('param2')7* def mockHandler = new com.intuit.karate.core.MockHandler()8* def paramExists = mockHandler.paramExists('param1')9* def paramExists = mockHandler.paramExists('param2')10* def mockHandler = new com.intuit.karate.core.MockHandler()11* def paramExists = mockHandler.paramExists('param1')12* def paramExists = mockHandler.paramExists('param2')13* def mockHandler = new com.intuit.karate.core.MockHandler()14* def paramExists = mockHandler.paramExists('param1')15* def paramExists = mockHandler.paramExists('param2')16* def mockHandler = new com.intuit.karate.core.MockHandler()17* def paramExists = mockHandler.paramExists('param1')18* def paramExists = mockHandler.paramExists('param2')19* def mockHandler = new com.intuit.karate.core.MockHandler()20* def paramExists = mockHandler.paramExists('param1')21* def paramExists = mockHandler.paramExists('param2')

Full Screen

Full Screen

paramExists

Using AI Code Generation

copy

Full Screen

1 def mockHandler = new com.intuit.karate.core.MockHandler()2 def request = read('classpath:com/intuit/karate/mock/request.json')3 def paramExists = mockHandler.paramExists(params, 'param1')4 def mockHandler = new com.intuit.karate.core.MockHandler()5 def request = read('classpath:com/intuit/karate/mock/request.json')6 def paramExists = mockHandler.paramExists(params, 'param2')7 def mockHandler = new com.intuit.karate.core.MockHandler()8 def request = read('classpath:com/intuit/karate/mock/request.json')9 def paramExists = mockHandler.paramExists(params, 'param3')10 def mockHandler = new com.intuit.karate.core.MockHandler()11 def request = read('classpath:com/intuit/karate/mock/request.json')12 def paramExists = mockHandler.paramExists(params, 'param4')13 def mockHandler = new com.intuit.karate.core.MockHandler()14 def request = read('classpath:com/intuit/karate/mock/request.json')15 def paramExists = mockHandler.paramExists(params, 'param5')16 def mockHandler = new com.intuit.karate.core.MockHandler()17 def request = read('classpath:com/intuit/karate/mock/request.json')18 def paramExists = mockHandler.paramExists(params, 'param6')19 def mockHandler = new com.intuit.karate.core.MockHandler()20 def request = read('classpath:

Full Screen

Full Screen

paramExists

Using AI Code Generation

copy

Full Screen

1def mockHandler = new com.intuit.karate.core.MockHandler()2mockHandler.paramExists('paramName')3def mockHandler = new com.intuit.karate.core.MockHandler()4mockHandler.paramExists('paramName')5def mockHandler = new com.intuit.karate.core.MockHandler()6mockHandler.paramExists('paramName')7def mockHandler = new com.intuit.karate.core.MockHandler()8mockHandler.paramExists('paramName')9def mockHandler = new com.intuit.karate.core.MockHandler()10mockHandler.paramExists('paramName')11def mockHandler = new com.intuit.karate.core.MockHandler()12mockHandler.paramExists('paramName')13def mockHandler = new com.intuit.karate.core.MockHandler()14mockHandler.paramExists('paramName')15def mockHandler = new com.intuit.karate.core.MockHandler()16mockHandler.paramExists('paramName')17def mockHandler = new com.intuit.karate.core.MockHandler()18mockHandler.paramExists('paramName')19def mockHandler = new com.intuit.karate.core.MockHandler()20mockHandler.paramExists('paramName')

Full Screen

Full Screen

paramExists

Using AI Code Generation

copy

Full Screen

1* mockHandler.paramExists('firstName', 'lastName')2* def response = karate.call('classpath:com/intuit/karate/mock/paramExists.feature', {mockHandler: mockHandler})3* mockHandler.paramExists('firstName', 'lastName', 'custom message')4* def response = karate.call('classpath:com/intuit/karate/mock/paramExists.feature', {mockHandler: mockHandler})5* mockHandler.paramExists('firstName', 'lastName', 'custom message', 400)6* def response = karate.call('classpath:com/intuit/karate/mock/paramExists.feature', {mockHandler: mockHandler})7* mockHandler.paramExists('firstName', 'lastName', 'custom message', 400, 'text/plain')8* def response = karate.call('classpath:com/intuit/karate/mock/paramExists.feature', {mockHandler: mockHandler})9* mockHandler.paramExists('firstName', 'lastName', 'custom message', 400, 'text/plain', {'x-custom-header': 'value'})10* def response = karate.call('classpath:com/intuit/karate/mock/paramExists.feature', {mockHandler: mockHandler})

Full Screen

Full Screen

paramExists

Using AI Code Generation

copy

Full Screen

1def mock = MockHandler()2def paramExists = mock.paramExists('name')3def paramValue = mock.paramValue('name')4def getParam = mock.getParam('name')5def setParam = mock.setParam('name', 'value')6def getParams = mock.getParams()7def setParams = mock.setParams(['name1':'value1', 'name2':'value2'])8def getHeader = mock.getHeader('name')9def setHeader = mock.setHeader('name', 'value')10def getHeaders = mock.getHeaders()11def setHeaders = mock.setHeaders(['name1':'value1', 'name2':'value2'])12def getCookie = mock.getCookie('name')13def setCookie = mock.setCookie('name', 'value')14def getCookies = mock.getCookies()15def setCookies = mock.setCookies(['name1':'value1', 'name2':'value2'])16def getBody = mock.getBody()17def setBody = mock.setBody('value')18def getStatusCode = mock.getStatusCode()19def setStatusCode = mock.setStatusCode(200)20def getResponseTime = mock.getResponseTime()21def setResponseTime = mock.setResponseTime(100)22def getResponse = mock.getResponse()23def setResponse = mock.setResponse('value')24def getResponseBody = mock.getResponseBody()25def setResponseBody = mock.setResponseBody('value')26def getResponseHeaders = mock.getResponseHeaders()27def setResponseHeaders = mock.setResponseHeaders(['name1':'value1', 'name2':'value2'])28def getResponseCookies = mock.getResponseCookies()29def setResponseCookies = mock.setResponseCookies(['name1':'value1', 'name2':'value2'])30def getResponseStatusCode = mock.getResponseStatusCode()31def setResponseStatusCode = mock.setResponseStatusCode(200)32def getResponseTime = mock.getResponseTime()33def setResponseTime = mock.setResponseTime(100)34def getResponseContentType = mock.getResponseContentType()35def setResponseContentType = mock.setResponseContentType('application/json')36def getResponseContentEncoding = mock.getResponseContentEncoding()37def setResponseContentEncoding = mock.setResponseContentEncoding('utf-8')38def getResponseContentLength = mock.getResponseContentLength()39def setResponseContentLength = mock.setResponseContentLength(100)40def getResponseContent = mock.getResponseContent()41def setResponseContent = mock.setResponseContent('value')42def getResponseContentAsString = mock.getResponseContentAsString()43def setResponseContentAsString = mock.setResponseContentAsString('value')

Full Screen

Full Screen

paramExists

Using AI Code Generation

copy

Full Screen

1* def mockHandler = new com.intuit.karate.core.MockHandler()2* mockHandler.setMockData(mockData)3* mockHandler.setMockContext(mockContext)4* def paramExists = mockHandler.paramExists('someParam')5* def paramExists = mockHandler.paramExists('someParam2')6* def paramExists = mockHandler.paramExists('someParam3')7* def paramExists = mockHandler.paramExists('someParam3', 'someParam4')8[github.com](github.com/intuit/karate/blob/m...) 9#### [intuit/karate/blob/master/karate-core/src/test/java/mock/MockHandlerTest.java](github.com/intuit/karate/blob/m...)10 package mock;11 import com.intuit.karate.core.MockContext;12 import com.intuit.karate.core.MockData;13 import com.intuit.karate.core.MockHandler;14 import com.intuit.karate.core.MockServer;15 import com.intuit.karate.core.MockStep;16 import com.intuit.karate.core.MockStepResult;17 import com.intuit.karate.core.MockVariable;18 import com.intuit.karate.core.MockVariableType;19 import com.intuit.karate.core.MockVariableValue;20 import com.intuit.karate.core.MockVariableValueList;21 import com.intuit.karate.core.MockVariableValueMap;22 import com.intuit.karate.core.MockVariableValueString;23 import com.intuit.karate.core.MockVariableValueStringList;24 import com.intuit.karate.core.MockVariableValueStringMap;25 import com.intuit.karate.core.MockVariableValueStringMapList;26 import com.intuit.karate.core.MockVariableValueStringMapString;27 import com.intuit.karate.core.MockVariableValueStringMapStringList;28 import com.intuit.karate.core.MockVariableValueStringMapStringMap;29 import com.intuit.karate.core.MockVariableValueStringMapStringMapList;30 import com.intuit.karate.core.MockVariableValueStringMapStringMapString;31 import com.intuit.karate.core.MockVariableValueStringMapStringString;32 import com.intuit.karate.core.MockVariableValueStringString;33 import com.intuit.karate.core

Full Screen

Full Screen

paramExists

Using AI Code Generation

copy

Full Screen

1* def mockHandler = karate.call('classpath:com/intuit/karate/mock/mock-handler.feature').mockHandler2* def paramExists = mockHandler.paramExists('name')3* def name = mockHandler.paramExists('name', 'default')4* def mockHandler = karate.call('classpath:com/intuit/karate/mock/mock-handler.feature').mockHandler5* def paramExists = mockHandler.paramExists('name')6* def name = mockHandler.paramExists('name', 'default')7* def mockHandler = karate.call('classpath:com/intuit/karate/mock/mock-handler.feature').mockHandler8* def paramExists = mockHandler.paramExists('name')9* def name = mockHandler.paramExists('name', 'default')10* def mockHandler = karate.call('classpath:com/intuit/karate/mock/mock-handler.feature').mockHandler11* def paramExists = mockHandler.paramExists('name')12* def name = mockHandler.paramExists('name', 'default')

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful