def test_urllib_interceptor_host(): hostname = str(uuid4()) port = 9999 with UrllibInterceptor(app=app, host=hostname, port=port) as url: response = urlopen(url) assert response.code == 200 assert 'WSGI intercept successful!' in response.read().decode('utf-8')
def test_urllib_interceptor_url(): hostname = str(uuid4()) port = 9999 url = 'http://%s:%s/' % (hostname, port) with UrllibInterceptor(app=app, url=url) as target_url: response = urlopen(target_url) assert response.code == 200 assert 'WSGI intercept successful!' in response.read().decode('utf-8')
def test_parse_robots(self): try: import six.moves.urllib_robotparser as robotparser except ImportError as ex: raise unittest.SkipTest() rp = robotparser.RobotFileParser() with UrllibInterceptor(self.app, host='127.0.0.1', port=80) as url: rp.set_url("{}/robots.txt".format(url)) rp.read() assert not rp.can_fetch("*", url)
def test_urllib_in_out(): hostname = str(uuid4()) port = 9999 url = 'http://%s:%s/' % (hostname, port) with UrllibInterceptor(app=app, url=url) as target_url: response = urlopen(target_url) assert response.code == 200 assert 'WSGI intercept successful!' in response.read().decode('utf-8') # outside the context manager the intercept does not work with py.test.raises(URLError): urlopen(url)