Sha256: ae49659549b8004703c6e6eac52a7b7ad2d2e94578c551ebeb33658efc867e5b

Contents?: true

Size: 1.48 KB

Versions: 7

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require 'grpc_mock/request_pattern'
require 'grpc_mock/response'
require 'grpc_mock/response_sequence'
require 'grpc_mock/errors'

module GrpcMock
  class RequestStub
    # @param path [String] gRPC path like /${service_name}/${method_name}
    def initialize(path)
      @request_pattern = RequestPattern.new(path)
      @response_sequence = []
    end

    def with(request = nil, &block)
      @request_pattern.with(request, &block)
      self
    end

    def to_return(*values, &block)
      responses = [*values].flatten.map { |v| Response::Value.new(v) }
      responses << Response::BlockValue.new(block) if block
      @response_sequence << GrpcMock::ResponsesSequence.new(responses)
      self
    end

    def to_raise(*exceptions)
      responses = [*exceptions].flatten.map { |e| Response::ExceptionValue.new(e) }
      @response_sequence << GrpcMock::ResponsesSequence.new(responses)
      self
    end

    def response
      if @response_sequence.empty?
        raise GrpcMock::NoResponseError, 'Must be set some values by using #GrpMock::RequestStub#to_run'
      elsif @response_sequence.size == 1
        @response_sequence.first.next
      else
        if @response_sequence.first.end?
          @response_sequence.shift
        end

        @response_sequence.first.next
      end
    end

    # @param path [String]
    # @param request [Object]
    # @return [Bool]
    def match?(path, request)
      @request_pattern.match?(path, request)
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/grpc_mock-0.4.6/lib/grpc_mock/request_stub.rb
grpc_mock-0.4.6 lib/grpc_mock/request_stub.rb
grpc_mock-0.4.5 lib/grpc_mock/request_stub.rb
grpc_mock-0.4.4 lib/grpc_mock/request_stub.rb
grpc_mock-0.4.3 lib/grpc_mock/request_stub.rb
grpc_mock-0.4.2 lib/grpc_mock/request_stub.rb
grpc_mock-0.4.1 lib/grpc_mock/request_stub.rb