'Test authentication workaround for Android'

def test_android_auth_workaround(self):

    r = router()
    with TestServer(r.dispatch) as server:
        r.auth_controller.log = server.log
        conn = server.connect()
        conn.request(u'GET', u'/android')
        r = conn.getresponse()
        self.ae(r.status, httplib.UNAUTHORIZED)
        auth_handler = urllib2.HTTPDigestAuthHandler()
        url = (u'http://localhost:%d%s' % (server.address[1], u'/android'))
        auth_handler.add_password(realm=REALM, uri=url, user=u'testuser', passwd=u'testpw')
        cj = cookielib.CookieJar()
        cookie_handler = urllib2.HTTPCookieProcessor(cj)
        r = urllib2.build_opener(auth_handler, cookie_handler).open(url)
        self.ae(r.getcode(), httplib.OK)
        cookies = tuple(cj)
        self.ae(len(cookies), 1)
        cookie = cookies[0]
        self.assertIn(':', cookie.value)
        self.ae(cookie.path, '/android')
        r = urllib2.build_opener(cookie_handler).open(url)
        self.ae(r.getcode(), httplib.OK)
        self.ae(r.read(), 'android')
        try:
            urllib2.build_opener(cookie_handler).open((url + u'2'))
            assert u'Replay    attack    succeeded'
        except urllib2.HTTPError as e:
            self.ae(e.code, httplib.UNAUTHORIZED)
