eykrehbein / strest

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

Variables don't resolve Fake() expressions #98

Open finestructure opened 5 years ago

finestructure commented 5 years ago

Describe the bug I'm trying to set up a variable with a random number in order to test PUT updates:

variables:
  note_update: Update Fake(random.number)

requests:

  update_contract:
    url: Var(apiUrl)/v1/contract/JsonPath{{contract_list[0].id}}
    method: PUT
    headers:
      Authorization: Bearer Value(userToken.access_token)
    data:
      json:
        notes: Var(note_update)
    validate:
      code: 204

  validate_update:
    url: Var(apiUrl)/v1/contract/JsonPath{{contract_list[0].id}}
    method: GET
    headers:
      Authorization: Bearer Value(userToken.access_token)
    validate:
      code: 200
      json:
        notes: Var(note_update)

While this passes the test, it doesn't work as intended, because the value actually saved is literally

Update Fake(random.number)

and not something like

Update 32078

To Reproduce Steps to reproduce the behavior

Expected behavior Var(note_update) should resolve to Update \d+

Additional context

I have alternatively tried to rewrite my test as follows:

requests:

  update_contract:
    url: Var(apiUrl)/v1/contract/JsonPath{{contract_list[0].id}}
    method: PUT
    headers:
      Authorization: Bearer Value(userToken.access_token)
    data:
      json:
        notes: Update Fake(random.number)
    validate:
      code: 204

  validate_update:
    url: Var(apiUrl)/v1/contract/JsonPath{{contract_list[0].id}}
    method: GET
    headers:
      Authorization: Bearer Value(userToken.access_token)
    validate:
      code: 200
      json:
        notes: Value(update_contract.notes)

but this fails, because update_contract is a 204 - No content and therefore Value(update_contract.notes) is not available.

I can't see a way to provide a random input but also save it for later cross checking.

jgroom33 commented 5 years ago

The workaround for this would be to use a service that returns the value:

Note: using stREST release 2.0 syntax

version: 1

requests:
  setValue:
    request:
      url: https://postman-echo.com/post
      method: POST
      postData:
        mimeType: application/json
        text: 
          value: <$ Faker("random.number") $>
    log: true
  useValue:
    request:
      url: https://postman-echo.com/post
      method: POST
      postData:
        mimeType: application/json
        text: 
          reused: <$ setValue.content.data.value $>
    log: true