From df98db2c3c11c7b1b96e175dd275bbeb0acb250f Mon Sep 17 00:00:00 2001 From: Roberto Hueso Gómez Date: Thu, 16 Jan 2020 11:55:56 +0100 Subject: Add /shell/run tests These tests cover: - Correct request with echo - Correct request without echo - No json request - Malformed json request --- tests/units/test_0003_shellrun.py | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 tests/units/test_0003_shellrun.py (limited to 'tests/units') diff --git a/tests/units/test_0003_shellrun.py b/tests/units/test_0003_shellrun.py new file mode 100644 index 0000000..5a77346 --- /dev/null +++ b/tests/units/test_0003_shellrun.py @@ -0,0 +1,65 @@ +from server import Server +from client import Client +import unittest + +class TestShellRunMethods(unittest.TestCase): + + def test_post_with_echo(self): + req_json = '{"run":"echo \\"croqueta\\"", "echo":true}' + response_json = '{"out": "\\"croqueta\\"\\n"}' + req = 'POST /shell/run HTTP/1.0\r\nContent-Length:'+ \ + str(len(req_json)) + \ + '\r\nContent-Type:application/json\r\n\r\n' + req_json + resp = 'HTTP/1.0 200 OK\r\nContent-Length:' + \ + str(len(response_json)) + \ + '\r\nContent-Type:application/json\r\n\r\n' + response_json + c = Client() + s = Server() + s.connect() + s.send(req) + server_response = s.recv() + s.stop() + c.stop() + self.assertEqual(server_response, resp) + + def test_post_without_echo(self): + req_json = '{"run":"echo 1", "echo":false}' + req = 'POST /shell/run HTTP/1.0\r\nContent-Length:'+ \ + str(len(req_json)) + \ + '\r\nContent-Type:application/json\r\n\r\n' + req_json + resp = 'HTTP/1.0 200 OK\r\n\r\n' + c = Client() + s = Server() + s.connect() + s.send(req) + server_response = s.recv() + s.stop() + c.stop() + self.assertEqual(server_response, resp) + + def test_no_json(self): + c = Client() + s = Server() + s.connect(probe=False) + s.send('POST /shell/run HTTP/1.0\r\nContent-Length:0\r\n\r\n') + response = s.recv() + s.stop() + c.stop() + self.assertEqual(response, 'HTTP/1.0 400 Bad Request\r\n\r\n') + + def test_malformed_json(self): + json = '{"wrong_param": 0}' + len_json = str(len(json)) + msg = 'POST /shell/run HTTP/1.0\r\nContent-Length:' + len_json + \ + '\r\nContent-Type:application/json\r\n\r\n' + json + c = Client() + s = Server() + s.connect(probe=False) + s.send(msg) + response = s.recv() + s.stop() + c.stop() + self.assertEqual(response, 'HTTP/1.0 400 Bad Request\r\n\r\n') + +if __name__ == '__main__': + unittest.main() -- cgit v1.2.3-18-g5258