Exemple #1
0
def test_httplib2_interceptor_no_host():
    # no hostname or port, one will be generated automatically
    # we never actually know what it is
    http = Http()
    with Httplib2Interceptor(app=app) as url:
        response, content = http.request(url)
        assert response.status == 200
        assert 'WSGI intercept successful!' in content.decode('utf-8')
Exemple #2
0
def test_httplib2_interceptor_host():
    hostname = str(uuid4())
    port = 9999
    http = Http()
    with Httplib2Interceptor(app=app, host=hostname, port=port) as url:
        response, content = http.request(url)
        assert response.status == 200
        assert 'WSGI intercept successful!' in content.decode('utf-8')
Exemple #3
0
def test_httplib2_interceptor_url():
    hostname = str(uuid4())
    port = 9999
    url = 'http://%s:%s/' % (hostname, port)
    http = Http()
    with Httplib2Interceptor(app=app, url=url) as target_url:
        response, content = http.request(target_url)
        assert response.status == 200
        assert 'WSGI intercept successful!' in content.decode('utf-8')
Exemple #4
0
def test_intercept_by_url_no_port():
    # Test for https://github.com/cdent/wsgi-intercept/issues/41
    hostname = str(uuid4())
    url = 'http://%s/foobar' % hostname
    interceptor = Httplib2Interceptor(app=app, url=url)
    assert isinstance(interceptor, Interceptor)
    assert interceptor.app == app
    assert interceptor.host == hostname
    assert interceptor.port == 80
    assert interceptor.script_name == '/foobar'
    assert interceptor.url == url
Exemple #5
0
def test_interceptor_instance():
    hostname = str(uuid4())
    port = 9999
    interceptor = Httplib2Interceptor(app=app, host=hostname, port=port,
                                      prefix='/foobar')
    assert isinstance(interceptor, Interceptor)
    assert interceptor.app == app
    assert interceptor.host == hostname
    assert interceptor.port == port
    assert interceptor.script_name == '/foobar'
    assert interceptor.url == 'http://%s:%s/foobar' % (hostname, port)
Exemple #6
0
def test_httplib2_in_out():
    hostname = str(uuid4())
    port = 9999
    url = 'http://%s:%s/' % (hostname, port)
    http = Http()
    with Httplib2Interceptor(app=app, url=url) as target_url:
        response, content = http.request(target_url)
        assert response.status == 200
        assert 'WSGI intercept successful!' in content.decode('utf-8')

    # outside the context manager the intercept does not work
    with py.test.raises(ServerNotFoundError):
        http.request(url)
Exemple #7
0
def test_download(rootdir, file, expected):
    """Test authenticating and downloading a file.

    Uses a local server that simulates the interaction with the google API
    """
    keyfile_path = str(rootdir.join('good_keyfile.json'))
    url = 'https://www.googleapis.com/download?path={0}'.format(
        str(rootdir.join(file)), )
    scope = 'www.googleapis.com'

    with Httplib2Interceptor(get_intercept_app,
                             host='oauth2.googleapis.com',
                             port=443):
        _, data = download_file(url, keyfile_path, scope)

    assert expected in data
Exemple #8
0
def test_download_wrong_url(rootdir):
    """Test authenticating and trying to download a file from an invalid url.

    Uses a local server that simulates the interaction with the google API.
    """
    keyfile_path = str(rootdir.join('good_keyfile.json'))
    url = 'https://www.googleapis.com/download?path={0}'.format(
        str(rootdir.join('inexistent_file')))
    scope = 'www.googleapis.com'

    with Httplib2Interceptor(get_intercept_app,
                             host='oauth2.googleapis.com',
                             port=443):
        headers, data = download_file(url, keyfile_path, scope)

    assert 'NOT FOUND' in data.upper()
    assert headers['status'] == '404'
    def test_target_url_parsing_standard_port(self):
        self.server = lambda: Httplib2Interceptor(SimpleWsgi, self.host, 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)
    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: Httplib2Interceptor(SimpleWsgi, host, 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)]
Exemple #11
0
def setup_module(module):
    module.host = str(uuid4())
    module.intercept = Httplib2Interceptor(app, host=module.host)
    module.intercept.install_intercept()
Exemple #12
0
 def __init__(self, app_factory, host='nanohttp.org', port=80, **kw):
     super(WsgiTester, self).__init__()
     self.interceptor = Httplib2Interceptor(app_factory,
                                            host=host,
                                            port=port,
                                            **kw)