gstroup / apimocker

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

Switch statement not responding to multiple values for the same key #86

Open MarqueIV opened 6 years ago

MarqueIV commented 6 years ago

We have an endpoint that we're trying to mock. In short, there is a 'symbols' key which takes a comma-separated list of values. We have easily set up switching for single values. However, when specifying multiple, comma-separated values, we can't seem to ever trigger a match.

Here's our config (note I manually deleted a lot of stuff here so this may have a typo, etc., but shows the issue.)

We're trying to get it to respond to 'getQuotes?symbols=AAPL,MSFT' The third section under 'switchResponses' is what I'm not sure how to properly set/configure.

"getQuotes": {
    "switch": [
        "symbols"
    ],
    "verbs": [
        "get"
    ],
    "responses": {
        "get": {
            "mockFile": "/Quotes_Get_Stock_ALL.json"
        }
    },
    "switchResponses": {
        "symbolsAAPL": {
            "mockFile": "/Quotes_Get_Stock_AAPL.json"
        },
        "symbolsMSFT": {
            "mockFile": "Quotes_Get_Stock_MSFT.json"
        },
        "symbolsAAPL,MSFT": {
            "mockFile": "Quotes_Get_Stock_AAPL-MSFT.json"
        }
    }
}

A couple of additional questions.

  1. If an endpoint has multiple verbs, do you add those under the 'switchResponses' section like you do elsewhere? Above, it's configured for a 'GET", but 'GET' isn't referenced in the 'switchResponses' section and this endpoint actually also responds to a POST which we would want different responses for (based on the parameters.)

  2. If we are using a two-parameter switch (not to be confused with the two-values-on-one-parameter mentioned above), say 'symbols' and 'accounts', currently you write the switch as 'symbolsAAPLaccounts1234' or 'symbolsMSFTaccounts5678'. Is it possible to set a catch-all for just one of the parameters (e.g. for 'accounts1234' regardless of the value of symbol?)

  3. Is it possible to use the values as part of the filename itself? (i.e. for 'symbolsAAPL' I want to return 'responseAAPL.json', etc. without having to explicitly map it. This allows us to bulk-generate the responses without having to configure each one manually.)

  4. I know you also have the ability to specify one large JSON file, then based on parameters, return either all items in a JSON array, or a single item/entity from it. However, can you get even the single case to return as a one-element array instead of just the element by itself?

  5. When dealing with multiple parameters, does the order of the test call matter? In other words, are these the same? 5a Under the 'switchResponses' section: 'symbolsAAPLaccounts1234' and 'accounts1234symbolsAAPL' 5b In the URL itself: 'someUrl?symbols=MSFT&accounts=1234' and 'someUrl?accounts=1234&symbols=MSFT'

Thanks!

M