-
Notifications
You must be signed in to change notification settings - Fork 140
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
treq.auth omnibus #315
treq.auth omnibus #315
Conversation
Closes twisted#313.
Instead, copy and update. Closes twisted#314.
Also accept bytes to allow user control over encoding. Closes twisted#268.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great improvement. Thanks!
Only minor comments.
And this PR can be merged as is.
Happy to see mock.Mock gone :)
For my tests, I stopped using spies that record and for which you assert the behaviour later,
and instead I am using and ReplayAgentBacked similar to the angularjs $httpBackend
You pass a list of expected URLs and request data, but you also able to return a response.
In this way, you can test chained agent interactions, without touching the network.
For example, I am using for SAML login testing in which more than 1 requests are made...and the response of the first request is used in the second request.
src/treq/_recorder.py
Outdated
return d | ||
|
||
|
||
def recorder(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor comment
maybe rename it as get_http_agent_spy
and allow to pass any list?
Right now, having the list managed outside of recorder() makes no difference and maybe just keep it as get_http_agent_spy
and return a tupple.
def recorder(): | |
def get_http_agent_spy(accumulator_callable): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the "agent spy" terminology, but I definitely want a function like recorder()
that is low lines-of-code to use in a test. If using this API takes more than one or two lines of code folks will move it to setUp
which then makes the tests more verbose and less amenable to local reasoning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you write one, read many times :) ... and with a good name, you read the name and you don't have to search for the documentation of that name :)
and then, I guess that all developers have auto completion :)
And if someones adds stuff to setUp
just complain during review.
It's your call.
I don't think that is fair to make the code harder to read just because some people are lazy :p
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I renamed recorder
to agent_spy
and _RequestRecordAgent
to _AgentSpy
. The class could be made public to provide the bring-your-own-accumulator API, or another wrapper function added. Probably the latter is best to prevent subclassing.
src/treq/_recorder.py
Outdated
|
||
@implementer(IAgent) | ||
@attr.s | ||
class _RequestRecordAgent(object): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good. I think that this test helper would also be very useful in core Twisted HTTP as it might be used by other tests in which twisted.web.iweb.IAgent is used.
If you have time for a review, I could try to add it to Twisted, but I need a review buddy.
If if you want to add it to twisted, I would be happy to review it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be happy to see it added there. Indeed it woudn't surprise me if something similar already exists somewhere in twisted.web.test
. I'd be happy to review if you port it over, or I can do so.
I filed https://twistedmatrix.com/trac/ticket/10072 (with AgentSpy
as the name) to add this to Twisted. Please self-assign the ticket when you start working on it; I'll do the same if I get to it first.
More of this to come!
|
I think that the spy is OK for unit tests and |
Thank you very much for the review and feedback, @adiroiban! |
treq.test.test_auth
to use a newtreq._recorder.recorder()
utility rather than mock._RequestHeaderSettingAgent
as anIAgent
implementer.treq.auth
— add docstrings and add to the API page.Headers
passed to_RequestHeaderSettingAgent
bytes
as an escape hatch.