def test_valid_request_with_view_args(self): unregister_metrics() self.server = Server([{ '/testing/<int:a>/<b>/<path:c>': { 'actions': [{ 'log': { 'message': 'Parameters: ' 'a={{ request.view_args["a"] }} ' 'b={{ request.view_args["b"] }} ' 'c={{ request.view_args["c"] }} ' 'x={{ request.view_args["x"] }} <' } }] } }]) self.server.app.testing = True self.client = self.server.app.test_client() with capture_stream() as sout: response = self.client.post('/testing/1/abc/def/ghi', data='{}', content_type='application/json') self.assertEqual(200, response.status_code) content = sout.dumps() self.assertIn('a=1', content) self.assertIn('b=abc', content) self.assertIn('c=def/ghi', content) self.assertIn('x= <', content)
def test_valid_request_with_view_args(self): unregister_metrics() self.server = Server([{ "/testing/<int:a>/<b>/<path:c>": { "actions": [{ "log": { "message": "Parameters: " 'a={{ request.view_args["a"] }} ' 'b={{ request.view_args["b"] }} ' 'c={{ request.view_args["c"] }} ' 'x={{ request.view_args["x"] }} <' } }] } }]) self.server.app.testing = True self.client = self.server.app.test_client() with capture_stream() as sout: response = self.client.post("/testing/1/abc/def/ghi", data="{}", content_type="application/json") self.assertEqual(200, response.status_code) content = sout.dumps() self.assertIn("a=1", content) self.assertIn("b=abc", content) self.assertIn("c=def/ghi", content) self.assertIn("x= <", content)
def test_fail_on_error(self): with capture_stream('stderr') as stderr: output = self._invoke_http( headers={'X-Fail': '500'}, fail_on_error=True, expected_status_code=500 ) error = stderr.dumps() self.assertIn('ActionInvocationException: HTTP call failed (HTTP 500)', error)
def test_fail_on_error(self): with capture_stream("stderr") as stderr: output = self._invoke_http( headers={"X-Fail": "500"}, fail_on_error=True, expected_status_code=500 ) error = stderr.dumps() self.assertIn( "ActionInvocationException: HTTP call failed (HTTP 500)", error )
def test_replay(self): import requests helper.initialize() # give it some time to start up time.sleep(1) # use the Flask test client instead of requests original_requests_request = requests.request def test_request(method, url, headers, json): self.assertTrue(url.endswith('/testing'), msg='Unexpected URL: %s' % url) self.assertIn('testing', json) self.assertEqual(json['testing'], True) return self._client.open(url, method=method, headers=headers, data=jsonlib.dumps(json)) requests.request = test_request try: invocations = [] @action('remember') class RememberAction(Action): def _run(self): invocations.append(1) self._invoke([{ 'log': { 'message': 'Invoked the testing endpoint' }, 'remember': {}, 'eval': { 'block': '{{ replay(0.33) }}' } }]) self.assertEqual(1, sum(invocations)) with capture_stream(echo=False): time.sleep(2) self.assertGreater(sum(invocations), 1) finally: requests.request = original_requests_request helper.shutdown()
def test_replay(self): import requests helper.initialize() # give it some time to start up time.sleep(1) # use the Flask test client instead of requests original_requests_request = requests.request def test_request(method, url, headers, json): self.assertTrue(url.endswith("/testing"), msg="Unexpected URL: %s" % url) self.assertIn("testing", json) self.assertEqual(json["testing"], True) return self._client.open( url, method=method, headers=headers, data=jsonlib.dumps(json) ) requests.request = test_request try: invocations = [] @action("remember") class RememberAction(Action): def _run(self): invocations.append(1) self._invoke( [ { "log": {"message": "Invoked the testing endpoint"}, "remember": {}, "eval": {"block": "{{ replay(0.33) }}"}, } ] ) self.assertEqual(1, sum(invocations)) with capture_stream(echo=False): time.sleep(2) self.assertGreater(sum(invocations), 1) finally: requests.request = original_requests_request helper.shutdown()
def test_raising_error(self): actions = [{'log': {'message': '{% if request.json.fail %}\n' ' {{ error("Failing with : %s"|format(request.json.fail)) }}\n' '{% else %}\n' ' All good\n' '{% endif %}'}}] output = self._invoke(actions) self.assertIn('All good', output) with capture_stream('stderr', echo=True) as output: self._invoke(actions, expected_status_code=500, body={'fail': 'test-failure'}) output = output.dumps() self.assertIn('Failing with : test-failure', output)
def test_raising_error(self): actions = [{ "log": { "message": "{% if request.json.fail %}\n" ' {{ error("Failing with : %s"|format(request.json.fail)) }}\n' "{% else %}\n" " All good\n" "{% endif %}" } }] output = self._invoke(actions) self.assertIn("All good", output) with capture_stream("stderr", echo=True) as output: self._invoke(actions, expected_status_code=500, body={"fail": "test-failure"}) output = output.dumps() self.assertIn("Failing with : test-failure", output)