def test_double_nested_context_interceptor(): hostname = str(uuid4()) url1 = 'http://%s:%s/' % (hostname, 9998) url2 = 'http://%s:%s/' % (hostname, 9999) with Urllib3Interceptor(app=app, url=url1): with Urllib3Interceptor(app=app, url=url2): response = httppool.request('GET', url1) assert response.status == 200 assert 'WSGI intercept successful!' in str(response.data) response = httppool.request('GET', url2) assert response.status == 200 assert 'WSGI intercept successful!' in str(response.data) response = httppool.request('GET', url1) assert response.status == 200 assert 'WSGI intercept successful!' in str(response.data) # outside the inner context manager url2 does not work with py.test.raises(urllib3.exceptions.HTTPError): httppool.request('GET', url2, retries=False) # outside both context managers neither url works with py.test.raises(urllib3.exceptions.HTTPError): httppool.request('GET', url2, retries=False) with py.test.raises(urllib3.exceptions.HTTPError): httppool.request('GET', url1, retries=False)
def test_urllib3_interceptor_host(): hostname = str(uuid4()) port = 9999 with Urllib3Interceptor(app=app, host=hostname, port=port) as url: response = httppool.request('GET', url) assert response.status == 200 assert 'WSGI intercept successful!' in str(response.data)
def test_urllib3_interceptor_url(): hostname = str(uuid4()) port = 9999 url = 'http://%s:%s/' % (hostname, port) with Urllib3Interceptor(app=app, url=url) as target_url: response = httppool.request('GET', target_url) assert response.status == 200 assert 'WSGI intercept successful!' in str(response.data)
def test_urllib3_in_out(): hostname = str(uuid4()) port = 9999 url = 'http://%s:%s/' % (hostname, port) with Urllib3Interceptor(app=app, url=url) as target_url: response = httppool.request('GET', target_url) assert response.status == 200 assert 'WSGI intercept successful!' in str(response.data) # outside the context manager the intercept does not work with py.test.raises(urllib3.exceptions.ProtocolError): httppool.request('GET', url, retries=False)
def setUp(self): super(RunnerTest, self).setUp() # NB: random host ensures that we're not accidentally connecting to an # actual server host, port = (str(uuid4()), 8000) self.host = host self.port = port self.server = lambda: Urllib3Interceptor( SimpleWsgi, host=host, port=port) self._stdin = sys.stdin self._stdout = sys.stdout sys.stdout = StringIO() # swallow output to avoid confusion self._stderr = sys.stderr sys.stderr = StringIO() # swallow output to avoid confusion self._argv = sys.argv sys.argv = ['gabbi-run', '%s:%s' % (host, port)]
def test_target_url_parsing_standard_port(self): # NOTE(cdent): For reasons unclear this regularly fails in # py.test and sometimes fails with testr. So there is # some state that is not being properly cleard somewhere. # Within SimpleWsgi, the environ thinks url_scheme is # 'https'. self.server = lambda: Urllib3Interceptor( SimpleWsgi, host=self.host, port=80) sys.argv = ['gabbi-run', 'http://%s/foo' % self.host] sys.stdin = StringIO(""" tests: - name: expected success GET: /baz status: 200 response_headers: x-gabbi-url: http://%s/foo/baz """ % self.host) with self.server(): try: runner.run() except SystemExit as err: self.assertSuccess(err)