def _post_data(self, path, data): norm_path = self._path_join(self._config['metadata_url'], path) LOG.debug('Posting metadata to: %s', norm_path) url_helper.read_url(norm_path, data=data, retries=self._config['retries'], timeout=self._config['timeout'])
def _check_url(url, retries_count=_MAX_URL_CHECK_RETRIES): LOG.debug("Testing url: %s", url) try: url_helper.read_url(url, retries=retries_count) return True except url_helper.UrlError: return False
def test_response_has_url(self): body = b'it worked!' url = 'http://www.yahoo.com/' httpretty.register_uri(httpretty.GET, url, body=body) resp = url_helper.read_url(url) self.assertEqual(resp.url, url) self.assertEqual(body, resp.contents)
def test_url_fetch(self): httpretty.register_uri(httpretty.GET, "http://www.yahoo.com", body=b'it worked!') resp = url_helper.read_url("http://www.yahoo.com") self.assertEqual(b"it worked!", resp.contents) self.assertEqual(url_helper.OK, resp.status_code)
def test_retry_url_fetch(self): httpretty.register_uri(httpretty.GET, "http://www.yahoo.com", responses=[ httpretty.Response(body=b"no worky", status=400), httpretty.Response(body=b"it worked!", status=200), ]) resp = url_helper.read_url("http://www.yahoo.com", retries=2) self.assertEqual(b"it worked!", resp.contents) self.assertEqual(url_helper.OK, resp.status_code)
def test_no_protocol_url(self): body = b'it worked!' no_proto = 'www.yahoo.com' httpretty.register_uri(httpretty.GET, "http://" + no_proto, body=body) resp = url_helper.read_url(no_proto) self.assertTrue(resp.url.startswith("http://"))