Saturday, 20 October 2018

If-else dynamic API mock responses

Question

How do I generate dynamic API mock/stub responses and use if + else if + else in GRPC?

Answer

You can start with multiple equals blocks one after another. For example:

{{#equal (jsonPath request.body '$.itemCode') '100'}}
    { countryCode: 'ES' }
{{/equal}}
{{#equal (jsonPath request.body '$.itemCode') '200'}}
    { countryCode: 'IC' }
{{/equal}}
{{#equal (jsonPath request.body '$.itemCode') '300'}}
    { countryCode: 'TW' }
{{/equal}}
{{#equal (jsonPath request.body '$.itemCode') '400'}}
    { countryCode: 'CH' }
{{/equal}}

If that does not satisfy your needs, you can do nested equals blocks.

{{#equal thing 'something'}}
   {{#equal 'one' 'two'}}
       Yes!
   {{else}}
       No!
   {{/equal}}
{{else}}
   Not a thing!
{{/equal}}

No comments:

Post a Comment