Mountebank predicates doesn't check headers - mountebank

I have below code and looks like it is not checking headers as a predicate.
{
"responses": [
{
"inject": "<%- stringify(filename, 'Scripts/MyDept/CutOffTime.ejs') %>"
}
],
"predicates": [
{
"matches": {
"method": "GET",
"path": "/cutoff-times",
"query": {
"country": "\\w+"
},
"headers": {
"X-CLIENT-ID": "^[ A-Za-z0-9]*$"
}
}
}
]
}
Strangely, when I pass # as the value to header X-CLIENT-ID it validate and shows the message as no predicate match. Because it is not part of the regex.

Identified the issue,
Basically if you need have multiple predicates need to merge them as below,(using and / or)
{
"responses": [
{
"inject": "<%- stringify(filename, 'Scripts/MyDept/CutOffTime.ejs') %>"
}
],
"predicates": [
{
"and": [
{
"exists": {
"headers": {
"X-CLIENT-ID": true,
}
}
},
{
"matches": {
"method": "GET",
"path": "/cutoff-times",
"headers": {
"X-CLIENT-ID": "^[ A-Za-z0-9]*$"
},
"query": {
"country": "\\w+"
}
}
}
]
}
]
}
Mountebank site
Further matches predicate doesn't check the existence (e.g. header existence)

Related

Unable to match predicate of path and value for grpc request

I am using the grpc plugin to create stubs for grpc requests. It works very well when matching the path only. However, I am facing issues to match both the path and the value. In particular, I am using the jsonpath to select the fields to match. Here is a sample o the request:
{
"path": "/service.DataProvider/ReadData",
"value": {
"identifiers": [
{
"unique_id": "unset",
"data_id": "point1",
"system_id": "mock"
}
],
"start_time": {
"seconds": "1650431509",
"nanos": 0
},
"end_time": {
"seconds": "1650431569",
"nanos": 0
}
}
}
and the predicates:
"predicates": [
{
"deepEquals": {
"path": "/service.DataProvider/ReadData"
}
},
{
"jsonpath": {
"selector": "$.identifiers[0].data_id"
},
"deepEquals": {
"value": "point1"
}
},
{
"jsonpath": {
"selector": "$.identifiers[0].system_id"
},
"deepEquals": {
"value": "mock"
}
}
]
Any idea of what is wrong on the predicates above? Any comments will be highly appreciated. Thanks!

Can mountebank be used to mock a GET octet-stream image file using stub and predicate?

When using a text file, this works like a regular GET
{
"responses": [
{
"is": {
"headers": {
"content-disposition": "attachment; filename=sample_text_file.txt"
},
"statusCode": 200,
"body": "<%- stringify(filename, 'templates/attachments/sample_text_file.txt') %>"
}
}
],
"predicates": [
{
"and": [
{
"equals": {
"method": "GET",
"path": "/resources/4df3dab6-003b-440d"
}
}
]
}
]
}
However, when an image file is used in place of text file, the Mountebank fails to start.

Is there way to pass path parameter in Mountbank

I was trying to pass path parameter using Mountebank.
Below is working, but there path is static no any parameters.
"predicates": [
{
"equals": {
"method": "GET",
"path": "/accounts",
"query": {
"permissionId": "xxx"
}
}
}
],
"responses": [
{
..... }
]
In case if I need to do GET /accounts/[account-no] where account-no is a parameter
Below regex worked, Please note use matches in case of regex instead of equal
"predicates": [
{
"matches": {
"method": "GET",
"path": "/accounts/\\d+",
"query": {
"permissionId": "xxx"
}
}
}
],
"responses": [
{
..... }
]

Use Mountebank copy behavior in multiple stubs

Is it possible to share a variable between Mountebank stubs?
Here's a high level example:
Stub A:
{
"predicates": [
{
"matches": {
"body": "amount=420"
}
}
],
"responses": [
{
"is": {
"statusCode": 200
},
"body": {
"transaction_id": "123456",
"amount": 420
},
"_behaviors": {
"copy": [{
"from": {"query": "transaction_id"},
"into": "${TRANSACTION1}",
"using": {
"method": "regex",
"selector": "(?<=transaction_id%5D=).{6}"
}
}]
}
Stub B:
{
"predicates": [
{
"matches": {
"body": "approved=420"
}
}
],
"responses": [
{
"is": {
"statusCode": 200
},
"body": {
"transaction_id": "${TRANSACTION1}",
"amount": 420
}
}
The copy _behavior approaches I have tried for Stub B do not seem to recognize the variable specified in Stub A. From the documentation, it seems as though I cannot use copy between these stubs.
As per the developer, this behavior isn't supported at this time. See: https://github.com/bbyars/mountebank/issues/476

Elastic Search: Matching fields in different nested objects

I am new to Elastic Search and this is my user index:
{
"user": {
"properties": {
"branches": {
"type": "nested"
},
"lists": {
"type": "nested"
},
"events": {
"type": "nested"
},
"optOuts": {
"type": "nested"
}
}
}
}
Here, branches, events and lists will contain the field id(int),countryIso(String)..
I need to find users having emails who belong to countryIso 'XX' for example.
{
"query": {
"bool": {
"must": [
{
"exists": {
"field": "email"
}
},
{
"match": {
"prog_id": 3
}
},
{
"nested": {
"path": [
"branches"
],
"query": {
"query_string": {
"fields": [
"branches.countryIso"
],
"query": "AE KW"
}
}
}
}
]
}
}
}
This way I can get them if they have that country in the branches object. What I want is that the countryIso is there in the branches or lists or events.
Note: any of these might be empty i.e. branches may not be there or lists miht not be there etc. Or lists might be there with no countryIso..
I tried this:
{
"query": {
"bool": {
"must": [
{
"exists": {
"field": "email"
}
},
{
"match": {
"prog_id": 3
}
},
{
"nested": {
"path": [
"branches"
],
"query": {
"query_string": {
"fields": [
"branches.countryIso"
],
"query": "AE KW"
}
}
}
},
{
"nested": {
"path": [
"lists"
],
"query": {
"query_string": {
"fields": [
"lists.countryIso"
],
"query": "AE KW"
}
}
}
}
]
}
}
}
AND
{
"query": {
"bool": {
"must": [
{
"exists": {
"field": "email"
}
},
{
"match": {
"prog_id": 3
}
},
{
"nested": {
"path": [
"branches",
"lists"
],
"query": {
"query_string": {
"fields": [
"branches.countryIso",
"lists.countryIso"
],
"query": "AE KW"
}
}
}
}
]
}
}
}
But neither works.

Resources