gstroup / apimocker

node.js module to run a simple http server for mock service responses.
MIT License
280 stars 81 forks source link

POST- Body regex support #2

Closed raDiesle closed 10 years ago

raDiesle commented 10 years ago

Sers,

a common use case for e.g. POST, PUT is to have e.g. json in the request body. Within the request, an ID is given, which I want to return a concrete response to.

A simple regex would be enough, I think.

How do you think about such an improvement?

gstroup commented 10 years ago

Hi, Sorry for the slow response. I think that sounds like a good improvement to make. Let me make sure I understand your request. So, for example, a JSON request would contain a field, like {"customerId": "1234"}. Then you'd like apimocker to read the value "1234", and return a specific response, like maybe a file named "1234.json"?? A request like {"customerId": "6789"} would return a different file? I think we could add that feature...

raDiesle commented 10 years ago

ya, thats is !

gstroup commented 10 years ago

OK, I finally got some time to make this update. There's a new "switch" feature in version 1.8, which allows you to return different responses, based on a request parameter. Let me know how it works for you!

dploeger commented 9 years ago

@gstroup I'd like to reopen this to support my question in #33. I think, this would be the easiest way.

The switch-value would be enclosed with "/" to indicate, that it's a regexp and contains one regexp-group, that switch operates on.

dploeger commented 9 years ago

As I just saw in the code, the Switch itself is used to build up the filename. That will result in various problems when using regexp (I wonder, how the jsonpath-support even worked this way. :) ) So another feature would be to (optionally) specify a "description" about the switch.

dploeger commented 9 years ago

Proposal: an optional switch object like this:

switches: [
  "customerId", 
  {
    "type": "regexp",
    "switch": "/testreg([^p]*)p/",
    "key": "testregexppart"
  },
  {
    "type": "jsonpath",
    "switch": "$.car.engine.part",
    "key": "enginepart"
  }
]

Would result in files like "customerId1234.mock.json", "testregexppartex.mock.json" and "enginepartTiming%20Belt.json"

raDiesle commented 9 years ago

ya