bbyars / mountebank

Over the wire test doubles
http://www.mbtest.org
MIT License
2k stars 262 forks source link

Fix errors when creating a predicate with a request body containing a field of type number. #768

Open srmppn opened 3 months ago

srmppn commented 3 months ago

Description

I encountered an error when creating a predicate that matches the request body with type number using contains, startsWith, or endsWith. For example, when creating a predicate for the API /transfer-money with an amount that starts with 200:

predicates = {
    startsWith: { amount: 200 }
}

Sending a request with an amount of 200.01 results in the following error:

{
    "errors": [
        {
            "message": "actual.indexOf is not a function",
            "name": "TypeError",
            "stack": "TypeError: actual.indexOf is not a function\n    at ..."
        }
    ]
}

Screenshot 2567-05-17 at 19 37 17

Modifications

I have created three new methods (contains, startsWith, endsWith) that check whether the actual type is a string. If it is not, the value is converted to a string before calling the .indexOf method.

However, I'm considering instead of allow using contains, startsWith, endsWith with type number it might be better to throw an exception and provide more understandable error messages, such as:

Type not supported for this parameter {name}. Consider using equals or matches instead. 

Or maybe we could add features such as lessThan or greaterThan to support specific types of numbers. What do you think?

Details

Mountebank version 2.9.1