source: ogClient-Git/tests/units/test_0003_shellrun.py

Last change on this file was cb9edc8, checked in by OpenGnSys Support Team <soporte-og@…>, 4 years ago

ogClient is AGPLv3+

Update license header in files.

  • Property mode set to 100644
File size: 3.3 KB
Line 
1#
2# Copyright (C) 2020-2021 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; either version 3 of the License, or
7# (at your option) any later version.
8
9from server import Server
10from client import Client
11import unittest
12
13class TestShellRunMethods(unittest.TestCase):
14
15    def test_post_with_echo(self):
16        req_json = '{"run":"echo \\"croqueta\\"", "echo":true}'
17        response_json = '{"out": \"croqueta\\n\"}'
18        req = 'POST /shell/run HTTP/1.0\r\nContent-Length: '+ \
19              str(len(req_json)) + \
20              '\r\nContent-Type: application/json\r\n\r\n' + req_json
21        resp = 'HTTP/1.0 200 OK\r\nContent-Length: ' + \
22               str(len(response_json)) + \
23               '\r\nContent-Type: application/json\r\n\r\n' + response_json
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}'
35        req = 'POST /shell/run HTTP/1.0\r\nContent-Length: '+ \
36              str(len(req_json)) + \
37              '\r\nContent-Type: application/json\r\n\r\n' + req_json
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()
51        s.connect()
52        s.send('POST /shell/run HTTP/1.0\r\nContent-Length: 0\r\n\r\n')
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))
61        msg = 'POST /shell/run HTTP/1.0\r\nContent-Length: ' + len_json + \
62              '\r\nContent-Type: application/json\r\n\r\n' + json
63        c = Client()
64        s = Server()
65        s.connect()
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
72    def test_serial_requests(self):
73        req1_json = '{"run":"echo 1", "echo":true}'
74        req1 = 'POST /shell/run HTTP/1.0\r\nContent-Length: '+ \
75               str(len(req1_json)) + \
76               '\r\nContent-Type: application/json\r\n\r\n' + req1_json
77        req2_json = '{"run":"echo 2", "echo":true}'
78        req2 = 'POST /shell/run HTTP/1.0\r\nContent-Length: '+ \
79               str(len(req2_json)) + \
80               '\r\nContent-Type: application/json\r\n\r\n' + req2_json
81        response_json = '{"out": "2\n"}'
82        resp = 'HTTP/1.0 200 OK\r\nContent-Length: ' + \
83               str(len(response_json)) + \
84               '\r\nContent-Type: application/json\r\n\r\n' + response_json
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
95if __name__ == '__main__':
96    unittest.main()
Note: See TracBrowser for help on using the repository browser.