webmock-twirp 0.1.0 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 961de376102cbbb75708a544bb34e42c065beab4ad92ef4e4441036d47569d1d
4
- data.tar.gz: 0de10400686561ba24d597d06ea23c4b77ca3445af78c7f7a98997e1bd32d86c
3
+ metadata.gz: 0264d26c4984559b4163ff5740fda0df43882736fa174881a63939c195e85331
4
+ data.tar.gz: 9d0da633242e3f722d18c0c97042c5312b16ba4fb28fb6509479a04f5c4201eb
5
5
  SHA512:
6
- metadata.gz: e1edcd51eff12eb65d75dd96713ff202410447cdd9ef3ddfbf1fff7c090cc94d370ec381a6bf4966b7e1cd0034c59552f1ed1953e498f7b24b06df4bf1beaad9
7
- data.tar.gz: b6a646485bc5d8a49f27229cddfc55d3045fb000a0d0186ed03216dbd8436331186d058dcd4257ca15ee5d6ebd757026d7fb5a9fb4be69f4c1d512e4efd0286e
6
+ metadata.gz: 369e27e257599a09541feb244e9a4c1ff59a7b069c3df154c361c6567ce8f15f0e3b46cbfb0252ebe7c72beed20cb31bb8c947857d84372aec7ae3d697e21602
7
+ data.tar.gz: 7aa1544321a07d5f8350203f16f331d2e9ee90b5b38928d29d1b4fb2750101109298796ff7b571684108af8aaf3c80d4b2d595a27cdef0b33715c888cb99f1cd
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ### v0.1.2 (2022-10-17)
2
+ - improved matching
3
+ - test coverage
4
+
5
+ ### v0.1.1 (2022-10-11)
6
+ - test coverage
7
+ - error messages
8
+ - fix and_return alias
9
+ - Update README.md
10
+
1
11
  ### v0.1.0 (2022-10-09)
2
12
  - dynamic client/rpc lookup
3
13
 
data/README.md CHANGED
@@ -2,7 +2,12 @@ WebMock::Twirp
2
2
  ======
3
3
  Twirp support for [WebMock](https://github.com/bblimke/webmock). All our favorite http request stubbing for Twirp RPCs - message and error serialization done automatically.
4
4
 
5
+ ### Install
6
+ ```ruby
7
+ gem "webmock-twirp"
8
+ ```
5
9
 
10
+ ### Example
6
11
  ```ruby
7
12
  require "webmock/twirp"
8
13
 
@@ -37,6 +42,59 @@ end
37
42
  ```
38
43
 
39
44
 
45
+ ## Usage
46
+
47
+ ### .with
48
+ `stub_twirp_request.with` allows you to only stub requests which match specific attributes. It accepts a hash or a `Google::Protobuf::MessageExts` instance. The hash supports constants, regexes, and rspec matchers.
49
+
50
+ ```ruby
51
+ stub_twirp_request.with(message: "hi")
52
+ stub_twirp_request.with(message: /^h/)
53
+ stub_twirp_request.with(message: include("i"))
54
+
55
+ expected_request = MyTwirpRequest.new(message: "hi")
56
+ stub_twirp_request.with(expected_request)
57
+ ```
58
+
59
+
60
+ If you want even more control over the matching criteria, use the block mode. A `Protobuf` instance is passed into the block with the request's parameters.
61
+
62
+ ```ruby
63
+ stub_twirp_request.with do |request|
64
+ request.message == "hi"
65
+ end
66
+ ```
67
+
68
+
69
+ ### .to_return
70
+ `stub_twirp_request.to_return` allows you to specify a response, or use a default response. It can be a hash or `Protobuf` instance. To return an error, specify an error code, http status, or `Twirp::Error`.
71
+
72
+ ```ruby
73
+ stub_twirp_request.to_return # ie. `MyTwirpResponse.new`
74
+
75
+ stub_twirp_request.to_return(msg: "bye")
76
+
77
+ response = MyTwirpResponse.new(msg: "bye")
78
+ stub_twirp_request.to_return(response)
79
+
80
+ # errors
81
+ stub_twirp_request.to_return(:not_found)
82
+ stub_twirp_request.to_return(404)
83
+ stub_twirp_request.to_return(Twirp::Error.not_found("Nope"))
84
+ ```
85
+
86
+ The block mode passes in the request Protobuf.
87
+ ```ruby
88
+ stub_twirp_request.to_return do |request|
89
+ if request.message == "hi"
90
+ { msg: "bye" }
91
+ else
92
+ :not_found
93
+ end
94
+ end
95
+ ```
96
+
97
+
40
98
  ----
41
99
  ## Contributing
42
100
 
@@ -6,6 +6,15 @@ module WebMock
6
6
  find(&block)&.tap { |x| delete(x) }
7
7
  end
8
8
  end
9
+
10
+ refine Google::Protobuf::MessageExts do
11
+ def normalized_hash(symbolize_keys: true)
12
+ JSON.parse(
13
+ to_json(preserve_proto_fieldnames: true),
14
+ symbolize_names: symbolize_keys,
15
+ )
16
+ end
17
+ end
9
18
  end
10
19
  end
11
20
  end
@@ -76,10 +76,17 @@ module WebMock
76
76
  decoded_request = input_class.decode(request.body)
77
77
 
78
78
  if attrs.any?
79
- attr_matcher = Matchers::HashIncludingMatcher.new(**attrs)
80
- attr_hash = WebMock::Util::HashKeysStringifier.stringify_keys!(decoded_request.to_h, deep: true)
79
+ if defined?(RSpec::Matchers::BuiltIn::Include)
80
+ matcher = RSpec::Matchers::BuiltIn::Include.new(attrs)
81
+ attr_hash = decoded_request.to_h
82
+ normalize = ->{ decoded_request.normalized_hash }
83
+ else
84
+ matcher = WebMock::Matchers::HashIncludingMatcher.new(attrs)
85
+ attr_hash = WebMock::Util::HashKeysStringifier.stringify_keys!(decoded_request.to_h, deep: true)
86
+ normalize = ->{ decoded_request.normalized_hash(symbolize_keys: false) }
87
+ end
81
88
 
82
- matched &= attr_matcher === attr_hash
89
+ matched &= (matcher === attr_hash || matcher === normalize.call)
83
90
  end
84
91
 
85
92
  if block
@@ -117,6 +124,7 @@ module WebMock
117
124
 
118
125
  super(*response_hashes, &decoder)
119
126
  end
127
+ alias_method :and_return, :to_return # update existing alias
120
128
 
121
129
  def to_return_json(*)
122
130
  raise NotImplementedError
@@ -168,10 +176,10 @@ module WebMock
168
176
  if code = ::Twirp::ERROR_CODES_TO_HTTP_STATUS.key(obj)
169
177
  ::Twirp::Error.new(code, code)
170
178
  else
171
- raise ArgumentError, "invalid error code: #{obj}"
179
+ raise ArgumentError, "invalid http error status: #{obj}"
172
180
  end
173
181
  else
174
- raise NotImplementedError
182
+ raise ArgumentError, "can not generate twirp reponse from: #{obj}"
175
183
  end
176
184
 
177
185
  if res.is_a?(Google::Protobuf::MessageExts)
@@ -1,5 +1,5 @@
1
1
  module WebMock
2
2
  module Twirp
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
data/lib/webmock/twirp.rb CHANGED
@@ -31,8 +31,7 @@ if $LOADED_FEATURES.find { |x| x =~ %r{/webmock/rspec.rb} }
31
31
  # require "webmock/rspec"
32
32
  RSpec.configure { |c| c.include WebMock::Twirp::API }
33
33
  else
34
- # patch Webmock to also export stub_twirp_request
35
-
34
+ # patch WebMock to also export stub_twirp_request
36
35
  module WebMock
37
36
  module API
38
37
  include WebMock::Twirp::API
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmock-twirp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pepper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-09 00:00:00.000000000 Z
11
+ date: 2022-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: webmock