[05b1088] | 1 | # |
---|
| 2 | # Copyright (C) 2020 Soleta Networks <info@soleta.eu> |
---|
| 3 | # |
---|
| 4 | # This program is free software: you can redistribute it and/or modify it under |
---|
| 5 | # the terms of the GNU Affero General Public License as published by the |
---|
| 6 | # Free Software Foundation, version 3. |
---|
| 7 | # |
---|
| 8 | |
---|
[df98db2] | 9 | from server import Server |
---|
| 10 | from client import Client |
---|
| 11 | import unittest |
---|
| 12 | |
---|
| 13 | class TestShellRunMethods(unittest.TestCase): |
---|
| 14 | |
---|
| 15 | def test_post_with_echo(self): |
---|
| 16 | req_json = '{"run":"echo \\"croqueta\\"", "echo":true}' |
---|
[9890d60] | 17 | response_json = '{"out": \"croqueta\\n\"}' |
---|
[f86999d] | 18 | req = 'POST /shell/run HTTP/1.0\r\nContent-Length: '+ \ |
---|
[df98db2] | 19 | str(len(req_json)) + \ |
---|
[f86999d] | 20 | '\r\nContent-Type: application/json\r\n\r\n' + req_json |
---|
| 21 | resp = 'HTTP/1.0 200 OK\r\nContent-Length: ' + \ |
---|
[df98db2] | 22 | str(len(response_json)) + \ |
---|
[f86999d] | 23 | '\r\nContent-Type: application/json\r\n\r\n' + response_json |
---|
[df98db2] | 24 | c = Client() |
---|
| 25 | s = Server() |
---|
| 26 | s.connect() |
---|
| 27 | s.send(req) |
---|
| 28 | server_response = s.recv() |
---|
| 29 | s.stop() |
---|
| 30 | c.stop() |
---|
| 31 | self.assertEqual(server_response, resp) |
---|
| 32 | |
---|
| 33 | def test_post_without_echo(self): |
---|
| 34 | req_json = '{"run":"echo 1", "echo":false}' |
---|
[f86999d] | 35 | req = 'POST /shell/run HTTP/1.0\r\nContent-Length: '+ \ |
---|
[df98db2] | 36 | str(len(req_json)) + \ |
---|
[f86999d] | 37 | '\r\nContent-Type: application/json\r\n\r\n' + req_json |
---|
[df98db2] | 38 | resp = 'HTTP/1.0 200 OK\r\n\r\n' |
---|
| 39 | c = Client() |
---|
| 40 | s = Server() |
---|
| 41 | s.connect() |
---|
| 42 | s.send(req) |
---|
| 43 | server_response = s.recv() |
---|
| 44 | s.stop() |
---|
| 45 | c.stop() |
---|
| 46 | self.assertEqual(server_response, resp) |
---|
| 47 | |
---|
| 48 | def test_no_json(self): |
---|
| 49 | c = Client() |
---|
| 50 | s = Server() |
---|
[65a5c95] | 51 | s.connect() |
---|
[f86999d] | 52 | s.send('POST /shell/run HTTP/1.0\r\nContent-Length: 0\r\n\r\n') |
---|
[df98db2] | 53 | response = s.recv() |
---|
| 54 | s.stop() |
---|
| 55 | c.stop() |
---|
| 56 | self.assertEqual(response, 'HTTP/1.0 400 Bad Request\r\n\r\n') |
---|
| 57 | |
---|
| 58 | def test_malformed_json(self): |
---|
| 59 | json = '{"wrong_param": 0}' |
---|
| 60 | len_json = str(len(json)) |
---|
[f86999d] | 61 | msg = 'POST /shell/run HTTP/1.0\r\nContent-Length: ' + len_json + \ |
---|
| 62 | '\r\nContent-Type: application/json\r\n\r\n' + json |
---|
[df98db2] | 63 | c = Client() |
---|
| 64 | s = Server() |
---|
[65a5c95] | 65 | s.connect() |
---|
[df98db2] | 66 | s.send(msg) |
---|
| 67 | response = s.recv() |
---|
| 68 | s.stop() |
---|
| 69 | c.stop() |
---|
| 70 | self.assertEqual(response, 'HTTP/1.0 400 Bad Request\r\n\r\n') |
---|
| 71 | |
---|
[65a5c95] | 72 | def test_serial_requests(self): |
---|
| 73 | req1_json = '{"run":"echo 1", "echo":true}' |
---|
[f86999d] | 74 | req1 = 'POST /shell/run HTTP/1.0\r\nContent-Length: '+ \ |
---|
[65a5c95] | 75 | str(len(req1_json)) + \ |
---|
[f86999d] | 76 | '\r\nContent-Type: application/json\r\n\r\n' + req1_json |
---|
[65a5c95] | 77 | req2_json = '{"run":"echo 2", "echo":true}' |
---|
[f86999d] | 78 | req2 = 'POST /shell/run HTTP/1.0\r\nContent-Length: '+ \ |
---|
[65a5c95] | 79 | str(len(req2_json)) + \ |
---|
[f86999d] | 80 | '\r\nContent-Type: application/json\r\n\r\n' + req2_json |
---|
[65a5c95] | 81 | response_json = '{"out": "2\n"}' |
---|
[f86999d] | 82 | resp = 'HTTP/1.0 200 OK\r\nContent-Length: ' + \ |
---|
[65a5c95] | 83 | str(len(response_json)) + \ |
---|
[f86999d] | 84 | '\r\nContent-Type: application/json\r\n\r\n' + response_json |
---|
[65a5c95] | 85 | c = Client() |
---|
| 86 | s = Server() |
---|
| 87 | s.connect() |
---|
| 88 | s.send(req1) |
---|
| 89 | s.send(req2) |
---|
| 90 | client_response = s.recv() |
---|
| 91 | s.stop() |
---|
| 92 | c.stop() |
---|
| 93 | self.assertEqual(client_response, client_response) |
---|
| 94 | |
---|
[df98db2] | 95 | if __name__ == '__main__': |
---|
| 96 | unittest.main() |
---|