eykrehbein / strest

⚡️ CI-ready tests for REST APIs configured in YAML
MIT License
1.74k stars 59 forks source link

Extract from array if #59

Closed jgroom33 closed 6 years ago

jgroom33 commented 6 years ago

Assume a request returns:

[ {"foo":1, "bar": 1},  {"foo":2, "bar": 2} ]

It would be beneficial to extract the value of key bar given a value for foo.

This is typically handled in code using loops or find functions.

jgroom33 commented 6 years ago

Potential implementations or workarounds (assuming that modifying the request URL is not possible) :

  1. Add a new yaml function: Eval()
  2. Add a node library like lodash, jq, or jinja and expose as a new yaml function: _() or Jq()
  3. Post process in cli (using jq or other) after log.file.true

Any other approaches?

jgroom33 commented 6 years ago

JSONPath seems to be a good option: https://github.com/dchester/jsonpath#jpvalueobj-pathexpression-newvalue

This works: $.phoneNumbers[?(@.type=="iPhone")].number using: http://jsonpath.com/

jgroom33 commented 6 years ago

Presumed implementation JsonPath():

version: 1

requests:
  todoOne:
    url: https://jsonplaceholder.typicode.com/posts
    method: POST
    data:
      json:
        myArray:
        - foo: 1
          bar: 1
        - foo: 2
          bar: winner winner chicken dinner
  todoTwo:
    url: https://jsonplaceholder.typicode.com/posts
    method: POST
    data:
      json:
       baz: JsonPath(todoOne.$.myArray[?(@.foo==2)].bar # evaluates to: "winner winner chicken dinner"