Karate Mock Server - match request body that contains a specific string in the request

Viewed 441

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 Answers

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('./***/***/**')
Related