svanoort / pyresttest

Python Rest Testing
Apache License 2.0
1.15k stars 325 forks source link

How can I use template in headers. #225

Open sohamnavadiya opened 7 years ago

sohamnavadiya commented 7 years ago

I want to use token into header, but It should be dynamic means I am getting token in the response of login API and I am storing into variable. I want to use this token into header. How can I do this? I tried template but it doesn't work. see below my code.


- config:
    - testset: "Benchmark tests using test app"
    # Variables to use in the test set
    - variable_binds: { email: 'lucky@yopmail.com',
    email2: 'abc12@gmail.com',
    email3: 'pqr@gmail.com',
    email4: 'qwed@gmail.com',
    password: 'abc123',
    user_id: 123,
    token: 'Token 4cc1e782b6c50d91dcd269749a9c2faec2c296f',
    group_name: 'group_my_third'}

- test:
    - group: "UserLogin"
    - name: "User Login using email and password"
    - url: "api/v2/login/"
    - method: "POST"
    - body: {template: '{"email": "$email","password": "$password","device_os": "android", "device_token": "abcxyz"}'}
    - headers: {'Content-Type': 'application/json'}
    - expected_status: [200]
    - extract_binds:
        - 'user_id': {'jsonpath_mini': 'id'}
        - 'token': {'jsonpath_mini': 'token'}

- test:
    - group: "Group"
    - name: "Create group"
    - url: {'template': "api/v2/users/$user_id/groups/"}
    - method: "POST"
    - body: {template: '{"name": "$group_name"}'}
    - headers: {template: '{"Content-Type": "application/json", "Authorization": "$token"}'}
    - expected_status: [400]

I am getting below error stack.

Traceback (most recent call last):
  File "resttest.py", line 934, in <module>
    command_line_run(sys.argv[1:])
  File "resttest.py", line 930, in command_line_run
    main(args)
  File "resttest.py", line 872, in main
    failures = run_testsets(tests)
  File "resttest.py", line 660, in run_testsets
    result = run_test(test, test_config=myconfig, context=context, curl_handle=curl_handle)
  File "resttest.py", line 319, in run_test
    templated_test = mytest.realize(my_context)
  File "/home/soham/Desktop/django_rest_test/pyresttest/pyresttest/tests.py", line 253, in realize
    selfcopy._headers = self.get_headers(context=context)
  File "/home/soham/Desktop/django_rest_test/pyresttest/pyresttest/tests.py", line 207, in get_headers
    return dict(map(template_tuple, self._headers.items()))
AttributeError: 'str' object has no attribute 'items'

How can I solve this?

badprogrammist commented 7 years ago

@sohamnavadiya there is a mistake in your code. Instead of this {template: '{"name": "$group_name"}'} there should be this {template: {"name": "$group_name"}}. Go to https://github.com/svanoort/pyresttest/issues/44` for more information