svanoort / pyresttest

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

Cookie extractor from Set-Cookie response. #184

Open danielatdattrixdotcom opened 8 years ago

danielatdattrixdotcom commented 8 years ago

I needed this for working with an API that uses Django auth/sessions to work with CSRF cookie and X-CSRFToken header.

svanoort-jenkins commented 8 years ago

Can one of the admins verify this patch?

svanoort commented 8 years ago

@danielatdattrixdotcom Thank you, this is a feature that I know is in fairly high demand. I've added a couple comments (small changes) before this is ready to be merged. It also needs some sort of small unit test in test_validators.py.

This weekend I'm going to play with it a bit more and look more closely at the format of output (it might make sense to add a few options).

You get kudos by the way for looking closely at how the other extractors are implemented and following that (and adding documentation as well)!

danielatdattrixdotcom commented 8 years ago

@svanoort Let me know on the args/options to extractors in the YAML/JSON files and your thoughts on that and I'll make the updates and tests.

alswl commented 8 years ago

Oh no, I had write the same feature code, but @danielatdattrixdotcom 's documents is nice. 😂

svanoort commented 8 years ago

@danielatdattrixdotcom Coming back to this now that the crunch period at work is settling down (spoiler: Jenkins, and specifically work on Pipeline Stage View plugin): I think it's not worth adding options to the extractor right now - I had a notion of eventually allowing use of regexes or other matchers in extraction, but it's not useful yet. We need support for templating with collections (or transformations on collections), and/or some embedded transformation language, if we're going to go down that route. It's planned in the future, but too far off to worry about now.

Added one comment about a Python 3 compatibility challenge (just an import change).

So, from my point of view, to get to merge, what we need are automated tests, covering a couple key edge cases: cookie doesn't exist, cookie exists with multiple values, normal cookie case. This means a unit test in test_validators.py, and a functional test in functionaltest.py -- the unit test just covers the extractor parsing and behavior (you can use some cookie info taken from a browser session).

The functional test needs to work with real HTTP requests; I'd prefer to add cookie-setting to the test Django-tastypie app (probably easiest done with the sessions middleware option), but if that proves hard we can just invoke a public API or site, as long as the request is trivial. I've been using GitHub for this in the past.