twirp 1.4.1 → 1.5.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: 764fa4b20e00cd8c88643863bdcbc7d51d35bc17e60aa53ab86c8eb35e61a761
4
- data.tar.gz: 1a8c622d398aff28244a6a9c3bec6a6a74d22a8f5cf6c71e65273e06021c501b
3
+ metadata.gz: '07397331bd18c688d70740c3ad5cec2401103a325bab3331dda34d3072223c89'
4
+ data.tar.gz: 684074c539bb00ddb32e814601378b6d3e0f967b48e8c758832a56a74ceba0a2
5
5
  SHA512:
6
- metadata.gz: 521bdc421efb85856bfcab5ea2c9446d21072296cba21b007ec4f9015ec345ce14a166db5c8e078c8f754d61aaa9d19d039c96ab2b2a0d291c5768a4ef7dd5e8
7
- data.tar.gz: db8f9343a888c4385cd4452a61020afc85664041f4e4b676e4b89220043ef03b2daddf8558435e71406c53d43f2ba38f9e0f40c1777c20e0d1ea73b47ad983b7
6
+ metadata.gz: '079722ee218b74194884e10383fc9ebb53febf02e215d7fa001c14c864e3c2eff9fddee8079c29e06396a766901efab7e696c14d9f9e0afd6df1034a10e5aa56'
7
+ data.tar.gz: d91d578d6cbd5245b38d5b1838892db43bc53730cdfb00391a1cad266d3193522625d87c3b5e2bfb0fc807bb90e6d1cd90063c95a139c5096ea4ef425106e2f7
@@ -16,9 +16,10 @@ module Twirp
16
16
  # Valid Twirp error codes and their mapping to related HTTP status.
17
17
  # This can also be used to check if a code is valid (check if not nil).
18
18
  ERROR_CODES_TO_HTTP_STATUS = {
19
- canceled: 408, # RequestTimeout
20
- invalid_argument: 400, # BadRequest
21
- deadline_exceeded: 408, # RequestTimeout
19
+ canceled: 408, # Request Timeout
20
+ invalid_argument: 400, # Bad Request
21
+ malformed: 400, # Bad Request
22
+ deadline_exceeded: 408, # Request Timeout
22
23
  not_found: 404, # Not Found
23
24
  bad_route: 404, # Not Found
24
25
  already_exists: 409, # Conflict
@@ -107,29 +107,30 @@ module Twirp
107
107
  private
108
108
 
109
109
  # Parse request and fill env with rpc data.
110
- # Returns a bad_route error if something went wrong.
110
+ # Returns a bad_route error if could not be properly routed to a Twirp method.
111
+ # Returns a malformed error if could not decode the body (either bad JSON or bad Protobuf)
111
112
  def route_request(rack_env, env)
112
113
  rack_request = Rack::Request.new(rack_env)
113
114
 
114
115
  if rack_request.request_method != "POST"
115
- return bad_route_error("HTTP request method must be POST", rack_request)
116
+ return route_err(:bad_route, "HTTP request method must be POST", rack_request)
116
117
  end
117
118
 
118
119
  content_type = rack_request.get_header("CONTENT_TYPE")
119
120
  if !Encoding.valid_content_type?(content_type)
120
- return bad_route_error("Unexpected Content-Type: #{content_type.inspect}. Content-Type header must be one of #{Encoding.valid_content_types.inspect}", rack_request)
121
+ return route_err(:bad_route, "Unexpected Content-Type: #{content_type.inspect}. Content-Type header must be one of #{Encoding.valid_content_types.inspect}", rack_request)
121
122
  end
122
123
  env[:content_type] = content_type
123
124
 
124
125
  path_parts = rack_request.fullpath.split("/")
125
126
  if path_parts.size < 3 || path_parts[-2] != self.full_name
126
- return bad_route_error("Invalid route. Expected format: POST {BaseURL}/#{self.full_name}/{Method}", rack_request)
127
+ return route_err(:bad_route, "Invalid route. Expected format: POST {BaseURL}/#{self.full_name}/{Method}", rack_request)
127
128
  end
128
129
  method_name = path_parts[-1]
129
130
 
130
131
  base_env = self.class.rpcs[method_name]
131
132
  if !base_env
132
- return bad_route_error("Invalid rpc method #{method_name.inspect}", rack_request)
133
+ return route_err(:bad_route, "Invalid rpc method #{method_name.inspect}", rack_request)
133
134
  end
134
135
  env.merge!(base_env) # :rpc_method, :input_class, :output_class
135
136
 
@@ -141,7 +142,7 @@ module Twirp
141
142
  if e.is_a?(Google::Protobuf::ParseError)
142
143
  error_msg += ": #{e.message.strip}"
143
144
  end
144
- return bad_route_error(error_msg, rack_request)
145
+ return route_err(:malformed, error_msg, rack_request)
145
146
  end
146
147
 
147
148
  env[:input] = input
@@ -149,12 +150,11 @@ module Twirp
149
150
  return
150
151
  end
151
152
 
152
- def bad_route_error(msg, req)
153
- Twirp::Error.bad_route msg, twirp_invalid_route: "#{req.request_method} #{req.fullpath}"
153
+ def route_err(code, msg, req)
154
+ Twirp::Error.new code, msg, twirp_invalid_route: "#{req.request_method} #{req.fullpath}"
154
155
  end
155
156
 
156
157
 
157
-
158
158
  # Call handler method and return a Protobuf Message or a Twirp::Error.
159
159
  def call_handler(env)
160
160
  m = env[:ruby_method]
@@ -12,5 +12,5 @@
12
12
  # permissions and limitations under the License.
13
13
 
14
14
  module Twirp
15
- VERSION = "1.4.1"
15
+ VERSION = "1.5.0"
16
16
  end
@@ -5,7 +5,7 @@ require_relative '../lib/twirp/error'
5
5
  class TestErrorCodes < Minitest::Test
6
6
 
7
7
  def test_error_codes
8
- assert_equal 17, Twirp::ERROR_CODES.size
8
+ assert_equal 18, Twirp::ERROR_CODES.size
9
9
 
10
10
  # all codes should be symbols
11
11
  Twirp::ERROR_CODES.each do |code|
@@ -19,7 +19,7 @@ class TestErrorCodes < Minitest::Test
19
19
  end
20
20
 
21
21
  def test_codes_to_http_status
22
- assert_equal 17, Twirp::ERROR_CODES_TO_HTTP_STATUS.size
22
+ assert_equal 18, Twirp::ERROR_CODES_TO_HTTP_STATUS.size
23
23
 
24
24
  assert_equal 404, Twirp::ERROR_CODES_TO_HTTP_STATUS[:not_found]
25
25
  assert_equal 500, Twirp::ERROR_CODES_TO_HTTP_STATUS[:internal]
@@ -145,10 +145,10 @@ class ServiceTest < Minitest::Test
145
145
  method: "POST", input: 'bad json', "CONTENT_TYPE" => "application/json"
146
146
  status, headers, body = haberdasher_service.call(rack_env)
147
147
 
148
- assert_equal 404, status
148
+ assert_equal 400, status
149
149
  assert_equal 'application/json', headers['Content-Type']
150
150
  assert_equal({
151
- "code" => 'bad_route',
151
+ "code" => 'malformed',
152
152
  "msg" => 'Invalid request body for rpc method "MakeHat" with Content-Type=application/json: ' +
153
153
  "Error occurred during parsing: Parse error at 'bad json'",
154
154
  "meta" => {"twirp_invalid_route" => "POST /example.Haberdasher/MakeHat"},
@@ -160,10 +160,10 @@ class ServiceTest < Minitest::Test
160
160
  method: "POST", input: 'bad protobuf', "CONTENT_TYPE" => "application/protobuf"
161
161
  status, headers, body = haberdasher_service.call(rack_env)
162
162
 
163
- assert_equal 404, status
163
+ assert_equal 400, status
164
164
  assert_equal 'application/json', headers['Content-Type']
165
165
  assert_equal({
166
- "code" => 'bad_route',
166
+ "code" => 'malformed',
167
167
  "msg" => 'Invalid request body for rpc method "MakeHat" with Content-Type=application/protobuf: ' +
168
168
  'Error occurred during parsing: Unexpected EOF inside skipped data',
169
169
  "meta" => {"twirp_invalid_route" => "POST /example.Haberdasher/MakeHat"},
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.required_ruby_version = '>= 1.9'
22
22
  spec.add_runtime_dependency 'google-protobuf', '~> 3.0', '>= 3.0.0'
23
- spec.add_runtime_dependency 'faraday', '~> 0' # for clients
23
+ spec.add_runtime_dependency 'faraday', '< 2' # for clients
24
24
 
25
25
  spec.add_development_dependency 'bundler', '~> 1'
26
26
  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.1
4
+ version: 1.5.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: 2019-12-31 00:00:00.000000000 Z
12
+ date: 2020-04-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: google-protobuf
@@ -35,16 +35,16 @@ dependencies:
35
35
  name: faraday
36
36
  requirement: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - "<"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '2'
41
41
  type: :runtime
42
42
  prerelease: false
43
43
  version_requirements: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - "<"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '2'
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: bundler
50
50
  requirement: !ruby/object:Gem::Requirement