Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_default_synse_error(self, exception, code):
handler = errors.SynseErrorHandler()
actual = handler.default(None, exception('test'))
assert isinstance(actual, HTTPResponse)
assert actual.status == code
data = json.loads(actual.body)
assert data['description'] == exception.description
assert data['context'] == 'test'
assert data['http_code'] == code
assert 'timestamp' in data
async def test_synse_read_route(mock_read, no_pretty_json):
"""Test a successful read."""
result = await read_route(
utils.make_request('/synse/read'),
'rack-1', 'vec', '123456'
)
assert isinstance(result, HTTPResponse)
assert result.body == b'{"value":1}'
assert result.status == 200
async def test_synse_scan_route(mock_scan, no_pretty_json):
"""Test a successful scan."""
r = utils.make_request('/synse/scan')
result = await scan_route(r)
assert isinstance(result, HTTPResponse)
assert result.body == b'{"r":null,"b":null,"forced":false}'
assert result.status == 200
async def test_synse_power_read(mock_validate_device_type, mock_read, no_pretty_json):
"""Test a successful read."""
r = utils.make_request('/synse/power')
result = await power_route(r, 'rack-1', 'vec', '123456')
assert isinstance(result, HTTPResponse)
assert result.body == b'{"value":1}'
assert result.status == 200
def make_response(status, headers, payload, environ):
"""This function generates an appropriate response object for this async
mode.
"""
headers_dict = {}
content_type = None
for h in headers:
if h[0].lower() == 'content-type':
content_type = h[1]
else:
headers_dict[h[0]] = h[1]
return HTTPResponse(body_bytes=payload, content_type=content_type,
status=int(status.split()[0]), headers=headers_dict)
async def route_wrapper(self, route, req, context, request_args, request_kw,
*decorator_args, **decorator_kw):
_ = decorator_kw.pop('with_context') # ignore this.
_options = decorator_kw
options = get_cors_options(context.app, _options)
if options.get('automatic_options') and req.method == 'OPTIONS':
resp = response.HTTPResponse()
else:
resp = route(req, *request_args, **request_kw)
while isawaitable(resp):
resp = await resp
# resp can be `None` or `[]` if using Websockets
if not resp:
return None
request_context = context.request[id(req)]
set_cors_headers(req, resp, context, options)
request_context[SANIC_CORS_EVALUATED] = "1"
return resp
def raw(
body, status=200, headers=None, content_type="application/octet-stream"
):
"""
Returns response object without encoding the body.
:param body: Response data.
:param status: Response code.
:param headers: Custom Headers.
:param content_type: the content type (string) of the response.
"""
return HTTPResponse(
body_bytes=body,
status=status,
headers=headers,
content_type=content_type,
)
async def swagger(request):
return HTTPResponse(
body_bytes=encoded_spec,
headers={
"Access-Control-Allow-Origin": "*"
},
content_type="application/json",
)
async def route_wrapper(self, route, req, context, request_args, request_kw,
*decorator_args, **decorator_kw):
_ = decorator_kw.pop('with_context') # ignore this.
_options = decorator_kw
options = get_cors_options(context.app, _options)
if options.get('automatic_options') and req.method == 'OPTIONS':
resp = response.HTTPResponse()
else:
resp = route(req, *request_args, **request_kw)
while isawaitable(resp):
resp = await resp
# resp can be `None` or `[]` if using Websockets
if not resp:
return None
request_context = context.request[id(req)]
set_cors_headers(req, resp, context, options)
request_context[SANIC_CORS_EVALUATED] = "1"
return resp
return await self.render_graphiql(
params=all_params[0],
result=result
)
return HTTPResponse(
result,
status=status_code,
content_type='application/json'
)
else:
return self.process_preflight(request)
except HttpQueryError as e:
return HTTPResponse(
self.encode({
'errors': [default_format_error(e)]
}),
status=e.status_code,
headers=e.headers,
content_type='application/json'
)