Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward a copy of http requests to another (test) environment

I want all the production data for my web-app to also flow through my testing environment. Essentially, I want to forward every http request for the production site to the test site (and also have the production website serve it!).

What is a good way to do this? My site is built with Django, and served by mod_wsgi. Is this best implemented at the app-level (Django), web server level (Apache), or the mod_wsgi-level?

like image 583
raviv Avatar asked Mar 03 '10 06:03

raviv


People also ask

How do I test a HTTP POST request?

Here are some tips for testing POST requests: Create a resource with a POST request and ensure a 200 status code is returned. Next, make a GET request for that resource, and ensure the data was saved correctly. Add tests that ensure POST requests fail with incorrect or ill-formatted data.

What website can be used to catch HTTP requests from a server?

You can use RequestBin to create a public endpoint to receive and inspect HTTP requests from any source, and easily inspect the headers, payload and more.

How do I send a HTTP request?

The most common HTTP request methods have a call shortcut (such as http. get and http. post), but you can make any type of HTTP request by setting the call field to http. request and specifying the type of request using the method field.

What is HTTP what are the HTTP request methods?

What is HTTP? The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers. HTTP works as a request-response protocol between a client and server. Example: A client (browser) sends an HTTP request to the server; then the server returns a response to the client.


1 Answers

I managed to forward request like this

def view(request):
    # do what you planned to do here
    ...

    # processing headers
    def format_header_name(name):
        return "-".join([ x[0].upper()+x[1:] for x in name[5:].lower().split("_") ])
    headers = dict([ (format_header_name(k),v) for k,v in request.META.items() if k.startswith("HTTP_") ])
    headers["Cookie"] = "; ".join([ k+"="+v for k,v in request.COOKIES.items()])

    # this conversion is needed to avoid http://bugs.python.org/issue12398
    url = str(request.get_full_path())

    # forward the request to SERVER_DOMAIN
    conn = httplib.HTTPConnection("SERVER_DOMAIN")
    conn.request(
        request.method,
        url,
        request.raw_post_data,
        headers
    )
    response = conn.getresponse()

    # some error handling if needed
    if response.status != 200:
        ...

    # render web page as usual
    return render_to_response(...)

For code reuse, consider decorators

like image 109
observerss Avatar answered Sep 25 '22 00:09

observerss