Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


Jmeter ThreadGroups Share response variables

12 Sep 2022 »

https://stackoverflow.com/questions/63349902/how-to-handle-refresh-token-in-jmeter

Requester

{
	"lala": "ok",
	"threadNum": "${__threadNum}_${__counter(TRUE,)}",
	"randomNum": "${__RandomString(50,abcdefghijklmnopqrstuvwxyz)}",
	"access_token": "${__P(access_token)}",
	"url": "${__P(url)}"
}

Header Manager

${__P(access_token)}

Requester Token

JSON Extractor

BeanShell Assertion

${__setProperty(access_token, ${access_token})}

Debug sampler

Backend

get_token.py

from flask import Flask, request, jsonify, json, abort, redirect, url_for, render_template
from flask_cors import CORS, cross_origin
import os
import random
import re
import subprocess
import traceback

app = Flask(__name__, template_folder='template')
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route('/', methods=['GET', 'POST'])
def main():

    payload = request.json

    print(">p", payload)

    ret = {
        "access_token": str(random.randint(10, 200))
    }

    print(ret)

    return jsonify(ret)

    return "hello world"

# gunicorn --workers=2 'app:create_app()' --bind=0.0.0.0:<port>
def create_app():
    return app

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=9001)
    
    #test 
    #with app.test_client() as c:
    #    rs = c.get("/")
    #    print(rs.data)
>p None
{'access_token': '168'}
192.168.0.3 - - [13/Sep/2022 00:45:04] "POST / HTTP/1.1" 200 -
>p None
{'access_token': '158'}
192.168.0.3 - - [13/Sep/2022 00:45:09] "POST / HTTP/1.1" 200 -
>p None
{'access_token': '102'}
192.168.0.3 - - [13/Sep/2022 00:45:14] "POST / HTTP/1.1" 200 -
>p None
{'access_token': '197'}
192.168.0.3 - - [13/Sep/2022 00:45:19] "POST / HTTP/1.1" 200 -
>p None

client.py

from flask import Flask, request, jsonify, json, abort, redirect, url_for, render_template
from flask_cors import CORS, cross_origin
import os
import re
import subprocess
import traceback

app = Flask(__name__, template_folder='template')
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route('/', methods=['GET', 'POST'])
def main():

    payload = request.json

    print(">p", payload)

    print(">h", request.headers)

    return "hello world"

# gunicorn --workers=2 'app:create_app()' --bind=0.0.0.0:<port>
def create_app():
    return app

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=9000)
    
    #test 
    #with app.test_client() as c:
    #    rs = c.get("/")
    #    print(rs.data)
>p {'lala': 'ok', 'threadNum': '2_7', 'randomNum': 'cwctxdamynezmqeezqtggoehbwkndfyqdjcumlqgcmjmoxflfq', 'access_token': '71', 'url': '1'}
>h Connection: keep-alive
Content-Type: application/json
Haha: 71
Content-Length: 150
Host: lab001:9000
User-Agent: Apache-HttpClient/4.5.12 (Java/17.0.3.1)


192.168.0.3 - - [13/Sep/2022 00:44:34] "POST / HTTP/1.1" 200 -
>p {'lala': 'ok', 'threadNum': '1_7', 'randomNum': 'qlexteavrtavuytqdhkvdxbnuslxotbdcbfwryyncjfwcpigov', 'access_token': '71', 'url': '1'}
>h Connection: keep-alive
Content-Type: application/json
Haha: 71
Content-Length: 150
Host: lab001:9000
User-Agent: Apache-HttpClient/4.5.12 (Java/17.0.3.1)