def testStaticFile(self): ConfigManager.removeConfig('development') ConfigManager.addConfig(testConfig) app = shimehari.Shimehari(__name__) app.setStaticFolder('static') with app.testRequestContext(): rv = app.sendStaticFile('index.html') cc = parse_cache_control_header(rv.headers['Cache-Control']) self.assertEqual(cc.max_age, 12 * 60 * 60) rv = shimehari.sendFile(os.path.join(app.rootPath, 'static/index.html')) cc = parse_cache_control_header(rv.headers['Cache-Control']) self.assertEqual(cc.max_age, 12 * 60 * 60) app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600 with app.testRequestContext(): rv = app.sendStaticFile('index.html') cc = parse_cache_control_header(rv.headers['Cache-Control']) self.assertEqual(cc.max_age, 3600) rv = shimehari.sendFile(os.path.join(app.rootPath, 'static/index.html')) cc = parse_cache_control_header(rv.headers['Cache-Control']) self.assertEqual(cc.max_age, 3600) class StaticFileApp(shimehari.Shimehari): def getSendFileMaxAge(self, filename): return 10 app = StaticFileApp(__name__) app.setStaticFolder('static') with app.testRequestContext(): rv = app.sendStaticFile('index.html') cc = parse_cache_control_header(rv.headers['Cache-Control']) self.assertEqual(cc.max_age, 10) rv = shimehari.sendFile(os.path.join(app.rootPath, 'static/index.html')) cc = parse_cache_control_header(rv.headers['Cache-Control']) self.assertEqual(cc.max_age, 10)
def testAttachment(self): app = shimehari.Shimehari(__name__) with catchWarnings() as captured: with app.testRequestContext(): f = open(os.path.join(app.rootPath, 'static/index.html')) rv = shimehari.sendFile(f, asAttachment=True) value, options = parse_options_header( rv.headers['Content-Disposition']) self.assertEqual(value, 'attachment') self.assertEqual(len(captured), 2) with app.testRequestContext(): self.assertEqual(options['filename'], 'index.html') rv = shimehari.sendFile(os.path.join(app.rootPath, 'static/index.html'), asAttachment=True) value, options = parse_options_header( rv.headers['Content-Disposition']) self.assertEqual(value, 'attachment') self.assertEqual(options['filename'], 'index.html') with app.testRequestContext(): rv = shimehari.sendFile(StringIO('Test'), asAttachment=True, attachmentFilename='index.txt', addEtags=True) self.assertEqual(rv.mimetype, 'text/plain') value, options = parse_options_header( rv.headers['Content-Disposition']) self.assertEqual(value, 'attachment') self.assertEqual(options['filename'], 'index.txt')
def testSendFileRegular(self): app = shimehari.Shimehari(__name__) with app.testRequestContext(): rv = shimehari.sendFile(os.path.join(app.rootPath, 'static/index.html')) self.assert_(rv.direct_passthrough) self.assertEqual(rv.mimetype, 'text/html') with app.openFile('static/index.html') as f: self.assertEqual(rv.data, f.read())
def testSendFileObject(self): app = shimehari.Shimehari(__name__) with catchWarnings() as captured: with app.testRequestContext(): f = open(os.path.join(app.rootPath, 'static/index.html')) rv = shimehari.sendFile(f) with app.openFile('static/index.html') as f: self.assertEqual(rv.data, f.read()) self.assertEqual(rv.mimetype, 'text/html') self.assertEqual(len(captured), 2) app.useXSendFile = True with catchWarnings() as captured: with app.testRequestContext(): f = open(os.path.join(app.rootPath, 'static/index.html')) rv = shimehari.sendFile(f) self.assertEqual(rv.mimetype, 'text/html') self.assert_('x-sendfile' in rv.headers) self.assertEqual( rv.headers['x-sendfile'], os.path.join(app.rootPath, 'static/index.html')) self.assertEqual(len(captured), 2) app.useXSendFile = False with app.testRequestContext(): with catchWarnings() as captured: f = StringIO('Test') rv = shimehari.sendFile(f) self.assertEqual(rv.data, 'Test') self.assertEqual(rv.mimetype, 'application/octet-stream') self.assertEqual(len(captured), 1) with catchWarnings() as captured: f = StringIO('Test') rv = shimehari.sendFile(f, mimetype='text/plain') self.assertEqual(rv.data, 'Test') self.assertEqual(rv.mimetype, 'text/plain') self.assertEqual(len(captured), 1) app.useXSendFile = True with catchWarnings() as captured: with app.testRequestContext(): f = StringIO('Test') rv = shimehari.sendFile(f) self.assert_('x-sendfile' not in rv.headers) self.assertEqual(len(captured), 1)
def testSendFileObject(self): app = shimehari.Shimehari(__name__) with catchWarnings() as captured: with app.testRequestContext(): f = open(os.path.join(app.rootPath, 'static/index.html')) rv = shimehari.sendFile(f) with app.openFile('static/index.html') as f: self.assertEqual(rv.data, f.read()) self.assertEqual(rv.mimetype, 'text/html') self.assertEqual(len(captured), 2) app.useXSendFile = True with catchWarnings() as captured: with app.testRequestContext(): f = open(os.path.join(app.rootPath, 'static/index.html')) rv = shimehari.sendFile(f) self.assertEqual(rv.mimetype, 'text/html') self.assert_('x-sendfile' in rv.headers) self.assertEqual(rv.headers['x-sendfile'], os.path.join(app.rootPath, 'static/index.html')) self.assertEqual(len(captured), 2) app.useXSendFile = False with app.testRequestContext(): with catchWarnings() as captured: f = StringIO('Test') rv = shimehari.sendFile(f) self.assertEqual(rv.data, 'Test') self.assertEqual(rv.mimetype, 'application/octet-stream') self.assertEqual(len(captured), 1) with catchWarnings() as captured: f = StringIO('Test') rv = shimehari.sendFile(f, mimetype='text/plain') self.assertEqual(rv.data, 'Test') self.assertEqual(rv.mimetype, 'text/plain') self.assertEqual(len(captured), 1) app.useXSendFile = True with catchWarnings() as captured: with app.testRequestContext(): f = StringIO('Test') rv = shimehari.sendFile(f) self.assert_('x-sendfile' not in rv.headers) self.assertEqual(len(captured), 1)
def testSendFileXSendFile(self): app = shimehari.Shimehari(__name__) app.useXSendFile = True with app.testRequestContext(): rv = shimehari.sendFile(os.path.join(app.rootPath, 'static/index.html')) self.assert_(rv.direct_passthrough) self.assert_('x-sendfile' in rv.headers) self.assertEqual(rv.headers['x-sendfile'], os.path.join(app.rootPath, 'static/index.html')) self.assertEqual(rv.mimetype, 'text/html')
def testSendFileRegular(self): app = shimehari.Shimehari(__name__) with app.testRequestContext(): rv = shimehari.sendFile( os.path.join(app.rootPath, 'static/index.html')) self.assert_(rv.direct_passthrough) self.assertEqual(rv.mimetype, 'text/html') with app.openFile('static/index.html') as f: self.assertEqual(rv.data, f.read())
def testSendFileXSendFile(self): app = shimehari.Shimehari(__name__) app.useXSendFile = True with app.testRequestContext(): rv = shimehari.sendFile( os.path.join(app.rootPath, 'static/index.html')) self.assert_(rv.direct_passthrough) self.assert_('x-sendfile' in rv.headers) self.assertEqual(rv.headers['x-sendfile'], os.path.join(app.rootPath, 'static/index.html')) self.assertEqual(rv.mimetype, 'text/html')
def testStaticFile(self): ConfigManager.removeConfig('development') ConfigManager.addConfig(testConfig) app = shimehari.Shimehari(__name__) app.staticFolder = 'static' with app.testRequestContext(): print app.appFolder print app.staticFolder rv = app.sendStaticFile('index.html') cc = parse_cache_control_header(rv.headers['Cache-Control']) self.assertEqual(cc.max_age, 12 * 60 * 60) rv = shimehari.sendFile( os.path.join(app.rootPath, 'static/index.html')) cc = parse_cache_control_header(rv.headers['Cache-Control']) self.assertEqual(cc.max_age, 12 * 60 * 60) app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600 with app.testRequestContext(): rv = app.sendStaticFile('index.html') cc = parse_cache_control_header(rv.headers['Cache-Control']) self.assertEqual(cc.max_age, 3600) rv = shimehari.sendFile( os.path.join(app.rootPath, 'static/index.html')) cc = parse_cache_control_header(rv.headers['Cache-Control']) self.assertEqual(cc.max_age, 3600) class StaticFileApp(shimehari.Shimehari): def getSendFileMaxAge(self, filename): return 10 app = StaticFileApp(__name__) app.staticFolder = 'static' with app.testRequestContext(): rv = app.sendStaticFile('index.html') cc = parse_cache_control_header(rv.headers['Cache-Control']) self.assertEqual(cc.max_age, 10) rv = shimehari.sendFile( os.path.join(app.rootPath, 'static/index.html')) cc = parse_cache_control_header(rv.headers['Cache-Control']) self.assertEqual(cc.max_age, 10)
def testAttachment(self): app = shimehari.Shimehari(__name__) with catchWarnings() as captured: with app.testRequestContext(): f = open(os.path.join(app.rootPath, 'static/index.html')) rv = shimehari.sendFile(f, asAttachment=True) value, options = parse_options_header(rv.headers['Content-Disposition']) self.assertEqual(value, 'attachment') self.assertEqual(len(captured), 2) with app.testRequestContext(): self.assertEqual(options['filename'], 'index.html') rv = shimehari.sendFile(os.path.join(app.rootPath, 'static/index.html'), asAttachment=True) value, options = parse_options_header(rv.headers['Content-Disposition']) self.assertEqual(value, 'attachment') self.assertEqual(options['filename'], 'index.html') with app.testRequestContext(): rv = shimehari.sendFile(StringIO('Test'), asAttachment=True, attachmentFilename='index.txt', addEtags=True) self.assertEqual(rv.mimetype, 'text/plain') value, options = parse_options_header(rv.headers['Content-Disposition']) self.assertEqual(value, 'attachment') self.assertEqual(options['filename'], 'index.txt')