GithubHelp home page GithubHelp logo

Comments (5)

bernardobridge avatar bernardobridge commented on August 17, 2024

Hi @bear-su ,

I just created a server that modelled that response, and I was able to get the capture working in the before section just fine. Are you sure your response is being returned correctly from the API? It might be worth trying to log the response from the API using an afterResponse hook

This is what I used:

const express = require('express');
const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken');

const app = express();
const port = 9099;

app.use(bodyParser.json());

app.post('/auth', (req, res) => {
  const adminData = {
    id: 7,
    adminId: "bears",
    name: "",
    introduction: "",
    profileImageUrl: "",
    adminRole: "",
    createdBy: 1,
    updatedBy: 1,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  };

  const token = jwt.sign(
    {
      admin_role: "abc",
      admin_id: "asdfasdf",
      name: "asfas",
      id: "7",
      exp: Math.floor(Date.now() / 1000) + (60 * 60) // 1 hour expiration
    },
    'your-secret-key',
    { algorithm: 'HS512' }
  );

  res.json({
    status: "OK",
    data: {
      admin: adminData,
      token: token
    },
    message: null
  });
});

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});
config:
  target: 'http://localhost:9099'
  phases:
    - duration: 5
      arrivalRate: 5

before:
  flow:
    - log: 'Get auth token'
    - post:
        url: '/auth'
        json:
          username: 'arto'
          password: 'localpassword'
        capture:
          - json: $.data.token
            as: token

scenarios:
  - flow:
      - log: "Token is {{ token }}"

from artillery.

bear-su avatar bear-su commented on August 17, 2024

@bernardobridge Hi ! I closely checked it again. It still shows the same error.

I remove capture keyword and tried running. It runs well and shows "Token is undefined. "

config:
  target: 'http://localhost:9099'
  phases:
    - duration: 5
      arrivalRate: 5

before:
  flow:
    - log: 'Get auth token'
    - post:
        url: '/auth'
        json:
          username: 'arto'
          password: 'localpassword'

scenarios:
  - flow:
      - log: "Token is {{ token }}"

from artillery.

bernardobridge avatar bernardobridge commented on August 17, 2024

Hello @bear-su ,

I still can't reproduce the error with the test server example I put above... It works fine in a before hook.

Perhaps you can inspect the response that's coming from the post and see if anything stands out as wrong. You can attach an afterResponse hook to check that:

config:
  target: 'http://localhost:9099'
  phases:
    - duration: 5
      arrivalRate: 5
  processor: ./helpers.js

before:
  flow:
    - log: 'Get auth token'
    - post:
        url: '/auth'
        json:
          username: 'arto'
          password: 'localpassword'
        afterResponse: logResponse

scenarios:
  - flow:
      - log: "Token is {{ token }}"

And create a ./helpers.js file:

function logResponse(req, res, context, ee, next) {
    console.log(`Response body is:`)
    console.log(res.body);
    console.log(`Status Code is: ${res.statusCode}`);

    next();
};

module.exports = {
    logResponse
}

from artillery.

bear-su avatar bear-su commented on August 17, 2024

@bernardobridge I tried it and share the result.

[.yml]

config:  
  target: 'http://localhost:9099' # 테스트 진행할 URL  phases:  
    - duration: 1  
      arrivalRate: 1  
  processor: helper.js  
  
before:  
  flow:  
    - post:  
        url: '/admins/login'  
        json:  
          username: 'arto'  
          password: 'localpassword'  
        capture:  
          - json: $.data.token  
            as: token  
  
scenarios:  
  - flow:  
      - post:  
          url: '/admin/articles/recommendations'  
          headers:  
            authorization: 'Bearer {{ token }}'  
                - post:
          url: '/admin/articles/recommendations'
          headers:
            authorization: 'Bearer {{ token }}'
          json:
            article:
              - {{ title }}
              - {{ content }}
              - {{ author }}
              - {{ domesticStockArticleType }}
          afterResponse: 'logResponse'

[helper.js]

function logResponse(req, res, context, ee, next) {  
    console.log("This is test")  
    console.log(`Response body is:`)  
    console.log(res.body);  
    console.log(`Status Code is: ${res.statusCode}`);  
  
    next();  
};  
  
module.exports = {  
    logResponse  
}

[console.log]
DEBUG=http artillery run test/domestic-stock-register-test.yml

Test run id: t7apy_z5bqjd9dpre6m4y77xjwkcxf8mkf8_64mj
⠋   http request: {
  "url": "http://localhost:9099/admins/login",
  "method": "POST",
  "headers": {
    "user-agent": "Artillery (https://artillery.io)"
  },
  "json": {
    "username": "arto",
    "password": "localpassword"
  }
} +0ms
  http captures and matches: +1ms
  http {} +0ms
  http { token: { value: undefined, strict: undefined, failed: true } } +1ms
/Users/arto/.nvm/versions/node/v22.2.0/lib/node_modules/artillery/node_modules/@artilleryio/int-core/lib/engine_http.js:677
                    return done(new Error('Failed capture or match'), context);
                                ^

Error: Failed capture or match
    at /Users/arto/.nvm/versions/node/v22.2.0/lib/node_modules/artillery/node_modules/@artilleryio/int-core/lib/engine_http.js:677:33
    at /Users/arto/.nvm/versions/node/v22.2.0/lib/node_modules/artillery/node_modules/async/dist/async.js:473:16
    at replenish (/Users/arto/.nvm/versions/node/v22.2.0/lib/node_modules/artillery/node_modules/async/dist/async.js:1009:25)
    at /Users/arto/.nvm/versions/node/v22.2.0/lib/node_modules/artillery/node_modules/async/dist/async.js:1019:9
    at eachLimit$1 (/Users/arto/.nvm/versions/node/v22.2.0/lib/node_modules/artillery/node_modules/async/dist/async.js:3199:24)
    at Object.<anonymous> (/Users/arto/.nvm/versions/node/v22.2.0/lib/node_modules/artillery/node_modules/async/dist/async.js:1049:16)
    at captured (/Users/arto/.nvm/versions/node/v22.2.0/lib/node_modules/artillery/node_modules/@artilleryio/int-core/lib/engine_http.js:632:21)
    at /Users/arto/.nvm/versions/node/v22.2.0/lib/node_modules/artillery/node_modules/@artilleryio/int-commons/engine_util.js:499:16
    at /Users/arto/.nvm/versions/node/v22.2.0/lib/node_modules/artillery/node_modules/async/dist/async.js:473:16
    at replenish (/Users/arto/.nvm/versions/node/v22.2.0/lib/node_modules/artillery/node_modules/async/dist/async.js:1009:25)
  • It doesn't seem logging my response or showing useful messages.

from artillery.

bear-su avatar bear-su commented on August 17, 2024

I dig into the problem and I found the problem. It doesn't catch it as an error since I use custom API Response which is this.

@Getter
@Builder
public final class APIResponse<T> {
    private final HttpStatus status;
    private final T data;
    private final String message;
}

So i added the debug log in engine_http.js

if (process.env.DEBUG) {
  debug("Captured: {}", resForCapture);
}

from artillery.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.