def test_serve_serve_404(self): """ This tests ensure that ``StaticPage`` will return 404 if a file could not be found. """ 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/nofile.html') self.assertEquals(404, r.status_code)
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)
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)
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)
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)
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)
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)