def test_expand(transport: str = 'grpc'):
    client = EchoClient(
        credentials=credentials.AnonymousCredentials(),
        transport=transport,
    )

    # Everything is optional in proto3 as far as the runtime is concerned,
    # and we are mocking out the actual API, so just send an empty request.
    request = gs_echo.ExpandRequest()

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(type(client._transport.expand), '__call__') as call:
        # Designate an appropriate return value for the call.
        call.return_value = iter([gs_echo.EchoResponse()])

        response = client.expand(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 response is the type that we expect.
    for message in response:
        assert isinstance(message, gs_echo.EchoResponse)
Example #2
0
def test_expand():
  print("=================test expand ===================")
  client = EchoClient(transport=transport)
  request = gs_echo.ExpandRequest(content='one two three four')
  responses = client.expand(request)
  for message in responses:
    print(message)
  print("trailing metadata...")
  print(responses.trailing_metadata())
def test_expand_flattened_error():
    client = EchoClient(credentials=credentials.AnonymousCredentials(), )

    # Attempting to call a method with both a request object and flattened
    # fields is an error.
    with pytest.raises(ValueError):
        client.expand(
            gs_echo.ExpandRequest(),
            content='content_value',
            error=status.Status(code=411),
        )
def run_should_pass():
    print("================= should pass ====================")
    header_adder_interceptor = client_interceptor.header_adder_interceptor(
        'one-time-password', '42')
    channel = grpc.insecure_channel('localhost:50051')
    intercept_channel = grpc.intercept_channel(channel,
                                               header_adder_interceptor)
    transport = transports.EchoGrpcTransport(channel=intercept_channel)
    client = EchoClient(transport=transport)
    request = gs_echo.ExpandRequest(content='one two three four')
    responses = client.expand(request)
    for message in responses:
        print(message)
    print("trailing metadata...")
    print(responses.trailing_metadata())
def run_should_fail():
    print(
        "================= should fail with 'Access denied!' ===================="
    )
    channel = grpc.insecure_channel('localhost:50051')
    transport = transports.EchoGrpcTransport(channel=channel)
    client = EchoClient(transport=transport)
    try:
        request = gs_echo.ExpandRequest(content='one two three four')
        responses = client.expand(request)
        for message in responses:
            print(message)
        print("trailing metadata...")
        print(responses.trailing_metadata())
    except:
        print(sys.exc_info())
Example #6
0
    def expand(
            self,
            request: gs_echo.ExpandRequest = None,
            *,
            content: str = None,
            error: status.Status = None,
            retry: retries.Retry = gapic_v1.method.DEFAULT,
            timeout: float = None,
            metadata: Sequence[Tuple[str, str]] = (),
    ) -> gs_echo.EchoResponse:
        r"""This method split the given content into words and
        will pass each word back through the stream. This method
        showcases server-side streaming rpcs.

        Args:
            request (:class:`~.gs_echo.ExpandRequest`):
                The request object. The request message for the Expand
                method.
            content (:class:`str`):
                The content that will be split into
                words and returned on the stream.
                This corresponds to the ``content`` field
                on the ``request`` instance; if ``request`` is provided, this
                should not be set.
            error (:class:`~.status.Status`):
                The error that is thrown after all
                words are sent on the stream.
                This corresponds to the ``error`` field
                on the ``request`` instance; if ``request`` is provided, this
                should not be set.
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.
            timeout (float): The timeout for this request.
            metadata (Sequence[Tuple[str, str]]): Strings which should be
                sent along with the request as metadata.

        Returns:
            ~.gs_echo.EchoResponse:
                The response message for the Echo
                methods.

        """
        # Create or coerce a protobuf request object.
        # Sanity check: If we got a request object, we should *not* have
        # gotten any keyword arguments that map to the request.
        if request is not None and any([content, error]):
            raise ValueError('If the `request` argument is set, then none of '
                             'the individual field arguments should be set.')

        # If we have keyword arguments corresponding to fields on the
        # request, apply these.
        request = gs_echo.ExpandRequest(request)
        if content is not None:
            request.content = content
        if error is not None:
            request.error = error

        # Wrap the RPC method; this adds retry and timeout information,
        # and friendly error handling.
        rpc = gapic_v1.method.wrap_method(
            self._transport.expand,
            default_timeout=None,
            client_info=_client_info,
        )

        # Send the request.
        response = rpc(
            request,
            retry=retry,
            timeout=timeout,
            metadata=metadata,
        )

        # Done; return the response.
        return response