def test_list_topic_subscriptions_field_headers():
    client = PublisherClient(credentials=credentials.AnonymousCredentials(), )

    # Any value that is part of the HTTP/1.1 URI should be sent as
    # a field header. Set these to a non-empty value.
    request = pubsub.ListTopicSubscriptionsRequest(topic='topic/value', )

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(type(client._transport.list_topic_subscriptions),
                           '__call__') as call:
        call.return_value = pubsub.ListTopicSubscriptionsResponse()
        client.list_topic_subscriptions(request)

        # Establish that the underlying gRPC stub method was called.
        assert len(call.mock_calls) == 1
        _, args, _ = call.mock_calls[0]
        assert args[0] == request

    # Establish that the field header was sent.
    _, _, kw = call.mock_calls[0]
    assert (
        'x-goog-request-params',
        'topic=topic/value',
    ) in kw['metadata']
def test__get_default_mtls_endpoint():
    api_endpoint = "example.googleapis.com"
    api_mtls_endpoint = "example.mtls.googleapis.com"
    sandbox_endpoint = "example.sandbox.googleapis.com"
    sandbox_mtls_endpoint = "example.mtls.sandbox.googleapis.com"
    non_googleapi = "api.example.com"

    assert PublisherClient._get_default_mtls_endpoint(None) == None
    assert PublisherClient._get_default_mtls_endpoint(
        api_endpoint) == api_mtls_endpoint
    assert PublisherClient._get_default_mtls_endpoint(
        api_mtls_endpoint) == api_mtls_endpoint
    assert PublisherClient._get_default_mtls_endpoint(
        sandbox_endpoint) == sandbox_mtls_endpoint
    assert PublisherClient._get_default_mtls_endpoint(
        sandbox_mtls_endpoint) == sandbox_mtls_endpoint
    assert PublisherClient._get_default_mtls_endpoint(
        non_googleapi) == non_googleapi
def test_transport_instance():
    # A client may be instantiated with a custom transport instance.
    transport = transports.PublisherGrpcTransport(
        credentials=credentials.AnonymousCredentials(), )
    client = PublisherClient(transport=transport)
    assert client._transport is transport
def test_publisher_client_client_options():
    # Check that if channel is provided we won't create a new one.
    with mock.patch(
            'google.pubsub_v1.services.publisher.PublisherClient.get_transport_class'
    ) as gtc:
        transport = transports.PublisherGrpcTransport(
            credentials=credentials.AnonymousCredentials())
        client = PublisherClient(transport=transport)
        gtc.assert_not_called()

    # Check mTLS is not triggered with empty client options.
    options = client_options.ClientOptions()
    with mock.patch(
            'google.pubsub_v1.services.publisher.PublisherClient.get_transport_class'
    ) as gtc:
        transport = gtc.return_value = mock.MagicMock()
        client = PublisherClient(client_options=options)
        transport.assert_called_once_with(
            credentials=None,
            host=client.DEFAULT_ENDPOINT,
        )

    # Check mTLS is not triggered if api_endpoint is provided but
    # client_cert_source is None.
    options = client_options.ClientOptions(api_endpoint="squid.clam.whelk")
    with mock.patch(
            'google.pubsub_v1.services.publisher.transports.PublisherGrpcTransport.__init__'
    ) as grpc_transport:
        grpc_transport.return_value = None
        client = PublisherClient(client_options=options)
        grpc_transport.assert_called_once_with(
            api_mtls_endpoint=None,
            client_cert_source=None,
            credentials=None,
            host="squid.clam.whelk",
        )

    # Check mTLS is triggered if client_cert_source is provided.
    options = client_options.ClientOptions(
        client_cert_source=client_cert_source_callback)
    with mock.patch(
            'google.pubsub_v1.services.publisher.transports.PublisherGrpcTransport.__init__'
    ) as grpc_transport:
        grpc_transport.return_value = None
        client = PublisherClient(client_options=options)
        grpc_transport.assert_called_once_with(
            api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT,
            client_cert_source=client_cert_source_callback,
            credentials=None,
            host=client.DEFAULT_ENDPOINT,
        )

    # Check mTLS is triggered if api_endpoint and client_cert_source are provided.
    options = client_options.ClientOptions(
        api_endpoint="squid.clam.whelk",
        client_cert_source=client_cert_source_callback)
    with mock.patch(
            'google.pubsub_v1.services.publisher.transports.PublisherGrpcTransport.__init__'
    ) as grpc_transport:
        grpc_transport.return_value = None
        client = PublisherClient(client_options=options)
        grpc_transport.assert_called_once_with(
            api_mtls_endpoint="squid.clam.whelk",
            client_cert_source=client_cert_source_callback,
            credentials=None,
            host="squid.clam.whelk",
        )