def test_block_unmocked_requests(): """Ensure the `block_unmocked_requests` fixture prevents requests from hitting the network.""" url = 'https://example.com' with pytest.raises(RuntimeError, match='Tests must mock all HTTP requests!'): fetch_text(url) with responses.RequestsMock() as rsps: rsps.add(responses.GET, url, body='Mocked requests still work') text = fetch_text(url) assert text == 'Mocked requests still work'
def fetch_log(job_log): try: log_text = fetch_text(job_log.url) except HTTPError as e: job_log.update_status(JobLog.FAILED) if e.response is not None and e.response.status_code in (403, 404): logger.warning("Unable to retrieve log for %s: %s", job_log.url, e) return raise if not log_text: return return (json.loads(item) for item in log_text.splitlines())
def download_artifact(root_url, task_id, path): """ Downloads a Taskcluster artifact. Supports specific file formats like json and yaml. Returns either the parsed json, the parsed yaml or the plain response. """ artifact_url = taskcluster_urls.api( root_url, 'queue', 'v1', 'task/{}/artifacts/{}'.format(task_id, path)) if path.endswith(".json"): return fetch_json(artifact_url) if path.endswith(".yml"): return yaml.safe_load(fetch_text(artifact_url)) return make_request(artifact_url)