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 +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +39 -0
- data/lib/webmock/twirp/error.rb +2 -1
- data/lib/webmock/twirp/refinements.rb +3 -3
- data/lib/webmock/twirp/request_stub.rb +18 -5
- data/lib/webmock/twirp/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 292380b89f2b938b849edb2e4ce46cb84e197b6a39592d16425440446adce9a1
|
4
|
+
data.tar.gz: 64be8b4452a96b256adc5bfe9d08a479392d9cb916f4fe0ab1135cc763728f87
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4f52185995140dc0d2aa2646227bbd9195a87941a1ab40a20d6e1dfb1b29bf24c9b0a50796db26fecd453f802fecc5bd5003eee284f325ccea3a96bf4f152a6
|
7
|
+
data.tar.gz: fa2e3ed00ed69f960dae3c500979fc8a745cd6e78015c68d6b963ee85ffad842070855abcdd8d50e6f37263f2789ead2f231d50092698c496bd0e4e5b8251970
|
data/CHANGELOG.md
CHANGED
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
|
data/lib/webmock/twirp/error.rb
CHANGED
@@ -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:
|
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
|
11
|
+
def normalized_hash
|
12
12
|
res = {}
|
13
13
|
|
14
14
|
self.class.descriptor.each do |field|
|
15
|
-
key =
|
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
|
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
|
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.
|
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-
|
11
|
+
date: 2022-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: webmock
|