Blame tests/test_pagure_lib_mimetype.py

Shengjing Zhu c28196
# -*- coding: utf-8 -*-
Shengjing Zhu c28196
"""
Shengjing Zhu c28196
Tests for :module:`pagure.lib.mimetype`.
Shengjing Zhu c28196
"""
Shengjing Zhu c28196
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),
Shengjing Zhu c28196
            ('hello.html', '#!', 'text/html', 'ascii'),
Shengjing Zhu c28196
            ('hello', '#!', 'text/plain', 'ascii'),
Shengjing Zhu c28196
            ('hello.jpg', None, 'image/jpeg', None),
Shengjing Zhu c28196
            ('hello.jpg', '#!', 'image/jpeg', None),
Shengjing Zhu c28196
            ('hello.jpg', '\0', 'image/jpeg', None),
Shengjing Zhu c28196
            (None, '😋', 'text/plain', 'utf-8'),
Shengjing Zhu c28196
            ('hello', '\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])
Shengjing Zhu c28196
            self.assertEqual((data[2], data[3]), result)
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 = [
Shengjing Zhu c28196
            ('hello', '#!', 'text/plain; charset=ascii'),
Shengjing Zhu c28196
            ('hello.jpg', None, 'image/jpeg'),
Shengjing Zhu c28196
            ('hello.jpg', '#!', 'image/jpeg'),
Shengjing Zhu c28196
            ('hello.jpg', '\0', 'image/jpeg'),
Shengjing Zhu c28196
            (None, '😋', 'text/plain; charset=utf-8'),
Shengjing Zhu c28196
            ('hello', '\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])
Shengjing Zhu c28196
            self.assertEqual(result['Content-Type'], data[2])
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)