def test_file_does_not_exist_stat(self): url_map = appinfo.URLMap(url='/', static_files='index.html') h = static_files_handler.StaticContentHandler(root_path=None, url_map=url_map, url_pattern='/$') error = IOError() error.errno = errno.ENOENT os.path.getmtime('/home/appdir/index.html').AndRaise(error) self.mox.ReplayAll() self.assertResponse('404 Not Found', {}, '', h._handle_path, '/home/appdir/index.html', { 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/' }) self.mox.VerifyAll()
def test_no_permission_read(self): url_map = appinfo.URLMap(url='/', static_files='index.html') h = static_files_handler.StaticContentHandler(root_path=None, url_map=url_map, url_pattern='/$') os.path.getmtime('/home/appdir/index.html').AndReturn(12345.6) error = IOError() error.errno = errno.EPERM static_files_handler.StaticContentHandler._read_file( '/home/appdir/index.html').AndRaise(error) self.mox.ReplayAll() self.assertResponse('403 Forbidden', {}, '', h._handle_path, '/home/appdir/index.html', { 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/' }) self.mox.VerifyAll()
def test_if_match_no_file(self): url_map = appinfo.URLMap(url='/', static_files='index.html') h = static_files_handler.StaticContentHandler(root_path=None, url_map=url_map, url_pattern='/$') error = IOError() error.errno = errno.ENOENT os.path.getmtime('/home/appdir/index.html').AndRaise(error) self.mox.ReplayAll() self.assertResponse('412 Precondition Failed', {}, '', h._handle_path, '/home/appdir/index.html', { 'REQUEST_METHOD': 'GET', 'HTTP_IF_MATCH': '"nomatch"' }) self.mox.VerifyAll() self.assertEqual( static_files_handler.StaticContentHandler. _filename_to_mtime_and_etag, {})
def test_nonstandard_mimetype(self): url_map = appinfo.URLMap(url='/', static_files='simple.dart') h = static_files_handler.StaticContentHandler(root_path=None, url_map=url_map, url_pattern='/$') os.path.getmtime('/home/appdir/simple.dart').AndReturn(12345.6) static_files_handler.StaticContentHandler._read_file( '/home/appdir/simple.dart').AndReturn('void main() {}') self.mox.ReplayAll() self.assertResponse( '200 OK', { 'Content-type': 'application/dart', 'Content-length': '14', 'Expires': 'Fri, 01 Jan 1990 00:00:00 GMT', 'Cache-Control': 'no-cache', 'ETag': '"LTE2OTA2MzYyMTM="' }, 'void main() {}', h._handle_path, '/home/appdir/simple.dart', {'REQUEST_METHOD': 'GET'}) self.mox.VerifyAll()
def test_if_none_match_with_match(self): url_map = appinfo.URLMap(url='/', static_files='index.html') h = static_files_handler.StaticContentHandler(root_path=None, url_map=url_map, url_pattern='/$') os.path.getmtime('/home/appdir/index.html').AndReturn(12345.6) static_files_handler.StaticContentHandler._read_file( '/home/appdir/index.html').AndReturn('Hello World!') self.mox.ReplayAll() self.assertResponse('304 Not Modified', {'ETag': '"NDcyNDU2MzU1"'}, '', h._handle_path, '/home/appdir/index.html', { 'REQUEST_METHOD': 'GET', 'HTTP_IF_NONE_MATCH': '"NDcyNDU2MzU1"' }) self.mox.VerifyAll() self.assertEqual( static_files_handler.StaticContentHandler. _filename_to_mtime_and_etag, {'/home/appdir/index.html': (12345.6, 'NDcyNDU2MzU1')})
def test_custom_headers(self): http_headers = appinfo.HttpHeadersDict() http_headers['Content-type'] = 'text/xml' http_headers['ETag'] = 'abc123' http_headers['Expires'] = 'tomorrow' http_headers['Cache-Control'] = 'private' http_headers['Custom-Header'] = 'custom,value' url_map = appinfo.URLMap(url='/', static_files='index.html', http_headers=http_headers) h = static_files_handler.StaticContentHandler( root_path=None, url_map=url_map, url_pattern='/$') os.path.getmtime('/home/appdir/index.html').AndReturn(12345.6) static_files_handler.StaticContentHandler._read_file( '/home/appdir/index.html').AndReturn('Hello World!') self.mox.ReplayAll() self.assertResponse('200 OK', {'Content-length': '12', 'Content-type': 'text/xml', 'ETag': 'abc123', 'Expires': 'tomorrow', 'Cache-Control': 'private', 'Custom-Header': 'custom,value'}, 'Hello World!', h._handle_path, '/home/appdir/index.html', {'REQUEST_METHOD': 'GET'}) self.mox.VerifyAll() self.assertEqual( static_files_handler.StaticContentHandler._filename_to_mtime_and_etag, {'/home/appdir/index.html': (12345.6, 'NDcyNDU2MzU1')})
def test_cached_if_match_without_match(self): static_files_handler.StaticContentHandler._filename_to_mtime_and_etag = { '/home/appdir/index.html': (12345.6, 'abc') } url_map = appinfo.URLMap(url='/', static_files='index.html') h = static_files_handler.StaticContentHandler(root_path=None, url_map=url_map, url_pattern='/$') os.path.getmtime('/home/appdir/index.html').AndReturn(12345.6) self.mox.ReplayAll() self.assertResponse('412 Precondition Failed', {'ETag': '"abc"'}, '', h._handle_path, '/home/appdir/index.html', { 'REQUEST_METHOD': 'GET', 'HTTP_IF_MATCH': '"nomatch"' }) self.mox.VerifyAll() self.assertEqual( static_files_handler.StaticContentHandler. _filename_to_mtime_and_etag, {'/home/appdir/index.html': (12345.6, 'abc')})