svanoort / pyresttest

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

Generate a Token and use that in the Authorization header #175

Closed bwaybandit closed 8 years ago

bwaybandit commented 8 years ago

First time with pyresttest. Great test harness for REST apis. Need to be able to create a custom token for the Authorization part of the header.

Inputs: 2 strings Output: string (that will be included in the headers for Authorization)

Do I need to create a custom extractor? Is there an example on how to do this?

svanoort commented 8 years ago

Hi @bwaybandit , I'm glad you're liking PyRestTest,

The answer to this will depend on where the string inputs are coming from. Do they come from previous requests? If so, an extractor is the way to go, and a template in the headers for the output.

There's an extractor example here: https://github.com/svanoort/pyresttest/blob/master/extensions.md#extractors-now-things-get-a-bit-more-complex

You get access to both the request body (as raw bytes, in Python 2 a 'str' object, in Python 3 a 'bytes' object), and the headers - as a list of (key,value) tuples. The latter is because duplicate headers with the same name are allowed, per the HTTP spec.

If you use headers, make sure to set the 'is_header_extractor' field to true, so that the framework is aware to generate headers for consumption.

Actual working examples of extractor extensions are here: https://github.com/svanoort/pyresttest/tree/master/pyresttest/ext

And of course the main ones here: https://github.com/svanoort/pyresttest/blob/master/pyresttest/validators.py

If you're using user-supplied data to generate the token value, what you want to register is a custom GENERATOR. The first link has a section on generators, and there are examples in the main codebase here: https://github.com/svanoort/pyresttest/blob/master/pyresttest/generators.py

Does that answer your question? Cheers, Sam

bwaybandit commented 8 years ago

Perfect. Thanks Sam. Will write a custom generator. Thanks for pointing me in the right direction. Much appreciated. --Len