Exemplo n.º 1
0
    def test_manage_cookies_off(self):
        """
        Test the behavior when manage_cookies is off (the default). Cookies shouldn't be sent
        """
        self.client = HTTPClient()

        def assert_no_cookie(req_handler):
            return 'Cookie' not in req_handler.headers

        test_requests = [({
            'path': '/',
            'req_assert_function': assert_no_cookie
        }, {
            'body': b'nothing',
            'headers': {
                'Set-Cookie': "testcookie=42"
            }
        }),
                         ({
                             'path': '/',
                             'req_assert_function': assert_no_cookie
                         }, {
                             'body': b'nothing'
                         })]
        with mock_httpd(TESTSERVER_ADDRESS, test_requests):
            self.client.open(TESTSERVER_URL + '/')
            self.client.open(TESTSERVER_URL + '/')
Exemplo n.º 2
0
 def test_https_untrusted_root(self):
     if not supports_ssl_default_context:
         raise pytest.skip("old python versions require ssl_ca_certs")
     self.client = HTTPClient('https://untrusted-root.badssl.com/')
     try:
         self.client.open('https://untrusted-root.badssl.com/')
     except HTTPClientError as e:
         assert_re(e.args[0], r'Could not verify connection to URL')
Exemplo n.º 3
0
 def test_https_no_ssl_module_insecure(self):
     from mapproxy.client import http
     old_ssl = http.ssl
     try:
         http.ssl = None
         self.client = HTTPClient('https://www.google.com/', insecure=True)
         self.client.open('https://www.google.com/')
     finally:
         http.ssl = old_ssl
Exemplo n.º 4
0
def parse_capabilities_url(url):
    url = wms_capabilities_url(url)
    log.info('fetching capabilities from: %s', url)

    client = HTTPClient()
    try:
        resp = client.open(url)
    except HTTPClientError, ex:
        # TODO error handling
        raise ex
Exemplo n.º 5
0
def parse_capabilities_url(url):
    url = wms_capabilities_url(url)
    log.info('fetching capabilities from: %s', url)

    client = HTTPClient()
    try:
        resp = client.open(url)
    except HTTPClientError, ex:
        # TODO error handling
        raise ex
Exemplo n.º 6
0
 def test_https_invalid_cert(self):
     # load 'wrong' root cert
     with TempFile() as tmp:
         with open(tmp, 'wb') as f:
             f.write(GOOGLE_ROOT_CERT)
         self.client = HTTPClient('https://untrusted-root.badssl.com/',
                                  ssl_ca_certs=tmp)
         try:
             self.client.open('https://untrusted-root.badssl.com/')
         except HTTPClientError as e:
             assert_re(e.args[0], r'Could not verify connection to URL')
Exemplo n.º 7
0
 def test_https_valid_ca_cert_file(self):
     # verify with fixed ca_certs file
     cert_file = '/etc/ssl/certs/ca-certificates.crt'
     if os.path.exists(cert_file):
         self.client = HTTPClient('https://www.google.com/', ssl_ca_certs=cert_file)
         self.client.open('https://www.google.com/')
     else:
         with TempFile() as tmp:
             with open(tmp, 'wb') as f:
                 f.write(GOOGLE_ROOT_CERT)
             self.client = HTTPClient('https://www.google.com/', ssl_ca_certs=tmp)
             self.client.open('https://www.google.com/')
Exemplo n.º 8
0
 def test_https_valid_ca_cert_file(self):
     # verify with fixed ca_certs file
     cert_file = '/etc/ssl/certs/ca-certificates.crt'
     if os.path.exists(cert_file):
         self.client = HTTPClient('https://www.google.com/', ssl_ca_certs=cert_file)
         self.client.open('https://www.google.com/')
     else:
         with TempFile() as tmp:
             with open(tmp, 'wb') as f:
                 f.write(GOOGLE_ROOT_CERT)
             self.client = HTTPClient('https://www.google.com/', ssl_ca_certs=tmp)
             self.client.open('https://www.google.com/')
Exemplo n.º 9
0
 def test_https_no_ssl_module_error(self):
     from mapproxy.client import http
     old_ssl = http.ssl
     try:
         http.ssl = None
         try:
             self.client = HTTPClient('https://www.google.com/')
         except ImportError:
             pass
         else:
             assert False, 'no ImportError for missing ssl module'
     finally:
         http.ssl = old_ssl
Exemplo n.º 10
0
    def test_https_invalid_cert(self):
        try:
            import ssl
            ssl
        except ImportError:
            raise SkipTest()

        with TempFile() as tmp:
            self.client = HTTPClient('https://www.google.com/',
                                     ssl_ca_certs=tmp)
            try:
                self.client.open('https://www.google.com/')
            except HTTPClientError as e:
                assert_re(e.args[0], r'Could not verify connection to URL')
Exemplo n.º 11
0
    def test_timeouts(self):
        test_req = ({
            'path': '/',
            'req_assert_function': lambda x: time.sleep(0.9) or True
        }, {
            'body': b'nothing'
        })

        import mapproxy.client.http

        client1 = HTTPClient(timeout=0.1)
        client2 = HTTPClient(timeout=0.5)
        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client1.open(TESTSERVER_URL + '/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration1 = time.time() - start

        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client2.open(TESTSERVER_URL + '/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration2 = time.time() - start

        # check individual timeouts
        assert 0.1 <= duration1 < 0.5, duration1
        assert 0.5 <= duration2 < 0.9, duration2
Exemplo n.º 12
0
def create_source(raster_source, app_state):
    url = raster_source.url
    username = raster_source.username
    password = raster_source.password

    http_client = HTTPClient(url, username, password)
                            # , insecure=insecure,
                             # ssl_ca_certs=ssl_ca_certs, timeout=timeout,
                             # headers=headers)

    grid = DEFAULT_GRID
    image_opts = None
    coverage = coverage_from_geojson(raster_source.download_coverage)
    format = raster_source.format

    url_template = TileURLTemplate(
        '%s/%s/%s-%%(z)s-%%(x)s-%%(y)s/tile' % (url, raster_source.layer, raster_source.matrix_set),
        format=format)
    client = TileClient(url_template, http_client=http_client, grid=grid)

    if app_state.tilebox.is_running():
        port = app_state.config.get('tilebox', 'port')
        url_template = TileURLTemplate(
            'http://127.0.0.1:%d/%s/%s-%%(z)s-%%(x)s-%%(y)s/tile' % (port, raster_source.layer, raster_source.matrix_set),
            format=format)
        tilebox_client =  TileClient(url_template, http_client=RequestsHTTPClient(), grid=grid)
        client = FallbackTileClient(tilebox_client, client)

    return TiledSource(grid, client, coverage=coverage, image_opts=image_opts)
Exemplo n.º 13
0
 def __init__(self, request_template, http_client=None,
              http_method=None, lock=None, fwd_req_params=None):
     self.request_template = request_template
     self.http_client = http_client or HTTPClient()
     self.http_method = http_method
     self.lock = lock
     self.fwd_req_params = fwd_req_params or set()
Exemplo n.º 14
0
 def __init__(self, request_template, supported_srs=None, http_client=None):
     self.request_template = request_template
     self.http_client = http_client or HTTPClient()
     if not supported_srs and self.request_template.params.srs is not None:
         supported_srs = SupportedSRS(
             [SRS(self.request_template.params.srs)])
     self.supported_srs = supported_srs
Exemplo n.º 15
0
    def test_https_valid_cert(self):
        try:
            import ssl; ssl
        except ImportError:
            raise SkipTest()

        cert_file = '/etc/ssl/certs/ca-certificates.crt'
        if os.path.exists(cert_file):
            self.client = HTTPClient('https://www.google.com/', ssl_ca_certs=cert_file)
            self.client.open('https://www.google.com/')
        else:
            with TempFile() as tmp:
                with open(tmp, 'wb') as f:
                    f.write(GOOGLE_ROOT_CERT)
                self.client = HTTPClient('https://www.google.com/', ssl_ca_certs=tmp)
                self.client.open('https://www.google.com/')
Exemplo n.º 16
0
    def test_basic_auth(self):
        http_client = HTTPClient(self.req_template.url,
                                 username='******',
                                 password='******')
        self.client.http_client = http_client

        def assert_auth(req_handler):
            assert 'Authorization' in req_handler.headers
            auth_data = req_handler.headers['Authorization'].split()[1]
            auth_data = base64.b64decode(
                auth_data.encode('utf-8')).decode('utf-8')
            eq_(auth_data, 'foo:bar@')
            return True

        expected_req = ({
            'path':
            r'/service?LAYERS=foo&SERVICE=WMS&FORMAT=image%2Fpng'
            '&REQUEST=GetMap&HEIGHT=512&SRS=EPSG%3A4326'
            '&VERSION=1.1.1&BBOX=0.0,10.0,10.0,20.0&WIDTH=512&STYLES=',
            'require_basic_auth':
            True,
            'req_assert_function':
            assert_auth
        }, {
            'body': b'no image',
            'headers': {
                'content-type': 'image/png'
            }
        })
        with mock_httpd(TEST_SERVER_ADDRESS, [expected_req]):
            q = MapQuery((0.0, 10.0, 10.0, 20.0), (512, 512), SRS(4326))
            self.source.get_map(q)
Exemplo n.º 17
0
 def test_https_no_ssl_module_insecure(self):
     from mapproxy.client import http
     old_ssl = http.ssl
     try:
         http.ssl = None
         self.client = HTTPClient('https://www.google.com/', insecure=True)
         self.client.open('https://www.google.com/')
     finally:
         http.ssl = old_ssl
Exemplo n.º 18
0
 def test_https_untrusted_root(self):
     if not supports_ssl_default_context:
         # old python versions require ssl_ca_certs
         raise SkipTest()
     self.client = HTTPClient('https://untrusted-root.badssl.com/')
     try:
         self.client.open('https://untrusted-root.badssl.com/')
     except HTTPClientError as e:
         assert_re(e.args[0], r'Could not verify connection to URL')
Exemplo n.º 19
0
    def test_https_valid_cert(self):
        try:
            import ssl
            ssl
        except ImportError:
            raise SkipTest()

        cert_file = '/etc/ssl/certs/ca-certificates.crt'
        if os.path.exists(cert_file):
            self.client = HTTPClient('https://www.google.com/',
                                     ssl_ca_certs=cert_file)
            self.client.open('https://www.google.com/')
        else:
            with TempFile() as tmp:
                with open(tmp, 'wb') as f:
                    f.write(GOOGLE_ROOT_CERT)
                self.client = HTTPClient('https://www.google.com/',
                                         ssl_ca_certs=tmp)
                self.client.open('https://www.google.com/')
Exemplo n.º 20
0
 def __init__(self, request_template, supported_srs=None, http_client=None,
         return_geometries=False,
         tolerance=5,
     ):
     self.request_template = request_template
     self.http_client = http_client or HTTPClient()
     if not supported_srs and self.request_template.params.srs is not None:
         supported_srs = [SRS(self.request_template.params.srs)]
     self.supported_srs = supported_srs or []
     self.return_geometries = return_geometries
     self.tolerance = tolerance
Exemplo n.º 21
0
 def test_https_invalid_cert(self):
     # load 'wrong' root cert
     with TempFile() as tmp:
         with open(tmp, 'wb') as f:
             f.write(GOOGLE_ROOT_CERT)
         self.client = HTTPClient(
             'https://untrusted-root.badssl.com/', ssl_ca_certs=tmp)
         try:
             self.client.open('https://untrusted-root.badssl.com/')
         except HTTPClientError as e:
             assert_re(e.args[0], r'Could not verify connection to URL')
Exemplo n.º 22
0
    def test_https_invalid_cert(self):
        try:
            import ssl; ssl
        except ImportError:
            raise SkipTest()

        with TempFile() as tmp:
            self.client = HTTPClient('https://www.google.com/', ssl_ca_certs=tmp)
            try:
                self.client.open('https://www.google.com/')
            except HTTPClientError as e:
                assert_re(e.args[0], r'Could not verify connection to URL')
Exemplo n.º 23
0
    def test_timeouts(self):
        test_req = ({'path': '/', 'req_assert_function': lambda x: time.sleep(0.9) or True},
                    {'body': b'nothing'})

        import mapproxy.client.http

        client1 = HTTPClient(timeout=0.1)
        client2 = HTTPClient(timeout=0.5)
        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client1.open(TESTSERVER_URL + '/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration1 = time.time() - start

        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client2.open(TESTSERVER_URL + '/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration2 = time.time() - start

        # check individual timeouts
        assert 0.1 <= duration1 < 0.5, duration1
        assert 0.5 <= duration2 < 0.9, duration2
Exemplo n.º 24
0
 def test_https_no_ssl_module_error(self):
     from mapproxy.client import http
     old_ssl = http.ssl
     try:
         http.ssl = None
         try:
             self.client = HTTPClient('https://www.google.com/')
         except ImportError:
             pass
         else:
             assert False, 'no ImportError for missing ssl module'
     finally:
         http.ssl = old_ssl
Exemplo n.º 25
0
 def test_internal_error_hide_exception_url(self):
     try:
         with mock_httpd(TESTSERVER_ADDRESS, [({
                 'path': '/'
         }, {
                 'status': '500',
                 'body': b''
         })]):
             HTTPClient(hide_exception_url=True).open(TESTSERVER_URL + '/')
     except HTTPClientError as e:
         assert_re(e.args[0], r'HTTP Error: 500')
     else:
         assert False, 'expected HTTPClientError'
Exemplo n.º 26
0
 def test_internal_error_hide_error_details(self):
     try:
         with mock_httpd(TESTSERVER_ADDRESS, [({
                 'path': '/'
         }, {
                 'status': '500',
                 'body': b''
         })]):
             HTTPClient(hide_error_details=True).open(TESTSERVER_URL + '/')
     except HTTPClientError as e:
         assert_re(e.args[0],
                   r'HTTP Error \(see logs for URL and reason\).')
     else:
         assert False, 'expected HTTPClientError'
Exemplo n.º 27
0
    def test_manage_cookies_on(self):
        """
        Test behavior of manage_cookies=True. Once the remote server sends a cookie back, it should
        be included in future requests
        """
        self.client = HTTPClient(manage_cookies=True)

        def assert_no_cookie(req_handler):
            return 'Cookie' not in req_handler.headers

        def assert_cookie(req_handler):
            assert 'Cookie' in req_handler.headers
            cookie_name, cookie_val = req_handler.headers['Cookie'].split(
                ';')[0].split('=')
            assert cookie_name == 'testcookie'
            assert cookie_val == '42'
            return True

        test_requests = [({
            'path': '/',
            'req_assert_function': assert_no_cookie
        }, {
            'body': b'nothing',
            'headers': {
                'Set-Cookie': "testcookie=42"
            }
        }),
                         ({
                             'path': '/',
                             'req_assert_function': assert_cookie
                         }, {
                             'body': b'nothing'
                         })]
        with mock_httpd(TESTSERVER_ADDRESS, test_requests):
            self.client.open(TESTSERVER_URL + '/')
            self.client.open(TESTSERVER_URL + '/')
Exemplo n.º 28
0
    def test_timeouts(self):
        test_req = ({
            'path': '/',
            'req_assert_function': lambda x: time.sleep(0.5) or True
        }, {
            'body': b'nothing'
        })

        import mapproxy.client.http

        old_timeout = mapproxy.client.http._max_set_timeout
        mapproxy.client.http._max_set_timeout = None

        client1 = HTTPClient(timeout=0.1)
        client2 = HTTPClient(timeout=0.2)
        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client1.open(TESTSERVER_URL + '/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration1 = time.time() - start

        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client2.open(TESTSERVER_URL + '/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration2 = time.time() - start

        if sys.version_info >= (2, 6):
            # check individual timeouts
            assert 0.1 <= duration1 < 0.2
            assert 0.2 <= duration2 < 0.3
        else:
            # use max timeout in Python 2.5
            assert 0.2 <= duration1 < 0.3
            assert 0.2 <= duration2 < 0.3

        mapproxy.client.http._max_set_timeout = old_timeout
Exemplo n.º 29
0
    def test_timeouts(self):
        test_req = ({'path': '/', 'req_assert_function': lambda x: time.sleep(0.5) or True},
                    {'body': b'nothing'})

        import mapproxy.client.http

        old_timeout = mapproxy.client.http._max_set_timeout
        mapproxy.client.http._max_set_timeout = None

        client1 = HTTPClient(timeout=0.1)
        client2 = HTTPClient(timeout=0.2)
        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client1.open(TESTSERVER_URL+'/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration1 = time.time() - start

        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client2.open(TESTSERVER_URL+'/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration2 = time.time() - start

        if sys.version_info >= (2, 6):
            # check individual timeouts
            assert 0.1 <= duration1 < 0.2
            assert 0.2 <= duration2 < 0.3
        else:
            # use max timeout in Python 2.5
            assert 0.2 <= duration1 < 0.3
            assert 0.2 <= duration2 < 0.3

        mapproxy.client.http._max_set_timeout = old_timeout
Exemplo n.º 30
0
 def setup(self):
     self.client = HTTPClient()
Exemplo n.º 31
0
class TestHTTPClient(object):
    def setup(self):
        self.client = HTTPClient()

    def test_post(self):
        with mock_httpd(TESTSERVER_ADDRESS, [({
                'path': '/service?foo=bar',
                'method': 'POST'
        }, {
                'status': '200',
                'body': b''
        })]):
            self.client.open(TESTSERVER_URL + '/service', data=b"foo=bar")

    def test_internal_error_response(self):
        try:
            with mock_httpd(TESTSERVER_ADDRESS, [({
                    'path': '/'
            }, {
                    'status': '500',
                    'body': b''
            })]):
                self.client.open(TESTSERVER_URL + '/')
        except HTTPClientError as e:
            assert_re(e.args[0], r'HTTP Error ".*": 500')
        else:
            assert False, 'expected HTTPClientError'

    def test_invalid_url_type(self):
        try:
            self.client.open('htp://example.org')
        except HTTPClientError as e:
            assert_re(e.args[0],
                      r'No response .* "htp://example.*": unknown url type')
        else:
            assert False, 'expected HTTPClientError'

    def test_invalid_url(self):
        try:
            self.client.open('this is not a url')
        except HTTPClientError as e:
            assert_re(e.args[0],
                      r'URL not correct "this is not.*": unknown url type')
        else:
            assert False, 'expected HTTPClientError'

    def test_unknown_host(self):
        try:
            self.client.open('http://thishostshouldnotexist000136really42.org')
        except HTTPClientError as e:
            assert_re(e.args[0], r'No response .* "http://thishost.*": .*')
        else:
            assert False, 'expected HTTPClientError'

    def test_no_connect(self):
        try:
            self.client.open('http://localhost:53871')
        except HTTPClientError as e:
            assert_re(
                e.args[0],
                r'No response .* "http://localhost.*": Connection refused')
        else:
            assert False, 'expected HTTPClientError'

    def test_internal_error_hide_error_details(self):
        try:
            with mock_httpd(TESTSERVER_ADDRESS, [({
                    'path': '/'
            }, {
                    'status': '500',
                    'body': b''
            })]):
                HTTPClient(hide_error_details=True).open(TESTSERVER_URL + '/')
        except HTTPClientError as e:
            assert_re(e.args[0],
                      r'HTTP Error \(see logs for URL and reason\).')
        else:
            assert False, 'expected HTTPClientError'

    @pytest.mark.online
    def test_https_untrusted_root(self):
        if not supports_ssl_default_context:
            raise pytest.skip("old python versions require ssl_ca_certs")
        self.client = HTTPClient('https://untrusted-root.badssl.com/')
        try:
            self.client.open('https://untrusted-root.badssl.com/')
        except HTTPClientError as e:
            assert_re(e.args[0], r'Could not verify connection to URL')

    @pytest.mark.online
    def test_https_insecure(self):
        self.client = HTTPClient('https://untrusted-root.badssl.com/',
                                 insecure=True)
        self.client.open('https://untrusted-root.badssl.com/')

    @pytest.mark.online
    def test_https_valid_ca_cert_file(self):
        # verify with fixed ca_certs file
        cert_file = '/etc/ssl/certs/ca-certificates.crt'
        if os.path.exists(cert_file):
            self.client = HTTPClient('https://www.google.com/',
                                     ssl_ca_certs=cert_file)
            self.client.open('https://www.google.com/')
        else:
            with TempFile() as tmp:
                with open(tmp, 'wb') as f:
                    f.write(GOOGLE_ROOT_CERT)
                self.client = HTTPClient('https://www.google.com/',
                                         ssl_ca_certs=tmp)
                self.client.open('https://www.google.com/')

    @pytest.mark.online
    def test_https_valid_default_cert(self):
        # modern python should verify by default
        if not supports_ssl_default_context:
            raise pytest.skip("old python versions require ssl_ca_certs")
        self.client = HTTPClient('https://www.google.com/')
        self.client.open('https://www.google.com/')

    @pytest.mark.online
    def test_https_invalid_cert(self):
        # load 'wrong' root cert
        with TempFile() as tmp:
            with open(tmp, 'wb') as f:
                f.write(GOOGLE_ROOT_CERT)
            self.client = HTTPClient('https://untrusted-root.badssl.com/',
                                     ssl_ca_certs=tmp)
            try:
                self.client.open('https://untrusted-root.badssl.com/')
            except HTTPClientError as e:
                assert_re(e.args[0], r'Could not verify connection to URL')

    def test_timeouts(self):
        test_req = ({
            'path': '/',
            'req_assert_function': lambda x: time.sleep(0.9) or True
        }, {
            'body': b'nothing'
        })

        import mapproxy.client.http

        client1 = HTTPClient(timeout=0.1)
        client2 = HTTPClient(timeout=0.5)
        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client1.open(TESTSERVER_URL + '/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration1 = time.time() - start

        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client2.open(TESTSERVER_URL + '/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration2 = time.time() - start

        # check individual timeouts
        assert 0.1 <= duration1 < 0.5, duration1
        assert 0.5 <= duration2 < 0.9, duration2

    def test_manage_cookies_off(self):
        """
        Test the behavior when manage_cookies is off (the default). Cookies shouldn't be sent
        """
        self.client = HTTPClient()

        def assert_no_cookie(req_handler):
            return 'Cookie' not in req_handler.headers

        test_requests = [({
            'path': '/',
            'req_assert_function': assert_no_cookie
        }, {
            'body': b'nothing',
            'headers': {
                'Set-Cookie': "testcookie=42"
            }
        }),
                         ({
                             'path': '/',
                             'req_assert_function': assert_no_cookie
                         }, {
                             'body': b'nothing'
                         })]
        with mock_httpd(TESTSERVER_ADDRESS, test_requests):
            self.client.open(TESTSERVER_URL + '/')
            self.client.open(TESTSERVER_URL + '/')

    def test_manage_cookies_on(self):
        """
        Test behavior of manage_cookies=True. Once the remote server sends a cookie back, it should
        be included in future requests
        """
        self.client = HTTPClient(manage_cookies=True)

        def assert_no_cookie(req_handler):
            return 'Cookie' not in req_handler.headers

        def assert_cookie(req_handler):
            assert 'Cookie' in req_handler.headers
            cookie_name, cookie_val = req_handler.headers['Cookie'].split(
                ';')[0].split('=')
            assert cookie_name == 'testcookie'
            assert cookie_val == '42'
            return True

        test_requests = [({
            'path': '/',
            'req_assert_function': assert_no_cookie
        }, {
            'body': b'nothing',
            'headers': {
                'Set-Cookie': "testcookie=42"
            }
        }),
                         ({
                             'path': '/',
                             'req_assert_function': assert_cookie
                         }, {
                             'body': b'nothing'
                         })]
        with mock_httpd(TESTSERVER_ADDRESS, test_requests):
            self.client.open(TESTSERVER_URL + '/')
            self.client.open(TESTSERVER_URL + '/')
Exemplo n.º 32
0
class TestHTTPClient(object):
    def setup(self):
        self.client = HTTPClient()

    def test_post(self):
        with mock_httpd(TESTSERVER_ADDRESS, [({
                'path': '/service?foo=bar',
                'method': 'POST'
        }, {
                'status': '200',
                'body': b''
        })]):
            self.client.open(TESTSERVER_URL + '/service', data=b"foo=bar")

    def test_internal_error_response(self):
        try:
            with mock_httpd(TESTSERVER_ADDRESS, [({
                    'path': '/'
            }, {
                    'status': '500',
                    'body': b''
            })]):
                self.client.open(TESTSERVER_URL + '/')
        except HTTPClientError as e:
            assert_re(e.args[0], r'HTTP Error ".*": 500')
        else:
            assert False, 'expected HTTPClientError'

    def test_invalid_url_type(self):
        try:
            self.client.open('htp://example.org')
        except HTTPClientError as e:
            assert_re(e.args[0],
                      r'No response .* "htp://example.*": unknown url type')
        else:
            assert False, 'expected HTTPClientError'

    def test_invalid_url(self):
        try:
            self.client.open('this is not a url')
        except HTTPClientError as e:
            assert_re(e.args[0],
                      r'URL not correct "this is not.*": unknown url type')
        else:
            assert False, 'expected HTTPClientError'

    def test_unknown_host(self):
        try:
            self.client.open('http://thishostshouldnotexist000136really42.org')
        except HTTPClientError as e:
            assert_re(e.args[0], r'No response .* "http://thishost.*": .*')
        else:
            assert False, 'expected HTTPClientError'

    def test_no_connect(self):
        try:
            self.client.open('http://*****:*****@attr('online')
    def test_https_no_ssl_module_error(self):
        from mapproxy.client import http
        old_ssl = http.ssl
        try:
            http.ssl = None
            try:
                self.client = HTTPClient('https://www.google.com/')
            except ImportError:
                pass
            else:
                assert False, 'no ImportError for missing ssl module'
        finally:
            http.ssl = old_ssl

    @attr('online')
    def test_https_no_ssl_module_insecure(self):
        from mapproxy.client import http
        old_ssl = http.ssl
        try:
            http.ssl = None
            self.client = HTTPClient('https://www.google.com/', insecure=True)
            self.client.open('https://www.google.com/')
        finally:
            http.ssl = old_ssl

    @attr('online')
    def test_https_valid_cert(self):
        try:
            import ssl
            ssl
        except ImportError:
            raise SkipTest()

        cert_file = '/etc/ssl/certs/ca-certificates.crt'
        if os.path.exists(cert_file):
            self.client = HTTPClient('https://www.google.com/',
                                     ssl_ca_certs=cert_file)
            self.client.open('https://www.google.com/')
        else:
            with TempFile() as tmp:
                with open(tmp, 'wb') as f:
                    f.write(GOOGLE_ROOT_CERT)
                self.client = HTTPClient('https://www.google.com/',
                                         ssl_ca_certs=tmp)
                self.client.open('https://www.google.com/')

    @attr('online')
    def test_https_invalid_cert(self):
        try:
            import ssl
            ssl
        except ImportError:
            raise SkipTest()

        with TempFile() as tmp:
            self.client = HTTPClient('https://www.google.com/',
                                     ssl_ca_certs=tmp)
            try:
                self.client.open('https://www.google.com/')
            except HTTPClientError as e:
                assert_re(e.args[0], r'Could not verify connection to URL')

    def test_timeouts(self):
        test_req = ({
            'path': '/',
            'req_assert_function': lambda x: time.sleep(0.5) or True
        }, {
            'body': b'nothing'
        })

        import mapproxy.client.http

        old_timeout = mapproxy.client.http._max_set_timeout
        mapproxy.client.http._max_set_timeout = None

        client1 = HTTPClient(timeout=0.1)
        client2 = HTTPClient(timeout=0.2)
        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client1.open(TESTSERVER_URL + '/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration1 = time.time() - start

        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client2.open(TESTSERVER_URL + '/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration2 = time.time() - start

        if sys.version_info >= (2, 6):
            # check individual timeouts
            assert 0.1 <= duration1 < 0.2
            assert 0.2 <= duration2 < 0.3
        else:
            # use max timeout in Python 2.5
            assert 0.2 <= duration1 < 0.3
            assert 0.2 <= duration2 < 0.3

        mapproxy.client.http._max_set_timeout = old_timeout
Exemplo n.º 33
0
 def test_https_valid_default_cert(self):
     # modern python should verify by default
     if not supports_ssl_default_context:
         raise SkipTest()
     self.client = HTTPClient('https://www.google.com/')
     self.client.open('https://www.google.com/')
Exemplo n.º 34
0
 def test_https_valid_default_cert(self):
     # modern python should verify by default
     if not supports_ssl_default_context:
         raise pytest.skip("old python versions require ssl_ca_certs")
     self.client = HTTPClient('https://www.google.com/')
     self.client.open('https://www.google.com/')
Exemplo n.º 35
0
 def test_https_insecure(self):
     self.client = HTTPClient(
         'https://untrusted-root.badssl.com/', insecure=True)
     self.client.open('https://untrusted-root.badssl.com/')
Exemplo n.º 36
0
class TestHTTPClient(object):
    def setup(self):
        self.client = HTTPClient()

    def test_post(self):
        with mock_httpd(TESTSERVER_ADDRESS, [({'path': '/service?foo=bar', 'method': 'POST'},
                                              {'status': '200', 'body': b''})]):
            self.client.open(TESTSERVER_URL + '/service', data=b"foo=bar")

    def test_internal_error_response(self):
        try:
            with mock_httpd(TESTSERVER_ADDRESS, [({'path': '/'},
                                                  {'status': '500', 'body': b''})]):
                self.client.open(TESTSERVER_URL + '/')
        except HTTPClientError as e:
            assert_re(e.args[0], r'HTTP Error ".*": 500')
        else:
            assert False, 'expected HTTPClientError'
    def test_invalid_url_type(self):
        try:
            self.client.open('htp://example.org')
        except HTTPClientError as e:
            assert_re(e.args[0], r'No response .* "htp://example.*": unknown url type')
        else:
            assert False, 'expected HTTPClientError'
    def test_invalid_url(self):
        try:
            self.client.open('this is not a url')
        except HTTPClientError as e:
            assert_re(e.args[0], r'URL not correct "this is not.*": unknown url type')
        else:
            assert False, 'expected HTTPClientError'
    def test_unknown_host(self):
        try:
            self.client.open('http://thishostshouldnotexist000136really42.org')
        except HTTPClientError as e:
            assert_re(e.args[0], r'No response .* "http://thishost.*": .*')
        else:
            assert False, 'expected HTTPClientError'
    def test_no_connect(self):
        try:
            self.client.open('http://*****:*****@attr('online')
    def test_https_untrusted_root(self):
        if not supports_ssl_default_context:
            # old python versions require ssl_ca_certs
            raise SkipTest()
        self.client = HTTPClient('https://untrusted-root.badssl.com/')
        try:
            self.client.open('https://untrusted-root.badssl.com/')
        except HTTPClientError as e:
            assert_re(e.args[0], r'Could not verify connection to URL')

    @attr('online')
    def test_https_insecure(self):
        self.client = HTTPClient(
            'https://untrusted-root.badssl.com/', insecure=True)
        self.client.open('https://untrusted-root.badssl.com/')

    @attr('online')
    def test_https_valid_ca_cert_file(self):
        # verify with fixed ca_certs file
        cert_file = '/etc/ssl/certs/ca-certificates.crt'
        if os.path.exists(cert_file):
            self.client = HTTPClient('https://www.google.com/', ssl_ca_certs=cert_file)
            self.client.open('https://www.google.com/')
        else:
            with TempFile() as tmp:
                with open(tmp, 'wb') as f:
                    f.write(GOOGLE_ROOT_CERT)
                self.client = HTTPClient('https://www.google.com/', ssl_ca_certs=tmp)
                self.client.open('https://www.google.com/')

    @attr('online')
    def test_https_valid_default_cert(self):
        # modern python should verify by default
        if not supports_ssl_default_context:
            raise SkipTest()
        self.client = HTTPClient('https://www.google.com/')
        self.client.open('https://www.google.com/')

    @attr('online')
    def test_https_invalid_cert(self):
        # load 'wrong' root cert
        with TempFile() as tmp:
            with open(tmp, 'wb') as f:
                f.write(GOOGLE_ROOT_CERT)
            self.client = HTTPClient(
                'https://untrusted-root.badssl.com/', ssl_ca_certs=tmp)
            try:
                self.client.open('https://untrusted-root.badssl.com/')
            except HTTPClientError as e:
                assert_re(e.args[0], r'Could not verify connection to URL')

    def test_timeouts(self):
        test_req = ({'path': '/', 'req_assert_function': lambda x: time.sleep(0.9) or True},
                    {'body': b'nothing'})

        import mapproxy.client.http

        client1 = HTTPClient(timeout=0.1)
        client2 = HTTPClient(timeout=0.5)
        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client1.open(TESTSERVER_URL + '/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration1 = time.time() - start

        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client2.open(TESTSERVER_URL + '/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration2 = time.time() - start

        # check individual timeouts
        assert 0.1 <= duration1 < 0.5, duration1
        assert 0.5 <= duration2 < 0.9, duration2
Exemplo n.º 37
0
 def __init__(self, request_template, http_client=None):
     self.request_template = request_template
     self.http_client = http_client or HTTPClient()
Exemplo n.º 38
0
 def __init__(self, static_url, http_client=None):
     self.url = static_url
     self.http_client = http_client or HTTPClient()
Exemplo n.º 39
0
class TestHTTPClient(object):
    def setup(self):
        self.client = HTTPClient()

    def test_post(self):
        with mock_httpd(TESTSERVER_ADDRESS, [({'path': '/service?foo=bar', 'method': 'POST'},
                                              {'status': '200', 'body': b''})]):
            self.client.open(TESTSERVER_URL + '/service', data=b"foo=bar")

    def test_internal_error_response(self):
        try:
            with mock_httpd(TESTSERVER_ADDRESS, [({'path': '/'},
                                                  {'status': '500', 'body': b''})]):
                self.client.open(TESTSERVER_URL + '/')
        except HTTPClientError as e:
            assert_re(e.args[0], r'HTTP Error ".*": 500')
        else:
            assert False, 'expected HTTPClientError'
    def test_invalid_url_type(self):
        try:
            self.client.open('htp://example.org')
        except HTTPClientError as e:
            assert_re(e.args[0], r'No response .* "htp://example.*": unknown url type')
        else:
            assert False, 'expected HTTPClientError'
    def test_invalid_url(self):
        try:
            self.client.open('this is not a url')
        except HTTPClientError as e:
            assert_re(e.args[0], r'URL not correct "this is not.*": unknown url type')
        else:
            assert False, 'expected HTTPClientError'
    def test_unknown_host(self):
        try:
            self.client.open('http://thishostshouldnotexist000136really42.org')
        except HTTPClientError as e:
            assert_re(e.args[0], r'No response .* "http://thishost.*": .*')
        else:
            assert False, 'expected HTTPClientError'
    def test_no_connect(self):
        try:
            self.client.open('http://*****:*****@attr('online')
    def test_https_no_ssl_module_error(self):
        from mapproxy.client import http
        old_ssl = http.ssl
        try:
            http.ssl = None
            try:
                self.client = HTTPClient('https://www.google.com/')
            except ImportError:
                pass
            else:
                assert False, 'no ImportError for missing ssl module'
        finally:
            http.ssl = old_ssl

    @attr('online')
    def test_https_no_ssl_module_insecure(self):
        from mapproxy.client import http
        old_ssl = http.ssl
        try:
            http.ssl = None
            self.client = HTTPClient('https://www.google.com/', insecure=True)
            self.client.open('https://www.google.com/')
        finally:
            http.ssl = old_ssl

    @attr('online')
    def test_https_valid_cert(self):
        try:
            import ssl; ssl
        except ImportError:
            raise SkipTest()

        cert_file = '/etc/ssl/certs/ca-certificates.crt'
        if os.path.exists(cert_file):
            self.client = HTTPClient('https://www.google.com/', ssl_ca_certs=cert_file)
            self.client.open('https://www.google.com/')
        else:
            with TempFile() as tmp:
                with open(tmp, 'wb') as f:
                    f.write(GOOGLE_ROOT_CERT)
                self.client = HTTPClient('https://www.google.com/', ssl_ca_certs=tmp)
                self.client.open('https://www.google.com/')

    @attr('online')
    def test_https_invalid_cert(self):
        try:
            import ssl; ssl
        except ImportError:
            raise SkipTest()

        with TempFile() as tmp:
            self.client = HTTPClient('https://www.google.com/', ssl_ca_certs=tmp)
            try:
                self.client.open('https://www.google.com/')
            except HTTPClientError as e:
                assert_re(e.args[0], r'Could not verify connection to URL')

    def test_timeouts(self):
        test_req = ({'path': '/', 'req_assert_function': lambda x: time.sleep(0.9) or True},
                    {'body': b'nothing'})

        import mapproxy.client.http

        old_timeout = mapproxy.client.http._max_set_timeout
        mapproxy.client.http._max_set_timeout = None

        client1 = HTTPClient(timeout=0.1)
        client2 = HTTPClient(timeout=0.5)
        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client1.open(TESTSERVER_URL+'/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration1 = time.time() - start

        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client2.open(TESTSERVER_URL+'/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration2 = time.time() - start

        # check individual timeouts
        assert 0.1 <= duration1 < 0.5, duration1
        assert 0.5 <= duration2 < 0.9, duration2

        mapproxy.client.http._max_set_timeout = old_timeout
Exemplo n.º 40
0
 def setup(self):
     self.client = HTTPClient()
     self.args = ["command_dummy", "--host", TESTSERVER_URL + "/service"]
Exemplo n.º 41
0
 def setup(self):
     self.client = HTTPClient()
     self.args = ['command_dummy', '--host', TESTSERVER_URL + '/service']
Exemplo n.º 42
0
class TestHTTPClient(object):
    def setup(self):
        self.client = HTTPClient()

    def test_post(self):
        with mock_httpd(TESTSERVER_ADDRESS, [({
                'path': '/service?foo=bar',
                'method': 'POST'
        }, {
                'status': '200',
                'body': b''
        })]):
            self.client.open(TESTSERVER_URL + '/service', data=b"foo=bar")

    def test_internal_error_response(self):
        try:
            with mock_httpd(TESTSERVER_ADDRESS, [({
                    'path': '/'
            }, {
                    'status': '500',
                    'body': b''
            })]):
                self.client.open(TESTSERVER_URL + '/')
        except HTTPClientError as e:
            assert_re(e.args[0], r'HTTP Error ".*": 500')
        else:
            assert False, 'expected HTTPClientError'

    def test_invalid_url_type(self):
        try:
            self.client.open('htp://example.org')
        except HTTPClientError as e:
            assert_re(e.args[0],
                      r'No response .* "htp://example.*": unknown url type')
        else:
            assert False, 'expected HTTPClientError'

    def test_invalid_url(self):
        try:
            self.client.open('this is not a url')
        except HTTPClientError as e:
            assert_re(e.args[0],
                      r'URL not correct "this is not.*": unknown url type')
        else:
            assert False, 'expected HTTPClientError'

    def test_unknown_host(self):
        try:
            self.client.open('http://thishostshouldnotexist000136really42.org')
        except HTTPClientError as e:
            assert_re(e.args[0], r'No response .* "http://thishost.*": .*')
        else:
            assert False, 'expected HTTPClientError'

    def test_no_connect(self):
        try:
            self.client.open('http://*****:*****@attr('online')
    def test_https_untrusted_root(self):
        if not supports_ssl_default_context:
            # old python versions require ssl_ca_certs
            raise SkipTest()
        self.client = HTTPClient('https://untrusted-root.badssl.com/')
        try:
            self.client.open('https://untrusted-root.badssl.com/')
        except HTTPClientError as e:
            assert_re(e.args[0], r'Could not verify connection to URL')

    @attr('online')
    def test_https_insecure(self):
        self.client = HTTPClient('https://untrusted-root.badssl.com/',
                                 insecure=True)
        self.client.open('https://untrusted-root.badssl.com/')

    @attr('online')
    def test_https_valid_ca_cert_file(self):
        # verify with fixed ca_certs file
        cert_file = '/etc/ssl/certs/ca-certificates.crt'
        if os.path.exists(cert_file):
            self.client = HTTPClient('https://www.google.com/',
                                     ssl_ca_certs=cert_file)
            self.client.open('https://www.google.com/')
        else:
            with TempFile() as tmp:
                with open(tmp, 'wb') as f:
                    f.write(GOOGLE_ROOT_CERT)
                self.client = HTTPClient('https://www.google.com/',
                                         ssl_ca_certs=tmp)
                self.client.open('https://www.google.com/')

    @attr('online')
    def test_https_valid_default_cert(self):
        # modern python should verify by default
        if not supports_ssl_default_context:
            raise SkipTest()
        self.client = HTTPClient('https://www.google.com/')
        self.client.open('https://www.google.com/')

    @attr('online')
    def test_https_invalid_cert(self):
        # load 'wrong' root cert
        with TempFile() as tmp:
            with open(tmp, 'wb') as f:
                f.write(GOOGLE_ROOT_CERT)
            self.client = HTTPClient('https://untrusted-root.badssl.com/',
                                     ssl_ca_certs=tmp)
            try:
                self.client.open('https://untrusted-root.badssl.com/')
            except HTTPClientError as e:
                assert_re(e.args[0], r'Could not verify connection to URL')

    def test_timeouts(self):
        test_req = ({
            'path': '/',
            'req_assert_function': lambda x: time.sleep(0.9) or True
        }, {
            'body': b'nothing'
        })

        import mapproxy.client.http

        client1 = HTTPClient(timeout=0.1)
        client2 = HTTPClient(timeout=0.5)
        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client1.open(TESTSERVER_URL + '/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration1 = time.time() - start

        with mock_httpd(TESTSERVER_ADDRESS, [test_req]):
            try:
                start = time.time()
                client2.open(TESTSERVER_URL + '/')
            except HTTPClientError as ex:
                assert 'timed out' in ex.args[0]
            else:
                assert False, 'HTTPClientError expected'
            duration2 = time.time() - start

        # check individual timeouts
        assert 0.1 <= duration1 < 0.5, duration1
        assert 0.5 <= duration2 < 0.9, duration2
Exemplo n.º 43
0
 def setup(self):
     self.client = HTTPClient()
Exemplo n.º 44
0
 def test_https_insecure(self):
     self.client = HTTPClient('https://untrusted-root.badssl.com/',
                              insecure=True)
     self.client.open('https://untrusted-root.badssl.com/')
Exemplo n.º 45
0
 def test_https_valid_default_cert(self):
     # modern python should verify by default
     if not supports_ssl_default_context:
         raise SkipTest()
     self.client = HTTPClient('https://www.google.com/')
     self.client.open('https://www.google.com/')