Login

Load response.content in browser (for debugging)

Author:
tin_nqn
Posted:
May 7, 2012
Language:
Python
Version:
1.4
Score:
1 (after 1 ratings)

When debugging tests you frequently need to inspect response content, making a pdb. set_trace() breakpoint and printing response.content

but html isn't enough human readable (even for programmers :D) so, why not open it in your browser? Suposse you save this code in utils.py and you break your testcase as this:

response = self.client.get(self.url)
import pdb; pdb.set_trace()

Then:

(pdb) from utils import load_response_on_firefox
(pdb) load_response_on_firefox(response)

Ta-Da!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
def dump_response_on_file(response, filepath=None):
    """Useful when debugging responses. Calls to this method should not be
    committed."""
    if not filepath:
        from tempfile import mkstemp
        _, filepath = mkstemp(text=True)
    f = file(filepath, 'w')
    f.write(response.content)
    f.close()
    return filepath

def load_response_on_firefox(response):
    """ load html of a response for debugging purposes
    Attention: Calls to this method should not be
    committed.
    """
    import subprocess
    fpath = dump_response_on_file(response)
    subprocess.call(['firefox', fpath])

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

Please login first before commenting.