testingartillery

How to set the token in


How can I capture the token and pass the scenarios correctly? Currently he does not recognize and gives 401 in the tests

config:
    target: "http://localhost:3333"
    phases:
        - name: "Load Test Periodico"
          arrivalRate: 1
          duration: 5
before:
    flow:
        - post:
              url: "/sessions"
              json:
                  email: "email@email.com"
                  password: "senha123"
                  capture:
                      - json: "$token.token"
                        as: "token"
                  expect:
                      - statusCode: 200
scenarios:
    - name: "ProcedureController.index"
      flow:
          - get:
                url: "/procedures"
                headers:
                    Authorization: "Bearer {{ token }}"

How can I capture the token and pass the scenarios correctly?


Solution

  • The capture/expect are indented into the wrong block (they're inside the json block, rather than at the same level). Try this:

    before:
        flow:
            - post:
                  url: "/sessions"
                  json:
                      email: "email@email.com"
                      password: "senha123"
                  capture:
                      - json: "$.token"
                        as: "token"
                  expect:
                      - statusCode: 200
    

    Also, I'm assuming the response from /sessions you're expecting is {token: "sometoken"}, so I changed your capture from $token.token to $.token, which would be the correct way to capture that. Let me know if that's not the case