<commit_before>from icap import ICAPRequest, ICAPResponse, RequestLine, HeadersDict, HTTPRequest, HTTPResponse
from icap.errors import ICAPAbort
from icap.models import ICAPMessage, HTTPMessage

class TestHTTPMessage(object):
    def test_is_response_and_is_response(self):
        m = HTTPMessage()
        assert not (m.is_request or m.is_response)

        m = HTTPRequest()
        assert m.is_request
        assert not m.is_response

        m = HTTPResponse()
        assert not m.is_request
        assert m.is_response


class TestICAPMessage(object):
    def test_is_response_and_is_response(self):
        m = ICAPMessage()
        assert not (m.is_request or m.is_response)

        m = ICAPRequest()
        assert m.is_request
        assert not m.is_response

        m = ICAPResponse()
        assert not m.is_request
        assert m.is_response

    def test_has_body(self):
        r = self.request('OPTIONS')
        assert not r.has_body

        r = self.request('OPTIONS')
        r.headers['Encapsulated'] = 'null-body=0'
        assert not r.has_body

        r = self.request('REQMOD')
        r.headers['Encapsulated'] = 'req-body=0'
        assert r.has_body

        r = self.request('RESPMOD')
        r.headers['Encapsulated'] = 'resp-body=0'
        assert r.has_body

    def request(self, method, *args, **kwargs):
        return ICAPRequest(RequestLine(method, '/', 'ICAP/1.0'), *args, **kwargs)


class TestICAPRequest(object):
    def test_allow_204(self):
        request = self.request('REQMOD')
        assert not request.allow_204

        request = self.request('REQMOD', headers=HeadersDict([('Allow', '204')]))
        assert request.allow_204

    def test_init_defaults(self):
        d = ICAPRequest()
        assert bytes(d.request_line) == b'UNKNOWN / ICAP/1.0'

    def test_is_reqmod(self):
        assert self.request('REQMOD').is_reqmod
        assert not self.request('RESPMOD').is_reqmod

    def test_is_respmod(self):
        assert self.request('RESPMOD').is_respmod
        assert not self.request('REQMOD').is_respmod

    def test_is_options(self):
        assert not self.request('REQMOD').is_options
        assert not self.request('RESPMOD').is_options
        assert self.request('OPTIONS').is_options

    def request(self, method, *args, **kwargs):
        return ICAPRequest(RequestLine(method, '/', 'ICAP/1.0'), *args, **kwargs)


class TestICAPResponse(object):
    def test_from_error(self):
        s = ICAPResponse.from_error(200)
        assert bytes(s) == b'ICAP/1.0 200 OK\r\n'

        s = ICAPResponse.from_error(ICAPAbort(200))
        assert bytes(s) == b'ICAP/1.0 200 OK\r\n'

        s = ICAPResponse.from_error(ICAPAbort(204))
        assert bytes(s) == b'ICAP/1.0 204 No Modifications Needed\r\n'

        headers = HeadersDict([
            ('header', 'value'),
        ])

        s = ICAPResponse.from_error(ICAPAbort(204))
        s.headers = headers
        assert bytes(s) == b'ICAP/1.0 204 No Modifications Needed\r\nheader: value\r\n'<commit_msg>test for StatusLine defaults.<commit_after>from icap import ICAPRequest, ICAPResponse, RequestLine, HeadersDict, HTTPRequest, HTTPResponse, StatusLine
from icap.errors import ICAPAbort
from icap.models import ICAPMessage, HTTPMessage

class TestHTTPMessage(object):
    def test_is_response_and_is_response(self):
        m = HTTPMessage()
        assert not (m.is_request or m.is_response)

        m = HTTPRequest()
        assert m.is_request
        assert not m.is_response

        m = HTTPResponse()
        assert not m.is_request
        assert m.is_response


class TestICAPMessage(object):
    def test_is_response_and_is_response(self):
        m = ICAPMessage()
        assert not (m.is_request or m.is_response)

        m = ICAPRequest()
        assert m.is_request
        assert not m.is_response

        m = ICAPResponse()
        assert not m.is_request
        assert m.is_response

    def test_has_body(self):
        r = self.request('OPTIONS')
        assert not r.has_body

        r = self.request('OPTIONS')
        r.headers['Encapsulated'] = 'null-body=0'
        assert not r.has_body

        r = self.request('REQMOD')
        r.headers['Encapsulated'] = 'req-body=0'
        assert r.has_body

        r = self.request('RESPMOD')
        r.headers['Encapsulated'] = 'resp-body=0'
        assert r.has_body

    def request(self, method, *args, **kwargs):
        return ICAPRequest(RequestLine(method, '/', 'ICAP/1.0'), *args, **kwargs)


class TestICAPRequest(object):
    def test_allow_204(self):
        request = self.request('REQMOD')
        assert not request.allow_204

        request = self.request('REQMOD', headers=HeadersDict([('Allow', '204')]))
        assert request.allow_204

    def test_init_defaults(self):
        d = ICAPRequest()
        assert bytes(d.request_line) == b'UNKNOWN / ICAP/1.0'

    def test_is_reqmod(self):
        assert self.request('REQMOD').is_reqmod
        assert not self.request('RESPMOD').is_reqmod

    def test_is_respmod(self):
        assert self.request('RESPMOD').is_respmod
        assert not self.request('REQMOD').is_respmod

    def test_is_options(self):
        assert not self.request('REQMOD').is_options
        assert not self.request('RESPMOD').is_options
        assert self.request('OPTIONS').is_options

    def request(self, method, *args, **kwargs):
        return ICAPRequest(RequestLine(method, '/', 'ICAP/1.0'), *args, **kwargs)


class TestICAPResponse(object):
    def test_from_error(self):
        s = ICAPResponse.from_error(200)
        assert bytes(s) == b'ICAP/1.0 200 OK\r\n'

        s = ICAPResponse.from_error(ICAPAbort(200))
        assert bytes(s) == b'ICAP/1.0 200 OK\r\n'

        s = ICAPResponse.from_error(ICAPAbort(204))
        assert bytes(s) == b'ICAP/1.0 204 No Modifications Needed\r\n'

        headers = HeadersDict([
            ('header', 'value'),
        ])

        s = ICAPResponse.from_error(ICAPAbort(204))
        s.headers = headers
        assert bytes(s) == b'ICAP/1.0 204 No Modifications Needed\r\nheader: value\r\n'


def test_StatusLine_defaults():
    s = StatusLine('HTTP/1.1', 200)
    assert s.reason == 'OK'

    s = StatusLine('ICAP/1.1', 204)
    assert s.reason == 'No Modifications Needed'
{'fraction_non_alphanumeric': 0.11879770992366412, 'fraction_numerical': 0.018447837150127225, 'mean_word_length': 2.723505032563647, 'pattern_counts': {'":': 0, '<': 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': '1737924', 'n_tokens_mistral': 2105, 'n_tokens_neox': 1998, 'n_words': 427}