def create_view(self, relative_path, user_interface, file_path=None): if not user_interface is self: raise ProgrammerError('get_file called on %s with %s as user_interface' % (self, user_interface)) file_url_path = file_path filename = self.filesystem_path(file_url_path) logging.debug('Finding a static file on filesystem %s' % filename) if self.is_dynamic(filename): statics = self.statics(file_url_path) slot_contents = {'main_slot': DJHTMLWidget.factory(statics['div'])} return UrlBoundView(user_interface, file_url_path, statics['title'], slot_contents, cacheable=True) elif self.is_static(filename): return FileView(user_interface, FileOnDisk(filename, file_url_path)) raise CannotCreate()
def assemble(self): list_of_files = [FileOnDisk(one_file.name, 'one_file')] self.define_static_files('/morestaticfiles', list_of_files)
def test_file_download_details(web_fixture): """FileDownloadStub (the GET response for a StaticFileResource) works correctly in different scenarios of partial GETting too.""" file_content = b'some content' server_file = temp_file_with(file_content, 'afile.css', mode='w+b') @stubclass(FileDownload) class FileDownloadStub(FileDownload): chunk_size = 1 response = FileDownloadStub( FileOnDisk(server_file.name, '/path/for/the/file')) # Case: The whole content is sent, in chunk_size bits read = [i for i in response.app_iter] expected = [bytes((i, )) for i in file_content] assert read == expected # Case: Headers are set correctly assert response.content_type == 'text/css' assert not response.content_encoding assert response.content_length == len(file_content) mtime = datetime.datetime.fromtimestamp( int(os.path.getmtime(server_file.name))) assert response.last_modified.replace(tzinfo=None) == mtime tag_mtime, tag_size, tag_hash = response.etag.split('-') mtime = str(os.path.getmtime(server_file.name)) assert tag_mtime == mtime assert tag_size == str(len(file_content)) assert tag_hash == str(abs(hash(server_file.name))) # Case: conditional response is supported assert response.conditional_response # Case: partial response is supported - different cases: # - normal case actual = [i for i in response.app_iter.app_iter_range(3, 7)] expected = [bytes((i, )) for i in file_content[3:8]] assert actual == expected # - no end specified actual = [i for i in response.app_iter.app_iter_range(3)] expected = [bytes((i, )) for i in file_content[3:]] assert actual == expected # - no start specified actual = [i for i in response.app_iter.app_iter_range(end=7)] expected = [bytes((i, )) for i in file_content[:8]] assert actual == expected # - where the last chunk read would stretch past end response.chunk_size = 2 actual = b''.join([i for i in response.app_iter.app_iter_range(end=6)]) expected = file_content[:7] assert actual == expected # - where start > end response.chunk_size = 1 actual = [i for i in response.app_iter.app_iter_range(start=7, end=3)] expected = [b''] assert actual == expected # - where start < 0 actual = [i for i in response.app_iter.app_iter_range(start=-10, end=7)] expected = [bytes((i, )) for i in file_content[:8]] assert actual == expected # - where end > length of file actual = [i for i in response.app_iter.app_iter_range(start=3, end=2000)] expected = [bytes((i, )) for i in file_content[3:]] assert actual == expected # - where start > length of file actual = [i for i in response.app_iter.app_iter_range(start=700)] expected = [b''] assert actual == expected