Exemplo n.º 1
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.º 2
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.º 3
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.º 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_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.º 7
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.º 8
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.º 9
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.º 10
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.º 11
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 + '/')