Git Product home page Git Product logo

Comments (6)

lsemel avatar lsemel commented on May 13, 2024

BTW this is a nice library. I just used it in some of my tests!

from httpretty.

gabrielfalcao avatar gabrielfalcao commented on May 13, 2024

HI @lsemel, first of all, thanks for the feedback.

I don't quite understand what you mean with builtin urls. What is the goal you want to achieve?

from httpretty.

lsemel avatar lsemel commented on May 13, 2024

Here's an example from tests I wrote yesterday. I was testing code that makes HTTP requests, to ensure it reacts properly when it gets different response codes (specifically, that it retries a certain number of times after a certain interval when it gets a 500 error, but doesn't do this when it gets a 404 or 200).

To do this, I pointed it at the httpbin.org service, which provides URLs like this can be used to test HTTP clients, specifically https://httpbin.org/status/[status_code], which will always return the specified status code. This works well, except my tests are going to fail if httpbin.org ever goes down.

If I wanted to make my tests self-contained by replacing httpbin.org with HTTPretty, I'd have to do the following in my test setup:

HTTPretty.register_uri(HTTPretty.GET, "http://foobar.com/500",status=500)
HTTPretty.register_uri(HTTPretty.GET, "http://foobar.com/404",status=404)
HTTPretty.register_uri(HTTPretty.GET, "http://foobar.com/200",status=200)

And then reuse those URLs within my tests.

@httprettified
def test_500(self):
   result = do_something_with_url('http://foobar.com/500')

If httpretty had some of the urls of httpbin.org built in, for the most common cases (like returning a specific status code, returning specific response headers, or returning get and post data) it'd save a lot of repetition in registering urls to set up tests and mimic the experience of using something like httpbin.org

from httpretty.

gabrielfalcao avatar gabrielfalcao commented on May 13, 2024

What about the response rotation API?

from httpretty import httprettified, HTTPretty
from lsemel.core import API


@httprettified
def test_my_code_reacs_appropriately_when_taking_a_500():
    HTTPretty.register_uri(HTTPretty.GET, "http://api.lsemel.com/v1/whatever",
                           responses=[
                               HTTPretty.Response(status=200),
                               HTTPretty.Response(status=404),
                               HTTPretty.Response(status=500),
                            ]))

    api = API()

    assert api.fetch_whatever_data() == "OK"  # HTTPretty returned 200 so that's ok
    assert api.fetch_whatever_data() == "OK"  # HTTPretty returned 404 so that's ok

    assert api.fetch_whatever_data() == "FAILED!"  # HTTPretty returned 500 so that's bad!!!

from httpretty.

gabrielfalcao avatar gabrielfalcao commented on May 13, 2024

I might not have understood your idea completely, but from what I can understand, having a preset of some URL's does not make much sense because:

  • HTTPretty users would need to learn by heart what are the default URL's
  • The code you are testing will need to hardcode access to a fake URL (i.e: foobar.com)
  • It's just easier to declare responses in HTTPretty so rather than memorizing predefined URLs the developer that already knows how to use HTTPretty has full control over the expected responses.

Thoughts?!

from httpretty.

gabrielfalcao avatar gabrielfalcao commented on May 13, 2024

Can I close this issue?

from httpretty.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.