<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.http import HttpResponse
from django.test import TestCase
from django.test.client import RequestFactory

from responsive.conf import settings
from responsive.middleware import ResponsiveMiddleware
from responsive.utils import Device


def hello(request):
    html = """
        <html>
            <head>
                <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
            </head>
            <body>

            </body>
        </html>
    """
    return HttpResponse(html)


class MiddlewareTest(TestCase):

    def setUp(self):
        # Every test needs access to the request factory.
        self.factory = RequestFactory()
        self.middleware = ResponsiveMiddleware()

    def test_process_request_with_valid_cookie(self):
        # test given a valid responsive cookie, device obj is set on request
        request = self.factory.get('/')
        request.COOKIES = {
            settings.RESPONSIVE_COOKIE_NAME: '1024:768:2'  # valid responsive cookie
        }

        self.middleware.process_request(request)
        self.assertIsInstance(getattr(request, settings.RESPONSIVE_VARIABLE_NAME), Device)

    def test_process_request_with_a_bad_cookie(self):
        # test given a bad responsive cookie, an INVALID_RESPONSIVE_COOKIE flag is set
        request = self.factory.get('/')
        request.COOKIES = {
            settings.RESPONSIVE_COOKIE_NAME: 'xxx:xxx:x'  # cookie values should be numbers
        }

        self.middleware.process_request(request)
        self.assertTrue(getattr(request, 'INVALID_RESPONSIVE_COOKIE'))

        request.COOKIES = {
            settings.RESPONSIVE_COOKIE_NAME: 'xxxxxxx'  # 3 parts to a responsive cookie
        }

        self.middleware.process_request(request)
        self.assertTrue(getattr(request, 'INVALID_RESPONSIVE_COOKIE'))

    def test_reposnsive_htnl_snippet_is_injected_into_response(self):
        request = self.factory.get('/')
        response = hello(request)
        # test no <script> tag before any processing by the middleware
        self.assertFalse(b'</script>' in response.content)<commit_msg>Fix spelling<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.http import HttpResponse
from django.test import TestCase
from django.test.client import RequestFactory

from responsive.conf import settings
from responsive.middleware import ResponsiveMiddleware
from responsive.utils import Device


def hello(request):
    html = """
        <html>
            <head>
                <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
            </head>
            <body>

            </body>
        </html>
    """
    return HttpResponse(html)


class MiddlewareTest(TestCase):

    def setUp(self):
        # Every test needs access to the request factory.
        self.factory = RequestFactory()
        self.middleware = ResponsiveMiddleware()

    def test_process_request_with_valid_cookie(self):
        # test given a valid responsive cookie, device obj is set on request
        request = self.factory.get('/')
        request.COOKIES = {
            settings.RESPONSIVE_COOKIE_NAME: '1024:768:2'  # valid responsive cookie
        }

        self.middleware.process_request(request)
        self.assertIsInstance(getattr(request, settings.RESPONSIVE_VARIABLE_NAME), Device)

    def test_process_request_with_a_bad_cookie(self):
        # test given a bad responsive cookie, an INVALID_RESPONSIVE_COOKIE flag is set
        request = self.factory.get('/')
        request.COOKIES = {
            settings.RESPONSIVE_COOKIE_NAME: 'xxx:xxx:x'  # cookie values should be numbers
        }

        self.middleware.process_request(request)
        self.assertTrue(getattr(request, 'INVALID_RESPONSIVE_COOKIE'))

        request.COOKIES = {
            settings.RESPONSIVE_COOKIE_NAME: 'xxxxxxx'  # 3 parts to a responsive cookie
        }

        self.middleware.process_request(request)
        self.assertTrue(getattr(request, 'INVALID_RESPONSIVE_COOKIE'))

    def test_reponsive_html_snippet_is_injected_into_response(self):
        request = self.factory.get('/')
        response = hello(request)
        # test no <script> tag before any processing by the middleware
        self.assertFalse(b'</script>' in response.content)
{'fraction_non_alphanumeric': 0.09207512597343105, 'fraction_numerical': 0.005038937242327073, 'mean_word_length': 2.8307017543859647, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5183740', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1231, 'n_words': 284}