excon 0.5.6 → 0.5.7

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of excon might be problematic. Click here for more details.

data/Gemfile CHANGED
@@ -6,7 +6,7 @@ group :benchmark do
6
6
  gem 'em-http-request'
7
7
  gem 'httparty'
8
8
  gem 'rest-client'
9
- gem 'tach', '0.0.8'
9
+ gem 'tach', :path => '~/geemus/tach'
10
10
  gem 'typhoeus'
11
11
  gem 'sinatra'
12
12
  gem 'streamly_ffi'
@@ -52,11 +52,15 @@ require 'rest_client'
52
52
  require 'tach'
53
53
  require 'typhoeus'
54
54
 
55
- url = 'http://localhost:9292/data/1000'
55
+ size = 10_000
56
+ path = '/data/' << size.to_s
57
+ url = 'http://localhost:9292' << path
58
+
59
+ times = 1_000
56
60
 
57
61
  with_server do
58
62
 
59
- Tach.meter(1000) do
63
+ Tach.meter(times) do
60
64
 
61
65
  tach('curb (persistent)') do |n|
62
66
  curb = Curl::Easy.new
@@ -104,13 +108,13 @@ with_server do
104
108
  end
105
109
 
106
110
  tach('Net::HTTP') do
107
- # Net::HTTP.get('localhost', '/data/1000', 9292)
108
- Net::HTTP.start('localhost', 9292) {|http| http.get('/data/1000').body }
111
+ # Net::HTTP.get('localhost', path, 9292)
112
+ Net::HTTP.start('localhost', 9292) {|http| http.get(path).body }
109
113
  end
110
114
 
111
115
  Net::HTTP.start('localhost', 9292) do |http|
112
116
  tach('Net::HTTP (persistent)') do
113
- http.get('/data/1000').body
117
+ http.get(path).body
114
118
  end
115
119
  end
116
120
 
@@ -158,4 +162,4 @@ end
158
162
  # | open-uri | 2.987051 |
159
163
  # +--------------------------+----------+
160
164
  # | em-http-request | 4.123798 |
161
- # +--------------------------+----------+
165
+ # +--------------------------+----------+
@@ -1,3 +1,13 @@
1
+ 0.5.7 03/21/11
2
+ ==============
3
+
4
+ * lazily connect, rather than connecting at #initialize
5
+ * add rough first pass at stubbing
6
+ * minor optimizations
7
+ * ssl client certification support. thanks thommay
8
+ * skip figuring out/setting Content-Length if one is supplied. Thanks pweldon
9
+ * do not try to parse body for 205 and 304. Thanks seancribbs
10
+
1
11
  0.5.6 02/19/11
2
12
  ==============
3
13
 
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'excon'
16
- s.version = '0.5.6'
17
- s.date = '2011-02-19'
16
+ s.version = '0.5.7'
17
+ s.date = '2011-03-21'
18
18
  s.rubyforge_project = 'excon'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
@@ -64,7 +64,6 @@ Gem::Specification.new do |s|
64
64
  # = MANIFEST =
65
65
  s.files = %w[
66
66
  Gemfile
67
- Gemfile.lock
68
67
  README.rdoc
69
68
  Rakefile
70
69
  benchmarks/class_vs_lambda.rb
@@ -92,6 +91,7 @@ Gem::Specification.new do |s|
92
91
  tests/basic_tests.rb
93
92
  tests/rackups/basic.ru
94
93
  tests/rackups/thread_safety.ru
94
+ tests/stub_tests.rb
95
95
  tests/test_helper.rb
96
96
  tests/thread_safety_tests.rb
97
97
  ]
@@ -13,7 +13,7 @@ require 'excon/response'
13
13
 
14
14
  module Excon
15
15
  unless const_defined?(:VERSION)
16
- VERSION = '0.5.6'
16
+ VERSION = '0.5.7'
17
17
  end
18
18
 
19
19
  unless const_defined?(:CHUNK_SIZE)
@@ -28,7 +28,6 @@ module Excon
28
28
  }.merge!(params)
29
29
 
30
30
  @socket_key = '' << @connection[:host] << ':' << @connection[:port].to_s
31
- reset
32
31
  end
33
32
 
34
33
  # Sends the supplied request to the destination host.
@@ -53,6 +52,16 @@ module Excon
53
52
  params[:path].insert(0, '/')
54
53
  end
55
54
 
55
+ unless stubs.empty?
56
+ for stub, response in stubs
57
+ # all specified non-headers params match and no headers were specified or all specified headers match
58
+ if [stub.keys - [:headers]].all? {|key| stub[key] == params[key] } &&
59
+ (!stub.has_key?(:headers) || stub[:headers].keys.all? {|key| stub[:headers][key] == params[:headers][key]})
60
+ return Excon::Response.new(response)
61
+ end
62
+ end
63
+ end
64
+
56
65
  # start with "METHOD /path"
57
66
  request = params[:method].to_s.upcase << ' ' << params[:path]
58
67
 
@@ -63,15 +72,8 @@ module Excon
63
72
  when Hash
64
73
  request << '?'
65
74
  for key, values in params[:query]
66
- case values
67
- when nil
68
- request << key.to_s << '&'
69
- when Array
70
- values.each do |value|
71
- request << key.to_s << '=' << CGI.escape(value.to_s) << '&'
72
- end
73
- else
74
- request << key.to_s << '=' << CGI.escape(values.to_s) << '&'
75
+ for value in [*values]
76
+ request << key.to_s << '=' << CGI.escape(value.to_s) << '&'
75
77
  end
76
78
  end
77
79
  request.chop! # remove trailing '&'
@@ -81,15 +83,19 @@ module Excon
81
83
  request << HTTP_1_1
82
84
 
83
85
  # calculate content length and set to handle non-ascii
84
- params[:headers]['Content-Length'] = case params[:body]
85
- when File
86
- params[:body].binmode
87
- File.size(params[:body])
88
- when String
89
- params[:body].force_encoding('BINARY') if FORCE_ENC
90
- params[:body].length
91
- else
92
- 0
86
+ unless params[:headers].has_key?('Content-Length')
87
+ params[:headers]['Content-Length'] = case params[:body]
88
+ when File
89
+ params[:body].binmode
90
+ File.size(params[:body])
91
+ when String
92
+ if FORCE_ENC
93
+ params[:body].force_encoding('BINARY')
94
+ end
95
+ params[:body].length
96
+ else
97
+ 0
98
+ end
93
99
  end
94
100
 
95
101
  # add headers to request
@@ -137,9 +143,7 @@ module Excon
137
143
  end
138
144
 
139
145
  rescue => request_error
140
- if params[:idempotent] &&
141
- (request_error.is_a?(Excon::Errors::SocketError) ||
142
- (request_error.is_a?(Excon::Errors::HTTPStatusError) && response.status != 404))
146
+ if params[:idempotent] && [Excon::Errors::SocketError, Excon::Errors::HTTPStatusError].include?(request_error)
143
147
  retries_remaining ||= 4
144
148
  retries_remaining -= 1
145
149
  if retries_remaining > 0
@@ -156,6 +160,16 @@ module Excon
156
160
  (old_socket = sockets.delete(@socket_key)) && old_socket.close
157
161
  end
158
162
 
163
+ def stub(request_params, response_params)
164
+ stub = [request_params, response_params]
165
+ stubs << stub
166
+ stub
167
+ end
168
+
169
+ def stubs
170
+ @stubs ||= []
171
+ end
172
+
159
173
  private
160
174
  def connect
161
175
  new_socket = TCPSocket.open(@connection[:host], @connection[:port])
@@ -181,6 +195,11 @@ module Excon
181
195
  ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
182
196
  end
183
197
 
198
+ if @connection.has_key?(:client_cert) && @connection.has_key?(:client_key)
199
+ ssl_context.cert = OpenSSL::X509::Certificate.new(File.read(@connection[:client_cert]))
200
+ ssl_context.key = OpenSSL::PKey::RSA.new(File.read(@connection[:client_key]))
201
+ end
202
+
184
203
  # open ssl socket
185
204
  new_socket = OpenSSL::SSL::SSLSocket.new(new_socket, ssl_context)
186
205
  new_socket.sync_close = true
@@ -193,14 +212,7 @@ module Excon
193
212
  new_socket
194
213
  end
195
214
 
196
- def closed?
197
- sockets.has_key?(@socket_key) && sockets[@socket_key].closed?
198
- end
199
-
200
215
  def socket
201
- if closed?
202
- reset
203
- end
204
216
  sockets[@socket_key] ||= connect
205
217
  end
206
218
 
@@ -1,5 +1,7 @@
1
1
  module Excon
2
2
  class Response
3
+ NO_ENTITY = [204, 205, 304].freeze
4
+
3
5
  attr_accessor :body, :headers, :status
4
6
 
5
7
  def initialize(attrs={})
@@ -24,7 +26,7 @@ module Excon
24
26
  end
25
27
  end
26
28
 
27
- unless (params[:method].to_s.casecmp('HEAD') == 0) || response.status == 204
29
+ unless (params[:method].to_s.casecmp('HEAD') == 0) || NO_ENTITY.include?(response.status)
28
30
 
29
31
  # don't pass stuff into a block if there was an error
30
32
  if params[:expects] && ![*params[:expects]].include?(response.status)
@@ -5,7 +5,7 @@ with_rackup('basic.ru') do
5
5
  tests('GET /content-length/100') do
6
6
 
7
7
  connection = Excon.new('http://127.0.0.1:9292')
8
- response = connection.request(:method => 'GET', :path => '/content-length/100')
8
+ response = connection.request(:method => :get, :path => '/content-length/100')
9
9
 
10
10
  tests('response.status').returns(200) do
11
11
  response.status
@@ -23,11 +23,11 @@ with_rackup('basic.ru') do
23
23
  response.headers['Content-Type']
24
24
  end
25
25
 
26
- tests("Time.parse(response.headers['Date']).is_a?(Time)") do
26
+ test("Time.parse(response.headers['Date']).is_a?(Time)") do
27
27
  Time.parse(response.headers['Date']).is_a?(Time)
28
28
  end
29
29
 
30
- tests("!!(response.headers['Server'] =~ /^WEBrick/)") do
30
+ test("!!(response.headers['Server'] =~ /^WEBrick/)") do
31
31
  !!(response.headers['Server'] =~ /^WEBrick/)
32
32
  end
33
33
 
@@ -0,0 +1,21 @@
1
+ Shindo.tests('Excon stubs') do
2
+ tests("stub({:method => :get}, {:body => 'body', :status => 200})") do
3
+
4
+ connection = Excon.new('http://127.0.0.1:9292')
5
+ connection.stub({:method => :get}, {:body => 'body', :status => 200})
6
+ response = connection.request(:method => :get, :path => '/content-length/100')
7
+
8
+ tests('response.body').returns('body') do
9
+ response.body
10
+ end
11
+
12
+ tests('response.headers').returns({}) do
13
+ response.headers
14
+ end
15
+
16
+ tests('response.status').returns(200) do
17
+ response.status
18
+ end
19
+
20
+ end
21
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: excon
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 6
10
- version: 0.5.6
9
+ - 7
10
+ version: 0.5.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - geemus (Wesley Beary)
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-19 00:00:00 -08:00
18
+ date: 2011-03-21 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -86,7 +86,6 @@ extra_rdoc_files:
86
86
  - README.rdoc
87
87
  files:
88
88
  - Gemfile
89
- - Gemfile.lock
90
89
  - README.rdoc
91
90
  - Rakefile
92
91
  - benchmarks/class_vs_lambda.rb
@@ -114,6 +113,7 @@ files:
114
113
  - tests/basic_tests.rb
115
114
  - tests/rackups/basic.ru
116
115
  - tests/rackups/thread_safety.ru
116
+ - tests/stub_tests.rb
117
117
  - tests/test_helper.rb
118
118
  - tests/thread_safety_tests.rb
119
119
  has_rdoc: true
@@ -146,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  requirements: []
147
147
 
148
148
  rubyforge_project: excon
149
- rubygems_version: 1.4.2
149
+ rubygems_version: 1.4.1
150
150
  signing_key:
151
151
  specification_version: 2
152
152
  summary: speed, persistence, http(s)
@@ -1,55 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- excon (0.5.2)
5
-
6
- GEM
7
- remote: http://rubygems.org/
8
- specs:
9
- addressable (2.2.2)
10
- crack (0.1.8)
11
- curb (0.7.8)
12
- curl_ffi (0.0.8)
13
- ffi
14
- em-http-request (0.2.15)
15
- addressable (>= 2.0.0)
16
- eventmachine (>= 0.12.9)
17
- eventmachine (0.12.10)
18
- ffi (0.6.3)
19
- rake (>= 0.8.7)
20
- formatador (0.0.16)
21
- httparty (0.6.1)
22
- crack (= 0.1.8)
23
- mime-types (1.16)
24
- open4 (1.0.1)
25
- rack (1.2.1)
26
- rake (0.8.7)
27
- rest-client (1.6.1)
28
- mime-types (>= 1.16)
29
- shindo (0.2.0)
30
- formatador (>= 0.0.16)
31
- sinatra (1.1.0)
32
- rack (~> 1.1)
33
- tilt (~> 1.1)
34
- streamly_ffi (0.2.6)
35
- curl_ffi
36
- tach (0.0.8)
37
- formatador (>= 0.0.16)
38
- tilt (1.1)
39
- typhoeus (0.2.0)
40
-
41
- PLATFORMS
42
- ruby
43
-
44
- DEPENDENCIES
45
- curb
46
- em-http-request
47
- excon!
48
- httparty
49
- open4
50
- rest-client
51
- shindo (= 0.2.0)
52
- sinatra
53
- streamly_ffi
54
- tach (= 0.0.8)
55
- typhoeus