twirp 1.12.0 → 1.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/twirp/client.rb +1 -1
- data/lib/twirp/service.rb +5 -5
- data/lib/twirp/version.rb +1 -1
- data/test/client_test.rb +11 -0
- data/test/service_test.rb +20 -20
- data/twirp.gemspec +1 -1
- metadata +2 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78baa75e1f8c6d2009bb678de8263824d4bb691c8bb862dd8c3969e93f9a711e
|
4
|
+
data.tar.gz: a6d86c07b49e7ec371cee190942b1286d91208b2d45104a64f67c430207fa3a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f9f81587e713fd88ab7a2a7fd815b317e271d3a58a76692a4f6828be15076d7ba9682a984001e3fb82aa20dfa74dc14f2f01fc5a53d34e3470b9192793d6005
|
7
|
+
data.tar.gz: 1004bb764ef0e9572aee60b0b84f337cda920ed45d7cd03655c5d47c6299c3864e33737e44010ef2905cebe5095d4a89bd51e9c5517545b0966e9b8854c92055
|
data/lib/twirp/client.rb
CHANGED
data/lib/twirp/service.rb
CHANGED
@@ -32,7 +32,7 @@ module Twirp
|
|
32
32
|
# Rack response with a Twirp::Error
|
33
33
|
def error_response(twerr)
|
34
34
|
status = Twirp::ERROR_CODES_TO_HTTP_STATUS[twerr.code]
|
35
|
-
headers = {
|
35
|
+
headers = {Rack::CONTENT_TYPE => Encoding::JSON} # Twirp errors are always JSON, even if the request was protobuf
|
36
36
|
resp_body = Encoding.encode_json(twerr.to_h)
|
37
37
|
[status, headers, [resp_body]]
|
38
38
|
end
|
@@ -100,7 +100,7 @@ module Twirp
|
|
100
100
|
input = env[:input_class].new(input) if input.is_a? Hash
|
101
101
|
env[:input] = input
|
102
102
|
env[:content_type] ||= Encoding::PROTO
|
103
|
-
env[:http_response_headers] = {}
|
103
|
+
env[:http_response_headers] = defined?(Rack::Headers) ? Rack::Headers.new : {}
|
104
104
|
call_handler(env)
|
105
105
|
end
|
106
106
|
|
@@ -138,7 +138,7 @@ module Twirp
|
|
138
138
|
input = nil
|
139
139
|
begin
|
140
140
|
body_str = rack_request.body.read
|
141
|
-
rack_request.body.rewind # allow other middleware to read again (https://github.com/arthurnn/twirp-ruby/issues/50)
|
141
|
+
rack_request.body.rewind if rack_request.body.respond_to?(:rewind) # allow other middleware to read again (https://github.com/arthurnn/twirp-ruby/issues/50)
|
142
142
|
input = Encoding.decode(body_str, env[:input_class], content_type)
|
143
143
|
rescue => e
|
144
144
|
error_msg = "Invalid request body for rpc method #{method_name.inspect} with Content-Type=#{content_type}"
|
@@ -149,7 +149,7 @@ module Twirp
|
|
149
149
|
end
|
150
150
|
|
151
151
|
env[:input] = input
|
152
|
-
env[:http_response_headers] = {}
|
152
|
+
env[:http_response_headers] = defined?(Rack::Headers) ? Rack::Headers.new : {}
|
153
153
|
return
|
154
154
|
end
|
155
155
|
|
@@ -181,7 +181,7 @@ module Twirp
|
|
181
181
|
env[:output] = output
|
182
182
|
@on_success.each{|hook| hook.call(env) }
|
183
183
|
|
184
|
-
headers = env[:http_response_headers].merge(
|
184
|
+
headers = env[:http_response_headers].merge(Rack::CONTENT_TYPE => env[:content_type])
|
185
185
|
resp_body = Encoding.encode(output, env[:output_class], env[:content_type])
|
186
186
|
[200, headers, [resp_body]]
|
187
187
|
|
data/lib/twirp/version.rb
CHANGED
data/test/client_test.rb
CHANGED
@@ -58,6 +58,17 @@ class ClientTest < Minitest::Test
|
|
58
58
|
assert_equal num_mthds + 1, EmptyClient.instance_methods.size # new method added
|
59
59
|
end
|
60
60
|
|
61
|
+
def test_is_http_redirect
|
62
|
+
assert Twirp::Client.is_http_redirect? 300
|
63
|
+
assert Twirp::Client.is_http_redirect? 301
|
64
|
+
assert Twirp::Client.is_http_redirect? 302
|
65
|
+
assert Twirp::Client.is_http_redirect? 399
|
66
|
+
|
67
|
+
refute Twirp::Client.is_http_redirect? 200
|
68
|
+
refute Twirp::Client.is_http_redirect? 400
|
69
|
+
refute Twirp::Client.is_http_redirect? nil
|
70
|
+
end
|
71
|
+
|
61
72
|
|
62
73
|
# Call .rpc on Protobuf client
|
63
74
|
# ----------------------------
|
data/test/service_test.rb
CHANGED
@@ -17,7 +17,7 @@ class ServiceTest < Minitest::Test
|
|
17
17
|
twerr = Twirp::Error.invalid_argument('foo')
|
18
18
|
resp = Twirp::Service.error_response(twerr)
|
19
19
|
assert_equal 400, resp[0]
|
20
|
-
assert_equal 'application/json', resp[1][
|
20
|
+
assert_equal 'application/json', resp[1][Rack::CONTENT_TYPE]
|
21
21
|
assert_equal '{"code":"invalid_argument","msg":"foo"}', resp[2][0]
|
22
22
|
end
|
23
23
|
|
@@ -60,7 +60,7 @@ class ServiceTest < Minitest::Test
|
|
60
60
|
status, headers, body = haberdasher_service.call(rack_env)
|
61
61
|
|
62
62
|
assert_equal 200, status
|
63
|
-
assert_equal 'application/json', headers[
|
63
|
+
assert_equal 'application/json', headers[Rack::CONTENT_TYPE]
|
64
64
|
assert_equal({"inches" => 10, "color" => "white"}, JSON.parse(body[0]))
|
65
65
|
end
|
66
66
|
|
@@ -69,7 +69,7 @@ class ServiceTest < Minitest::Test
|
|
69
69
|
status, headers, body = haberdasher_service.call(rack_env)
|
70
70
|
|
71
71
|
assert_equal 200, status
|
72
|
-
assert_equal 'application/json; strict=true', headers[
|
72
|
+
assert_equal 'application/json; strict=true', headers[Rack::CONTENT_TYPE]
|
73
73
|
assert_equal({"inches" => 0, "color" => "white"}, JSON.parse(body[0]))
|
74
74
|
end
|
75
75
|
|
@@ -78,7 +78,7 @@ class ServiceTest < Minitest::Test
|
|
78
78
|
status, headers, body = haberdasher_service.call(rack_env)
|
79
79
|
|
80
80
|
assert_equal 200, status
|
81
|
-
assert_equal 'application/json', headers[
|
81
|
+
assert_equal 'application/json', headers[Rack::CONTENT_TYPE]
|
82
82
|
assert_equal({"inches" => 0, "color" => "white"}, JSON.parse(body[0]))
|
83
83
|
end
|
84
84
|
|
@@ -87,7 +87,7 @@ class ServiceTest < Minitest::Test
|
|
87
87
|
status, headers, body = haberdasher_service.call(rack_env)
|
88
88
|
|
89
89
|
assert_equal 200, status
|
90
|
-
assert_equal 'application/protobuf', headers[
|
90
|
+
assert_equal 'application/protobuf', headers[Rack::CONTENT_TYPE]
|
91
91
|
assert_equal Example::Hat.new(inches: 10, color: "white"), Example::Hat.decode(body[0])
|
92
92
|
end
|
93
93
|
|
@@ -96,7 +96,7 @@ class ServiceTest < Minitest::Test
|
|
96
96
|
status, headers, body = haberdasher_service.call(rack_env)
|
97
97
|
|
98
98
|
assert_equal 404, status
|
99
|
-
assert_equal 'application/json', headers[
|
99
|
+
assert_equal 'application/json', headers[Rack::CONTENT_TYPE]
|
100
100
|
assert_equal({
|
101
101
|
"code" => 'bad_route',
|
102
102
|
"msg" => 'Invalid rpc method "MakeUnicorns"',
|
@@ -110,7 +110,7 @@ class ServiceTest < Minitest::Test
|
|
110
110
|
status, headers, body = haberdasher_service.call(rack_env)
|
111
111
|
|
112
112
|
assert_equal 404, status
|
113
|
-
assert_equal 'application/json', headers[
|
113
|
+
assert_equal 'application/json', headers[Rack::CONTENT_TYPE]
|
114
114
|
assert_equal({
|
115
115
|
"code" => 'bad_route',
|
116
116
|
"msg" => 'HTTP request method must be POST',
|
@@ -124,7 +124,7 @@ class ServiceTest < Minitest::Test
|
|
124
124
|
status, headers, body = haberdasher_service.call(rack_env)
|
125
125
|
|
126
126
|
assert_equal 404, status
|
127
|
-
assert_equal 'application/json', headers[
|
127
|
+
assert_equal 'application/json', headers[Rack::CONTENT_TYPE]
|
128
128
|
assert_equal({
|
129
129
|
"code" => 'bad_route',
|
130
130
|
"msg" => 'Unexpected Content-Type: "text/plain". Content-Type header must be one of ["application/json", "application/protobuf"]',
|
@@ -137,7 +137,7 @@ class ServiceTest < Minitest::Test
|
|
137
137
|
status, headers, body = haberdasher_service.call(rack_env)
|
138
138
|
|
139
139
|
assert_equal 404, status
|
140
|
-
assert_equal 'application/json', headers[
|
140
|
+
assert_equal 'application/json', headers[Rack::CONTENT_TYPE]
|
141
141
|
assert_equal({
|
142
142
|
"code" => 'bad_route',
|
143
143
|
"msg" => 'Invalid route. Expected format: POST {BaseURL}/example.Haberdasher/{Method}',
|
@@ -150,7 +150,7 @@ class ServiceTest < Minitest::Test
|
|
150
150
|
status, headers, body = haberdasher_service.call(rack_env)
|
151
151
|
|
152
152
|
assert_equal 404, status
|
153
|
-
assert_equal 'application/json', headers[
|
153
|
+
assert_equal 'application/json', headers[Rack::CONTENT_TYPE] # error responses are always JSON, even for Protobuf requests
|
154
154
|
assert_equal({
|
155
155
|
"code" => 'bad_route',
|
156
156
|
"msg" => 'Invalid route. Expected format: POST {BaseURL}/example.Haberdasher/{Method}',
|
@@ -164,7 +164,7 @@ class ServiceTest < Minitest::Test
|
|
164
164
|
status, headers, body = haberdasher_service.call(rack_env)
|
165
165
|
|
166
166
|
assert_equal 400, status
|
167
|
-
assert_equal 'application/json', headers[
|
167
|
+
assert_equal 'application/json', headers[Rack::CONTENT_TYPE]
|
168
168
|
assert_equal({
|
169
169
|
"code" => 'malformed',
|
170
170
|
"msg" => 'Invalid request body for rpc method "MakeHat" with Content-Type=application/json: ' +
|
@@ -179,7 +179,7 @@ class ServiceTest < Minitest::Test
|
|
179
179
|
status, headers, body = haberdasher_service.call(rack_env)
|
180
180
|
|
181
181
|
assert_equal 400, status
|
182
|
-
assert_equal 'application/json', headers[
|
182
|
+
assert_equal 'application/json', headers[Rack::CONTENT_TYPE]
|
183
183
|
assert_equal({
|
184
184
|
"code" => 'malformed',
|
185
185
|
"msg" => 'Invalid request body for rpc method "MakeHat" with Content-Type=application/protobuf: ' +
|
@@ -193,7 +193,7 @@ class ServiceTest < Minitest::Test
|
|
193
193
|
status, headers, body = haberdasher_service.call(rack_env)
|
194
194
|
|
195
195
|
assert_equal 200, status
|
196
|
-
assert_equal 'application/json', headers[
|
196
|
+
assert_equal 'application/json', headers[Rack::CONTENT_TYPE]
|
197
197
|
assert_equal({"inches" => 10, "color" => "white"}, JSON.parse(body[0]))
|
198
198
|
end
|
199
199
|
|
@@ -202,7 +202,7 @@ class ServiceTest < Minitest::Test
|
|
202
202
|
status, headers, body = haberdasher_service.call(rack_env)
|
203
203
|
|
204
204
|
assert_equal 200, status
|
205
|
-
assert_equal 'application/json', headers[
|
205
|
+
assert_equal 'application/json', headers[Rack::CONTENT_TYPE]
|
206
206
|
assert_equal({"inches" => 10, "color" => "white"}, JSON.parse(body[0]))
|
207
207
|
end
|
208
208
|
|
@@ -211,7 +211,7 @@ class ServiceTest < Minitest::Test
|
|
211
211
|
status, headers, body = haberdasher_service.call(rack_env)
|
212
212
|
|
213
213
|
assert_equal 400, status
|
214
|
-
assert_equal 'application/json', headers[
|
214
|
+
assert_equal 'application/json', headers[Rack::CONTENT_TYPE]
|
215
215
|
assert_equal({
|
216
216
|
"code" => 'malformed',
|
217
217
|
"msg" => 'Invalid request body for rpc method "MakeHat" with Content-Type=application/json; strict=true: ' +
|
@@ -289,7 +289,7 @@ class ServiceTest < Minitest::Test
|
|
289
289
|
rack_env = proto_req "/example.Haberdasher/MakeHat", Example::Size.new(inches: 666)
|
290
290
|
status, headers, body = svc.call(rack_env)
|
291
291
|
assert_equal 400, status
|
292
|
-
assert_equal 'application/json', headers[
|
292
|
+
assert_equal 'application/json', headers[Rack::CONTENT_TYPE] # error responses are always JSON, even for Protobuf requests
|
293
293
|
assert_equal({
|
294
294
|
"code" => 'invalid_argument',
|
295
295
|
"msg" => "I don't like that size",
|
@@ -298,7 +298,7 @@ class ServiceTest < Minitest::Test
|
|
298
298
|
|
299
299
|
def test_handler_method_can_set_response_headers_through_the_env
|
300
300
|
svc = Example::Haberdasher.new(HaberdasherHandler.new do |size, env|
|
301
|
-
env[:http_response_headers][
|
301
|
+
env[:http_response_headers][Rack::CACHE_CONTROL] = "public, max-age=60"
|
302
302
|
{}
|
303
303
|
end)
|
304
304
|
|
@@ -306,8 +306,8 @@ class ServiceTest < Minitest::Test
|
|
306
306
|
status, headers, body = svc.call(rack_env)
|
307
307
|
|
308
308
|
assert_equal 200, status
|
309
|
-
assert_equal "public, max-age=60", headers[
|
310
|
-
assert_equal "application/protobuf", headers[
|
309
|
+
assert_equal "public, max-age=60", headers[Rack::CACHE_CONTROL] # set by the handler
|
310
|
+
assert_equal "application/protobuf", headers[Rack::CONTENT_TYPE] # set by Twirp::Service
|
311
311
|
end
|
312
312
|
|
313
313
|
def test_handler_returns_invalid_type_nil
|
@@ -533,7 +533,7 @@ class ServiceTest < Minitest::Test
|
|
533
533
|
status, headers, body = svc.call(rack_env)
|
534
534
|
|
535
535
|
assert_equal 500, status
|
536
|
-
assert_equal 'application/json', headers[
|
536
|
+
assert_equal 'application/json', headers[Rack::CONTENT_TYPE]
|
537
537
|
assert_equal({
|
538
538
|
"code" => 'intenal',
|
539
539
|
"msg" => 'hook1 failed',
|
data/twirp.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.required_ruby_version = '>= 1.9'
|
22
22
|
spec.add_runtime_dependency 'google-protobuf', '>= 3.25', '< 5.a'
|
23
23
|
spec.add_runtime_dependency 'faraday', '< 3' # for clients
|
24
|
-
spec.add_dependency 'rack', '>= 2.2.3'
|
24
|
+
spec.add_dependency 'rack', '>= 2.2.3'
|
25
25
|
|
26
26
|
spec.add_development_dependency 'bundler', '~> 2'
|
27
27
|
spec.add_development_dependency 'minitest', '>= 5'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twirp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.13.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cyrus A. Forbes
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2024-
|
12
|
+
date: 2024-11-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: google-protobuf
|
@@ -52,9 +52,6 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 2.2.3
|
55
|
-
- - "<"
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
version: '3'
|
58
55
|
type: :runtime
|
59
56
|
prerelease: false
|
60
57
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -62,9 +59,6 @@ dependencies:
|
|
62
59
|
- - ">="
|
63
60
|
- !ruby/object:Gem::Version
|
64
61
|
version: 2.2.3
|
65
|
-
- - "<"
|
66
|
-
- !ruby/object:Gem::Version
|
67
|
-
version: '3'
|
68
62
|
- !ruby/object:Gem::Dependency
|
69
63
|
name: bundler
|
70
64
|
requirement: !ruby/object:Gem::Requirement
|