pyresttest icon indicating copy to clipboard operation
pyresttest copied to clipboard

How can I use template in headers.

Open sohamnavadiya opened this issue 8 years ago • 1 comments

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: '[email protected]',
    email2: '[email protected]',
    email3: '[email protected]',
    email4: '[email protected]',
    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?

sohamnavadiya avatar Jan 16 '17 08:01 sohamnavadiya

@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

badprogrammist avatar Mar 03 '17 10:03 badprogrammist