Login

Tag "client"

Snippet List

Add get_addr() method to request object

I thought it would be useful to have a `get_addr()` method available on request objects, similar to the `get_host()` provided by Django. This middleware will add a `get_addr()` method to requests which uses the `X-Forwarded-For` header (useful if you're behind a proxy) if it's present and you have the `USE_X_FORWARDED_FOR` header set to `True` (default is `False`) and otherwise will use the `REMOTE_ADDR` environment variable. Note that if you are *not* behind a proxy and have `USE_X_FORWARDED_FOR` set to `True`, then clients can spoof their IP by simply setting the `X-Forwarded-For header`.

  • request
  • ip
  • header
  • address
  • client
  • remote-addr
  • x-forwarded-for
  • get-addr
Read More

Monkey-patch Django's test client to return WSGIRequest objects

Testing low-level functionality sometimes requires a WSGIRequest object. An example of this is testing template tags. This will monkey-patch the test Client object to return WSGIRequest objects Normal Django behavior: >>> client.get('/') <HttpResponse > With this code, get the request object: >>> client.request_from.get('/') <WSGIRequest > Installation: For this to work, you simply need to import the contents of this file. If you name this file `clientrequestpatch.py`, do this inside your Django tests. from django.test.testcases import TestCase from myproject.test import clientrequestpatch

  • request
  • test
  • client
  • wsgi
  • wsgirequest
Read More

Modify requests in your unit tests (improvement on RequestFactory)

This is an update to Simon Willison's snippet http://djangosnippets.org/snippets/963/, along with one of the comments in that snippet. This class lets you create a Request object that's gone through all the middleware. Suitable for unit testing when you need to modify something on the request directly, or pass in a mock object. (note: see http://labix.org/mocker for details on how to mock requests for testing) Example on how to use: from django.test import TestCase from yourapp import your_view from yourutils import RequestFactory class YourTestClass(TestCase): def setUp(self): pass def tearDown(self): pass # Create your own request, which you can modify, instead of using self.client. def test_your_view(self): # Create your request object rf = RequestFactory() request = rf.get('/your-url-here/') # ... modify the request to your liking ... response = your_view(request) self.assertEqual(response.status_code, 200) Suggestions/improvements are welcome. :)

  • testing
  • request
  • client
  • testcase
Read More

External service Test Client

The inbuilt test client can be used to only test single domain applications ie no support for supplying absolute URLs. But there are cases where one might like to test against URL rewrites, external domains/services like OpenID, OAuth etc. This client has an external dependency on httplib2, to maintain the sessions (cookie-based). The API is exactly similar to the inbuilt test client. >>> from client import TestClient >>> c = TestClient() >>> resp = c.get("http://www.google.com/") >>> resp.status_code 200 **Note**: Unlike the built-in test client, this test client cannot access the template and context attributes from the response even if testing a local application.

  • test
  • client
  • httplib2
Read More

"Zoom in" on rendered HTML that the test client returns

If you have this as your base class for all unit tests you can do the following: class TestViews(BaseTestCase): def test_generated_stats(self): "test that certain stuff in the response" ...create some content for testing or use fixtures... response = self.client.get('/some/page/') # At this point response.content is a huge string filled with HTML tags and # "junk" that you don't need for testing the content thus making it difficult # to debug the generated HTML because it so huge. # So we can zoom in on the <div id="stats>...</div> node html = self._zoom_html(response.content, '#stats') # the variable 'html' will now be something like this: """ <div id="stats"> <p> <strong>2</strong> students<br/> <em>1</em> warning. </p> </div> """ # This makes it easier to debug the response and easier to test # against but the HTML might still be in the way so this would fail: self.assertTrue('2 students' in html) # will fail # To strip away all html use _strip_html() content = self._strip_html(html) # Now this will work self.assertTrue('2 students' in content) # will work It works for me and I find this very useful so I thought I'd share it.

  • css
  • test
  • client
  • lxml
  • lxml.html
Read More

Dynamic growing model

This model is designed for my webshop. The Client-object is very 'stretch-able' by defining more fields to the client. These extra fields ares stored in the ClientConfig-object. Be sure to create a new Client-instance first and SAVE it! Without a client.id the ClientConfig won't work.

  • django
  • dynamic
  • model
  • example
  • client
Read More

6 snippets posted so far.