grpc_mock 0.2.1 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: dab269f850e0b8b76e59bf38cda65711c45777e9
4
- data.tar.gz: b4940189dfaf12d0709fe82bc8f35683b2d6f079
2
+ SHA256:
3
+ metadata.gz: 128da2bce960f7416a796a0b96bd0866a79bf30decc85e064d5c951b2f93a939
4
+ data.tar.gz: 7086f3897090c64f0d97aaa0cfbda4138787921929be89495f820dbeb866e533
5
5
  SHA512:
6
- metadata.gz: 9306c34913f4f82cad53287296c5d41add574e83976afb2292095f755146638cc100bb9256ea51ac514e1b98c26de7fb8c93d7c4b8c6070bb8dae6e2634b20ff
7
- data.tar.gz: 10113af184c522fc9cf9f29dd4198726a6eec7198d7b0182a0a156b7fd5819cb06f8991cf3bff0807a3f8bc950f822c0f52492a5882b7221ce533301cd6b8982
6
+ metadata.gz: 23511c5cacc91af0c26eafceee5f0a986605cb44feccf169608a35ff8f353a6f0933bc8ea0af22fbb9f04f190b5791ce6b76825040cb49e9655815bdb0f7b1a1
7
+ data.tar.gz: b069f76b458a0d95c7006a9f079e2c6bcd7c600ef563355716aa15f9208f79e3262a49a3efea6e7bc09169e5e2a8d60945f6bd772683896f44323bf3fbe7f5f4
@@ -1,6 +1,7 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
+ - 2.6.3
4
5
  - 2.5.3
5
6
  - 2.4.5
6
7
  before_install: gem install bundler -v 1.16.1
data/README.md CHANGED
@@ -30,35 +30,69 @@ require 'grpc_mock/rspec'
30
30
 
31
31
  See definition of protocol buffers and gRPC generated code in [spec/exmaples/hello](https://github.com/ganmacs/grpc_mock/tree/master/spec/examples/hello)
32
32
 
33
- ##### Stubbed request based on path and with the default response
33
+ ### Stubbed request based on path and with the default response
34
34
 
35
35
  ```ruby
36
36
  GrpcMock.stub_request("/hello.hello/Hello").to_return(Hello::HelloResponse.new(msg: 'test'))
37
37
 
38
38
  client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
39
- client = client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Hello::HelloResponse.new(msg: 'test')
39
+ client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Hello::HelloResponse.new(msg: 'test')
40
40
  ```
41
41
 
42
- ##### Stubbing requests based on path and request
42
+ ### Stubbing requests based on path and request
43
43
 
44
44
  ```ruby
45
45
  GrpcMock.stub_request("/hello.hello/Hello").with(Hello::HelloRequest.new(msg: 'hi')).to_return(Hello::HelloResponse.new(msg: 'test'))
46
46
 
47
47
  client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
48
- client = client.hello(Hello::HelloRequest.new(msg: 'hello')) # => send a request to server
49
- client = client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Hello::HelloResponse.new(msg: 'test') (without any requests to server)
48
+ client.hello(Hello::HelloRequest.new(msg: 'hello')) # => send a request to server
49
+ client client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Hello::HelloResponse.new(msg: 'test') (without any requests to server)
50
50
  ```
51
51
 
52
- ##### Real requests to network can be allowed or disabled
52
+ ### Responding dynamically to the stubbed requests
53
+
54
+ ```ruby
55
+ GrpcMock.stub_request("/hello.hello/Hello").to_return do |req|
56
+ Hello::HelloResponse.new(msg: "#{req.msg} too")
57
+ end
58
+
59
+ client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
60
+ client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Hello::HelloResponse.new(msg: 'hi too')
61
+ ```
62
+
63
+ ### Real requests to network can be allowed or disabled
53
64
 
54
65
  ```ruby
55
66
  client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
56
67
 
57
68
  GrpcMock.disable_net_connect!
58
- client = client.hello(Hello::HelloRequest.new(msg: 'hello')) # => Raise NetConnectNotAllowedError error
69
+ client.hello(Hello::HelloRequest.new(msg: 'hello')) # => Raise NetConnectNotAllowedError error
59
70
 
60
71
  GrpcMock.allow_net_connect!
61
- client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure) # => send a request to server
72
+ Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure) # => send a request to server
73
+ ```
74
+
75
+ ### Raising errors
76
+
77
+ **Exception declared by class**
78
+
79
+ ```ruby
80
+ GrpcMock.stub_request("/hello.hello/Hello").to_raise(StandardError)
81
+
82
+ client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
83
+ client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Raise StandardError
84
+ ```
85
+
86
+ **or by exception instance**
87
+
88
+ ```ruby
89
+ GrpcMock.stub_request("/hello.hello/Hello").to_raise(StandardError.new("Some error"))
90
+ ```
91
+
92
+ **or by string**
93
+
94
+ ```ruby
95
+ GrpcMock.stub_request("/hello.hello/Hello").to_raise("Some error")
62
96
  ```
63
97
 
64
98
  ## Contributing
@@ -22,12 +22,12 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ['lib']
24
24
 
25
- spec.add_dependency 'grpc', '>= 1.12.0', '< 1.18.0'
25
+ spec.add_dependency 'grpc', '>= 1.12.0', '< 2'
26
26
 
27
- spec.add_development_dependency 'bundler', '~> 1.16'
27
+ spec.add_development_dependency 'bundler'
28
28
  spec.add_development_dependency 'grpc-tools'
29
29
  spec.add_development_dependency 'pry-byebug'
30
- spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rake', '>= 12.3.3'
31
31
  spec.add_development_dependency 'rspec', '~> 3.0'
32
32
  spec.add_development_dependency 'rubocop'
33
33
  end
@@ -3,7 +3,7 @@
3
3
  module GrpcMock
4
4
  class NetConnectNotAllowedError < StandardError
5
5
  def initialize(sigunature)
6
- super("Real GRPC connections are disabled. #{sigunature} is requested")
6
+ super("Real gRPC connections are disabled. #{sigunature} is requested")
7
7
  end
8
8
  end
9
9
 
@@ -15,7 +15,7 @@ module GrpcMock
15
15
 
16
16
  mock = GrpcMock.stub_registry.response_for_request(method, request)
17
17
  if mock
18
- mock
18
+ mock.evaluate(request)
19
19
  elsif GrpcMock.config.allow_net_connect
20
20
  super
21
21
  else
@@ -32,7 +32,7 @@ module GrpcMock
32
32
  r = requests.to_a # FIXME: this may not work
33
33
  mock = GrpcMock.stub_registry.response_for_request(method, r)
34
34
  if mock
35
- mock
35
+ mock.evaluate(r)
36
36
  elsif GrpcMock.config.allow_net_connect
37
37
  super
38
38
  else
@@ -47,7 +47,7 @@ module GrpcMock
47
47
 
48
48
  mock = GrpcMock.stub_registry.response_for_request(method, request)
49
49
  if mock
50
- mock
50
+ mock.evaluate(request)
51
51
  elsif GrpcMock.config.allow_net_connect
52
52
  super
53
53
  else
@@ -63,7 +63,7 @@ module GrpcMock
63
63
  r = requests.to_a # FIXME: this may not work
64
64
  mock = GrpcMock.stub_registry.response_for_request(method, r)
65
65
  if mock
66
- mock
66
+ mock.evaluate(r)
67
67
  elsif GrpcMock.config.allow_net_connect
68
68
  super
69
69
  else
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'grpc_mock/request_pattern'
4
- require 'grpc_mock/resopnse_sequence'
4
+ require 'grpc_mock/response'
5
+ require 'grpc_mock/response_sequence'
5
6
  require 'grpc_mock/errors'
6
7
 
7
8
  module GrpcMock
@@ -17,8 +18,16 @@ module GrpcMock
17
18
  self
18
19
  end
19
20
 
20
- def to_return(*resp)
21
- @response_sequence << GrpcMock::ResponsesSequence.new(resp)
21
+ def to_return(*values, &block)
22
+ responses = [*values].flatten.map { |v| Response::Value.new(v) }
23
+ responses << Response::BlockValue.new(block) if block
24
+ @response_sequence << GrpcMock::ResponsesSequence.new(responses)
25
+ self
26
+ end
27
+
28
+ def to_raise(*exceptions)
29
+ responses = [*exceptions].flatten.map { |e| Response::ExceptionValue.new(e) }
30
+ @response_sequence << GrpcMock::ResponsesSequence.new(responses)
22
31
  self
23
32
  end
24
33
 
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GrpcMock
4
+ module Response
5
+ class ExceptionValue
6
+ def initialize(exception)
7
+ @exception = case exception
8
+ when String
9
+ StandardError.new(exception)
10
+ when Class
11
+ exception.new('Exception from GrpcMock')
12
+ when Exception
13
+ exception
14
+ else
15
+ raise ArgumentError.new(message: "Invalid exception class: #{exception.class}")
16
+ end
17
+ end
18
+
19
+ def evaluate(_request = nil)
20
+ raise @exception.dup
21
+ end
22
+ end
23
+
24
+ class Value
25
+ def initialize(value)
26
+ @value = value
27
+ end
28
+
29
+ def evaluate(_request = nil)
30
+ @value.dup
31
+ end
32
+ end
33
+
34
+ class BlockValue
35
+ def initialize(block)
36
+ @block = block
37
+ end
38
+
39
+ def evaluate(request)
40
+ @block.call(request)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -24,7 +24,7 @@ module GrpcMock
24
24
  end
25
25
 
26
26
  if rstub
27
- rstub.response.dup
27
+ rstub.response
28
28
  end
29
29
  end
30
30
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GrpcMock
4
- VERSION = '0.2.1'
4
+ VERSION = '0.4.1'
5
5
  end
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.2.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuta Iwama
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-07 00:00:00.000000000 Z
11
+ date: 2020-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grpc
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: 1.12.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: 1.18.0
22
+ version: '2'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,21 +29,21 @@ dependencies:
29
29
  version: 1.12.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: 1.18.0
32
+ version: '2'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: bundler
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - "~>"
37
+ - - ">="
38
38
  - !ruby/object:Gem::Version
39
- version: '1.16'
39
+ version: '0'
40
40
  type: :development
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
- - - "~>"
44
+ - - ">="
45
45
  - !ruby/object:Gem::Version
46
- version: '1.16'
46
+ version: '0'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: grpc-tools
49
49
  requirement: !ruby/object:Gem::Requirement
@@ -76,16 +76,16 @@ dependencies:
76
76
  name: rake
77
77
  requirement: !ruby/object:Gem::Requirement
78
78
  requirements:
79
- - - "~>"
79
+ - - ">="
80
80
  - !ruby/object:Gem::Version
81
- version: '10.0'
81
+ version: 12.3.3
82
82
  type: :development
83
83
  prerelease: false
84
84
  version_requirements: !ruby/object:Gem::Requirement
85
85
  requirements:
86
- - - "~>"
86
+ - - ">="
87
87
  - !ruby/object:Gem::Version
88
- version: '10.0'
88
+ version: 12.3.3
89
89
  - !ruby/object:Gem::Dependency
90
90
  name: rspec
91
91
  requirement: !ruby/object:Gem::Requirement
@@ -124,7 +124,6 @@ files:
124
124
  - ".gitignore"
125
125
  - ".rspec"
126
126
  - ".rubocop.yml"
127
- - ".ruby-version"
128
127
  - ".travis.yml"
129
128
  - Gemfile
130
129
  - LICENSE.txt
@@ -143,7 +142,8 @@ files:
143
142
  - lib/grpc_mock/matchers/request_including_matcher.rb
144
143
  - lib/grpc_mock/request_pattern.rb
145
144
  - lib/grpc_mock/request_stub.rb
146
- - lib/grpc_mock/resopnse_sequence.rb
145
+ - lib/grpc_mock/response.rb
146
+ - lib/grpc_mock/response_sequence.rb
147
147
  - lib/grpc_mock/rspec.rb
148
148
  - lib/grpc_mock/stub_registry.rb
149
149
  - lib/grpc_mock/version.rb
@@ -166,8 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
166
  - !ruby/object:Gem::Version
167
167
  version: '0'
168
168
  requirements: []
169
- rubyforge_project:
170
- rubygems_version: 2.6.14.3
169
+ rubygems_version: 3.0.3
171
170
  signing_key:
172
171
  specification_version: 4
173
172
  summary: Library for stubbing grpc in Ruby
@@ -1 +0,0 @@
1
- 2.4