rxmnnxfpvg
rxmnnxfpvg

Reputation: 30993

Django: Parse HttpResponse object for URL string data

I'm writing a Django test for a redirect that "throws" a URL string error, but I'd like to do it without brittle regexes.

def test_redirect_contains_error_string(self):
    response = self.client.get('/sendme/', follow=True)
    // response.redirect_chain: [('https://e.com/?error=errortype', 302)]
    url = response.redirect_chain[0][0]
    self.assertTrue(re.search('error=errortype', url))
    // Ew!

Is there a method to parse URLs into dict-like objects, so I can just:

response_obj = something(url)
self.assertEquals(response_obj.get('error'), 'errortype')

Upvotes: 0

Views: 661

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174624

How about urlparse:

>>> import urlparse
>>> zoo = urlparse.urlparse('http://www.example.com/foo?bar=zoo')
>>> urlparse.parse_qs(zoo.query)
{'bar': ['zoo']}

Upvotes: 5

Related Questions