webmock-twirp 0.3.0 → 0.4.0

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
2
  SHA256:
3
- metadata.gz: f1a34362dd929abe47a175e407335cc6f94a307c00fd902742d06285550fcd78
4
- data.tar.gz: efefa4e116a259eeab30129f7afe2b803f682daf0e0c15785e48720c5efe5d70
3
+ metadata.gz: 292380b89f2b938b849edb2e4ce46cb84e197b6a39592d16425440446adce9a1
4
+ data.tar.gz: 64be8b4452a96b256adc5bfe9d08a479392d9cb916f4fe0ab1135cc763728f87
5
5
  SHA512:
6
- metadata.gz: 04c11a624eb11547a7d0df20fcddd5b40015e75b6af94311e764484d98bc1333ffd728b085e0b5a29b08b4b039945d209a7ae74a9312bd0b65d9bebdcf87e43c
7
- data.tar.gz: 2179804f8df26c7c65d17781cd0a4df2c5c9806697e97c1f794f2bb008a5d5febdad89522df69b853ddfd6d2a869092b8dc572a5f5ac500fb5db3a5e30995215
6
+ metadata.gz: c4f52185995140dc0d2aa2646227bbd9195a87941a1ab40a20d6e1dfb1b29bf24c9b0a50796db26fecd453f802fecc5bd5003eee284f325ccea3a96bf4f152a6
7
+ data.tar.gz: fa2e3ed00ed69f960dae3c500979fc8a745cd6e78015c68d6b963ee85ffad842070855abcdd8d50e6f37263f2789ead2f231d50092698c496bd0e4e5b8251970
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ### v0.4.0 (2022-11-16)
2
+ - .to_return(Twirp::ClientResp)
3
+ - revert client class name filter
4
+ - unqualified client class filter
5
+ - uri matching
6
+
1
7
  ### v0.3.0 (2022-10-28)
2
8
  - twirpify all the things
3
9
  - improve refinements
data/README.md CHANGED
@@ -97,6 +97,45 @@ stub_twirp_request.to_return do |request|
97
97
  end
98
98
  ```
99
99
 
100
+ ## Improved WebMock Errors
101
+ Before
102
+ ```ruby
103
+ > client = EchoClient.new("http://example.com/twirp")
104
+ ...
105
+ > client.echo(msg: "Hi")
106
+ /lib/webmock/http_lib_adapters/net_http.rb:104:in `request': Real HTTP connections are disabled. Unregistered request:
107
+ POST http://example.com/twirp/Echo/Echo with body ' (WebMock::NetConnectNotAllowedError)
108
+ Hi' with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/protobuf', 'User-Agent'=>'Faraday v1.10.2'}
109
+
110
+ You can stub this request with the following snippet:
111
+
112
+ stub_request(:post, "http://example.com/twirp/Echo/Echo").
113
+ with(
114
+ body: "\n\x02Hi",
115
+ headers: {
116
+ 'Accept'=>'*/*',
117
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
118
+ 'Content-Type'=>'application/protobuf',
119
+ 'User-Agent'=>'Faraday v1.10.2'
120
+ }).
121
+ to_return(status: 200, body: "", headers: {})
122
+ ```
123
+
124
+ After
125
+ ```ruby
126
+ > require "webmock-twirp"
127
+ > client = EchoClient.new("http://example.com/twirp")
128
+ ...
129
+ > client.echo(msg: "Hi")
130
+ /lib/webmock/http_lib_adapters/net_http.rb:104:in `request': Real Twirp connections are disabled. Unregistered request:
131
+ EchoClient(http://example.com/twirp/Echo/Echo).echo(msg: "Hi") (WebMock::NetConnectNotAllowedError)
132
+
133
+ You can stub this request with the following snippet:
134
+
135
+ stub_twirp_request(:echo).with(
136
+ msg: "Hi",
137
+ ).to_return(...)
138
+ ```
100
139
 
101
140
  ----
102
141
  ## Contributing
@@ -9,7 +9,8 @@ module WebMock
9
9
  snippet = WebMock::RequestSignatureSnippet.new(@request_signature)
10
10
 
11
11
  text = [
12
- "Real Twirp connections are disabled. Unregistered request: #{@request_signature}",
12
+ "Real Twirp connections are disabled. Unregistered request:",
13
+ @request_signature,
13
14
  snippet.stubbing_instructions,
14
15
  snippet.request_stubs,
15
16
  "="*60
@@ -8,16 +8,16 @@ module WebMock
8
8
  end
9
9
 
10
10
  refine Google::Protobuf::MessageExts do
11
- def normalized_hash(symbolize_keys: true)
11
+ def normalized_hash
12
12
  res = {}
13
13
 
14
14
  self.class.descriptor.each do |field|
15
- key = symbolize_keys ? field.name.to_sym : field.name
15
+ key = field.name.to_sym
16
16
  value = field.get(self)
17
17
 
18
18
  if value.is_a?(Google::Protobuf::MessageExts)
19
19
  # recursively serialize sub-message
20
- value = value.normalized_hash(symbolize_keys: symbolize_keys)
20
+ value = value.normalized_hash
21
21
  end
22
22
 
23
23
  res[key] = value unless field.default == value
@@ -1,13 +1,11 @@
1
+ require "uri"
2
+
1
3
  module WebMock
2
4
  module Twirp
3
5
  class RequestStub < WebMock::RequestStub
4
6
  using Refinements
5
7
 
6
- attr_reader :twirp_client, :rpc_name, :with_attrs
7
-
8
8
  def initialize(*filters)
9
- rpc_name = filters.snag { |x| x.is_a?(Symbol) }
10
-
11
9
  client = filters.snag { |x| x.is_a?(::Twirp::Client) }
12
10
 
13
11
  klass = client&.class
@@ -15,11 +13,21 @@ module WebMock
15
13
  x.is_a?(Class) && (x < ::Twirp::Client || x < ::Twirp::Service)
16
14
  end
17
15
 
16
+ rpc_name = filters.snag { |x| x.is_a?(Symbol) }
17
+
18
+ uri = filters.snag do |x|
19
+ x.is_a?(String) && x.start_with?("http") && x =~ URI::regexp
20
+ end
21
+
22
+ if client && uri
23
+ raise ArgumentError, "specify uri or client instance, but not both"
24
+ end
25
+
18
26
  unless filters.empty?
19
27
  raise ArgumentError, "unexpected arguments: #{filters}"
20
28
  end
21
29
 
22
- uri = ""
30
+ uri ||= ""
23
31
 
24
32
  if client
25
33
  conn = client.instance_variable_get(:@conn)
@@ -135,6 +143,9 @@ module WebMock
135
143
  raise NotImplementedError
136
144
  end
137
145
 
146
+ # for internal, package use only
147
+ attr_reader :twirp_client, :rpc_name, :with_attrs
148
+
138
149
  private
139
150
 
140
151
  def generate_http_response(request, obj)
@@ -155,6 +166,8 @@ module WebMock
155
166
  end
156
167
 
157
168
  obj
169
+ when ::Twirp::ClientResp
170
+ obj.error || obj.data
158
171
  when ::Twirp::Error
159
172
  obj
160
173
  when Symbol
@@ -1,5 +1,5 @@
1
1
  module WebMock
2
2
  module Twirp
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
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.3.0
4
+ version: 0.4.0
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-29 00:00:00.000000000 Z
11
+ date: 2022-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: webmock