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?
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.
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.
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? 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With