summaryrefslogtreecommitdiffstats
path: root/Lib/packaging/tests/test_pypi_server.py
blob: 2c4ec0d34c347ea505213935c2cf9124561903c3 (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
84
85
86
87
"""Tests for packaging.command.bdist."""
import sys

import urllib.request
import urllib.parse
import urllib.error

try:
    import threading
    from packaging.tests.pypi_server import PyPIServer, PYPI_DEFAULT_STATIC_PATH
except ImportError:
    threading = None
    PyPIServer = None
    PYPI_DEFAULT_STATIC_PATH = None

from packaging.tests import unittest


@unittest.skipIf(threading is None, "Needs threading")
class PyPIServerTest(unittest.TestCase):

    def test_records_requests(self):
        # We expect that PyPIServer can log our requests
        server = PyPIServer()
        server.default_response_status = 200

        try:
            server.start()
            self.assertEqual(len(server.requests), 0)

            data = b'Rock Around The Bunker'

            headers = {"X-test-header": "Mister Iceberg"}

            request = urllib.request.Request(server.full_address, data, headers)
            urllib.request.urlopen(request)
            self.assertEqual(len(server.requests), 1)
            handler, request_data = server.requests[-1]
            self.assertIn(data, request_data)
            self.assertIn("x-test-header", handler.headers)
            self.assertEqual(handler.headers["x-test-header"], "Mister Iceberg")

        finally:
            server.stop()


    def test_serve_static_content(self):
        # PYPI Mocked server can serve static content from disk.

        def uses_local_files_for(server, url_path):
            """Test that files are served statically (eg. the output from the
            server is the same than the one made by a simple file read.
            """
            url = server.full_address + url_path
            request = urllib.request.Request(url)
            response = urllib.request.urlopen(request)
            with open(PYPI_DEFAULT_STATIC_PATH + "/test_pypi_server"
                      + url_path) as file:
                return response.read().decode() == file.read()

        server = PyPIServer(static_uri_paths=["simple", "external"],
            static_filesystem_paths=["test_pypi_server"])
        server.start()
        try:
            # the file does not exists on the disc, so it might not be served
            url = server.full_address + "/simple/unexisting_page"
            request = urllib.request.Request(url)
            try:
                urllib.request.urlopen(request)
            except urllib.error.HTTPError as e:
                self.assertEqual(e.code, 404)

            # now try serving a content that do exists
            self.assertTrue(uses_local_files_for(server, "/simple/index.html"))

            # and another one in another root path
            self.assertTrue(uses_local_files_for(server, "/external/index.html"))

        finally:
            server.stop()


def test_suite():
    return unittest.makeSuite(PyPIServerTest)

if __name__ == '__main__':
    unittest.main(defaultTest="test_suite")