如何在空手道模拟服务器模式下验证传入请求模式? [英] How to validate incoming request schema in karate mock server mode?
本文介绍了如何在空手道模拟服务器模式下验证传入请求模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试用空手道编写一个模拟API服务器,我想确保传入的请求与给定的模式(或this example中给出的空手道模糊表达式)匹配
以下是我正在尝试的内容,但会导致以下错误:
错误
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: 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
问题
是否有方法验证传入请求正文的架构?
编辑
根据Peter下面的回答,我尝试了此操作,但结果总是显示200 OK响应:
Scenario: pathMatches('/manufacturers') && methodIs('post')
* def schemaCheck = karate.match('request == schema')
* eval
"""
if (schemaCheck.pass) {
responseStatus = 200;
response = request;
} else {
responseStatus = 400;
response = schemaCheck;
}
"""
推荐答案
不,这是错误的方法。
Scenario: pathMatches('/manufacturers') && methodIs('post')
* def result = karate.match('request == schema')
* if (!result.pass) responseStatus = 400
* def response = request
另请参阅:https://stackoverflow.com/a/50350442/143475
编辑:默认情况下,空手道鼓励JS的一个内衬,为方便起见,会检测到if
。
但您可以像这样委托给JS的多行:
* eval
"""
if (result.pass) {
responseStatus = 200;
} else {
responseStatus = 400;
}
"""
提示:您可以使用karate.abort()
提前退出。有很多选项,例如调用第二个功能文件。调用时,您可以使用变量切换文件。您可以使用JSswitch-case
。要有创意:)
这篇关于如何在空手道模拟服务器模式下验证传入请求模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文