0

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')

1 Answer 1

5

How about urlparse:

>>> import urlparse
>>> zoo = urlparse.urlparse('http://www.example.com/foo?bar=zoo')
>>> urlparse.parse_qs(zoo.query)
{'bar': ['zoo']}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. That module and your example are perfect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.