I am trying to use regex for URI parameters in mountebank in java

3,302 views
Skip to first unread message

Shekhar Bhardwaj

unread,
Jan 12, 2016, 2:30:37 PM1/12/16
to mountebank-discuss
Hi, 


 I am newbie to mountebank.

 My code creates URI on the fly and uses accountID, customerUID etc. as parameters, since I am mocking my API's I don't care for any specific accountID or customerUID, I want my Imposter to return exact same response for all accountID or customerUID,

for example:- 

  String url = "https://127.0.0.1:9095/submit/custAccount/"+{accountRefId}+"/vist-cards";

now when I create below given imposter and provide the path, no matter what {accountRefId} is passed it should always fetch the same response.

Imposter expectedImposter = new Imposter();

expectedImposter = new ImposterBuilder().

onPort(6363).

stub().

response().

is().

header("Content-Type", "application/json").

statusCode(200).body(new File ("/workspace_luna/Mock/src/main/java/com/foocomp/mockers/service/MockResponse.json")).

end().

end().

predicate().

equals().method("GET").

end().

contains().

path("/submit/custAccount/ueyudhwiuey84387r8347r834fhjkehfjksfkjnjfy8934yr3478/vist-cards").

end().

deepEquals().

query("enhanced", "true").

end().

end().

end().

build();


Regards

Shekhar

Brandon Byars

unread,
Jan 15, 2016, 11:55:27 AM1/15/16
to Shekhar Bhardwaj, mountebank-discuss
Hi there,
The 'matches' predicate will do the trick.  The imposter setup will look something like this:

{
"protocol": "http",
"port": 6363,
"stubs": [{
"predicates": [{
"equals": {
"method": "GET",
"query": {
"enhanced": "true"
}
}
}, {
"matches": {
"path": "/submit/custAccount/[^/]+/vist-cards"
}
}],
"responses": [{
"is": {
"headers": {
"Content-Type": "application/json"
},
"statusCode": 200,
"body": "PUT YOUR BODY HERE"
}

}]
}]
}

--
You received this message because you are subscribed to the Google Groups "mountebank-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shekhar Bhardwaj

unread,
Jan 19, 2016, 11:25:06 AM1/19/16
to mountebank-discuss, shekh...@gmail.com
thanks Brandon, but the now the issue is due to regex in the path it matches for other url's too, it has become too generic. I've tried to be more specific and rearranging my stubs to avoid wrong condition satisfaction, but issue that persist now is that URI path using regex has become universal satisfactor.



Brandon Byars

unread,
Jan 19, 2016, 11:31:02 AM1/19/16
to Shekhar Bhardwaj, mountebank-discuss
Should be easy to fix.  Can you give me a few path examples - some you want matched and some you don't?

On Tue, Jan 19, 2016 at 10:25 AM, Shekhar Bhardwaj <shekh...@gmail.com> wrote:
thanks Brandon, but the now the issue is due to regex in the path it matches for other url's too, it has become too generic. I've tried to be more specific and rearranging my stubs to avoid wrong condition satisfaction, but issue that persist now is that URI path using regex has become universal satisfactor.



Shekhar Bhardwaj

unread,
Jan 19, 2016, 6:08:38 PM1/19/16
to mountebank-discuss, shekh...@gmail.com

For example -

  /custAccount/deb/{regex}/txn?return=true

  /custAccount/deb/diposit/vistCard  
 
  /custAccount/{regex}/detailedTxn/{regex}     etc.


I have more than 15 stubs with similar URI pattern, I have tried to re-arrange them in different positions but few of them are too similar and regex satisfy the condition before it reached to the next one.

I hope my example would make sense.

Regards
Shekhar


Brandon Byars

unread,
Jan 20, 2016, 10:10:53 AM1/20/16
to Shekhar Bhardwaj, mountebank-discuss
Can you send the actual regexes you're using?  It's possible that you're using an overly liberal regex (something like ".*") that would match, for example, both /custAccount/deb/1343/txn?return=true and /custAccount/deb/deposit/visitCard.  You'll want to constrain your regular expression to either the characters that can make up the ID (e.g. \w+), or ensure that it matches everything but a forward slash (e.g. [^/]+) to make sure it only matches the bit of the path you're interested in.
-Brandon

Shekhar Bhardwaj

unread,
Jan 20, 2016, 1:05:00 PM1/20/16
to mountebank-discuss, shekh...@gmail.com
thanks brandon, you were right I was using a liberal regex.
To unsubscribe from this group and stop receiving emails from it, send an email to mountebank-discuss+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages