def on_publish_ok(pub): res = {'id': pub.id} body = dump_json(res, True).encode('utf8') self._complete_request(request, 200, body, log_category="AR200", reason="OK")
def _deny_request(self, request, code, **kwargs): """ Called when client request is denied. """ if "log_category" not in kwargs.keys(): kwargs["log_category"] = "AR" + str(code) self.log.debug(code=code, **kwargs) body = dump_json({"error": log_categories[kwargs['log_category']], "args": [], "kwargs": {}}, True).encode('utf8') request.setResponseCode(code) return body
def test_failure(self): """ A failed call returns the error to the client. """ session = TestSession(types.ComponentConfig(u"realm1")) self.session_factory.add(session, authrole=u"test_role") session2 = ApplicationSession(types.ComponentConfig(u"realm1")) self.session_factory.add(session2, authrole=u"test_role") resource = CallerResource({}, session2) tests = [ ( u"com.myapp.sqrt", (0,), {u"error": u"wamp.error.runtime_error", u"args": [u"don't ask foolish questions ;)"], u"kwargs": {}}, ), (u"com.myapp.checkname", ("foo",), {u"error": u"com.myapp.error.reserved", u"args": [], u"kwargs": {}}), ( u"com.myapp.checkname", ("*",), {u"error": u"com.myapp.error.invalid_length", u"args": [], u"kwargs": {"min": 3, "max": 10}}, ), ( u"com.myapp.checkname", ("hello",), {u"error": u"com.myapp.error.mixed_case", u"args": ["hello", "HELLO"], u"kwargs": {}}, ), (u"com.myapp.compare", (1, 10), {u"error": u"com.myapp.error1", u"args": [9], u"kwargs": {}}), ] for procedure, args, err in tests: with LogCapturer() as l: request = yield renderResource( resource, b"/", method=b"POST", headers={b"Content-Type": [b"application/json"]}, body=dump_json({"procedure": procedure, "args": args}).encode("utf8"), ) self.assertEqual(request.code, 200) self.assertEqual(json.loads(native_string(request.get_written_data())), err) logs = l.get_category("AR458") self.assertEqual(len(logs), 1) self.assertEqual(logs[0]["code"], 200) # We manually logged the errors; we can flush them from the log self.flushLoggedErrors()
def _deny_request(self, request, code, **kwargs): """ Called when client request is denied. """ if "log_category" not in kwargs.keys(): kwargs["log_category"] = "AR" + str(code) self.log.debug(code=code, **kwargs) error_str = log_categories[kwargs['log_category']].format(**kwargs) body = dump_json({"error": error_str, "args": [], "kwargs": {}}, True).encode('utf8') request.setResponseCode(code) return body
def on_call_ok(value): # a WAMP procedure call result may have a single return value, but also # multiple, positional return values as well as keyword-based return values # if isinstance(value, CallResult): res = {} if value.results: res['args'] = value.results if value.kwresults: res['kwargs'] = value.kwresults else: res = {'args': [value]} body = dump_json(res, True).encode('utf8') return self._complete_request(request, 200, body, log_category="AR202")
def on_call_ok(value): # a WAMP procedure call result may have a single return value, but also # multiple, positional return values as well as keyword-based return values # if isinstance(value, CallResult): res = {} if value.results: res['args'] = value.results if value.kwresults: res['kwargs'] = value.kwresults else: res = {'args': [value]} body = dump_json(res, True).encode('utf8') return self._complete_request( request, 200, body, log_category="AR202")
def test_failure(self): """ A failed call returns the error to the client. """ session = TestSession(types.ComponentConfig(u'realm1')) self.session_factory.add(session, authrole=u"test_role") session2 = ApplicationSession(types.ComponentConfig(u'realm1')) self.session_factory.add(session2, authrole=u"test_role") resource = CallerResource({}, session2) tests = [ (u"com.myapp.sqrt", (0,), {u"error": u"wamp.error.runtime_error", u"args": [u"don't ask foolish questions ;)"], u"kwargs": {}}), (u"com.myapp.checkname", ("foo",), {u"error": u"com.myapp.error.reserved", u"args": [], u"kwargs": {}}), (u"com.myapp.checkname", ("*",), {u"error": u"com.myapp.error.invalid_length", u"args": [], u"kwargs": {"min": 3, "max": 10}}), (u"com.myapp.checkname", ("hello",), {u"error": u"com.myapp.error.mixed_case", u"args": ["hello", "HELLO"], u"kwargs": {}}), (u"com.myapp.compare", (1, 10), {u"error": u"com.myapp.error1", u"args": [9], u"kwargs": {}}), ] for procedure, args, err in tests: with LogCapturer() as l: request = yield renderResource( resource, b"/", method=b"POST", headers={b"Content-Type": [b"application/json"]}, body=dump_json({"procedure": procedure, "args": args}).encode('utf8')) self.assertEqual(request.code, 200) self.assertEqual(json.loads(native_string(request.get_written_data())), err) logs = l.get_category("AR458") self.assertEqual(len(logs), 1) self.assertEqual(logs[0]["code"], 200) # We manually logged the errors; we can flush them from the log self.flushLoggedErrors()
def on_publish_ok(pub): res = {'id': pub.id} body = dump_json(res, True).encode('utf8') self._complete_request(request, 200, body, log_category="AR200")