<commit_before>from __future__ import unicode_literals
from django.test import SimpleTestCase
from django.http import HttpResponse

from asgiref.inmemory import ChannelLayer
from channels.handler import AsgiHandler
from channels.message import Message


class FakeAsgiHandler(AsgiHandler):
    """
    Handler subclass that just returns a premade response rather than
    go into the view subsystem.
    """

    chunk_size = 30

    def __init__(self, response):
        assert isinstance(response, HttpResponse)
        self._response = response
        super(FakeAsgiHandler, self).__init__()

    def get_response(self, request):
        return self._response


class HandlerTests(SimpleTestCase):
    """
    Tests that the handler works correctly and round-trips things into a
    correct response.
    """

    def setUp(self):
        """
        Make an in memory channel layer for testing
        """
        self.channel_layer = ChannelLayer()
        self.make_message = lambda m, c: Message(m, c, self.channel_layer)

    def test_basic(self):
        """
        Tests a simple request
        """
        # Make stub request and desired response
        message = self.make_message({
            "reply_channel": "test",
            "http_version": "1.1",
            "method": "GET",
            "path": b"/test/",
        }, "test")
        response = HttpResponse(b"Hi there!", content_type="text/plain")
        # Run the handler
        handler = FakeAsgiHandler(response)
        reply_messages = list(handler(message))
        # Make sure we got the right number of messages
        self.assertEqual(len(reply_messages), 1)
        reply_message = reply_messages[0]
        # Make sure the message looks correct
        self.assertEqual(reply_message["content"], b"Hi there!")
        self.assertEqual(reply_message["status"], 200)
        self.assertEqual(reply_message["status_text"], "OK")
        self.assertEqual(reply_message.get("more_content", False), False)
        self.assertEqual(
            reply_message["headers"],
            [("Content-Type", b"text/plain")],
        )

    def test_large(self):
        """
        Tests a large response (will need chunking)
        """
        # Make stub request and desired response
        message = self.make_message({
            "reply_channel": "test",
            "http_version": "1.1",<commit_msg>OKAY I THINK THIS IS THE LAST STUPID MISTAKE<commit_after>from __future__ import unicode_literals
from django.test import SimpleTestCase
from django.http import HttpResponse

from asgiref.inmemory import ChannelLayer
from channels.handler import AsgiHandler
from channels.message import Message


class FakeAsgiHandler(AsgiHandler):
    """
    Handler subclass that just returns a premade response rather than
    go into the view subsystem.
    """

    chunk_size = 30

    def __init__(self, response):
        assert isinstance(response, HttpResponse)
        self._response = response
        super(FakeAsgiHandler, self).__init__()

    def get_response(self, request):
        return self._response


class HandlerTests(SimpleTestCase):
    """
    Tests that the handler works correctly and round-trips things into a
    correct response.
    """

    def setUp(self):
        """
        Make an in memory channel layer for testing
        """
        self.channel_layer = ChannelLayer()
        self.make_message = lambda m, c: Message(m, c, self.channel_layer)

    def test_basic(self):
        """
        Tests a simple request
        """
        # Make stub request and desired response
        message = self.make_message({
            "reply_channel": "test",
            "http_version": "1.1",
            "method": "GET",
            "path": b"/test/",
        }, "test")
        response = HttpResponse(b"Hi there!", content_type="text/plain")
        # Run the handler
        handler = FakeAsgiHandler(response)
        reply_messages = list(handler(message))
        # Make sure we got the right number of messages
        self.assertEqual(len(reply_messages), 1)
        reply_message = reply_messages[0]
        # Make sure the message looks correct
        self.assertEqual(reply_message["content"], b"Hi there!")
        self.assertEqual(reply_message["status"], 200)
        self.assertEqual(reply_message["status_text"], b"OK")
        self.assertEqual(reply_message.get("more_content", False), False)
        self.assertEqual(
            reply_message["headers"],
            [("Content-Type", b"text/plain")],
        )

    def test_large(self):
        """
        Tests a large response (will need chunking)
        """
        # Make stub request and desired response
        message = self.make_message({
            "reply_channel": "test",
            "http_version": "1.1",
{'fraction_non_alphanumeric': 0.09879116298457691, 'fraction_numerical': 0.004585243851604835, 'mean_word_length': 2.5521835677276092, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '2737200', 'n_tokens_mistral': 1292, 'n_tokens_neox': 1251, 'n_words': 367}