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')
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
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)
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
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())
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')
def test_bytes_starts_with_verb(): r = Request(verb='TEST') result = bytes(r) assert result.startswith(b'TEST')
def test_init_headers(): r = Request(verb='TEST') assert hasattr(r, 'headers')
def test_init_version(): r = Request(verb='TEST', version='4.2') assert r.version == '4.2'
def test_init_verb(): r = Request(verb='TEST') assert r.verb == 'TEST'
def test_str(): r = Request(verb='TEST') result = str(r) assert result == '<TEST: aiospamc.requests.Request object at {}>'.format( id(r))
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
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
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
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
def test_request_from_parser_result(request_with_body): p = RequestParser().parse(request_with_body) r = Request(**p) assert r is not None
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)
def test_request_instantiates(): request = Request('TEST') assert 'request' in locals()