grpc_mock 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/grpc_mock/grpc_stub_adapter.rb +12 -8
- data/lib/grpc_mock/mocked_call.rb +72 -0
- data/lib/grpc_mock/response.rb +4 -4
- data/lib/grpc_mock/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed55b4c744bb9a0b6c705e5e3c05e54a8ddc8739f1e5a660387f8269a7454170
|
4
|
+
data.tar.gz: aad2eee18de57d3f752f85c3e1f013032603bf487c3dc8a98044116507b6e43d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c92bfe07183760fea13c77e3d999da4e57c7d92e9e664572a4c421344fa775061dc9b72772a9bec1f4ac908fcbedb8b1b657ecf27220e6dfa0d9c70dba3bc8c
|
7
|
+
data.tar.gz: d507ea97f5124eef53fb5e8d5fe61748f0105f6defa1b50879ec6f7770928887fd906b7a84520d9a3726e9217b341570873807a9e7f70472f887b22d88b03540
|
data/README.md
CHANGED
@@ -52,7 +52,7 @@ client client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Hello::HelloResp
|
|
52
52
|
### Responding dynamically to the stubbed requests
|
53
53
|
|
54
54
|
```ruby
|
55
|
-
GrpcMock.stub_request("/hello.hello/Hello").to_return do |req|
|
55
|
+
GrpcMock.stub_request("/hello.hello/Hello").to_return do |req, call|
|
56
56
|
Hello::HelloResponse.new(msg: "#{req.msg} too")
|
57
57
|
end
|
58
58
|
|
@@ -2,20 +2,22 @@
|
|
2
2
|
|
3
3
|
require 'grpc'
|
4
4
|
require 'grpc_mock/errors'
|
5
|
+
require 'grpc_mock/mocked_call'
|
5
6
|
|
6
7
|
module GrpcMock
|
7
8
|
class GrpcStubAdapter
|
8
9
|
# To make hook point for GRPC::ClientStub
|
9
10
|
# https://github.com/grpc/grpc/blob/bec3b5ada2c5e5d782dff0b7b5018df646b65cb0/src/ruby/lib/grpc/generic/service.rb#L150-L186
|
10
11
|
class AdapterClass < GRPC::ClientStub
|
11
|
-
def request_response(method, request, *args)
|
12
|
+
def request_response(method, request, *args, metadata: {}, **kwargs)
|
12
13
|
unless GrpcMock::GrpcStubAdapter.enabled?
|
13
14
|
return super
|
14
15
|
end
|
15
16
|
|
16
17
|
mock = GrpcMock.stub_registry.response_for_request(method, request)
|
17
18
|
if mock
|
18
|
-
|
19
|
+
call = GrpcMock::MockedCall.new(metadata: metadata)
|
20
|
+
mock.evaluate(request, call.single_req_view)
|
19
21
|
elsif GrpcMock.config.allow_net_connect
|
20
22
|
super
|
21
23
|
else
|
@@ -24,7 +26,7 @@ module GrpcMock
|
|
24
26
|
end
|
25
27
|
|
26
28
|
# TODO
|
27
|
-
def client_streamer(method, requests, *args)
|
29
|
+
def client_streamer(method, requests, *args, metadata: {}, **kwargs)
|
28
30
|
unless GrpcMock::GrpcStubAdapter.enabled?
|
29
31
|
return super
|
30
32
|
end
|
@@ -32,7 +34,8 @@ module GrpcMock
|
|
32
34
|
r = requests.to_a # FIXME: this may not work
|
33
35
|
mock = GrpcMock.stub_registry.response_for_request(method, r)
|
34
36
|
if mock
|
35
|
-
|
37
|
+
call = GrpcMock::MockedCall.new(metadata: metadata)
|
38
|
+
mock.evaluate(r, call.multi_req_view)
|
36
39
|
elsif GrpcMock.config.allow_net_connect
|
37
40
|
super
|
38
41
|
else
|
@@ -40,14 +43,15 @@ module GrpcMock
|
|
40
43
|
end
|
41
44
|
end
|
42
45
|
|
43
|
-
def server_streamer(method, request, *args)
|
46
|
+
def server_streamer(method, request, *args, metadata: {}, **kwargs)
|
44
47
|
unless GrpcMock::GrpcStubAdapter.enabled?
|
45
48
|
return super
|
46
49
|
end
|
47
50
|
|
48
51
|
mock = GrpcMock.stub_registry.response_for_request(method, request)
|
49
52
|
if mock
|
50
|
-
|
53
|
+
call = GrpcMock::MockedCall.new(metadata: metadata)
|
54
|
+
mock.evaluate(request, call.single_req_view)
|
51
55
|
elsif GrpcMock.config.allow_net_connect
|
52
56
|
super
|
53
57
|
else
|
@@ -55,7 +59,7 @@ module GrpcMock
|
|
55
59
|
end
|
56
60
|
end
|
57
61
|
|
58
|
-
def bidi_streamer(method, requests, *args)
|
62
|
+
def bidi_streamer(method, requests, *args, metadata: {}, **kwargs)
|
59
63
|
unless GrpcMock::GrpcStubAdapter.enabled?
|
60
64
|
return super
|
61
65
|
end
|
@@ -63,7 +67,7 @@ module GrpcMock
|
|
63
67
|
r = requests.to_a # FIXME: this may not work
|
64
68
|
mock = GrpcMock.stub_registry.response_for_request(method, r)
|
65
69
|
if mock
|
66
|
-
mock.evaluate(r)
|
70
|
+
mock.evaluate(r, nil) # FIXME: provide BidiCall equivalent
|
67
71
|
elsif GrpcMock.config.allow_net_connect
|
68
72
|
super
|
69
73
|
else
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc'
|
4
|
+
|
5
|
+
module GrpcMock
|
6
|
+
class MockedCall
|
7
|
+
attr_reader :deadline, :metadata
|
8
|
+
|
9
|
+
def initialize(metadata: {}, deadline: nil)
|
10
|
+
@metadata = sanitize_metadata(metadata)
|
11
|
+
@deadline = deadline
|
12
|
+
end
|
13
|
+
|
14
|
+
def multi_req_view
|
15
|
+
GRPC::ActiveCall::MultiReqView.new(self)
|
16
|
+
end
|
17
|
+
|
18
|
+
def single_req_view
|
19
|
+
GRPC::ActiveCall::SingleReqView.new(self)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def sanitize_metadata(metadata)
|
25
|
+
# Largely based on
|
26
|
+
# - grpc_rb_md_ary_fill_hash_cb https://github.com/grpc/grpc/blob/v1.29.1/src/ruby/ext/grpc/rb_call.c#L390-L465
|
27
|
+
# - grpc_rb_md_ary_convert https://github.com/grpc/grpc/blob/v1.29.1/src/ruby/ext/grpc/rb_call.c#L490-L511
|
28
|
+
# - grpc_rb_md_ary_to_h https://github.com/grpc/grpc/blob/v1.29.1/src/ruby/ext/grpc/rb_call.c#L513-L541
|
29
|
+
# See also https://github.com/grpc/grpc/blob/v1.29.1/doc/PROTOCOL-HTTP2.md for specification
|
30
|
+
|
31
|
+
raise TypeError, "got <#{metadata.class}>, want <Hash>" unless metadata.is_a?(Hash)
|
32
|
+
|
33
|
+
headers = []
|
34
|
+
metadata.each do |key, value|
|
35
|
+
raise TypeError, "bad type for key parameter" unless key.is_a?(String) || key.is_a?(Symbol)
|
36
|
+
|
37
|
+
key = key.to_s
|
38
|
+
# https://github.com/grpc/grpc/blob/v1.29.1/src/core/lib/surface/validate_metadata.cc#L61-L79
|
39
|
+
raise ArgumentError, "'#{key}' is an invalid header key" unless key.match?(/\A[a-z0-9-_.]+\z/) && key != ''
|
40
|
+
raise ArgumentError, "Header values must be of type string or array" unless value.is_a?(String) || value.is_a?(Array)
|
41
|
+
|
42
|
+
Array(value).each do |elem|
|
43
|
+
raise TypeError, "Header value must be of type string" unless elem.is_a?(String)
|
44
|
+
|
45
|
+
unless key.end_with?('-bin')
|
46
|
+
# Non-binary metadata are translated as plain HTTP2 headers, thus this requirement.
|
47
|
+
# https://github.com/grpc/grpc/blob/v1.29.1/src/core/lib/surface/validate_metadata.cc#L85-L92
|
48
|
+
raise ArgumentError, "Header value '#{elem}' has invalid characters" unless elem.match(/\A[ -~]+\z/)
|
49
|
+
|
50
|
+
# "ASCII-Value should not have leading or trailing whitespace. If it contains leading or trailing whitespace, it may be stripped."
|
51
|
+
# https://github.com/grpc/grpc/blob/v1.29.1/doc/PROTOCOL-HTTP2.md
|
52
|
+
elem = elem.strip
|
53
|
+
end
|
54
|
+
headers << [key, elem]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
metadata = {}
|
59
|
+
headers.each do |key, elem|
|
60
|
+
if metadata[key].nil?
|
61
|
+
metadata[key] = elem
|
62
|
+
elsif metadata[key].is_a?(Array)
|
63
|
+
metadata[key] << elem
|
64
|
+
else
|
65
|
+
metadata[key] = [metadata[key], elem]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
metadata
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/lib/grpc_mock/response.rb
CHANGED
@@ -16,7 +16,7 @@ module GrpcMock
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
def evaluate(_request = nil)
|
19
|
+
def evaluate(_request = nil, _call = nil)
|
20
20
|
raise @exception.dup
|
21
21
|
end
|
22
22
|
end
|
@@ -26,7 +26,7 @@ module GrpcMock
|
|
26
26
|
@value = value
|
27
27
|
end
|
28
28
|
|
29
|
-
def evaluate(_request = nil)
|
29
|
+
def evaluate(_request = nil, _call = nil)
|
30
30
|
@value.dup
|
31
31
|
end
|
32
32
|
end
|
@@ -36,8 +36,8 @@ module GrpcMock
|
|
36
36
|
@block = block
|
37
37
|
end
|
38
38
|
|
39
|
-
def evaluate(request)
|
40
|
-
@block.call(request)
|
39
|
+
def evaluate(request, call = nil)
|
40
|
+
@block.call(request, call)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
data/lib/grpc_mock/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grpc_mock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuta Iwama
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-06-
|
11
|
+
date: 2020-06-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grpc
|
@@ -140,6 +140,7 @@ files:
|
|
140
140
|
- lib/grpc_mock/grpc_stub_adapter.rb
|
141
141
|
- lib/grpc_mock/matchers/hash_argument_matcher.rb
|
142
142
|
- lib/grpc_mock/matchers/request_including_matcher.rb
|
143
|
+
- lib/grpc_mock/mocked_call.rb
|
143
144
|
- lib/grpc_mock/request_pattern.rb
|
144
145
|
- lib/grpc_mock/request_stub.rb
|
145
146
|
- lib/grpc_mock/response.rb
|