在模拟服务 URL 中使用通配符

Fed*_*ria 5 rest groovy mocking soapui

我需要模拟一个休息服务,该模拟已经在 SoapUI 上运行了。问题是我创建了一个带有这样的 URL 的模拟/test/userA/

有没有办法创建一个也能响应 url 的模拟/test/userB/?我在想类似/test/user?/或 的东西test/user[A-Z]/。用 SoapUI 可以吗?

alb*_*iff 4

我有点晚了,但这是一个可能的解决方案......

无法在 SOAPUI mockService 中配置此类路径,但是您可以使用以下方法模拟您的目标:

您只能配置为 url 路径,/这样所有请求都http://your.mock.host:port/将在 mockService 中处理。

然后,要检查路径是否是您想要的/test/user[A-Z]/,并根据 [AZ] 进行不同的响应,您可以在onRequest script您的模拟服务的选项卡中使用以下 groovy 脚本,或者在Dispatch(SCRIPT)模拟服务内的操作选项卡中使用以下 groovy 脚本:

// the regex
def pathRegex = /^\/test\/user([A-Z])\//  

// get the request path
def path = mockRequest.getPath()

// check if the path is correct
def matcher = (path =~ pathRegex)  

// path is not correct 
// change assert to do your logic break... 
assert matcher.matches()

// path is correct get the exaclty user[A-Z] to do 
// your logic
def letter = matcher.group(1)
log.info letter
// do your logic depends on letter
...
Run Code Online (Sandbox Code Playgroud)

该脚本的行为如下:

对于网址/wrong/url/

输出:

assert matcher.matches()
           |       |
           |       false
           java.util.regex.Matcher[pattern=^/test/test([A-Z])/ region=0,10 lastmatch=]
Run Code Online (Sandbox Code Playgroud)

对于网址/test/userA/

Wed Nov 04 10:02:06 CET 2015:INFO:A

对于网址/test/userW/

Wed Nov 04 10:02:33 CET 2015:INFO:W

希望这可以帮助,