summaryrefslogtreecommitdiff
path: root/src/tests/test_remote.py
blob: d0805cdffbaee4faa97922df3eb73175fa836441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import tempfile
import threading
import unittest
from http.server import BaseHTTPRequestHandler
from http.server import HTTPServer
from urllib.parse import urlsplit

from vyos.remote import HttpC


class HeadRejectHandler(BaseHTTPRequestHandler):
    body = b'192.0.2.1\n'
    head_status = 405

    def do_HEAD(self):
        self.send_response(self.head_status)
        self.end_headers()

    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-Type', 'text/plain')
        self.send_header('Content-Length', str(len(self.body)))
        self.end_headers()
        self.wfile.write(self.body)

    def log_message(self, format, *args):
        pass


class Head405Handler(HeadRejectHandler):
    head_status = 405


class Head501Handler(HeadRejectHandler):
    head_status = 501


class TestHttpCDownload(unittest.TestCase):
    def _download_from_server(self, handler):
        server = HTTPServer(('127.0.0.1', 0), handler)
        port = server.server_address[1]
        thread = threading.Thread(target=server.serve_forever, daemon=True)
        thread.start()

        url = f'http://127.0.0.1:{port}/list.txt'
        with tempfile.NamedTemporaryFile(delete=False) as tmp:
            tmp_path = tmp.name

        try:
            client = HttpC(urlsplit(url))
            client.download(tmp_path)
            with open(tmp_path, 'rb') as downloaded:
                self.assertEqual(downloaded.read(), handler.body)
        finally:
            server.shutdown()
            server.server_close()
            thread.join(timeout=1)
            os.unlink(tmp_path)

    def test_download_without_head_support_405(self):
        self._download_from_server(Head405Handler)

    def test_download_without_head_support_501(self):
        self._download_from_server(Head501Handler)


if __name__ == '__main__':
    unittest.main()