def main(host_info: dict, timeout: int = 3) -> dict:
    """
    Get raw HTTP response as decoded bytes (headers + response body)
    :param host_info: host information
    :param timeout: host timeout
    :return: dictionary with status and data
    """
    output = {"http_response": "", "status": "Success"}

    try:
        proto = "https" if host_info.get("port") in [443, 8443] else "http"
        url = f"{proto}://{host_info.get('ip')}:{host_info.get('port')}"
        response = get(url, verify=False, timeout=timeout)
        response_dump = bytearray()
        dump._dump_response_data(response,
                                 prefixes=dump.PrefixSettings(b"", b""),
                                 bytearr=response_dump)
    except (TimeoutError, ConnectTimeout):
        output.update({"status": "Timeout error was caught"})
    except ConnectionError as connection_err:
        output.update(
            {"status": f"Connection error was caught: {str(connection_err)}"})
    except ContentDecodingError as content_err:
        output.update({
            "status":
            f"Content decoding error was caight: {str(content_err)}"
        })
    except Exception as unexp_err:
        output.update(
            {"status": f"Unexpected error was caught: {str(unexp_err)}"})
    else:
        output.update({"http_response": response_dump.decode("utf-8")})
    return output
    def test_dump_response_data(self):
        """Build up the response data into a bytearray."""
        self.configure_response(
            url='https://example.com/redirected',
            content=b'foobarbogus',
            reason=b'OK',
        )
        self.configure_httpresponse(
            headers={'Content-Type': 'application/json'},
            reason=b'OK',
            status=201,
        )

        array = bytearray()
        prefixes = dump.PrefixSettings('request:', 'response:')
        dump._dump_response_data(
            response=self.response,
            prefixes=prefixes,
            bytearr=array,
        )

        assert b'response:HTTP/1.1 201 OK\r\n' in array
        assert b'response:Content-Type: application/json\r\n' in array
Exemple #3
0
    def test_dump_response_data(self):
        """Build up the response data into a bytearray."""
        self.configure_response(
            url='https://example.com/redirected',
            content=b'foobarbogus',
            reason=b'OK',
        )
        self.configure_httpresponse(
            headers={'Content-Type': 'application/json'},
            reason=b'OK',
            status=201,
        )

        array = bytearray()
        prefixes = dump.PrefixSettings('request:', 'response:')
        dump._dump_response_data(
            response=self.response,
            prefixes=prefixes,
            bytearr=array,
        )

        assert b'response:HTTP/1.1 201 OK\r\n' in array
        assert b'response:Content-Type: application/json\r\n' in array