11
require 'excon/constants'
12
require 'excon/connection'
13
require 'excon/errors'
14
require 'excon/response'
15
require 'excon/socket'
16
require 'excon/ssl_socket'
12
# Define defaults first so they will be available to other files
21
16
# @return [Hash] defaults for Excon connections
19
:chunk_size => CHUNK_SIZE || DEFAULT_CHUNK_SIZE,
24
20
:connect_timeout => 60,
26
23
:instrumentor_name => 'excon',
25
Excon::Middleware::Expects,
26
Excon::Middleware::Idempotent,
27
Excon::Middleware::Instrumentor,
28
Excon::Middleware::Mock
31
:nonblock => DEFAULT_NONBLOCK,
28
32
:read_timeout => 60,
29
33
:retry_limit => DEFAULT_RETRY_LIMIT,
30
34
:ssl_ca_file => DEFAULT_CA_FILE,
31
35
:ssl_verify_peer => RbConfig::CONFIG['host_os'] !~ /mswin|win32|dos|cygwin|mingw/i,
32
37
:write_timeout => 60
39
44
@defaults = new_defaults
50
require 'excon/constants'
51
require 'excon/connection'
52
require 'excon/errors'
53
require 'excon/middlewares/base'
54
require 'excon/middlewares/expects'
55
require 'excon/middlewares/idempotent'
56
require 'excon/middlewares/instrumentor'
57
require 'excon/middlewares/mock'
58
require 'excon/response'
59
require 'excon/socket'
60
require 'excon/ssl_socket'
61
require 'excon/standard_instrumentor'
42
66
# Status of mocking
44
puts("Excon#mock is deprecated, pass Excon.defaults[:mock] instead (#{caller.first})")
68
$stderr.puts("Excon#mock is deprecated, pass Excon.defaults[:mock] instead (#{caller.first})")
45
69
self.defaults[:mock]
49
73
# false is the default and works as expected
50
74
# true returns a value from stubs or raises
51
75
def mock=(new_mock)
52
puts("Excon#mock is deprecated, pass Excon.defaults[:mock]= instead (#{caller.first})")
76
$stderr.puts("Excon#mock is deprecated, pass Excon.defaults[:mock]= instead (#{caller.first})")
53
77
self.defaults[:mock] = new_mock
56
80
# @return [String] The filesystem path to the SSL Certificate Authority
58
puts("Excon#ssl_ca_path is deprecated, use Excon.defaults[:ssl_ca_path] instead (#{caller.first})")
82
$stderr.puts("Excon#ssl_ca_path is deprecated, use Excon.defaults[:ssl_ca_path] instead (#{caller.first})")
59
83
self.defaults[:ssl_ca_path]
62
86
# Change path to the SSL Certificate Authority
63
87
# @return [String] The filesystem path to the SSL Certificate Authority
64
88
def ssl_ca_path=(new_ssl_ca_path)
65
puts("Excon#ssl_ca_path= is deprecated, use Excon.defaults[:ssl_ca_path]= instead (#{caller.first})")
89
$stderr.puts("Excon#ssl_ca_path= is deprecated, use Excon.defaults[:ssl_ca_path]= instead (#{caller.first})")
66
90
self.defaults[:ssl_ca_path] = new_ssl_ca_path
69
93
# @return [true, false] Whether or not to verify the peer's SSL certificate / chain
70
94
def ssl_verify_peer
71
puts("Excon#ssl_verify_peer= is deprecated, use Excon.defaults[:ssl_verify_peer]= instead (#{caller.first})")
95
$stderr.puts("Excon#ssl_verify_peer= is deprecated, use Excon.defaults[:ssl_verify_peer]= instead (#{caller.first})")
72
96
self.defaults[:ssl_verify_peer]
75
99
# Change the status of ssl peer verification
76
100
# @see Excon#ssl_verify_peer (attr_reader)
77
101
def ssl_verify_peer=(new_ssl_verify_peer)
78
puts("Excon#ssl_verify_peer is deprecated, use Excon.defaults[:ssl_verify_peer] instead (#{caller.first})")
102
$stderr.puts("Excon#ssl_verify_peer is deprecated, use Excon.defaults[:ssl_verify_peer] instead (#{caller.first})")
79
103
self.defaults[:ssl_verify_peer] = new_ssl_verify_peer
85
109
# @param [Hash<Symbol, >] params One or more option params to set on the Connection instance
86
110
# @return [Connection] A new Excon::Connection instance
87
111
def new(url, params = {})
88
Excon::Connection.new(url, params)
112
uri_parser = params[:uri_parser] || Excon.defaults[:uri_parser]
113
uri = uri_parser.parse(url)
117
:port => uri.port.to_s,
119
:scheme => uri.scheme,
120
:user => (URI.decode(uri.user) if uri.user),
121
:password => (URI.decode(uri.password) if uri.password),
123
Excon::Connection.new(params)
91
126
# push an additional stub onto the list to check for mock requests
92
127
# @param [Hash<Symbol, >] request params to match against, omitted params match all
93
128
# @param [Hash<Symbol, >] response params to return from matched request or block to call with params
94
def stub(request_params, response_params = nil)
129
def stub(request_params = {}, response_params = nil)
130
if url = request_params.delete(:url)
132
request_params.update(
135
:port => uri.port.to_s,
137
:scheme => uri.scheme
139
if uri.user || uri.password
140
request_params[:headers] ||= {}
141
user, pass = URI.decode_www_form_component(uri.user.to_s), URI.decode_www_form_component(uri.password.to_s)
142
request_params[:headers]['Authorization'] ||= 'Basic ' << ['' << user << ':' << pass].pack('m').delete(Excon::CR_NL)
96
146
if response_params
97
147
raise(ArgumentError.new("stub requires either response_params OR a block"))
104
154
raise(ArgumentError.new("stub requires either response_params OR a block"))
160
# get a stub matching params or nil
161
def stub_for(request_params={})
162
Excon.stubs.each do |stub, response|
163
captures = { :headers => {} }
164
headers_match = !stub.has_key?(:headers) || stub[:headers].keys.all? do |key|
165
case value = stub[:headers][key]
167
if match = value.match(request_params[:headers][key])
168
captures[:headers][key] = match.captures
172
value == request_params[:headers][key]
175
non_headers_match = (stub.keys - [:headers]).all? do |key|
176
case value = stub[key]
178
if match = value.match(request_params[key])
179
captures[key] = match.captures
183
value == request_params[key]
186
if headers_match && non_headers_match
187
request_params[:captures] = captures
110
194
# get a list of defined stubs
115
199
# Generic non-persistent HTTP methods
116
200
HTTP_VERBS.each do |method|
201
module_eval <<-DEF, __FILE__, __LINE__ + 1
118
202
def #{method}(url, params = {}, &block)
119
new(url).request(params.merge!(:method => :#{method}), &block)
203
new(url, params).request(:method => :#{method}, &block)