2

I have a request that is hitting my mock server... the request is in json, but one of the values is a string of about 2,000+ characters.. I am wanting to match the request if the string value (of 2,000+ characters) contains a specific substring value...

for example:

Scenario:
   pathMatches('/callService') &&
   methodIs('post') && request.clientDescription contains 'blue eyes'

(request.clientDescription = string of 2,000+ characters) It seems that it does not like the key word contains and I can't seem to find any information on the syntax I would use to search through a given string in a request and see if it contains a specific value.

I understand that I could try to match the entire string value using '==', but I am looking for a way to only match if it contains a substring.

2 Answers2

2

Here's a tip, whatever you see on the right of Scenario: is pure JavaScript, and methodIs() etc. happen to be pre-defined for your convenience.

So this should work, using String.includes()

Scenario: request.clientDescription.includes('blue eyes')

Also please refer this answer for other ideas: https://stackoverflow.com/a/57463815/143475

And one more: https://stackoverflow.com/a/63708918/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    cool. I am sure the possibilities are endless now that I understand that it "is pure JavaScript". I feel silly for not picking up on that...shows that I have not done much development in JavaScript. I was too caught up in the Cucumber/Gherkin syntax for the assertions on the response, but it all makes sense now. Appreciate the quick response. Thanks! – Jf_foster05 May 18 '21 at 16:15
1

It did not seem to like when I added "&& request.clientDescription.includes('blue eyes')" in the Scenario, but it did lead me in the right direction, and I did find a solution. thanks!

Error : after adding String.includes to Scenario:

com.intuit.karate - scenario match evaluation failed: evaluation (js) failed: pathMatches('/callService') && methodIs('post') && request.clientDescription.includes('blue eyes'), javax.script.ScriptException: TypeError: request.clientDescription.includes is not a function in at line number 2 stack trace: jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)

A Solution:

  • defined a function in the background using karate.match

Code Example of Solution:

Background:

* def isBlueEyed = function(){return karate.match("request.clientDescription contains 'Blue Eyes'").pass}

Scenario:

    pathMatches('/callService') &&
    methodIs('post') && isBlueEyed()
    * def response = read('./***/***/**')