forked from ganmacs/grpc_mock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ganmacs#2 from ganmacs/support-stub_request
Support stub request
- Loading branch information
Showing
21 changed files
with
710 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'grpc_mock/request_stub' | ||
require 'grpc_mock/matchers/request_including_matcher' | ||
|
||
module GrpcMock | ||
module Api | ||
# @param path [String] | ||
def stub_request(path) | ||
GrpcMock.stub_registry.register_request_stub(GrpcMock::RequestStub.new(path)) | ||
end | ||
|
||
# @param values [Hash] | ||
def request_including(values) | ||
GrpcMock::Matchers::RequestIncludingMatcher.new(values) | ||
end | ||
|
||
def disable_net_connect! | ||
GrpcMock.config.allow_net_connect = false | ||
end | ||
|
||
def allow_net_connect! | ||
GrpcMock.config.allow_net_connect = true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module GrpcMock | ||
module Matchers | ||
# Base class for Hash matchers | ||
# https://github.com/rspec/rspec-mocks/blob/master/lib/rspec/mocks/argument_matchers.rb | ||
class HashArgumentMatcher | ||
def self.stringify_keys!(arg, options = {}) | ||
case arg | ||
when Array | ||
arg.map { |elem| | ||
options[:deep] ? stringify_keys!(elem, options) : elem | ||
} | ||
when Hash | ||
Hash[ | ||
*arg.map { |key, value| | ||
k = key.is_a?(Symbol) ? key.to_s : key | ||
v = (options[:deep] ? stringify_keys!(value, options) : value) | ||
[k,v] | ||
}.inject([]) {|r,x| r + x}] | ||
else | ||
arg | ||
end | ||
end | ||
|
||
def initialize(expected) | ||
@expected = Hash[ | ||
GrpcMock::Matchers::HashArgumentMatcher.stringify_keys!(expected, deep: true).sort, | ||
] | ||
end | ||
|
||
def ==(_actual, &block) | ||
@expected.all?(&block) | ||
rescue NoMethodError | ||
false | ||
end | ||
|
||
def self.from_rspec_matcher(matcher) | ||
new(matcher.instance_variable_get(:@expected)) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'grpc_mock/matchers/hash_argument_matcher' | ||
|
||
module GrpcMock | ||
module Matchers | ||
class RequestIncludingMatcher < HashArgumentMatcher | ||
def ==(actual) | ||
if actual.respond_to?(:to_h) | ||
actual = actual.to_h | ||
end | ||
|
||
actual = Hash[GrpcMock::Matchers::HashArgumentMatcher.stringify_keys!(actual, deep: true)] | ||
super { |key, value| inner_including(value, key, actual) } | ||
rescue NoMethodError | ||
false | ||
end | ||
|
||
private | ||
|
||
def inner_including(expect, key, actual) | ||
if actual.key?(key) | ||
actual_value = actual[key] | ||
if expect.is_a?(Hash) | ||
RequestIncludingMatcher.new(expect) == actual_value | ||
else | ||
expect === actual_value | ||
end | ||
else | ||
false | ||
end | ||
end | ||
|
||
def inspect | ||
"reqeust_including(#{@expected.inspect})" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module GrpcMock | ||
class RequestPattern | ||
# @param path [String] | ||
def initialize(path) | ||
@path = path | ||
@block = nil | ||
@request = nil | ||
end | ||
|
||
def with(request = nil, &block) | ||
if request.nil? && !block_given? | ||
raise ArgumentError, '#with method invoked with no arguments. Either options request or block must be specified.' | ||
end | ||
|
||
@request = request | ||
@block = block | ||
end | ||
|
||
def match?(path, request) | ||
@path == path && (@request.nil? || @request == request) && (@block.nil? || @block.call(path)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'grpc_mock/request_pattern' | ||
require 'grpc_mock/resopnse_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(*resp) | ||
@response_sequence << GrpcMock::ResponsesSequence.new(resp) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module GrpcMock | ||
class ResponsesSequence | ||
attr_accessor :repeat | ||
|
||
def initialize(responses) | ||
@repeat = 1 | ||
@responses = responses | ||
@current = 0 | ||
@last = @responses.length - 1 | ||
end | ||
|
||
def end? | ||
@repeat == 0 | ||
end | ||
|
||
def next | ||
if @repeat > 0 | ||
response = @responses[@current] | ||
next_pos | ||
response | ||
else | ||
@responses.last | ||
end | ||
end | ||
|
||
private | ||
|
||
def next_pos | ||
if @last == @current | ||
@current = 0 | ||
@repeat -= 1 | ||
else | ||
@current += 1 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,8 @@ | |
config.after(:suite) do | ||
GrpcMock.disable! | ||
end | ||
|
||
config.after(:each) do | ||
GrpcMock.reset! | ||
end | ||
end |
Oops, something went wrong.