sinew 2.0.3 → 2.0.4

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: e40f156ce6a8c625a93dd1cf0f9d60db825977cb352bf736f27b24afbc6bd182
4
- data.tar.gz: f4392ccdc53b3cc5d8a07a26fe0aef3525c0e43c3b570ab96465a208ab8c1969
3
+ metadata.gz: 7e7426a91f427a3c97969eb501ce0bd55a658ece54af0dfea994f8faffd479f7
4
+ data.tar.gz: baeb12b6af0fa2c5c11390de16ed1837458f52b4b00393dce8f9e66d4eb898a3
5
5
  SHA512:
6
- metadata.gz: aa8dbb122c0ef5665822d08bb62293798873529107609718399307a0a7f31682283f7a0576b9628a4e29475031b999ebd70e07b49ab212b4b5e4002470f568dc
7
- data.tar.gz: 9693d9fcd356e0c9e7732c186e18c228a4953117f07b57617e298a0e728e8bf8c31334de56d1ee99b43f8003fb7de0cbf219656d20f58ad68e345dc876cef229
6
+ metadata.gz: 1698acfc26dbab92c390cde0956a72011f22f8f9bb4c5ebb194d131ae0a8dfe6c58e2224d36e840167210c2bf472efd18fd8e0cc92b1c9640df590f1faf71473
7
+ data.tar.gz: e9a2688616dd866792cd286a1ebff094fe32ad9d8552ce77e7465648022fd65db83149ee558f88140f907d7a2da422c41312e8a32f73f0d1930f865862eab90f
@@ -10,7 +10,7 @@ module Sinew
10
10
  # this is used to break out of --limit
11
11
  class LimitError < StandardError; end
12
12
 
13
- attr_reader :sinew, :raw, :uri, :elapsed
13
+ attr_reader :sinew, :uri, :raw, :code, :elapsed
14
14
 
15
15
  def initialize(sinew)
16
16
  @sinew = sinew
@@ -59,7 +59,7 @@ module Sinew
59
59
 
60
60
  # fetch and make response available to callers
61
61
  response = sinew.http(method, url, options)
62
- @uri, @raw = response.uri, response.body
62
+ @uri, @raw, @code = response.uri, response.body, response.code
63
63
 
64
64
  # don't confuse the user
65
65
  nil
@@ -75,8 +75,8 @@ module Sinew
75
75
  begin
76
76
  @request_count += 1
77
77
  response = request.perform
78
- rescue Timeout::Error
79
- response = Response.from_timeout(request)
78
+ rescue HTTParty::RedirectionTooDeep, OpenSSL::SSL::SSLError, SocketError, SystemCallError, Timeout::Error => e
79
+ response = Response.from_error(request, e)
80
80
  end
81
81
  break if !response.error_500?
82
82
  end
@@ -73,16 +73,26 @@ module Sinew
73
73
  body&.dup
74
74
  end
75
75
 
76
- # build key, as a hash for before_generate_cache_key
76
+ # Build key, as a hash for before_generate_cache_key. Note that :scheme is
77
+ # just a placeholder in case someone wants to add it for real, so that
78
+ # it'll appear in the correct order. We remove the placerholder after we
79
+ # call the proc.
77
80
  key = {
78
81
  method: method.dup,
82
+ scheme: 'placeholder',
79
83
  path: uri.path,
80
84
  query: uri.query,
81
85
  body: body_key,
82
86
  }
83
- key = sinew.runtime_options.before_generate_cache_key.call(key)
84
87
 
85
- # strip method for gets
88
+ args = [ key ]
89
+ if sinew.runtime_options.before_generate_cache_key.arity == 2
90
+ args << uri
91
+ end
92
+ key = sinew.runtime_options.before_generate_cache_key.call(*args)
93
+
94
+ # strip defaults
95
+ key.delete(:scheme) if key[:scheme] == 'placeholder'
86
96
  key.delete(:method) if key[:method] == 'get'
87
97
 
88
98
  # pull out the values, join and pathify
@@ -46,11 +46,11 @@ module Sinew
46
46
  end
47
47
  end
48
48
 
49
- def self.from_timeout(request)
49
+ def self.from_error(request, error)
50
50
  Response.new.tap do |response|
51
51
  response.request = request
52
52
  response.uri = request.uri
53
- response.body = 'timeout'
53
+ response.body = error.to_s
54
54
  response.code = 999
55
55
  response.headers = {}
56
56
  end
@@ -1,4 +1,4 @@
1
1
  module Sinew
2
2
  # Gem version
3
- VERSION = '2.0.3'.freeze
3
+ VERSION = '2.0.4'.freeze
4
4
  end
@@ -64,6 +64,6 @@ class TestCache < MiniTest::Test
64
64
  end
65
65
  end
66
66
  assert_requested :get, 'http://httpbin.org/delay/1', times: 1
67
- assert_equal 'timeout', sinew.dsl.raw
67
+ assert_equal 'execution expired', sinew.dsl.raw
68
68
  end
69
69
  end
@@ -2,6 +2,9 @@ require 'minitest/autorun'
2
2
  require 'minitest/pride'
3
3
  require 'webmock/minitest' unless ENV['SINEW_TEST_NETWORK']
4
4
 
5
+ # to run one test, do this:
6
+ # TEST=test/unit/query_test.rb TESTOPTS="--name=test_parse" rake
7
+
5
8
  # a hint to sinew, so that it'll do things like set rate limit to zero
6
9
  ENV['SINEW_TEST'] = '1'
7
10
 
@@ -8,6 +8,7 @@ class TestRequests < MiniTest::Test
8
8
 
9
9
  def test_basic_methods
10
10
  sinew.dsl.get('http://httpbin.org/get', a: 1, b: 2)
11
+ assert_equal(200, sinew.dsl.code)
11
12
  assert_equal({ a: '1', b: '2' }, sinew.dsl.json[:args])
12
13
 
13
14
  sinew.dsl.post('http://httpbin.org/post', a: 1, b: 2)
@@ -38,13 +39,29 @@ class TestRequests < MiniTest::Test
38
39
  # 500
39
40
  assert_output(/failed with 500/) do
40
41
  sinew.dsl.get('http://httpbin.org/status/500')
42
+ assert_equal 500, sinew.dsl.code
41
43
  assert_equal '500', sinew.dsl.raw
42
44
  end
43
45
 
44
46
  # timeout
45
47
  assert_output(/failed with 999/) do
46
48
  sinew.dsl.get('http://httpbin.org/delay/1')
47
- assert_equal 'timeout', sinew.dsl.raw
49
+ assert_equal 999, sinew.dsl.code
50
+ end
51
+
52
+ # uncommon errors
53
+ errors = [
54
+ Errno::ECONNREFUSED,
55
+ HTTParty::RedirectionTooDeep.new(nil),
56
+ OpenSSL::SSL::SSLError.new,
57
+ SocketError.new,
58
+ ]
59
+ errors.each_with_index do |error, index|
60
+ stub_request(:get, %r{http://[^/]+/error#{index}}).to_return { raise error }
61
+ assert_output(/failed with 999/) do
62
+ sinew.dsl.get("http://httpbin.org/error#{index}")
63
+ assert_equal 999, sinew.dsl.code
64
+ end
48
65
  end
49
66
  end
50
67
 
@@ -104,9 +121,15 @@ class TestRequests < MiniTest::Test
104
121
  end
105
122
 
106
123
  def test_before_generate_cache_key
124
+ # arity 1
107
125
  sinew.runtime_options.before_generate_cache_key = method(:redact_cache_key)
108
126
  req = Sinew::Request.new(sinew, 'get', 'http://host', query: { secret: 'xyz' })
109
127
  assert_equal 'host/secret=redacted', req.cache_key
128
+
129
+ # arity 2
130
+ sinew.runtime_options.before_generate_cache_key = method(:add_scheme)
131
+ req = Sinew::Request.new(sinew, 'get', 'https://host/gub')
132
+ assert_equal 'host/https,gub', req.cache_key
110
133
  end
111
134
 
112
135
  def test_urls
@@ -132,4 +155,10 @@ class TestRequests < MiniTest::Test
132
155
  key
133
156
  end
134
157
  protected :redact_cache_key
158
+
159
+ def add_scheme(key, uri)
160
+ key[:scheme] = uri.scheme
161
+ key
162
+ end
163
+ protected :add_scheme
135
164
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinew
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Doppelt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-15 00:00:00.000000000 Z
11
+ date: 2018-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print