1

I'm trying to write a mock API server in Karate and I want to make sure that the incoming request matches a given schema (or Karate fuzzy expression as given in this example)

Here's what I'm trying, but it's resulting in the following error:

Error

11:42:13.342 [armeria-common-worker-nio-2-9]  WARN  com.intuit.karate - scenario match evaluation failed at line 14: pathMatches('/manufacturers') && methodIs('post') && match request != schema - com.intuit.karate.KarateException: >>>> js failed:
01: pathMatches('/manufacturers') && methodIs('post') && match request != schema
<<<<
org.graalvm.polyglot.PolyglotException: SyntaxError: Unnamed:1:59 Expected ; but found request
pathMatches('/manufacturers') && methodIs('post') && match request != schema

Feature file

Feature: Manufacturer Mock API Server

Background:
  * configure cors = true
  * def schema = 
    """
      {
        id: "#number",
        name: "#string",
        status: "#string"
      }
    """
  * url 'http://localhost:8080/manufacturers'

Scenario: pathMatches('/manufacturers') && methodIs('post') && match request != schema
  * def responseStatus 400

Scenario: pathMatches('/manufacturers') && methodIs('post') && match request == schema
  * def response = request

Question

Is there any way to validate the schema of an incoming request body?

Edits

Based on Peter's answer below, I tried this, but this is ALWAYS resulting in a 200 OK response:

Scenario: pathMatches('/manufacturers') && methodIs('post')
  * def schemaCheck = karate.match('request == schema')
  * eval
  """
  if (schemaCheck.pass) {
    responseStatus = 200;
    response = request;
  } else {
    responseStatus = 400;
    response = schemaCheck;
  }
  """
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60

1 Answers1

1

No that's the wrong approach.

Scenario: pathMatches('/manufacturers') && methodIs('post')
* def result = karate.match('request == schema')
* if (!result.pass) responseStatus = 400
* def response = request

Also refer: https://stackoverflow.com/a/50350442/143475

EDIT: By default, Karate encourages "one liners" of JS, and the if is detected for convenience.

But you can delegate to multiple lines of JS like this:

* eval
"""
if (result.pass) {
  responseStatus = 200;
} else {
  responseStatus = 400;
}
"""

Tip: You can use karate.abort() to exit early. There are plenty of options, such as calling a second feature file. When calling you can use a variable to "switch" files. You can use a JS switch-case. Be creative :)

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Can this be a complete `if-then-else`, such as `if karate.match('request==schema') then { 400 with error response } else { 200 with success response }` – Saurabh Nanda May 04 '21 at 06:32
  • Also, I'm confused with the `karate.match('request == schema')` syntax. Will the following work: `def schemaCheck = match request == schema` and then I write an `if - then - else` based on `schemaCheck` ? – Saurabh Nanda May 04 '21 at 06:34
  • @SaurabhNanda PLEASE read the link and the docs, thanks. then ask a new question if still confused – Peter Thomas May 04 '21 at 06:37
  • I've edited my question with the multi-line JS snippet, which doesn't seem to be working. – Saurabh Nanda May 04 '21 at 06:45
  • @SaurabhNanda great. I give up. follow this process please: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue – Peter Thomas May 04 '21 at 06:46
  • Are you saying I've found a bug? Or is this obviously documented? I'm new to Karate, so a deep link to the appropriate section in the docs would be appreciated. – Saurabh Nanda May 04 '21 at 06:48
  • @SaurabhNanda the `FAQ` at the above link will answer your question. – Peter Thomas May 04 '21 at 07:27