webmock-twirp 0.3.0 → 0.4.1

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: f1a34362dd929abe47a175e407335cc6f94a307c00fd902742d06285550fcd78
4
- data.tar.gz: efefa4e116a259eeab30129f7afe2b803f682daf0e0c15785e48720c5efe5d70
3
+ metadata.gz: fce3ed5388747c254df01adbd95139180c434b3b012c6c0983bd15a3973921bf
4
+ data.tar.gz: 61c27cfac06badede4fec86c61dd3b718e3731d8c6a4504bb8cbcb5ba3965181
5
5
  SHA512:
6
- metadata.gz: 04c11a624eb11547a7d0df20fcddd5b40015e75b6af94311e764484d98bc1333ffd728b085e0b5a29b08b4b039945d209a7ae74a9312bd0b65d9bebdcf87e43c
7
- data.tar.gz: 2179804f8df26c7c65d17781cd0a4df2c5c9806697e97c1f794f2bb008a5d5febdad89522df69b853ddfd6d2a869092b8dc572a5f5ac500fb5db3a5e30995215
6
+ metadata.gz: 030f3f2b9930d73e40ad4b2a4ed6fe26dcb84588e5c1378c7c892b20d64ee579d168df3906843878118eb435b1ebedc948944aefb8c45d552c4c3a5ec24d485e
7
+ data.tar.gz: 7cc323899b49557945f0e53796308d02cafde3dfe9829b9fbb0abfb91c4cdd5f4137828e2be87d20bd4ebe1f66d475fd18166df45eb1adf4da965152ea50b9f2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ ### v0.4.1 (2022-12-15)
2
+ - protobuf type support
3
+ - gemspec cleanup
4
+ - build upgrade
5
+ - dependabot
6
+ - respond to improvement
7
+
8
+ ### v0.4.0 (2022-11-16)
9
+ - .to_return(Twirp::ClientResp)
10
+ - revert client class name filter
11
+ - unqualified client class filter
12
+ - uri matching
13
+
1
14
  ### v0.3.0 (2022-10-28)
2
15
  - twirpify all the things
3
16
  - improve refinements
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,91 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ webmock-twirp (0.4.0)
5
+ twirp (>= 1)
6
+ webmock (>= 3)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.8.1)
12
+ public_suffix (>= 2.0.2, < 6.0)
13
+ byebug (11.1.3)
14
+ codecov (0.6.0)
15
+ simplecov (>= 0.15, < 0.22)
16
+ crack (0.4.5)
17
+ rexml
18
+ diff-lcs (1.5.0)
19
+ docile (1.4.0)
20
+ faraday (1.10.2)
21
+ faraday-em_http (~> 1.0)
22
+ faraday-em_synchrony (~> 1.0)
23
+ faraday-excon (~> 1.1)
24
+ faraday-httpclient (~> 1.0)
25
+ faraday-multipart (~> 1.0)
26
+ faraday-net_http (~> 1.0)
27
+ faraday-net_http_persistent (~> 1.0)
28
+ faraday-patron (~> 1.0)
29
+ faraday-rack (~> 1.0)
30
+ faraday-retry (~> 1.0)
31
+ ruby2_keywords (>= 0.0.4)
32
+ faraday-em_http (1.0.0)
33
+ faraday-em_synchrony (1.0.0)
34
+ faraday-excon (1.1.0)
35
+ faraday-httpclient (1.0.1)
36
+ faraday-multipart (1.0.4)
37
+ multipart-post (~> 2)
38
+ faraday-net_http (1.0.1)
39
+ faraday-net_http_persistent (1.2.0)
40
+ faraday-patron (1.0.0)
41
+ faraday-rack (1.0.0)
42
+ faraday-retry (1.0.3)
43
+ google-protobuf (3.21.12)
44
+ hashdiff (1.0.1)
45
+ multipart-post (2.2.3)
46
+ public_suffix (5.0.1)
47
+ rack (3.0.2)
48
+ rexml (3.2.5)
49
+ rspec (3.12.0)
50
+ rspec-core (~> 3.12.0)
51
+ rspec-expectations (~> 3.12.0)
52
+ rspec-mocks (~> 3.12.0)
53
+ rspec-core (3.12.0)
54
+ rspec-support (~> 3.12.0)
55
+ rspec-expectations (3.12.0)
56
+ diff-lcs (>= 1.2.0, < 2.0)
57
+ rspec-support (~> 3.12.0)
58
+ rspec-mocks (3.12.1)
59
+ diff-lcs (>= 1.2.0, < 2.0)
60
+ rspec-support (~> 3.12.0)
61
+ rspec-support (3.12.0)
62
+ ruby2_keywords (0.0.5)
63
+ simplecov (0.21.2)
64
+ docile (~> 1.1)
65
+ simplecov-html (~> 0.11)
66
+ simplecov_json_formatter (~> 0.1)
67
+ simplecov-html (0.12.3)
68
+ simplecov_json_formatter (0.1.4)
69
+ twirp (1.9.0)
70
+ faraday (< 2)
71
+ google-protobuf (~> 3.0, >= 3.7.0)
72
+ webmock (3.18.1)
73
+ addressable (>= 2.8.0)
74
+ crack (>= 0.3.2)
75
+ hashdiff (>= 0.4.0, < 2.0.0)
76
+ webrick (1.7.0)
77
+
78
+ PLATFORMS
79
+ ruby
80
+
81
+ DEPENDENCIES
82
+ byebug
83
+ codecov
84
+ rack
85
+ rspec
86
+ simplecov
87
+ webmock-twirp!
88
+ webrick
89
+
90
+ BUNDLED WITH
91
+ 2.3.17
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
@@ -7,14 +7,14 @@ module WebMock
7
7
  end
8
8
 
9
9
  def method_missing(name, *args, **kwargs, &block)
10
- super unless respond_to?(name)
10
+ return super unless respond_to_missing?(name)
11
11
  @stub.send(name, *args, **kwargs, &block)
12
12
 
13
13
  self
14
14
  end
15
15
 
16
- def respond_to?(method_name, include_private = false)
17
- super || @stub.respond_to?(method_name)
16
+ def respond_to_missing?(method_name, include_private = false)
17
+ @stub.respond_to?(method_name)
18
18
  end
19
19
 
20
20
  def matches?(block)
@@ -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
@@ -169,6 +182,12 @@ module WebMock
169
182
  else
170
183
  raise ArgumentError, "invalid http error status: #{obj}"
171
184
  end
185
+ when Class
186
+ unless obj == msg_class
187
+ raise TypeError, "Expected type #{msg_class}, found #{obj}"
188
+ end
189
+
190
+ obj.new
172
191
  else
173
192
  raise ArgumentError, "can not generate twirp reponse from: #{obj}"
174
193
  end
@@ -1,5 +1,5 @@
1
1
  module WebMock
2
2
  module Twirp
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.1"
4
4
  end
5
5
  end
data/service.rb ADDED
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "google/protobuf"
4
+ require "rack"
5
+ require "twirp"
6
+ require "webrick"
7
+ Dir["./spec/support/**/*.rb"].sort.each { |f| require f }
8
+
9
+ server = WEBrick::HTTPServer.new(Port: 3000)
10
+
11
+ handler = EchoHandler.new
12
+ service = EchoService.new(handler)
13
+ path_prefix = "/twirp/" + service.full_name
14
+ server.mount path_prefix, Rack::Handler::WEBrick, service
15
+
16
+ # handler = ComplexHandler.new
17
+ # service = ComplexService.new(handler)
18
+ # path_prefix = "/twirp/" + service.full_name
19
+ # server.mount path_prefix, Rack::Handler::WEBrick, service
20
+
21
+ server.start
22
+
23
+ # client = EchoClient.new("http://localhost:3000/twirp")
24
+ # client = ComplexClient.new("http://localhost:3000/twirp")
25
+ # resp = client.echo(msg: "World")
26
+ # puts resp.data
@@ -1,25 +1,15 @@
1
- package_name = File.basename(__FILE__).split(".")[0]
2
- load Dir.glob("lib/**/version.rb")[0]
3
-
1
+ require_relative "lib/webmock/twirp/version"
4
2
  package = WebMock::Twirp
5
3
 
6
-
7
4
  Gem::Specification.new do |s|
8
- s.name = package_name
9
- s.version = package.const_get "VERSION"
10
5
  s.authors = ["Daniel Pepper"]
11
- s.summary = package.to_s
12
6
  s.description = "Twirp support for WebMock"
13
- s.homepage = "https://github.com/dpep/#{package_name}"
7
+ s.files = `git ls-files * ':!:spec'`.split("\n")
8
+ s.homepage = "https://github.com/dpep/webmock-twirp"
14
9
  s.license = "MIT"
15
-
16
- s.files = Dir[
17
- __FILE__,
18
- 'lib/**/*',
19
- 'CHANGELOG*',
20
- 'LICENSE*',
21
- 'README*',
22
- ]
10
+ s.name = File.basename(__FILE__).split(".")[0]
11
+ s.summary = package.to_s
12
+ s.version = package.const_get "VERSION"
23
13
 
24
14
  s.add_dependency "webmock", ">= 3"
25
15
  s.add_dependency "twirp", ">= 1"
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.1
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-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: webmock
@@ -129,6 +129,8 @@ extensions: []
129
129
  extra_rdoc_files: []
130
130
  files:
131
131
  - CHANGELOG.md
132
+ - Gemfile
133
+ - Gemfile.lock
132
134
  - LICENSE.txt
133
135
  - README.md
134
136
  - lib/webmock-twirp.rb
@@ -143,6 +145,7 @@ files:
143
145
  - lib/webmock/twirp/request_stub.rb
144
146
  - lib/webmock/twirp/stub_request_snippet.rb
145
147
  - lib/webmock/twirp/version.rb
148
+ - service.rb
146
149
  - webmock-twirp.gemspec
147
150
  homepage: https://github.com/dpep/webmock-twirp
148
151
  licenses:
@@ -166,5 +169,5 @@ requirements: []
166
169
  rubygems_version: 3.3.7
167
170
  signing_key:
168
171
  specification_version: 4
169
- summary: Gem::Specification::WebMock::Twirp
172
+ summary: WebMock::Twirp
170
173
  test_files: []