def make_document(self, name, where, content='', path=None): """ Create a temporary file to be given to the file store to be tested. """ if path is not None: path = os.path.join(where, path) os.makedirs(path) else: path = where return temp_file(where=path, name=name, content=content)
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')
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_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)