bbyars / mountebank

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

QUESTION: How to copy (behavior) integer value from path to body field? #719

Open VVasyaev opened 1 year ago

VVasyaev commented 1 year ago

Expected behaviour

{
    "id": 1,
    "word": "some"
}

Actual behaviour

{
    "id": "1",
    "word": "some"
}

Steps to reproduce

  1. POST /imposters

    {
    "port": 21130,
    "protocol": "http",
    "stubs": [
        {
            "predicates": [
                {
                    "matches": {
                        "method": "GET",
                        "path": "/files/\\d+/info"
                    }
                }
            ],
            "responses": [
                {
                    "is": {
                        "body": {
                            "id": "${prod_id}",
                            "word": "some"
                        }
                    },
                    "behaviors": [
                        {
                            "copy": {
                                "from": "path",
                                "into": "${prod_id}",
                                "using": {
                                    "method": "regex",
                                    "selector": "\\d+"
                                }
                            }
                        }
                    ]
                }
            ]
        }
    ]
    }
  2. Call mocked URL: GET http://localhost:21130/files/1/info

Software versions used

OS         : Ubuntu 22.04.1
mountebank : 2.8.1

Log contents in mb.log when running mb --loglevel debug

Not sure if log needed for this case.

I need to have an integer value in the response body but can't use ${prod_id} without quotes because it brokes json structure.

pineshmenat commented 8 months ago

Regex by default outputs all matches as string, if you want an integer you can add a custom injection script that would execute regex against path and parse output of it using parseInt('123')

You can read more about injection here