Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
VVasyaev opened this issue Jan 20, 2023 · 1 comment
Open

Comments

@VVasyaev
Copy link

VVasyaev commented Jan 20, 2023

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+"
                                }
                            }
                        }
                    ]
                }
            ]
        }
    ]
}
  1. 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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants