killgrave icon indicating copy to clipboard operation
killgrave copied to clipboard

Allow variables in bodyFile path

Open eloo-abi opened this issue 1 year ago • 4 comments

Hi,

i would be great if we could allow variables in bodyFile path so it would be possible to have different responses for different path variables without creating a lot of imposters.

A possible config could look like this:

[
  {
    "request": {
      "method": "GET",
      "endpoint": "/orders/{order_id:.*}"
    },
    "response": {
      "status": 200,
      "headers": {
        "Content-Type": "application/json"
      },
      "bodyFile": "../bodies/order_{order_id}.json"
    }
  }
]

Best regards

eloo-abi avatar Aug 19 '24 08:08 eloo-abi

Hey @eloo-abi, thanks for the feature requests (and your contribution!) 🚀

However, I think that before moving forward with this development, would be nice to define better the expected behavior under different conditions, and reach consensus. For instance, what should happen when:

  • There's a syntax error (reference to a variable that isn't defined)
  • There's a variable with multiple values

Thanks!

joanlopez avatar Aug 21 '24 08:08 joanlopez

Hi,

can you give an example what a variable with multiple values is? I really don't get what you mean here.

There's a syntax error (reference to a variable that isn't defined) Then it will behave like the current implementation if the bodyFile could not be found.

The implementation here is super simple and just adjusts the bodyFile path. If the path is not valid it will exactly behave as if someone has entered a wrong path manually.

eloo-abi avatar Aug 21 '24 09:08 eloo-abi

Sorry, by looking at the PR, I got confused between mux methods, and I was thinking about query params (see Vars() vs Query() 😓 ), that's why I thought about cases where a variable can have multiple values (for instance, like ?id=5&id=3 or ?id[]=5&id[]=3). Apologies 🙏🏻

Please, update the PR with some tests, considering the edge cases I mentioned and I think we'll be able to merge it! Again, thanks for your contribution! 🙇🏻

joanlopez avatar Aug 21 '24 09:08 joanlopez

@joanlopez Is there any chance this can be merged? It's a pretty useful addition.

colins avatar Jan 30 '25 23:01 colins