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

Extend HTTPretty.decorators #234

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

syndbg
Copy link
Contributor

@syndbg syndbg commented Mar 14, 2015

Motivation:

Mainly Django decorators being DRY and making it really easy to see that X, Y, Z function/class/method will have a modified behavior.

In our case that's a mocked socket. So why not be able to use register_uri as a decorator?
Writing register_uri in the body isn't the most pythonic approach.

The change:

Instead of

@httpretty.activate
def test_one():
    httpretty.register_uri(httpretty.GET, "http://yipit.com/",
                           body="Find the best daily deals")

    response = requests.get('http://yipit.com')
    assert response.text == "Find the best daily deals"

We can now do this:

@httpretty.register(method=httpretty.GET, uri="http://yipit.com/", body="Find the best daily deals")
def test_one():
    response = requests.get('http://yipit.com')
    assert response.text == "Find the best daily deals"

Also decorating classes is supported.

TODO:

  • Update docs,
  • Add support for list of register_uri kwargs again through the new register decorator or a new registers decorator?
  • Add even more decorators?

Are you other users ok with this change? It's open to changes and it's definitely not final.
What do you think @gabrielfalcao ?

P.S this is rebased with #232

Linked httpretty.register with httpretty.activate_uri for convinience.
@syndbg
Copy link
Contributor Author

syndbg commented Mar 16, 2015

Updated docs.

Still, I have to check the code examples and current functional/unit tests. I hope the tests aren't as shallow as the tests provided in the docs. They don't test all args provided to register_uri. This could have bugs.

Linked register with activate_uri

It's consistent with activate.

Added support for dicts/lists args passed to register decorator

Code contains some sanity checks to make sure it handles correctly the args types.
However the tests aren't as complete as I would like them to be. I'll enhance them with supposed non-working scenarios that should raise exceptions/fails.

PR is almost ready for code review.

@syndbg
Copy link
Contributor Author

syndbg commented Apr 5, 2015

Ok, I think this is ready for review.

assert response.text == "Find the best daily deals"
response = requests.get("http://yipit.com/about/")
assert response.text == "About us"
response = requests..get("https://github.com/gabrielfalcao/HTTPretty")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request..get? (extra dot)

@BenjamenMeyer
Copy link

You provide examples for most of how httpretty works; but not the callback methodology. Would you please add an example of that too?

@syndbg
Copy link
Contributor Author

syndbg commented Apr 28, 2015

@BenjamenMeyer Since it's been a long time, I don't exactly remember any callback scenarios. Can you elaborate on what examples should I provide?

Sorry for the troubles and thanks for the review!

P.S the docs typo is fixed, but I want to have it in one commit with the examples you requested.

@BenjamenMeyer
Copy link

@syndbg See https://github.com/gabrielfalcao/HTTPretty#dynamic-responses-through-callbacks

Should be pretty simple as the function becomes the body parameter.

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

Successfully merging this pull request may close these issues.

None yet

3 participants