# Copyright 2013 django-htmlmin authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

import os
import sys
import unittest

from django.conf import settings
from htmlmin.middleware import HtmlMinifyMiddleware, MarkRequestMiddleware
from htmlmin.tests import TESTS_DIR
from htmlmin.tests.mocks import (RequestBareMock, RequestMock, ResponseMock,
                                 ResponseWithCommentMock)


class TestMiddleware(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        sys.path.insert(0, TESTS_DIR)
        os.environ['DJANGO_SETTINGS_MODULE'] = 'mock_settings'

    @classmethod
    def tearDownClass(cls):
        sys.path.remove(TESTS_DIR)
        del os.environ['DJANGO_SETTINGS_MODULE']

    def test_should_minify_response_when_mime_type_is_html(self):
        response_mock = ResponseMock()
        response = HtmlMinifyMiddleware().process_response(
            RequestMock(), response_mock,
        )

        minified = "<html><head></head><body>some text here</body></html>"
        self.assertEqual(minified, response.content)

    def test_should_minify_with_any_charset(self):
        response_mock = ResponseMock()
        response_mock['Content-Type'] = 'text/html; charset=utf-8'
        response = HtmlMinifyMiddleware().process_response(
            RequestMock(), response_mock,
        )

        minified = "<html><head></head><body>some text here</body></html>"
        self.assertEqual(minified, response.content)

    def test_should_not_minify_without_content(self):
        response_mock = ResponseMock()
        del response_mock['Content-Type']
        response = HtmlMinifyMiddleware().process_response(
            RequestMock(), response_mock,
        )

        html_not_minified = "<html>   <body>some text here</body>    </html>"
        self.assertEqual(html_not_minified, response.content)

    def test_should_not_minify_not_html_content(self):
        response_mock = ResponseMock()
        response_mock['Content-Type'] = 'application/json'
        response = HtmlMinifyMiddleware().process_response(
            RequestMock(), response_mock,
        )

        html_not_minified = "<html>   <body>some text here</body>    </html>"
        self.assertEqual(html_not_minified, response.content)

    def test_should_not_minify_url_marked_as_not_minifiable(self):
        html_not_minified = "<html>   <body>some text here</body>    </html>"
        response_mock = ResponseMock()
        response = HtmlMinifyMiddleware().process_response(
            RequestMock('/raw/'), response_mock,
        )
        self.assertEqual(html_not_minified, response.content)

    def test_should_minify_if_exclude_from_minifying_is_unset(self):
        old = settings.EXCLUDE_FROM_MINIFYING
        del settings.EXCLUDE_FROM_MINIFYING

        minified = "<html><head></head><body>some text here</body></html>"
        response = HtmlMinifyMiddleware().process_response(
            RequestMock(), ResponseMock(),
        )
        self.assertEqual(minified, response.content)

        settings.EXCLUDE_FROM_MINIFYING = old