|
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 |
|
|
Pierre-Yves Chibon |
73d120 |
sys.path.insert(
|
|
Pierre-Yves Chibon |
73d120 |
0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
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 = [
|
|
Pierre-Yves Chibon |
73d120 |
("hello.html", None, "text/html", None),
|
|
Pierre-Yves Chibon |
73d120 |
("hello.html", b"#!", "text/html", "ascii"),
|
|
Pierre-Yves Chibon |
73d120 |
("hello", b"#!", "text/plain", "ascii"),
|
|
Pierre-Yves Chibon |
73d120 |
("hello.jpg", None, "image/jpeg", None),
|
|
Pierre-Yves Chibon |
73d120 |
("hello.jpg", b"#!", "image/jpeg", None),
|
|
Pierre-Yves Chibon |
73d120 |
("hello.jpg", b"\0", "image/jpeg", None),
|
|
Pierre-Yves Chibon |
73d120 |
(None, "đ".encode("utf-8"), "text/plain", "utf-8"),
|
|
Pierre-Yves Chibon |
73d120 |
("hello", b"\0", "application/octet-stream", None),
|
|
Pierre-Yves Chibon |
73d120 |
("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(
|
|
Pierre-Yves Chibon |
73d120 |
(data[2], data[3]),
|
|
Pierre-Yves Chibon |
73d120 |
result,
|
|
Aurélien Bompard |
626417 |
"Wrong mimetype for filename %r and content %r"
|
|
Pierre-Yves Chibon |
73d120 |
% (data[0], data[1]),
|
|
Aurélien Bompard |
626417 |
)
|
|
Shengjing Zhu |
c28196 |
|
|
Shengjing Zhu |
c28196 |
def test_get_html_file_headers(self):
|
|
Pierre-Yves Chibon |
73d120 |
result = mimetype.get_type_headers("hello.html", None)
|
|
Shengjing Zhu |
c28196 |
expected = {
|
|
Pierre-Yves Chibon |
73d120 |
"Content-Type": "application/octet-stream",
|
|
Pierre-Yves Chibon |
73d120 |
"Content-Disposition": "attachment",
|
|
Pierre-Yves Chibon |
73d120 |
"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 = [
|
|
Pierre-Yves Chibon |
73d120 |
("hello", b"#!", "text/plain; charset=ascii"),
|
|
Pierre-Yves Chibon |
73d120 |
("hello.jpg", None, "image/jpeg"),
|
|
Pierre-Yves Chibon |
73d120 |
("hello.jpg", b"#!", "image/jpeg"),
|
|
Pierre-Yves Chibon |
73d120 |
("hello.jpg", b"\0", "image/jpeg"),
|
|
Pierre-Yves Chibon |
73d120 |
(None, "đ".encode("utf-8"), "text/plain; charset=utf-8"),
|
|
Pierre-Yves Chibon |
73d120 |
("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(
|
|
Pierre-Yves Chibon |
73d120 |
result["Content-Type"],
|
|
Pierre-Yves Chibon |
73d120 |
data[2],
|
|
Aurélien Bompard |
626417 |
"Wrong Content-Type for filename %r and content %r"
|
|
Pierre-Yves Chibon |
73d120 |
% (data[0], data[1]),
|
|
Aurélien Bompard |
626417 |
)
|
|
Shengjing Zhu |
c28196 |
|
|
Shengjing Zhu |
c28196 |
def test_get_none_header(self):
|
|
Pierre-Yves Chibon |
73d120 |
self.assertIsNone(mimetype.get_type_headers("hello", None))
|
|
Shengjing Zhu |
c28196 |
|
|
Shengjing Zhu |
c28196 |
|
|
Pierre-Yves Chibon |
73d120 |
if __name__ == "__main__":
|
|
Shengjing Zhu |
c28196 |
unittest.main(verbosity=2)
|