def test_http_status_code_to_type(self):
        _all_http_status_codes_ = list(map(int, pyhttpstatus_utils.HttpStatusCode))

        for http_status_code in _all_http_status_codes_:
            assert(pyhttpstatus_utils.http_status_code_to_type(http_status_code))
            assert(type(pyhttpstatus_utils.is_http_status_successful(http_status_code)) is bool)

            if pyhttpstatus_utils.is_http_status_successful(http_status_code):
                assert(pyhttpstatus_utils.http_status_code_to_type(http_status_code) == pyhttpstatus_utils.HttpStatusType.SUCCESSFUL.value)
            else:
                assert(pyhttpstatus_utils.http_status_code_to_type(http_status_code) != pyhttpstatus_utils.HttpStatusType.SUCCESSFUL.value)
示例#2
0
    def test_http_status_code_to_type(self):
        __all_http_status_codes__ = list(map(int, HTTPStatus))

        for http_status_code in __all_http_status_codes__:
            assert(pyhttpstatus_utils.get_http_status_type(http_status_code))
            assert(type(pyhttpstatus_utils.is_http_status_successful(http_status_code)) is bool)

            if pyhttpstatus_utils.is_http_status_successful(http_status_code):
                assert(pyhttpstatus_utils.get_http_status_type(
                    http_status_code) == pyhttpstatus_utils.HttpStatusType.SUCCESSFUL)
            else:
                assert(pyhttpstatus_utils.get_http_status_type(
                    http_status_code) != pyhttpstatus_utils.HttpStatusType.SUCCESSFUL)
def validate_response(
    response,
    request_curl,
    request_label=None,
):
    """Validate response

    Args:
        response:
        request_label:
        request_url:

    Returns:

    """
    response_extra = {}
    if request_label:
        response_extra.update({'request_label': request_label})

    if not response:
        log.error("Validate Response: Failed: None", extra=response_extra)

        raise TuneRequestModuleError(
            error_message="Validate Response: Failed: None",
            error_request_curl=request_curl,
            error_code=TuneRequestErrorCodes.REQ_ERR_SOFTWARE)
    else:
        log.debug("Validate Response: Defined", extra=response_extra)

    response_extra.update({'http_status_code': response.status_code})

    if hasattr(response, 'text'):
        response_text_length = len(response.text)
        response_extra.update({'response_text_length': response_text_length})

    if response.headers:
        if 'Content-Type' in response.headers:
            response_headers_content_type = \
                safe_str(response.headers['Content-Type'])
            response_extra.update(
                {'Content-Type': response_headers_content_type})

        if 'Content-Length' in response.headers:
            response_headers_content_length = \
                safe_int(response.headers['Content-Length'])
            response_extra.update({
                'Content-Length':
                convert_size(response_headers_content_length)
            })

        if 'Content-Encoding' in response.headers:
            response_content_encoding = \
                safe_str(response.headers['Content-Encoding'])
            response_extra.update(
                {'Content-Encoding': response_content_encoding})

        if 'Transfer-Encoding' in response.headers:
            response_transfer_encoding = \
                safe_str(response.headers['Transfer-Encoding'])
            response_extra.update(
                {'Transfer-Encoding': response_transfer_encoding})

    if not is_http_status_successful(http_status_code=response.status_code):
        log.error("Validate Response: Failed", extra=response_extra)

        raise TuneRequestModuleError(
            error_message="Validate Request: Failed",
            error_request_curl=request_curl,
            error_code=TuneRequestErrorCodes.REQ_ERR_SOFTWARE)
    else:
        log.debug("Validate Response: Success", extra=response_extra)
 def test_is_http_status_successful(self):
     assert(pyhttpstatus_utils.is_http_status_successful(200))
def validate_response(
    response,
    request_curl,
    request_label=None,
):
    """Validate response

    Args:
        response:
        request_curl:
        request_label:

    Returns:

    """
    response_extra = {}
    if request_label is None:
        request_label = 'Validate Response'

    if not response:
        error_message = "{0}: Failed: None".format(request_label)
        log.error(error_message, extra=response_extra)

        raise RequestsFortifiedModuleError(
            error_message=error_message,
            error_request_curl=request_curl,
            error_code=RequestsFortifiedErrorCodes.REQ_ERR_SOFTWARE
        )

    log.debug("{0}: Defined".format(request_label), extra=response_extra)

    response_extra.update({'http_status_code': response.status_code})

    # Not using hasattr on purpose
    # Assuming positive approach will give us speedup since when attribute exists
    # hasattr takes double the time.
    # Anyway we use text attribute here to logging purpose only
    try:
        response_extra.update({'response_text_length': len(response.text)})
    except AttributeError:
        pass

    if response.headers:
        if 'Content-Type' in response.headers:
            response_headers_content_type = \
                safe_str(response.headers['Content-Type'])
            response_extra.update({'Content-Type': response_headers_content_type})

        if 'Content-Length' in response.headers:
            response_headers_content_length = \
                safe_int(response.headers['Content-Length'])
            response_extra.update({'Content-Length': bytes_to_human(response_headers_content_length)})

        if 'Content-Encoding' in response.headers:
            response_content_encoding = \
                safe_str(response.headers['Content-Encoding'])
            response_extra.update({'Content-Encoding': response_content_encoding})

        if 'Transfer-Encoding' in response.headers:
            response_transfer_encoding = \
                safe_str(response.headers['Transfer-Encoding'])
            response_extra.update({'Transfer-Encoding': response_transfer_encoding})

    if not is_http_status_successful(http_status_code=response.status_code):
        error_message = "{0}: Failed".format(request_label)
        log.error(error_message, extra=response_extra)

        raise RequestsFortifiedModuleError(
            error_message=error_message,
            error_request_curl=request_curl,
            error_code=RequestsFortifiedErrorCodes.REQ_ERR_SOFTWARE
        )

    log.debug("{0}: Success".format(request_label), extra=response_extra)
示例#6
0
def validate_response(
    response,
    request_curl,
    request_label=None,
):
    """Validate response

    Args:
        response:
        request_curl:
        request_label:

    Returns:

    """
    response_extra = {}
    if request_label is None:
        request_label = 'Validate Response'

    if not response:
        error_message = f'{request_label}: Failed: None'
        log.error(error_message, extra=response_extra)

        raise TuneRequestModuleError(
            error_message=error_message,
            error_request_curl=request_curl,
            error_code=TuneRequestErrorCodes.REQ_ERR_SOFTWARE)

    log.debug(f'{request_label}: Defined', extra=response_extra)

    response_extra.update({'http_status_code': response.status_code})

    # Not using hasattr on purpose
    # Assuming positive approach will give us speedup since when attribute exists
    # hasattr takes double the time.
    # Anyway we use text attribute here to logging purpose only
    try:
        response_extra.update({'response_text_length': len(response.text)})
    except AttributeError:
        pass

    if response.headers:
        if 'Content-Type' in response.headers:
            response_headers_content_type = \
                safe_str(response.headers['Content-Type'])
            response_extra.update(
                {'Content-Type': response_headers_content_type})

        if 'Content-Length' in response.headers:
            response_headers_content_length = \
                safe_int(response.headers['Content-Length'])
            response_extra.update({
                'Content-Length':
                bytes_to_human(response_headers_content_length)
            })

        if 'Content-Encoding' in response.headers:
            response_content_encoding = \
                safe_str(response.headers['Content-Encoding'])
            response_extra.update(
                {'Content-Encoding': response_content_encoding})

        if 'Transfer-Encoding' in response.headers:
            response_transfer_encoding = \
                safe_str(response.headers['Transfer-Encoding'])
            response_extra.update(
                {'Transfer-Encoding': response_transfer_encoding})

    if not is_http_status_successful(http_status_code=response.status_code):
        error_message = f'{request_label}: Failed'
        log.error(error_message, extra=response_extra)

        raise TuneRequestModuleError(
            error_message=error_message,
            error_request_curl=request_curl,
            error_code=TuneRequestErrorCodes.REQ_ERR_SOFTWARE)

    log.debug(f'{request_label}: Success', extra=response_extra)
示例#7
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#  @namespace pyhttpstatus_utils

from pprintpp import pprint
import pyhttpstatus_utils

__all__ = list(map(int, pyhttpstatus_utils.HttpStatusCode))

for status_code in __all__:
    pprint(pyhttpstatus_utils.http_status_code_to_type(status_code))
    pprint(pyhttpstatus_utils.is_http_status_successful(status_code))

    if pyhttpstatus_utils.is_http_status_successful(status_code):
        pprint(pyhttpstatus_utils.http_status_code_to_type(status_code) == pyhttpstatus_utils.HttpStatusType.SUCCESSFUL)
    else:
        pprint(pyhttpstatus_utils.http_status_code_to_type(status_code) != pyhttpstatus_utils.HttpStatusType.SUCCESSFUL)