Exemplo n.º 1
0
    def test_raise_valueerror_if_outside_directory(self):
        """
        If the path leads to outside the directory given to the store, it
        it should raise ``ValueError``.
        """
        with temp_dir() as root_dir, \
                temp_dir(where=root_dir) as d, \
                temp_file(where=root_dir, name='passwd', content='mypass'):

            store = FileStore(directory=d)

            with self.assertRaises(ValueError):
                store.read('/../passwd')
Exemplo n.º 2
0
    def test_serve_serve_404_subdir(self):
        """
        This tests ensure that ``StaticPage`` will return 404 if a file could
        not be found in a subdirectory.
        """
        with temp_dir() as d, \
                temp_dir(where=d, name='a/b/c') as sd, \
                temp_file(where=sd, name='index.html', content='example') as f:

            page = StaticPage(directory=d)

            with Server(page):
                r = requests.get('http://localhost:8000/a/b/c/nofile.html')

                self.assertEquals(404, r.status_code)
Exemplo n.º 3
0
    def test_serve_serve_404_parent_dir(self):
        """
        This tests ensure that ``StaticPage`` will return 404 if a path tries
        to access a parent directory.
        """
        with temp_dir() as root_dir, \
                temp_dir(where=root_dir) as d, \
                temp_file(where=root_dir, name='passwd', content='example'):

            page = StaticPage(directory=d)

            with Server(page):
                r = requests.get('http://localhost:8000/../passwd')

                self.assertEquals(404, r.status_code)
                self.assertNotEquals('example', r.text)
Exemplo n.º 4
0
    def test_serve_subdir(self):
        """
        This tests ensure that ``StaticPage`` will serve the content of files
        from a subdirectory of the served dir.
        """
        with temp_dir() as d, \
                temp_dir(where=d, name='a/b/c') as sd, \
                temp_file(where=sd, name='index.html', content='example') as f:

            page = StaticPage(directory=d)

            with Server(page):
                r = requests.get('http://localhost:8000/a/b/c/index.html')

                self.assertEquals(200, r.status_code)
                self.assertEquals('example', r.text)

                r = requests.get('http://localhost:8000/a/b/c/')

                self.assertEquals(200, r.status_code)
                self.assertEquals('example', r.text)
Exemplo n.º 5
0
    def test_serve_static_page(self):
        """
        This tests ensure that ``StaticPage`` serves the content of a file if
        it is found in its directory.
        """
        with temp_dir() as d, \
                temp_file(where=d, name='index.html', content='example') as f:

            page = StaticPage(directory=d)

            with Server(page):
                r = requests.get('http://localhost:8000/index.html')

                self.assertEquals(200, r.status_code)
                self.assertEquals('example', r.text)
Exemplo n.º 6
0
    def test_page_has_default_index_html(self):
        """
        If we request the ``StaticPage`` root directory (e.g.
        ``http://localhost:8000``) it should provide a document with a basic
        introduction to the object itself.
        """
        with temp_dir() as d:
            store = FileStore(directory=d)
            page = StaticPage(store=store)

            with Server(page):
                r = requests.get('http://localhost:8000/index.html')

                self.assertEquals(200, r.status_code)
                self.assertTrue(r.text)
Exemplo n.º 7
0
    def test_page_can_use_store(self):
        """
        This tests ensure that ``StaticPage`` can receive a store instead of
        a directory directly.
        """
        with temp_dir() as d, \
                temp_file(where=d, name='index.html', content='example') as f:

            store = FileStore(directory=d)
            page = StaticPage(store=store)

            with Server(page):
                r = requests.get('http://localhost:8000/index.html')

                self.assertEquals(200, r.status_code)
                self.assertEquals('example', r.text)
Exemplo n.º 8
0
    def test_serve_index_html(self):
        """
        This tests ensure that ``StaticPage`` will serve the content of
        ``index.html`` if requested to serve a directory.
        """
        with temp_dir() as d, \
                temp_file(where=d, name='index.html', content='example') as f:

            page = StaticPage(directory=d)

            with Server(page):
                r = requests.get('http://localhost:8000/')

                self.assertEquals(200, r.status_code)
                self.assertEquals('example', r.text)

                r = requests.get('http://localhost:8000')

                self.assertEquals(200, r.status_code)
                self.assertEquals('example', r.text)
Exemplo n.º 9
0
 def make_container(self):
     """
     Create a temporary directory to be given to the file store to be
     tested.
     """
     return temp_dir()