gruffish 0.5.0.pre1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/workflows/rspec.yml +34 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.rubocop.yml +92 -0
- data/.travis.yml +7 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +94 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/release +16 -0
- data/bin/setup +8 -0
- data/gruffish.gemspec +33 -0
- data/lib/grpc_mock.rb +41 -0
- data/lib/grpc_mock/adapter.rb +21 -0
- data/lib/grpc_mock/api.rb +26 -0
- data/lib/grpc_mock/configuration.rb +11 -0
- data/lib/grpc_mock/errors.rb +15 -0
- data/lib/grpc_mock/grpc_stub_adapter.rb +97 -0
- data/lib/grpc_mock/matchers/hash_argument_matcher.rb +43 -0
- data/lib/grpc_mock/matchers/request_including_matcher.rb +39 -0
- data/lib/grpc_mock/operation_stub.rb +30 -0
- data/lib/grpc_mock/request_pattern.rb +25 -0
- data/lib/grpc_mock/request_stub.rb +54 -0
- data/lib/grpc_mock/response.rb +34 -0
- data/lib/grpc_mock/response_sequence.rb +39 -0
- data/lib/grpc_mock/rspec.rb +17 -0
- data/lib/grpc_mock/stub_registry.rb +31 -0
- data/lib/grpc_mock/version.rb +5 -0
- metadata +178 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6e778d20a1b478c8a447ff42145fc3f6c3ca23317dcc37573bc49db62b589f42
|
4
|
+
data.tar.gz: 53f384c585b03d95842f2c82533eed1c83b419b7a3579f6978cad0ea0ee8425a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cd17d8e80f93ba41abdcff9ede2f243c196d2755cb0580f53489b618d30946c66a4418774355f724accd0f9db5a4c03f5f2f8e763da205493aca89ae7171c66b
|
7
|
+
data.tar.gz: 443b9991ba0e953f9850fa68a2667b2678809bb0ee9281b24aea7767d63937465eba687560546516aa638c668a76e8e1dfbfe7d8e72c594c1134617cde30c511
|
@@ -0,0 +1,34 @@
|
|
1
|
+
name: RSpec
|
2
|
+
|
3
|
+
on: push
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
strategy:
|
9
|
+
matrix:
|
10
|
+
ruby:
|
11
|
+
- "2.6.x"
|
12
|
+
|
13
|
+
steps:
|
14
|
+
- uses: actions/checkout@v1
|
15
|
+
- uses: actions/cache@v1
|
16
|
+
with:
|
17
|
+
path: vendor/bundle
|
18
|
+
key: ${{ runner.os }}-gems-${{ matrix.ruby }}-${{ hashFiles('**/Gemfile.lock') }}
|
19
|
+
restore-keys: |
|
20
|
+
${{ runner.os }}-gems-${{ matrix.ruby }}
|
21
|
+
- name: Set up Ruby ${{ matrix.ruby }}
|
22
|
+
uses: actions/setup-ruby@v1
|
23
|
+
with:
|
24
|
+
ruby-version: ${{ matrix.ruby }}
|
25
|
+
- name: Set up dependencies
|
26
|
+
env:
|
27
|
+
BUNDLE_GEM__FURY__IO: ${{ secrets.GEMFURY_DEPLOY_TOKEN }}
|
28
|
+
run: |
|
29
|
+
gem install bundler
|
30
|
+
bundle config path vendor/bundle
|
31
|
+
bundle install --jobs 4 --retry 3
|
32
|
+
- name: Run RSpec
|
33
|
+
run: |
|
34
|
+
bundle exec rspec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- 'spec/examples/**/*'
|
4
|
+
- '**/**/*_pb.rb' # auto-generated
|
5
|
+
DisplayCopNames: true
|
6
|
+
TargetRubyVersion: 2.5
|
7
|
+
|
8
|
+
Style/AndOr:
|
9
|
+
EnforcedStyle: conditionals
|
10
|
+
|
11
|
+
Style/AsciiComments:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Style/Documentation:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Style/DoubleNegation:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Style/EmptyElse:
|
21
|
+
EnforcedStyle: empty
|
22
|
+
|
23
|
+
Style/FormatString:
|
24
|
+
EnforcedStyle: percent
|
25
|
+
|
26
|
+
Style/IfUnlessModifier:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Style/TrailingCommaInHashLiteral:
|
30
|
+
EnforcedStyleForMultiline: comma
|
31
|
+
|
32
|
+
Style/TrailingCommaInArrayLiteral:
|
33
|
+
EnforcedStyleForMultiline: comma
|
34
|
+
|
35
|
+
Style/TrailingCommaInArguments:
|
36
|
+
EnforcedStyleForMultiline: comma
|
37
|
+
|
38
|
+
Style/SafeNavigation:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
Style/WhileUntilModifier:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
Naming/PredicateName:
|
45
|
+
NamePrefixBlacklist:
|
46
|
+
- "is_"
|
47
|
+
- "have_"
|
48
|
+
NamePrefix:
|
49
|
+
- "is_"
|
50
|
+
- "have_"
|
51
|
+
|
52
|
+
Style/SignalException:
|
53
|
+
EnforcedStyle: only_raise
|
54
|
+
|
55
|
+
Style/SingleLineBlockParams:
|
56
|
+
Enabled: false
|
57
|
+
|
58
|
+
Style/NumericLiterals:
|
59
|
+
Enabled: false
|
60
|
+
|
61
|
+
Style/GuardClause:
|
62
|
+
Enabled: false
|
63
|
+
|
64
|
+
Style/NumericPredicate:
|
65
|
+
Enabled: false
|
66
|
+
|
67
|
+
Metrics/ParameterLists:
|
68
|
+
CountKeywordArgs: false
|
69
|
+
|
70
|
+
Lint/UnderscorePrefixedVariableName:
|
71
|
+
Enabled: false
|
72
|
+
|
73
|
+
Metrics/AbcSize:
|
74
|
+
Max: 50
|
75
|
+
|
76
|
+
Metrics/CyclomaticComplexity:
|
77
|
+
Max: 10
|
78
|
+
|
79
|
+
Metrics/LineLength:
|
80
|
+
Max: 160
|
81
|
+
|
82
|
+
Metrics/MethodLength:
|
83
|
+
Max: 40
|
84
|
+
|
85
|
+
Metrics/PerceivedComplexity:
|
86
|
+
Max: 20
|
87
|
+
|
88
|
+
Metrics/ClassLength:
|
89
|
+
Max: 200
|
90
|
+
|
91
|
+
Metrics/BlockLength:
|
92
|
+
Max: 40
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Yuta Iwama
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# GrpcMock [](https://travis-ci.org/ganmacs/grpc_mock)
|
2
|
+
|
3
|
+
Library for stubbing grpc in Ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'grpc_mock'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install grpc_mock
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
If you use [RSpec](https://github.com/rspec/rspec), add the following code to spec/spec_helper.rb:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'grpc_mock/rspec'
|
27
|
+
```
|
28
|
+
|
29
|
+
## Examples
|
30
|
+
|
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
|
+
|
33
|
+
### Stubbed request based on path and with the default response
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
GrpcMock.stub_request("/hello.hello/Hello").to_return(Hello::HelloResponse.new(msg: 'test'))
|
37
|
+
|
38
|
+
client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
|
39
|
+
client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Hello::HelloResponse.new(msg: 'test')
|
40
|
+
```
|
41
|
+
|
42
|
+
### Stubbing requests based on path and request
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
GrpcMock.stub_request("/hello.hello/Hello").with(Hello::HelloRequest.new(msg: 'hi')).to_return(Hello::HelloResponse.new(msg: 'test'))
|
46
|
+
|
47
|
+
client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
|
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
|
+
```
|
51
|
+
|
52
|
+
### Real requests to network can be allowed or disabled
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
|
56
|
+
|
57
|
+
GrpcMock.disable_net_connect!
|
58
|
+
client.hello(Hello::HelloRequest.new(msg: 'hello')) # => Raise NetConnectNotAllowedError error
|
59
|
+
|
60
|
+
GrpcMock.allow_net_connect!
|
61
|
+
Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure) # => send a request to server
|
62
|
+
```
|
63
|
+
|
64
|
+
### Raising errors
|
65
|
+
|
66
|
+
**Exception declared by class**
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
GrpcMock.stub_request("/hello.hello/Hello").to_raise(StandardError)
|
70
|
+
|
71
|
+
client = Hello::Hello::Stub.new('localhost:8000', :this_channel_is_insecure)
|
72
|
+
client.hello(Hello::HelloRequest.new(msg: 'hi')) # => Raise StandardError
|
73
|
+
```
|
74
|
+
|
75
|
+
**or by exception instance**
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
GrpcMock.stub_request("/hello.hello/Hello").to_raise(StandardError.new("Some error"))
|
79
|
+
```
|
80
|
+
|
81
|
+
**or by string**
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
GrpcMock.stub_request("/hello.hello/Hello").to_raise("Some error")
|
85
|
+
```
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ganmacs/grpc_mock. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
90
|
+
|
91
|
+
## License
|
92
|
+
|
93
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
94
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'grpc_mock'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/release
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require_relative "../lib/grpc_mock/version"
|
4
|
+
|
5
|
+
current_branch = `git rev-parse --abbrev-ref HEAD`.strip
|
6
|
+
|
7
|
+
unless current_branch == "master" || GrpcMock::VERSION =~ %r{\.pre\d*\z}
|
8
|
+
puts "Can only release from the master branch 😿"
|
9
|
+
exit 1
|
10
|
+
end
|
11
|
+
|
12
|
+
system "git tag v#{GrpcMock::VERSION}"
|
13
|
+
|
14
|
+
system "git push --tag"
|
15
|
+
|
16
|
+
system "git push fury #{current_branch}:master"
|
data/bin/setup
ADDED
data/gruffish.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'grpc_mock/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'gruffish'
|
9
|
+
spec.version = GrpcMock::VERSION
|
10
|
+
spec.authors = ['Yuta Iwama', 'Allen Rettberg']
|
11
|
+
spec.email = ['ganmacs@gmail.com', 'allen.rettberg@freshly.com']
|
12
|
+
|
13
|
+
spec.summary = 'Stubs your gRPCs with some gruffy magic'
|
14
|
+
spec.description = 'Stubs your gRPCs with some gruffy magic'
|
15
|
+
spec.homepage = 'https://github.com/Freshly/gruffish'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_dependency 'grpc', '>= 1.12.0', '< 2'
|
26
|
+
|
27
|
+
spec.add_development_dependency 'bundler'
|
28
|
+
spec.add_development_dependency 'grpc-tools'
|
29
|
+
spec.add_development_dependency 'pry-byebug'
|
30
|
+
spec.add_development_dependency 'rake', '>= 12.3.3'
|
31
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
32
|
+
spec.add_development_dependency 'rubocop'
|
33
|
+
end
|
data/lib/grpc_mock.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_mock/api'
|
4
|
+
require 'grpc_mock/version'
|
5
|
+
require 'grpc_mock/configuration'
|
6
|
+
require 'grpc_mock/adapter'
|
7
|
+
require 'grpc_mock/stub_registry'
|
8
|
+
|
9
|
+
module GrpcMock
|
10
|
+
extend GrpcMock::Api
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def enable!
|
14
|
+
adapter.enable!
|
15
|
+
end
|
16
|
+
|
17
|
+
def disable!
|
18
|
+
adapter.disable!
|
19
|
+
end
|
20
|
+
|
21
|
+
def reset!
|
22
|
+
GrpcMock.stub_registry.reset!
|
23
|
+
end
|
24
|
+
|
25
|
+
def stub_registry
|
26
|
+
@stub_registry ||= GrpcMock::StubRegistry.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def adapter
|
30
|
+
@adapter ||= Adapter.new
|
31
|
+
end
|
32
|
+
|
33
|
+
def config
|
34
|
+
@config ||= Configuration.new
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Hook into GRPC::ClientStub
|
39
|
+
# https://github.com/grpc/grpc/blob/bec3b5ada2c5e5d782dff0b7b5018df646b65cb0/src/ruby/lib/grpc/generic/service.rb#L150-L186
|
40
|
+
GRPC::ClientStub.prepend GrpcStubAdapter::MockStub
|
41
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_mock/grpc_stub_adapter'
|
4
|
+
|
5
|
+
module GrpcMock
|
6
|
+
class Adapter
|
7
|
+
def enable!
|
8
|
+
adapter.enable!
|
9
|
+
end
|
10
|
+
|
11
|
+
def disable!
|
12
|
+
adapter.disable!
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def adapter
|
18
|
+
@adapter ||= GrpcStubAdapter.new
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_mock/request_stub'
|
4
|
+
require 'grpc_mock/matchers/request_including_matcher'
|
5
|
+
|
6
|
+
module GrpcMock
|
7
|
+
module Api
|
8
|
+
# @param path [String]
|
9
|
+
def stub_request(path)
|
10
|
+
GrpcMock.stub_registry.register_request_stub(GrpcMock::RequestStub.new(path))
|
11
|
+
end
|
12
|
+
|
13
|
+
# @param values [Hash]
|
14
|
+
def request_including(values)
|
15
|
+
GrpcMock::Matchers::RequestIncludingMatcher.new(values)
|
16
|
+
end
|
17
|
+
|
18
|
+
def disable_net_connect!
|
19
|
+
GrpcMock.config.allow_net_connect = false
|
20
|
+
end
|
21
|
+
|
22
|
+
def allow_net_connect!
|
23
|
+
GrpcMock.config.allow_net_connect = true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrpcMock
|
4
|
+
class NetConnectNotAllowedError < StandardError
|
5
|
+
def initialize(sigunature)
|
6
|
+
super("Real gRPC connections are disabled. #{sigunature} is requested")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class NoResponseError < StandardError
|
11
|
+
def initialize(msg)
|
12
|
+
super("There is no response: #{msg}")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc'
|
4
|
+
require 'grpc_mock/errors'
|
5
|
+
require 'grpc_mock/operation_stub'
|
6
|
+
|
7
|
+
module GrpcMock
|
8
|
+
class GrpcStubAdapter
|
9
|
+
module MockStub
|
10
|
+
def request_response(method, request, *args, **opts)
|
11
|
+
return super unless GrpcMock::GrpcStubAdapter.enabled?
|
12
|
+
|
13
|
+
mock = GrpcMock.stub_registry.response_for_request(method, request)
|
14
|
+
|
15
|
+
if mock
|
16
|
+
if opts[:return_op]
|
17
|
+
OperationStub.new(metadata: opts[:metadata]) { mock.evaluate }
|
18
|
+
else
|
19
|
+
mock.evaluate
|
20
|
+
end
|
21
|
+
elsif GrpcMock.config.allow_net_connect
|
22
|
+
super
|
23
|
+
else
|
24
|
+
raise NetConnectNotAllowedError, method
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# TODO
|
29
|
+
def client_streamer(method, requests, *args)
|
30
|
+
unless GrpcMock::GrpcStubAdapter.enabled?
|
31
|
+
return super
|
32
|
+
end
|
33
|
+
|
34
|
+
r = requests.to_a # FIXME: this may not work
|
35
|
+
mock = GrpcMock.stub_registry.response_for_request(method, r)
|
36
|
+
if mock
|
37
|
+
mock.evaluate
|
38
|
+
elsif GrpcMock.config.allow_net_connect
|
39
|
+
super
|
40
|
+
else
|
41
|
+
raise NetConnectNotAllowedError, method
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def server_streamer(method, request, *args)
|
46
|
+
unless GrpcMock::GrpcStubAdapter.enabled?
|
47
|
+
return super
|
48
|
+
end
|
49
|
+
|
50
|
+
mock = GrpcMock.stub_registry.response_for_request(method, request)
|
51
|
+
if mock
|
52
|
+
mock.evaluate
|
53
|
+
elsif GrpcMock.config.allow_net_connect
|
54
|
+
super
|
55
|
+
else
|
56
|
+
raise NetConnectNotAllowedError, method
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def bidi_streamer(method, requests, *args)
|
61
|
+
unless GrpcMock::GrpcStubAdapter.enabled?
|
62
|
+
return super
|
63
|
+
end
|
64
|
+
|
65
|
+
r = requests.to_a # FIXME: this may not work
|
66
|
+
mock = GrpcMock.stub_registry.response_for_request(method, r)
|
67
|
+
if mock
|
68
|
+
mock.evaluate
|
69
|
+
elsif GrpcMock.config.allow_net_connect
|
70
|
+
super
|
71
|
+
else
|
72
|
+
raise NetConnectNotAllowedError, method
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.disable!
|
78
|
+
@enabled = false
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.enable!
|
82
|
+
@enabled = true
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.enabled?
|
86
|
+
@enabled
|
87
|
+
end
|
88
|
+
|
89
|
+
def enable!
|
90
|
+
GrpcMock::GrpcStubAdapter.enable!
|
91
|
+
end
|
92
|
+
|
93
|
+
def disable!
|
94
|
+
GrpcMock::GrpcStubAdapter.disable!
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrpcMock
|
4
|
+
module Matchers
|
5
|
+
# Base class for Hash matchers
|
6
|
+
# https://github.com/rspec/rspec-mocks/blob/master/lib/rspec/mocks/argument_matchers.rb
|
7
|
+
class HashArgumentMatcher
|
8
|
+
def self.stringify_keys!(arg, options = {})
|
9
|
+
case arg
|
10
|
+
when Array
|
11
|
+
arg.map do |elem|
|
12
|
+
options[:deep] ? stringify_keys!(elem, options) : elem
|
13
|
+
end
|
14
|
+
when Hash
|
15
|
+
Hash[
|
16
|
+
*arg.map do |key, value|
|
17
|
+
k = key.is_a?(Symbol) ? key.to_s : key
|
18
|
+
v = (options[:deep] ? stringify_keys!(value, options) : value)
|
19
|
+
[k, v]
|
20
|
+
end.inject([]) { |r, x| r + x }]
|
21
|
+
else
|
22
|
+
arg
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(expected)
|
27
|
+
@expected = Hash[
|
28
|
+
GrpcMock::Matchers::HashArgumentMatcher.stringify_keys!(expected, deep: true).sort,
|
29
|
+
]
|
30
|
+
end
|
31
|
+
|
32
|
+
def ==(_actual, &block)
|
33
|
+
@expected.all?(&block)
|
34
|
+
rescue NoMethodError
|
35
|
+
false
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.from_rspec_matcher(matcher)
|
39
|
+
new(matcher.instance_variable_get(:@expected))
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_mock/matchers/hash_argument_matcher'
|
4
|
+
|
5
|
+
module GrpcMock
|
6
|
+
module Matchers
|
7
|
+
class RequestIncludingMatcher < HashArgumentMatcher
|
8
|
+
def ==(actual)
|
9
|
+
if actual.respond_to?(:to_h)
|
10
|
+
actual = actual.to_h
|
11
|
+
end
|
12
|
+
|
13
|
+
actual = Hash[GrpcMock::Matchers::HashArgumentMatcher.stringify_keys!(actual, deep: true)]
|
14
|
+
super { |key, value| inner_including(value, key, actual) }
|
15
|
+
rescue NoMethodError
|
16
|
+
false
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def inner_including(expect, key, actual)
|
22
|
+
if actual.key?(key)
|
23
|
+
actual_value = actual[key]
|
24
|
+
if expect.is_a?(Hash)
|
25
|
+
RequestIncludingMatcher.new(expect) == actual_value
|
26
|
+
else
|
27
|
+
expect === actual_value
|
28
|
+
end
|
29
|
+
else
|
30
|
+
false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def inspect
|
35
|
+
"reqeust_including(#{@expected.inspect})"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrpcMock
|
4
|
+
class OperationStub
|
5
|
+
attr_reader :response_proc, :metadata, :trailing_metadata, :deadline
|
6
|
+
|
7
|
+
# @param metadata [Hash] Any metadata passed into the GRPC request
|
8
|
+
# @param deadline [Time] The deadline set on the GRPC request
|
9
|
+
# @yieldreturn [*] The stubbed value or error expected to be returned from the request
|
10
|
+
def initialize(metadata: nil, deadline: nil, &response_proc)
|
11
|
+
@response_proc = response_proc
|
12
|
+
@metadata = metadata
|
13
|
+
@deadline = deadline
|
14
|
+
|
15
|
+
# TODO: support stubbing
|
16
|
+
@trailing_metadata = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
# Calls the block given upon instantiation and returns the result
|
20
|
+
def response
|
21
|
+
response_proc.call
|
22
|
+
end
|
23
|
+
alias_method :execute, :response
|
24
|
+
|
25
|
+
# TODO: support stubbing
|
26
|
+
def cancelled?
|
27
|
+
false
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrpcMock
|
4
|
+
class RequestPattern
|
5
|
+
# @param path [String]
|
6
|
+
def initialize(path)
|
7
|
+
@path = path
|
8
|
+
@block = nil
|
9
|
+
@request = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def with(request = nil, &block)
|
13
|
+
if request.nil? && !block_given?
|
14
|
+
raise ArgumentError, '#with method invoked with no arguments. Either options request or block must be specified.'
|
15
|
+
end
|
16
|
+
|
17
|
+
@request = request
|
18
|
+
@block = block
|
19
|
+
end
|
20
|
+
|
21
|
+
def match?(path, request)
|
22
|
+
@path == path && (@request.nil? || @request == request) && (@block.nil? || @block.call(path))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_mock/request_pattern'
|
4
|
+
require 'grpc_mock/response'
|
5
|
+
require 'grpc_mock/response_sequence'
|
6
|
+
require 'grpc_mock/errors'
|
7
|
+
|
8
|
+
module GrpcMock
|
9
|
+
class RequestStub
|
10
|
+
# @param path [String] gRPC path like /${service_name}/${method_name}
|
11
|
+
def initialize(path)
|
12
|
+
@request_pattern = RequestPattern.new(path)
|
13
|
+
@response_sequence = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def with(request = nil, &block)
|
17
|
+
@request_pattern.with(request, &block)
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_return(*values)
|
22
|
+
responses = [*values].flatten.map { |v| Response::Value.new(v) }
|
23
|
+
@response_sequence << GrpcMock::ResponsesSequence.new(responses)
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_raise(*exceptions)
|
28
|
+
responses = [*exceptions].flatten.map { |e| Response::ExceptionValue.new(e) }
|
29
|
+
@response_sequence << GrpcMock::ResponsesSequence.new(responses)
|
30
|
+
self
|
31
|
+
end
|
32
|
+
|
33
|
+
def response
|
34
|
+
if @response_sequence.empty?
|
35
|
+
raise GrpcMock::NoResponseError, 'Must be set some values by using #GrpMock::RequestStub#to_run'
|
36
|
+
elsif @response_sequence.size == 1
|
37
|
+
@response_sequence.first.next
|
38
|
+
else
|
39
|
+
if @response_sequence.first.end?
|
40
|
+
@response_sequence.shift
|
41
|
+
end
|
42
|
+
|
43
|
+
@response_sequence.first.next
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# @param path [String]
|
48
|
+
# @param request [Object]
|
49
|
+
# @return [Bool]
|
50
|
+
def match?(path, request)
|
51
|
+
@request_pattern.match?(path, request)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,34 @@
|
|
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
|
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
|
30
|
+
@value.dup
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrpcMock
|
4
|
+
class ResponsesSequence
|
5
|
+
attr_accessor :repeat
|
6
|
+
|
7
|
+
def initialize(responses)
|
8
|
+
@repeat = 1
|
9
|
+
@responses = responses
|
10
|
+
@current = 0
|
11
|
+
@last = @responses.length - 1
|
12
|
+
end
|
13
|
+
|
14
|
+
def end?
|
15
|
+
@repeat == 0
|
16
|
+
end
|
17
|
+
|
18
|
+
def next
|
19
|
+
if @repeat > 0
|
20
|
+
response = @responses[@current]
|
21
|
+
next_pos
|
22
|
+
response
|
23
|
+
else
|
24
|
+
@responses.last
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def next_pos
|
31
|
+
if @last == @current
|
32
|
+
@current = 0
|
33
|
+
@repeat -= 1
|
34
|
+
else
|
35
|
+
@current += 1
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'grpc_mock'
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.before(:suite) do
|
7
|
+
GrpcMock.enable!
|
8
|
+
end
|
9
|
+
|
10
|
+
config.after(:suite) do
|
11
|
+
GrpcMock.disable!
|
12
|
+
end
|
13
|
+
|
14
|
+
config.after(:each) do
|
15
|
+
GrpcMock.reset!
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrpcMock
|
4
|
+
class StubRegistry
|
5
|
+
def initialize
|
6
|
+
@request_stubs = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def reset!
|
10
|
+
@request_stubs = []
|
11
|
+
end
|
12
|
+
|
13
|
+
# @param stub [GrpcMock::RequestStub]
|
14
|
+
def register_request_stub(stub)
|
15
|
+
@request_stubs.unshift(stub)
|
16
|
+
stub
|
17
|
+
end
|
18
|
+
|
19
|
+
# @param path [String]
|
20
|
+
# @param request [Object]
|
21
|
+
def response_for_request(path, request)
|
22
|
+
rstub = @request_stubs.find do |stub|
|
23
|
+
stub.match?(path, request)
|
24
|
+
end
|
25
|
+
|
26
|
+
if rstub
|
27
|
+
rstub.response
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gruffish
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0.pre1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuta Iwama
|
8
|
+
- Allen Rettberg
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2020-04-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: grpc
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 1.12.0
|
21
|
+
- - "<"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '2'
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: 1.12.0
|
31
|
+
- - "<"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2'
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: bundler
|
36
|
+
requirement: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
type: :development
|
42
|
+
prerelease: false
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: grpc-tools
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: pry-byebug
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
type: :development
|
70
|
+
prerelease: false
|
71
|
+
version_requirements: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
name: rake
|
78
|
+
requirement: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 12.3.3
|
83
|
+
type: :development
|
84
|
+
prerelease: false
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 12.3.3
|
90
|
+
- !ruby/object:Gem::Dependency
|
91
|
+
name: rspec
|
92
|
+
requirement: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
type: :development
|
98
|
+
prerelease: false
|
99
|
+
version_requirements: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
- !ruby/object:Gem::Dependency
|
105
|
+
name: rubocop
|
106
|
+
requirement: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
type: :development
|
112
|
+
prerelease: false
|
113
|
+
version_requirements: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
description: Stubs your gRPCs with some gruffy magic
|
119
|
+
email:
|
120
|
+
- ganmacs@gmail.com
|
121
|
+
- allen.rettberg@freshly.com
|
122
|
+
executables: []
|
123
|
+
extensions: []
|
124
|
+
extra_rdoc_files: []
|
125
|
+
files:
|
126
|
+
- ".github/workflows/rspec.yml"
|
127
|
+
- ".gitignore"
|
128
|
+
- ".rspec"
|
129
|
+
- ".rubocop.yml"
|
130
|
+
- ".travis.yml"
|
131
|
+
- Gemfile
|
132
|
+
- LICENSE.txt
|
133
|
+
- README.md
|
134
|
+
- Rakefile
|
135
|
+
- bin/console
|
136
|
+
- bin/release
|
137
|
+
- bin/setup
|
138
|
+
- gruffish.gemspec
|
139
|
+
- lib/grpc_mock.rb
|
140
|
+
- lib/grpc_mock/adapter.rb
|
141
|
+
- lib/grpc_mock/api.rb
|
142
|
+
- lib/grpc_mock/configuration.rb
|
143
|
+
- lib/grpc_mock/errors.rb
|
144
|
+
- lib/grpc_mock/grpc_stub_adapter.rb
|
145
|
+
- lib/grpc_mock/matchers/hash_argument_matcher.rb
|
146
|
+
- lib/grpc_mock/matchers/request_including_matcher.rb
|
147
|
+
- lib/grpc_mock/operation_stub.rb
|
148
|
+
- lib/grpc_mock/request_pattern.rb
|
149
|
+
- lib/grpc_mock/request_stub.rb
|
150
|
+
- lib/grpc_mock/response.rb
|
151
|
+
- lib/grpc_mock/response_sequence.rb
|
152
|
+
- lib/grpc_mock/rspec.rb
|
153
|
+
- lib/grpc_mock/stub_registry.rb
|
154
|
+
- lib/grpc_mock/version.rb
|
155
|
+
homepage: https://github.com/Freshly/gruffish
|
156
|
+
licenses:
|
157
|
+
- MIT
|
158
|
+
metadata: {}
|
159
|
+
post_install_message:
|
160
|
+
rdoc_options: []
|
161
|
+
require_paths:
|
162
|
+
- lib
|
163
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: '0'
|
168
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
169
|
+
requirements:
|
170
|
+
- - ">"
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: 1.3.1
|
173
|
+
requirements: []
|
174
|
+
rubygems_version: 3.1.2
|
175
|
+
signing_key:
|
176
|
+
specification_version: 4
|
177
|
+
summary: Stubs your gRPCs with some gruffy magic
|
178
|
+
test_files: []
|