Exemplo n.º 1
0
def test_bytes_headers(x_headers):
    r = Request(verb='TEST', headers=x_headers)
    result = bytes(r).partition(b'\r\n')[2]
    expected = bytes(r.headers)

    assert result.startswith(expected)
    assert result.endswith(b'\r\n\r\n')
Exemplo n.º 2
0
def test_bytes_headers(x_headers):
    r = Request(verb='TEST', headers=x_headers)
    result = bytes(r).split(b'\r\n')[
        1:-2]  # strip end of headers, body and first line
    expected = [bytes(header).rstrip(b'\r\n') for header in x_headers]

    for header_bytes in result:
        assert header_bytes in expected
Exemplo n.º 3
0
def test_bytes_body_compressed():
    test_input = b'Test body\n'
    r = Request(verb='TEST',
                headers={'Compress': CompressValue()},
                body=test_input)
    result = bytes(r).rpartition(b'\r\n')[2]

    assert result == zlib.compress(test_input)
Exemplo n.º 4
0
    async def ping(self):
        '''Sends a ping request to the SPAMD service and will receive a
        response if the serivce is alive.

        Returns
        -------
        :class:`aiospamc.responses.Response`
            Response message will contain 'PONG' if successful.

        Raises
        ------
        :class:`aiospamc.exceptions.BadResponse`
            If the response from SPAMD is ill-formed this exception will be
            raised.
        :class:`aiospamc.exceptions.AIOSpamcConnectionFailed`
            Raised if an error occurred when trying to connect.
        :class:`aiospamc.exceptions.UsageException`
            Error in command line usage.
        :class:`aiospamc.exceptions.DataErrorException`
            Error with data format.
        :class:`aiospamc.exceptions.NoInputException`
            Cannot open input.
        :class:`aiospamc.exceptions.NoUserException`
            Addressee unknown.
        :class:`aiospamc.exceptions.NoHostException`
            Hostname unknown.
        :class:`aiospamc.exceptions.UnavailableException`
            Service unavailable.
        :class:`aiospamc.exceptions.InternalSoftwareException`
            Internal software error.
        :class:`aiospamc.exceptions.OSErrorException`
            System error.
        :class:`aiospamc.exceptions.OSFileException`
            Operating system file missing.
        :class:`aiospamc.exceptions.CantCreateException`
            Cannot create output file.
        :class:`aiospamc.exceptions.IOErrorException`
            Input/output error.
        :class:`aiospamc.exceptions.TemporaryFailureException`
            Temporary failure, may reattempt.
        :class:`aiospamc.exceptions.ProtocolException`
            Error in the protocol.
        :class:`aiospamc.exceptions.NoPermissionException`
            Permission denied.
        :class:`aiospamc.exceptions.ConfigException`
            Error in configuration.
        :class:`aiospamc.exceptions.TimeoutException`
            Timeout during connection.
        '''

        request = Request('PING')
        self.logger.debug('Composed %s request (%s)', request.verb, id(request))
        response = await self.send(request)

        return response
Exemplo n.º 5
0
def test_request_bytes(verb, body, headers):
    request = Request(verb=verb, body=body, headers=headers)

    assert bytes(request).startswith(verb.encode())
    assert bytes(b'SPAMC/1.5\r\n') in bytes(request)
    assert all(bytes(header) in bytes(request) for header in headers)
    if body:
        if any(isinstance(header, Compress) for header in headers):
            assert bytes(request).endswith(zlib.compress(body.encode()))
        else:
            assert bytes(request).endswith(body.encode())
Exemplo n.º 6
0
def test_bytes_protocol():
    r = Request(verb='TEST', version='4.2')
    result = bytes(r).split(b'\r\n', 1)[0]

    assert result.endswith(b' SPAMC/4.2')
Exemplo n.º 7
0
def test_bytes_starts_with_verb():
    r = Request(verb='TEST')
    result = bytes(r)

    assert result.startswith(b'TEST')
Exemplo n.º 8
0
def test_init_headers():
    r = Request(verb='TEST')

    assert hasattr(r, 'headers')
Exemplo n.º 9
0
def test_init_version():
    r = Request(verb='TEST', version='4.2')

    assert r.version == '4.2'
Exemplo n.º 10
0
def test_init_verb():
    r = Request(verb='TEST')

    assert r.verb == 'TEST'
Exemplo n.º 11
0
def test_str():
    r = Request(verb='TEST')
    result = str(r)

    assert result == '<TEST: aiospamc.requests.Request object at {}>'.format(
        id(r))
Exemplo n.º 12
0
def test_bytes_body():
    test_input = b'Test body\n'
    r = Request(verb='TEST', body=test_input)
    result = bytes(r).rpartition(b'\r\n')[2]

    assert result == test_input
Exemplo n.º 13
0
    async def tell(self,
                   message_class,
                   message,
                   remove_action=None,
                   set_action=None,
                  ):
        '''Instruct the SPAMD service to to mark the message

        Parameters
        ----------
        message_class : :class:`aiospamc.options.MessageClassOption`
            An enumeration to classify the message as 'spam' or 'ham.'
        message : :obj:`str`
            A string containing the contents of the message to be scanned.

            SPAMD will perform a scan on the included message.  SPAMD expects an
            RFC 822 or RFC 2822 formatted email.
        remove_action : :class:`aiospamc.options.ActionOption`
            Remove message class for message in database.
        set_action : :class:`aiospamc.options.ActionOption`
            Set message class for message in database.

        Returns
        -------
        :class:`aiospamc.responses.Response`
            Will contain a 'Spam' header if the message is marked as spam as
            well as the score and threshold.

            The body will contain a report composed by the SPAMD service only if
            message is marked as being spam.

        Raises
        ------
        :class:`aiospamc.exceptions.BadResponse`
            If the response from SPAMD is ill-formed this exception will be
            raised.
        :class:`aiospamc.exceptions.AIOSpamcConnectionFailed`
            Raised if an error occurred when trying to connect.
        :class:`aiospamc.exceptions.UsageException`
            Error in command line usage.
        :class:`aiospamc.exceptions.DataErrorException`
            Error with data format.
        :class:`aiospamc.exceptions.NoInputException`
            Cannot open input.
        :class:`aiospamc.exceptions.NoUserException`
            Addressee unknown.
        :class:`aiospamc.exceptions.NoHostException`
            Hostname unknown.
        :class:`aiospamc.exceptions.UnavailableException`
            Service unavailable.
        :class:`aiospamc.exceptions.InternalSoftwareException`
            Internal software error.
        :class:`aiospamc.exceptions.OSErrorException`
            System error.
        :class:`aiospamc.exceptions.OSFileException`
            Operating system file missing.
        :class:`aiospamc.exceptions.CantCreateException`
            Cannot create output file.
        :class:`aiospamc.exceptions.IOErrorException`
            Input/output error.
        :class:`aiospamc.exceptions.TemporaryFailureException`
            Temporary failure, may reattempt.
        :class:`aiospamc.exceptions.ProtocolException`
            Error in the protocol.
        :class:`aiospamc.exceptions.NoPermissionException`
            Permission denied.
        :class:`aiospamc.exceptions.ConfigException`
            Error in configuration.
        :class:`aiospamc.exceptions.TimeoutException`
            Timeout during connection.
        '''

        request = Request(verb='TELL',
                          headers=(MessageClass(message_class),),
                          body=message)
        if remove_action:
            request.add_header(Set(remove_action))
        if set_action:
            request.add_header(Remove(set_action))
        self.logger.debug('Composed %s request (%s)', request.verb, id(request))
        response = await self.send(request)

        return response
Exemplo n.º 14
0
    async def symbols(self, message):
        '''Request the SPAMD service to check a message with a SYMBOLS request.

        The response will contain a 'Spam' header if the message is marked
        as spam as well as the score and threshold.

        Parameters
        ----------
        message : :obj:`str`
            A string containing the contents of the message to be scanned.

            SPAMD will perform a scan on the included message.  SPAMD expects an
            RFC 822 or RFC 2822 formatted email.

        Returns
        -------
        :class:`aiospamc.responses.Response`
            Will contain a 'Spam' header if the message is marked as spam as
            well as the score and threshold.

            The body will contain a comma separated list of all the rule names.

        Raises
        ------
        :class:`aiospamc.exceptions.BadResponse`
            If the response from SPAMD is ill-formed this exception will be
            raised.
        :class:`aiospamc.exceptions.AIOSpamcConnectionFailed`
            Raised if an error occurred when trying to connect.
        :class:`aiospamc.exceptions.UsageException`
            Error in command line usage.
        :class:`aiospamc.exceptions.DataErrorException`
            Error with data format.
        :class:`aiospamc.exceptions.NoInputException`
            Cannot open input.
        :class:`aiospamc.exceptions.NoUserException`
            Addressee unknown.
        :class:`aiospamc.exceptions.NoHostException`
            Hostname unknown.
        :class:`aiospamc.exceptions.UnavailableException`
            Service unavailable.
        :class:`aiospamc.exceptions.InternalSoftwareException`
            Internal software error.
        :class:`aiospamc.exceptions.OSErrorException`
            System error.
        :class:`aiospamc.exceptions.OSFileException`
            Operating system file missing.
        :class:`aiospamc.exceptions.CantCreateException`
            Cannot create output file.
        :class:`aiospamc.exceptions.IOErrorException`
            Input/output error.
        :class:`aiospamc.exceptions.TemporaryFailureException`
            Temporary failure, may reattempt.
        :class:`aiospamc.exceptions.ProtocolException`
            Error in the protocol.
        :class:`aiospamc.exceptions.NoPermissionException`
            Permission denied.
        :class:`aiospamc.exceptions.ConfigException`
            Error in configuration.
        :class:`aiospamc.exceptions.TimeoutException`
            Timeout during connection.
        '''

        request = Request('SYMBOLS', body=message)
        self.logger.debug('Composed %s request (%s)', request.verb, id(request))
        response = await self.send(request)

        return response
Exemplo n.º 15
0
def test_bytes_body():
    test_input = b'Test body\n'
    r = Request(verb='TEST', body=test_input)
    result = bytes(r).split(b'\r\n', 3)[-1]

    assert result == test_input
Exemplo n.º 16
0
def test_request_from_parser_result(request_with_body):
    p = RequestParser().parse(request_with_body)
    r = Request(**p)

    assert r is not None
Exemplo n.º 17
0
def test_bytes_body_compressed():
    test_input = b'Test body\n'
    r = Request(verb='TEST', headers=[Compress()], body=test_input)
    result = bytes(r).split(b'\r\n', 3)[-1]

    assert result == zlib.compress(test_input)
Exemplo n.º 18
0
def test_request_instantiates():
    request = Request('TEST')

    assert 'request' in locals()