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

Added expected_data parameter to register_uri #97

Open
wants to merge 3 commits into
base: master
from

Conversation

@maraujop
Copy link

maraujop commented Sep 16, 2013

This checks that POST payload received matches the one expected. This is very helpful for improving robustness of some mocked tests, when creating or updating objects, it is very helpful to check data sent to the endpoint was correct, otherwise a bug could be in between and there is no easy way to catch it.

Currently you can access last request's body as you know, but doing it this way is more elegant and easy to follow, as everything is gathered together in the same mock registration.

Thanks, cheers,
Miguel

This checks that POST payload received matches the one expected. This is
very helpful for improving robustness of some mocked tests.
"https://api.imaginary.com/v1/sweet/",
{"wrong": "data"}
)
raise Exception("Payload checked didn't work")

This comment has been minimized.

Copy link
@gabrielfalcao

gabrielfalcao Oct 2, 2013

Owner

You could have used:

requests.post.when.called_with("https://api.imaginary.com/v1/sweet/", {"wrong": "data"}).should.throw(ValueError)

http://falcao.it/sure/reference.html#callable-when-called_with-arg1--kwarg1-2--should-throw-exception-

@@ -277,6 +278,13 @@ def sendall(self, data, *args, **kw):

for matcher, value in httpretty._entries.items():
if matcher.matches(info):
if matcher.expected_data is not None:

This comment has been minimized.

Copy link
@gabrielfalcao

gabrielfalcao Oct 2, 2013

Owner

The code got a little cluttered here, the indentation got too deep. Maybe lines 281 to 287 could be moved into a function or method and simply be called if matcher.matches(info) ?

@maraujop

This comment has been minimized.

Copy link
Author

maraujop commented Oct 23, 2013

I've moved indented code to a method named check_expected_data that raises the exception if it fails. However maybe you prefer a method like is_expected_data_valid that returns a Boolean and whether if that is False, throw the exception in the core flow.

I've also adjusted the test as you mentioned. Thanks for the feedback.

BTW I had to do a push force because Github five hundred yesterday while i was pushing and the repository end up inconsistent.

Cheers,
Miguel

@gabrielfalcao

This comment has been minimized.

Copy link
Owner

gabrielfalcao commented Oct 23, 2013

@maraujop I love it, thank you for your time. I'm pretty busy rightnow but as soon as I get some free time I will apply this.

Again, thank you so much!

…cted_data

Conflicts:
	httpretty/core.py
	tests/functional/test_requests.py
@maraujop

This comment has been minimized.

Copy link
Author

maraujop commented Nov 28, 2013

Hi Gabriel, I've rebased the patch against latest master branch, so that it works in Mac. Tests are not passing, but I believe the ones that are broken are also broken in master branch. Could you please consider merging this now to avoid rebasing against future refactors?

@chris-martin

This comment has been minimized.

Copy link
Contributor

chris-martin commented May 21, 2014

I've done something similar in #168 which might subsume this feature, though its purpose is a little different.

if self.expected_data is not None:
body_dict = dict(parse_qsl(request.body))
if body_dict != self.expected_data:
raise ValueError("Body Post didn't match, expected %s, got %s" % (

This comment has been minimized.

Copy link
@gabrielfalcao

gabrielfalcao Mar 20, 2020

Owner

this should raise an AssertionError instead, that way the test will fail as failure rather than error

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

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.