Пример #1
0
def server():
    with open("packages.xml", "rb") as f:
        response = f.read()

    handler = create_handler(response)
    with MockExternalHTTPServer(handler=handler) as server:
        yield server
def test_audit_logging(server, insecure_server, client_cls, proxy_host,
                       exception):
    audit_log_fp = StringIO()
    params = {"method": "metric_data"}
    prefix = getattr(client_cls, "PREFIX_SCHEME", "https://")
    if exception:
        port = MockExternalHTTPServer.get_open_port()
    elif prefix == "https://":
        port = server.port
    else:
        port = insecure_server.port

    internal_metrics = CustomMetrics()

    with client_cls(
            "localhost",
            port,
            proxy_scheme="https",
            proxy_host=proxy_host,
            proxy_port=server.port if not exception else port,
            audit_log_fp=audit_log_fp,
            disable_certificate_validation=True,
    ) as client:
        with InternalTraceContext(internal_metrics):
            try:
                client.send_request(params=params)
                exc = ""
            except Exception as e:
                exc = callable_name(type(e.args[0]))

    internal_metrics = dict(internal_metrics.metrics())
    if exception and client_cls is ApplicationModeClient:
        if proxy_host:
            connection = "https-proxy"
        else:
            connection = "direct"
        assert internal_metrics == {
            "Supportability/Python/Collector/Failures": [1, 0, 0, 0, 0, 0],
            "Supportability/Python/Collector/Failures/%s" % connection:
            [1, 0, 0, 0, 0, 0],
            "Supportability/Python/Collector/Exception/%s" % exc:
            [1, 0, 0, 0, 0, 0],
        }
    else:
        assert not internal_metrics

    # Verify the audit log isn't empty
    assert audit_log_fp.tell()

    audit_log_fp.seek(0)
    audit_log_contents = audit_log_fp.read()
    assert prefix in audit_log_contents
def mock_external_http_server():
    response_values = []

    def respond_with_cat_header(self):
        headers, response_code = response_values.pop()
        self.send_response(response_code)
        for header, value in headers:
            self.send_header(header, value)
        self.end_headers()
        self.wfile.write(b"")

    with MockExternalHTTPServer(handler=respond_with_cat_header) as server:
        yield (server, response_values)
def cat_response_server():
    external = MockExternalHTTPServer(handler=cat_response_handler)
    with external:
        yield external
def test_closed_connection():
    with HttpClient("localhost", MockExternalHTTPServer.get_open_port()) as client:
        with pytest.raises(NetworkInterfaceException):
            client.send_request()
Пример #6
0
def server():
    with MockExternalHTTPServer() as _server:
        yield _server