Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to extract from response body #99

Closed
jbouzekri opened this issue Oct 23, 2015 · 6 comments
Closed

Unable to extract from response body #99

jbouzekri opened this issue Oct 23, 2015 · 6 comments
Labels

Comments

@jbouzekri
Copy link

Hi,

Thanks for your awesome work.

I am trying to test the authentication/logout workflow of an API. When I login, it returns a token that I can use to logout.

This is my test file :


---
- config:
    - testset: "Authentication feature"

- test:
    - name: "Unauthorized Access"
    - url: "/user/logout"
    - method: 'POST'
    - expected_status: [401]

- test:
    - name: "Authenticate"
    - url: "/user/login?mail=myemail&password=mypassword"
    - method: 'POST'
    - headers: {Content-Type: "application/json"}
    - extract_binds:
        - 'token': {'jsonpath_mini': 'response.token'}

- test:
    - name: "Logout"
    - url: "/user/logout?token=$token"
    - method: 'POST'
    - headers: {Authorization: "Bearer $token"}

As you can see in the last test, I tried to set $token in both the header and as a GET param. But it is never replaced by the value extracted from the previous tracker.

Please find bellow the body of the response when login :

{
    "response": {
         "token": "aaaabbbbbccccddddeeee"
    },
    "success": 1
}

Thanks in advance

@svanoort
Copy link
Owner

Hi, I'm glad you're finding it useful, and thanks for reaching out with your question!

The reason you're seeing issues is that templating must be explicitly declared for elements (it is not done automatically, for a couple reasons).

I've got a good example of this in the content-test. So, for your case, the final test will look like:

- test:
    - name: "Logout"
    - url: "/user/logout?token=$token"
    - method: 'POST'
    - headers: {template: {Authorization: "Bearer $token"}}

One note: for headers, if you declare them as using a template, all values in the header map will be templated (header names and values), so if you're using $ signs in there, make sure to escape them with two $ signs (i.e. $$escaped).

@svanoort
Copy link
Owner

@jbouzekri ^^ reply above :-)

@jbouzekri
Copy link
Author

Thanks for your prompt reply and sorry for my late one as I was in a meeting.

Your solution works for the header (my use case) but it did not replace the $token in the url (just a test). Any idea why ?

@svanoort
Copy link
Owner

Yes, it also needs to be templated in the same fashion: url: {template: "/user/logout?token=$token"}

I think it's time to add more templating in the examples and in main documentation (as well as in the advanced documentation, since people are using this feature a lot more than I initially thought (probably a good thing!).

@svanoort
Copy link
Owner

@jbouzekri Side note: you might find the docker containers I created for pyresttest development useful for other python work: https://github.com/svanoort/pyresttest/tree/master/docker

I have documentation on how to use them in building and Jenkins workflows for using the docker images in python testing.
With the catch that I'm still trying to work on the install test workflow; there's a bug in the workflow plugin itself that I need to isolate and fix (I work for the company that produced that plugin).

@jbouzekri
Copy link
Author

Yes it is working. I am all set to add functionnal testing to an old api in a few minutes thanks to the clear and readable syntax of your YAML file.

I will take a look at the additional reading you have provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants