5

I need to mock a rest service, the mock is already working on SoapUI. The problem is that I have created a mock with a URL like this /test/userA/.

Is there a way to create a mock that would also respond to a url /test/userB/? I was thinking something like /test/user?/ or test/user[A-Z]/. Is it possible with SoapUI?

albciff
  • 18,112
  • 4
  • 64
  • 89
Federico Nafria
  • 1,397
  • 14
  • 39

1 Answers1

4

I'm a bit late, but here it is a possible solution...

There is no way to configure this kind of path in SOAPUI mockService, however you can emulate your goal using the follow approach:

You can configure as url path only / this way all request to http://your.mock.host:port/ will be processed in mockService.

Then to check if the path is what you want /test/user[A-Z]/ and to respond differently based on the [A-Z] you can use the follow groovy script in for example onRequest script tab of your mockService or alternatively in Dispatch(SCRIPT) tab of your operation inside mockService:

// 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
...

This scripts behaves like:

For URL /wrong/url/

Output:

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

For URL /test/userA/

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

For URL /test/userW/

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

Hope this helps,

albciff
  • 18,112
  • 4
  • 64
  • 89