<commit_before># encoding: utf-8

from __future__ import unicode_literals

import socket

from pprint import pformat

from marrow.server.http.testing import HTTPTestCase, CRLF, EOH

from marrow.util.compat import unicode


log = __import__('logging').getLogger(__name__)


def clean(request):
    del request['SERVER_NAME']
    del request['SERVER_PORT']
    del request['wsgi.errors']
    
    request['wsgi.input'] = request['wsgi.input'].read()
    
    if not request['wsgi.input']:
        del request['wsgi.input']


def echo(request):
    clean(request)
    result = unicode(pformat(request)).encode('utf8')
    return b'200 OK', [
            (b'Content-Type', b'text/plain; charset=utf8'),
            (b'Content-Length', unicode(len(result)).encode('ascii'))
        ], [result]


def chunked(request):
    clean(request)
    result = unicode(pformat(request)).encode('utf8')
    return b'200 OK', [
            (b'Content-Type', b'text/plain; charset=utf8'),
        ], result.split(b"\n")


def body_echo(request):
    clean(request)
    
    headers = [(b'Content-Type', b'text/plain; charset=utf8')]
    
    if 'HTTP_CONTENT_LENGTH' in request:
        headers.append((b'Content-Length', unicode(len(request['wsgi.input'])).encode('ascii')))
    
    return b'200 OK', headers, [request['wsgi.input']]


class TestHTTP11Protocol(HTTPTestCase):
    arguments = dict(application=echo)
    
    def test_headers(self):
        response = self.request(headers=[(b'Connection', b'close')])
        
        self.assertEquals(response.protocol, b"HTTP/1.1")
        self.assertEquals(response.code, b"200")
        self.assertEquals(response.status, b"OK")
        self.assertEquals(response[b'content-type'], b"text/plain; charset=utf8")
        # self.assertEquals(response[b'content-length'], b"468")
    
    def test_request(self):
        response = self.request(headers=[(b'Connection', b'close')])
        
        self.assertEquals(response.protocol, b"HTTP/1.1")
        self.assertEquals(response.code, b"200")
        self.assertEquals(response.status, b"OK")
        self.assertEquals(response[b'content-type'], b"text/plain; charset=utf8")
        # self.assertEquals(response[b'content-length'], b"468")
        
        request = eval(response.body)
        
        expect = {
                'CONTENT_LENGTH': None,
                'CONTENT_TYPE': None,
                'FRAGMENT': b'',
                'HTTP_CONNECTION': b'close',
                'HTTP_HOST': b'localhost',
                'PARAMETERS': b'',
                'PATH_INFO': b'/',
                'QUERY_STRING': b'',
                'REMOTE_ADDR': b'127.0.0.1',
                'REQUEST_METHOD': b'GET',<commit_msg>Added logging to diagnose syntax error issue.<commit_after># encoding: utf-8

from __future__ import unicode_literals

import socket

from pprint import pformat

from marrow.server.http.testing import HTTPTestCase, CRLF, EOH

from marrow.util.compat import unicode


log = __import__('logging').getLogger(__name__)


def clean(request):
    del request['SERVER_NAME']
    del request['SERVER_PORT']
    del request['wsgi.errors']
    
    request['wsgi.input'] = request['wsgi.input'].read()
    
    if not request['wsgi.input']:
        del request['wsgi.input']


def echo(request):
    clean(request)
    result = unicode(pformat(request)).encode('utf8')
    return b'200 OK', [
            (b'Content-Type', b'text/plain; charset=utf8'),
            (b'Content-Length', unicode(len(result)).encode('ascii'))
        ], [result]


def chunked(request):
    clean(request)
    result = unicode(pformat(request)).encode('utf8')
    return b'200 OK', [
            (b'Content-Type', b'text/plain; charset=utf8'),
        ], result.split(b"\n")


def body_echo(request):
    clean(request)
    
    headers = [(b'Content-Type', b'text/plain; charset=utf8')]
    
    if 'HTTP_CONTENT_LENGTH' in request:
        headers.append((b'Content-Length', unicode(len(request['wsgi.input'])).encode('ascii')))
    
    return b'200 OK', headers, [request['wsgi.input']]


class TestHTTP11Protocol(HTTPTestCase):
    arguments = dict(application=echo)
    
    def test_headers(self):
        response = self.request(headers=[(b'Connection', b'close')])
        
        self.assertEquals(response.protocol, b"HTTP/1.1")
        self.assertEquals(response.code, b"200")
        self.assertEquals(response.status, b"OK")
        self.assertEquals(response[b'content-type'], b"text/plain; charset=utf8")
        # self.assertEquals(response[b'content-length'], b"468")
    
    def test_request(self):
        response = self.request(headers=[(b'Connection', b'close')])
        
        self.assertEquals(response.protocol, b"HTTP/1.1")
        self.assertEquals(response.code, b"200")
        self.assertEquals(response.status, b"OK")
        self.assertEquals(response[b'content-type'], b"text/plain; charset=utf8")
        # self.assertEquals(response[b'content-length'], b"468")
        
        log.info("Recieved: %r", response.body)
        request = eval(response.body)
        
        expect = {
                'CONTENT_LENGTH': None,
                'CONTENT_TYPE': None,
                'FRAGMENT': b'',
                'HTTP_CONNECTION': b'close',
                'HTTP_HOST': b'localhost',
                'PARAMETERS': b'',
                'PATH_INFO': b'/',
                'QUERY_STRING': b'',
                'REMOTE_ADDR': b'127.0.0.1',
                'REQUEST_METHOD': b'GET',
{'fraction_non_alphanumeric': 0.15180196578085184, 'fraction_numerical': 0.014925373134328358, 'mean_word_length': 2.750853242320819, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3997512', 'n_tokens_mistral': 1783, 'n_tokens_neox': 1661, 'n_words': 294}