friendsofgo / killgrave

Simple way to generate mock servers written in Go
https://friendsofgo.github.io/killgrave/
MIT License
523 stars 100 forks source link

Allow variables in bodyFile path #175

Open eloo-abi opened 3 months ago

eloo-abi commented 3 months ago

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

joanlopez commented 3 months ago

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:

Thanks!

eloo-abi commented 3 months ago

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.

joanlopez commented 3 months ago

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! 🙇🏻