0

URL文字列エラーを「スロー」するリダイレクトのDjangoテストを作成していますが、脆弱な正規表現を使用せずに実行したいと思います。

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!

URLをdictのようなオブジェクトに解析する方法はありますか?

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

1 に答える 1

5

どうですかurlparse

>>> import urlparse
>>> zoo = urlparse.urlparse('http://www.example.com/foo?bar=zoo')
>>> urlparse.parse_qs(zoo.query)
{'bar': ['zoo']}
于 2012-10-10T20:34:14.430 に答える