Example #1
0
    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')
Example #2
0
    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)
Example #3
0
    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)
Example #4
0
    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')