|
Shengjing Zhu |
c28196 |
# -*- coding: utf-8 -*-
|
|
Shengjing Zhu |
c28196 |
"""
|
|
Shengjing Zhu |
c28196 |
Tests for :module:`pagure.lib.mimetype`.
|
|
Shengjing Zhu |
c28196 |
"""
|
|
Shengjing Zhu |
c28196 |
|
|
Pierre-Yves Chibon |
67d1cc |
from __future__ import unicode_literals, absolute_import
|
|
Aurélien Bompard |
626417 |
|
|
Shengjing Zhu |
c28196 |
import os
|
|
Shengjing Zhu |
c28196 |
import unittest
|
|
Shengjing Zhu |
c28196 |
import sys
|
|
Shengjing Zhu |
c28196 |
|
|
Clement Verna |
7cb633 |
from pagure.lib import mimetype
|
|
Clement Verna |
7cb633 |
|
|
Shengjing Zhu |
c28196 |
sys.path.insert(0, os.path.join(os.path.dirname(
|
|
Shengjing Zhu |
c28196 |
os.path.abspath(__file__)), '..'))
|
|
Shengjing Zhu |
c28196 |
|
|
Shengjing Zhu |
c28196 |
|
|
Shengjing Zhu |
c28196 |
class TestMIMEType(unittest.TestCase):
|
|
Shengjing Zhu |
c28196 |
def test_guess_type(self):
|
|
Shengjing Zhu |
c28196 |
dataset = [
|
|
Shengjing Zhu |
c28196 |
('hello.html', None, 'text/html', None),
|
|
Aurélien Bompard |
626417 |
('hello.html', b'#!', 'text/html', 'ascii'),
|
|
Aurélien Bompard |
626417 |
('hello', b'#!', 'text/plain', 'ascii'),
|
|
Shengjing Zhu |
c28196 |
('hello.jpg', None, 'image/jpeg', None),
|
|
Aurélien Bompard |
626417 |
('hello.jpg', b'#!', 'image/jpeg', None),
|
|
Aurélien Bompard |
626417 |
('hello.jpg', b'\0', 'image/jpeg', None),
|
|
Aurélien Bompard |
626417 |
(None, 'đ'.encode("utf-8"), 'text/plain', 'utf-8'),
|
|
Aurélien Bompard |
626417 |
('hello', b'\0', 'application/octet-stream', None),
|
|
Shengjing Zhu |
c28196 |
('hello', None, None, None)
|
|
Shengjing Zhu |
c28196 |
]
|
|
Shengjing Zhu |
c28196 |
for data in dataset:
|
|
Shengjing Zhu |
c28196 |
result = mimetype.guess_type(data[0], data[1])
|
|
Aurélien Bompard |
626417 |
self.assertEqual(
|
|
Aurélien Bompard |
626417 |
(data[2], data[3]), result,
|
|
Aurélien Bompard |
626417 |
"Wrong mimetype for filename %r and content %r"
|
|
Aurélien Bompard |
626417 |
% (data[0], data[1])
|
|
Aurélien Bompard |
626417 |
)
|
|
Shengjing Zhu |
c28196 |
|
|
Shengjing Zhu |
c28196 |
def test_get_html_file_headers(self):
|
|
Shengjing Zhu |
c28196 |
result = mimetype.get_type_headers('hello.html', None)
|
|
Shengjing Zhu |
c28196 |
expected = {
|
|
Shengjing Zhu |
c28196 |
'Content-Type': 'application/octet-stream',
|
|
Shengjing Zhu |
c28196 |
'Content-Disposition': 'attachment',
|
|
Shengjing Zhu |
c28196 |
'X-Content-Type-Options': 'nosniff'
|
|
Shengjing Zhu |
c28196 |
}
|
|
Shengjing Zhu |
c28196 |
self.assertEqual(result, expected)
|
|
Shengjing Zhu |
c28196 |
|
|
Shengjing Zhu |
c28196 |
def test_get_normal_headers(self):
|
|
Shengjing Zhu |
c28196 |
dataset = [
|
|
Aurélien Bompard |
626417 |
('hello', b'#!', 'text/plain; charset=ascii'),
|
|
Shengjing Zhu |
c28196 |
('hello.jpg', None, 'image/jpeg'),
|
|
Aurélien Bompard |
626417 |
('hello.jpg', b'#!', 'image/jpeg'),
|
|
Aurélien Bompard |
626417 |
('hello.jpg', b'\0', 'image/jpeg'),
|
|
Aurélien Bompard |
626417 |
(None, 'đ'.encode("utf-8"), 'text/plain; charset=utf-8'),
|
|
Aurélien Bompard |
626417 |
('hello', b'\0', 'application/octet-stream')
|
|
Shengjing Zhu |
c28196 |
]
|
|
Shengjing Zhu |
c28196 |
for data in dataset:
|
|
Shengjing Zhu |
c28196 |
result = mimetype.get_type_headers(data[0], data[1])
|
|
Aurélien Bompard |
626417 |
self.assertEqual(
|
|
Aurélien Bompard |
626417 |
result['Content-Type'], data[2],
|
|
Aurélien Bompard |
626417 |
"Wrong Content-Type for filename %r and content %r"
|
|
Aurélien Bompard |
626417 |
% (data[0], data[1])
|
|
Aurélien Bompard |
626417 |
)
|
|
Shengjing Zhu |
c28196 |
|
|
Shengjing Zhu |
c28196 |
def test_get_none_header(self):
|
|
Shengjing Zhu |
c28196 |
self.assertIsNone(mimetype.get_type_headers('hello', None))
|
|
Shengjing Zhu |
c28196 |
|
|
Shengjing Zhu |
c28196 |
|
|
Shengjing Zhu |
c28196 |
if __name__ == '__main__':
|
|
Shengjing Zhu |
c28196 |
unittest.main(verbosity=2)
|