def forecast_mock(file): body = file_from_resource(f"{file}.json") request = HTTPRequest(method='POST', body=json.dumps(body), url='about:blank') resp = HTTPResponse(request, HTTPStatus.OK, buffer=json.dumps({})) resp._body = json.dumps(body) resp.text = json.dumps(body) resp.status_code = HTTPStatus.OK return resp
def test_tornado_load(mock_httpclient_fetch): cache = stub(get=lambda url: None, add=lambda url, content: None) response = HTTPResponse(HTTPRequest("http://tests.python-zeep.org/test.xml"), 200) response.buffer = True response._body = "x" mock_httpclient_fetch.return_value = response transport = TornadoAsyncTransport(cache=cache) result = transport.load("http://tests.python-zeep.org/test.xml") assert result == "x"
def build_response(self, request, response): resp = HTTPResponse(request, response.status, headers=response.headers, effective_url=request.url, error=None, buffer="") resp._body = response.data f = Future() f.content = None if response.status < 200 or response.status >= 300: resp.error = HTTPError(response.status, response=resp) ioloop.IOLoop().current().add_callback(f.set_exception, resp.error) else: ioloop.IOLoop().current().add_callback(f.set_result, resp) return f
def test_load(self, mock_httpclient_fetch): cache = stub(get=lambda url: None, add=lambda url, content: None) response = HTTPResponse(HTTPRequest('http://tests.python-zeep.org/test.xml'), 200) response.buffer = True response._body = 'x' mock_httpclient_fetch.return_value = response transport = TornadoAsyncTransport(cache=cache) result = transport.load('http://tests.python-zeep.org/test.xml') assert result == 'x'
def build_response(self, request, response): resp = HTTPResponse(request, response.status, headers=response.headers, effective_url=request.url, error=None, buffer="") resp._body = response.data f = Future() f.content = None if response.status < 200 or response.status >= 300: resp.error = HTTPError(response.status, response=resp) ioloop.IOLoop().current().add_callback(f.set_exception, resp.error) else: ioloop.IOLoop().current().add_callback(f.set_result, resp) return f
async def test_get_cisco_with_brackets(self, mock_http): self.task.api = '' json_data = '{"test_key": "test_value"}' response = HTTPResponse(code=200, buffer='', request=HTTPRequest('test_url')) mock_get = mock_http.instance.return_value.get mock_get.return_value = Future() mock_get.return_value.set_result(response) response._body = json_data.encode() cpe = CPE('cpe:2.3:o:cisco:ios:12.2\(52\)se:*:*:*:*:*:*:*') expected = '/cvefor/cpe%3A2.3%3Ao%3Acisco%3Aios%3A12.2%25252852%252529se%3A%2A%3A%2A%3A%2A%3A%2A%3A%2A%3A%2A%3A%2A' await self.task.api_cvefor(cpe) mock_get.assert_called_once_with(expected)
def test_post(self, mock_httpclient_fetch): cache = stub(get=lambda url: None, add=lambda url, content: None) response = HTTPResponse(HTTPRequest('http://tests.python-zeep.org/test.xml'), 200) response.buffer = True response._body = 'x' http_fetch_future = Future() http_fetch_future.set_result(response) mock_httpclient_fetch.return_value = http_fetch_future transport = TornadoAsyncTransport(cache=cache) envelope = etree.Element('Envelope') result = yield transport.post_xml( 'http://tests.python-zeep.org/test.xml', envelope=envelope, headers={}) assert result.content == 'x' assert result.status_code == 200
async def test_api_cvefor( self, mock_http, ): json_data = '{"test_key": "test_value"}' response = HTTPResponse(code=200, buffer='', request=HTTPRequest('test_url')) mock_get = mock_http.instance.return_value.get mock_get.return_value = Future() mock_get.return_value.set_result(response) response._body = json_data.encode() service = Service() service.cpe = self.cpe_txt expected = {'test_key': 'test_value'} result = await self.task.api_cvefor(service.cpe) self.assertEqual(result, expected) mock_get.assert_called_once_with( 'localhost:200/cvefor/cpe%3A2.3%3Aa%3Amicrosoft%3Ainternet_explorer%3A8.0.6001%3Abeta%3A%2A%3A%2A%3A%2A%3A%2A%3A%2A%3A%2A', )
def test_post(self, mock_httpclient_fetch): cache = stub(get=lambda url: None, add=lambda url, content: None) response = HTTPResponse( HTTPRequest("http://tests.python-zeep.org/test.xml"), 200 ) response.buffer = True response._body = "x" http_fetch_future = Future() http_fetch_future.set_result(response) mock_httpclient_fetch.return_value = http_fetch_future transport = TornadoAsyncTransport(cache=cache) envelope = etree.Element("Envelope") result = yield transport.post_xml( "http://tests.python-zeep.org/test.xml", envelope=envelope, headers={} ) assert result.content == "x" assert result.status_code == 200
def fetch(self, url, method='GET', body=None, headers=None, fail=True, freeze=False, follow_redirects=True, max_redirects=5, **kwargs): if not headers: headers = {} default_headers = copy(self._headers) default_headers.update(headers) headers = default_headers if body is not None and 'Content-Type' not in headers: headers['Content-Type'] = 'application/json' if method in self.METHODS_WITH_BODY and 'body_producer' not in kwargs: body = '' if body is None else body if headers.get('Content-Type', '') == 'application/json': body = self._make_json(body) params = copy(self._default_args) params.update(kwargs) last_exc = RuntimeError("Something wrong") for _ in range(max_redirects + 1): request = HTTPRequest(b(url), method=method, body=body, headers=HTTPHeaders(headers), follow_redirects=False, **params) request.headers['Cookie'] = '; '.join( '{0.key}={0.value}'.format(cookie) for cookie in self._cookies.values()) need_redirect = False try: response = yield self._client.fetch(request) response.fail = False except HTTPError as e: last_exc = e response = e.response if e.code == 599: response = HTTPResponse(request=request, code=e.code, headers=HTTPHeaders(), effective_url=request.url) response.fail = True if e.code in (301, 302, 303, 307) and follow_redirects: need_redirect = True else: response.fail = True if fail and e.response: content_type = e.response.headers.get('Content-Type', '') e.response._body = self._decode_body( content_type, response.body) if e.response.body and 'application/json' in content_type.lower( ): e.response._body = self._parse_json(e.response.body) if not need_redirect: break if not freeze: for cookie in response.headers.get_list('Set-Cookie'): self._cookies.load(cookie) else: response.fail = True if fail and response.fail: raise last_exc content_type = response.headers.get('Content-Type', '') response._body = self._decode_body(content_type, response.body) if response.body and 'json' in response.headers.get( 'Content-Type', ''): new_body = self._parse_json(response.body) response._body = _freeze_response(new_body) if not freeze: for cookie in response.headers.get_list('Set-Cookie'): self._cookies.load(cookie) raise Return(response)
async def dummy_fetch(req, **kwargs): res = HTTPResponse(req, code=200, buffer=BytesIO()) res._body = '{}' return res
def fetch(self, url, method='GET', body=None, headers=None, fail=True, freeze=False, follow_redirects=True, max_redirects=5, **kwargs): if not headers: headers = {} default_headers = copy(self._headers) default_headers.update(headers) headers = default_headers if body is not None and 'Content-Type' not in headers: headers['Content-Type'] = 'application/json' if method in self.METHODS_WITH_BODY and 'body_producer' not in kwargs: body = body or '' if headers.get('Content-Type', '') == 'application/json': body = self._make_json(body) params = copy(self._default_args) params.update(kwargs) last_exc = RuntimeError("Something wrong") for _ in range(max_redirects + 1): request = HTTPRequest( b(url), method=method, body=body, headers=HTTPHeaders(headers), follow_redirects=False, **params ) request.headers['Cookie'] = '; '.join( '{0.key}={0.value}'.format(cookie) for cookie in self._cookies.values() ) need_redirect = False try: response = yield self._client.fetch(request) response.fail = False except HTTPError as e: last_exc = e response = e.response if e.code == 599: response = HTTPResponse( request=request, code=e.code, headers=HTTPHeaders(), effective_url=request.url ) response.fail = True if e.code in (301, 302, 303, 307) and follow_redirects: need_redirect = True else: response.fail = True if fail and e.response: content_type = e.response.headers.get('Content-Type', '') e.response._body = self._decode_body(content_type, response.body) if e.response.body and 'application/json' in content_type.lower(): e.response._body = self._parse_json(e.response.body) if not need_redirect: break if not freeze: for cookie in response.headers.get_list('Set-Cookie'): self._cookies.load(cookie) else: response.fail = True if fail and response.fail: raise last_exc content_type = response.headers.get('Content-Type', '') response._body = self._decode_body(content_type, response.body) if response.body and 'json' in response.headers.get('Content-Type', ''): new_body = self._parse_json(response.body) response._body = _freeze_response(new_body) if not freeze: for cookie in response.headers.get_list('Set-Cookie'): self._cookies.load(cookie) raise Return(response)
async def mocked_requests(*args, **kwargs): response = HTTPResponse(HTTPRequest("kek"), 200) response._body = {"status": "success"} response.buffer = "trash" return response