rest-client-maestro 1.7.2.maestro

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.gitignore +8 -0
  2. data/.rspec +1 -0
  3. data/.ruby-version +1 -0
  4. data/.travis.yml +8 -0
  5. data/AUTHORS +69 -0
  6. data/Gemfile +7 -0
  7. data/README.rdoc +322 -0
  8. data/Rakefile +49 -0
  9. data/bin/restclient +93 -0
  10. data/history.md +134 -0
  11. data/lib/rest-client.rb +2 -0
  12. data/lib/rest_client.rb +2 -0
  13. data/lib/restclient/abstract_response.rb +106 -0
  14. data/lib/restclient/exceptions.rb +198 -0
  15. data/lib/restclient/net_http_ext.rb +55 -0
  16. data/lib/restclient/payload.rb +242 -0
  17. data/lib/restclient/raw_response.rb +34 -0
  18. data/lib/restclient/request.rb +346 -0
  19. data/lib/restclient/resource.rb +169 -0
  20. data/lib/restclient/response.rb +26 -0
  21. data/lib/restclient.rb +174 -0
  22. data/rest-client-maestro.gemspec +23 -0
  23. data/spec/integration/capath_equifax/578d5c04.0 +19 -0
  24. data/spec/integration/capath_equifax/594f1775.0 +19 -0
  25. data/spec/integration/capath_equifax/README +8 -0
  26. data/spec/integration/capath_equifax/equifax.crt +19 -0
  27. data/spec/integration/capath_verisign/415660c1.0 +14 -0
  28. data/spec/integration/capath_verisign/7651b327.0 +14 -0
  29. data/spec/integration/capath_verisign/README +8 -0
  30. data/spec/integration/capath_verisign/verisign.crt +14 -0
  31. data/spec/integration/certs/equifax.crt +19 -0
  32. data/spec/integration/certs/verisign.crt +14 -0
  33. data/spec/integration/integration_spec.rb +35 -0
  34. data/spec/integration/request_spec.rb +63 -0
  35. data/spec/spec_helper.rb +12 -0
  36. data/spec/unit/abstract_response_spec.rb +85 -0
  37. data/spec/unit/exceptions_spec.rb +95 -0
  38. data/spec/unit/master_shake.jpg +0 -0
  39. data/spec/unit/payload_spec.rb +245 -0
  40. data/spec/unit/raw_response_spec.rb +17 -0
  41. data/spec/unit/request2_spec.rb +32 -0
  42. data/spec/unit/request_spec.rb +621 -0
  43. data/spec/unit/resource_spec.rb +133 -0
  44. data/spec/unit/response_spec.rb +166 -0
  45. data/spec/unit/restclient_spec.rb +73 -0
  46. metadata +220 -0
data/history.md ADDED
@@ -0,0 +1,134 @@
1
+ # 1.6.7
2
+
3
+ - rebuild with 1.8.7 to avoid https://github.com/rubygems/rubygems/pull/57
4
+
5
+ # 1.6.6
6
+
7
+ - 1.6.5 was yanked
8
+
9
+ # 1.6.5
10
+
11
+ - RFC6265 requires single SP after ';' for separating parameters pairs in the 'Cookie:' header (patch provided by Hiroshi Nakamura)
12
+ - enable url parameters for all actions
13
+ - detect file parameters in arrays
14
+ - allow disabling the timeouts by passing -1 (patch provided by Sven Böhm)
15
+
16
+ # 1.6.4
17
+
18
+ - fix restclient script compatibility with 1.9.2
19
+ - fix unlinking temp file (patch provided by Evan Smith)
20
+ - monkeypatching ruby for http patch method (patch provided by Syl Turner)
21
+
22
+ # 1.6.3
23
+
24
+ - 1.6.2 was yanked
25
+
26
+ # 1.6.2
27
+
28
+ - add support for HEAD in resources (patch provided by tpresa)
29
+ - fix shell for 1.9.2
30
+ - workaround when some gem monkeypatch net/http (patch provided by Ian Warshak)
31
+ - DELETE requests should process parameters just like GET and HEAD
32
+ - adding :block_response parameter for manual processing
33
+ - limit number of redirections (patch provided by Chris Dinn)
34
+ - close and unlink the temp file created by playload (patch provided by Chris Green)
35
+ - make gemspec Rubygems 1.8 compatible (patch provided by David Backeus)
36
+ - added RestClient.reset_before_execution_procs (patch provided by Cloudify)
37
+ - added PATCH method (patch provided by Jeff Remer)
38
+ - hack for HTTP servers that use raw DEFLATE compression, see http://www.ruby-forum.com/topic/136825 (path provided by James Reeves)
39
+
40
+ # 1.6.1
41
+
42
+ - add response body in Exception#inspect
43
+ - add support for RestClient.options
44
+ - fix tests for 1.9.2 (patch provided by Niko Dittmann)
45
+ - block passing in Resource#[] (patch provided by Niko Dittmann)
46
+ - cookies set in a response should be kept in a redirect
47
+ - HEAD requests should process parameters just like GET (patch provided by Rob Eanes)
48
+ - exception message should never be nil (patch provided by Michael Klett)
49
+
50
+ # 1.6.0
51
+
52
+ - forgot to include rest-client.rb in the gem
53
+ - user, password and user-defined headers should survive a redirect
54
+ - added all missing status codes
55
+ - added parameter passing for get request using the :param key in header
56
+ - the warning about the logger when using a string was a bad idea
57
+ - multipart parameters names should not be escaped
58
+ - remove the cookie escaping introduced by migrating to CGI cookie parsing in 1.5.1
59
+ - add a streamed payload type (patch provided by Caleb Land)
60
+ - Exception#http_body works even when no response
61
+
62
+ # 1.5.1
63
+
64
+ - only converts headers keys which are Symbols
65
+ - use CGI for cookie parsing instead of custom code
66
+ - unescape user and password before using them (patch provided by Lars Gierth)
67
+ - expand ~ in ~/.restclientrc (patch provided by Mike Fletcher)
68
+ - ssl verification raise an exception when the ca certificate is incorrect (patch provided by Braintree)
69
+
70
+ # 1.5.0
71
+
72
+ - the response is now a String with the Response module a.k.a. the change in 1.4.0 was a mistake (Response.body is returning self for compatability)
73
+ - added AbstractResponse.to_i to improve semantic
74
+ - multipart Payloads ignores the name attribute if it's not set (patch provided by Tekin Suleyman)
75
+ - correctly takes into account user headers whose keys are strings (path provided by Cyril Rohr)
76
+ - use binary mode for payload temp file
77
+ - concatenate cookies with ';'
78
+ - fixed deeper parameter handling
79
+ - do not quote the boundary in the Content-Type header (patch provided by W. Andrew Loe III)
80
+
81
+ # 1.4.2
82
+
83
+ - fixed RestClient.add_before_execution_proc (patch provided by Nicholas Wieland)
84
+ - fixed error when an exception is raised without a response (patch provided by Caleb Land)
85
+
86
+ # 1.4.1
87
+
88
+ - fixed parameters managment when using hash
89
+
90
+ # 1.4.0
91
+
92
+ - Response is no more a String, and the mixin is replaced by an abstract_response, existing calls are redirected to response body with a warning.
93
+ - enable repeated parameters RestClient.post 'http://example.com/resource', :param1 => ['one', 'two', 'three'], => :param2 => 'foo' (patch provided by Rodrigo Panachi)
94
+ - fixed the redirect code concerning relative path and query string combination (patch provided by Kevin Read)
95
+ - redirection code moved to Response so redirection can be customized using the block syntax
96
+ - only get and head redirections are now followed by default, as stated in the specification
97
+ - added RestClient.add_before_execution_proc to hack the http request, like for oauth
98
+
99
+ The response change may be breaking in rare cases.
100
+
101
+ # 1.3.1
102
+
103
+ - added compatibility to enable responses in exception to act like Net::HTTPResponse
104
+
105
+ # 1.3.0
106
+
107
+ - a block can be used to process a request's result, this enable to handle custom error codes or paththrought (design by Cyril Rohr)
108
+ - cleaner log API, add a warning for some cases but should be compatible
109
+ - accept multiple "Set-Cookie" headers, see http://www.ietf.org/rfc/rfc2109.txt (patch provided by Cyril Rohr)
110
+ - remove "Content-Length" and "Content-Type" headers when following a redirection (patch provided by haarts)
111
+ - all http error codes have now a corresponding exception class and all of them contain the Reponse -> this means that the raised exception can be different
112
+ - changed "Content-Disposition: multipart/form-data" to "Content-Disposition: form-data" per RFC 2388 (patch provided by Kyle Crawford)
113
+
114
+ The only breaking change should be the exception classes, but as the new classes inherits from the existing ones, the breaking cases should be rare.
115
+
116
+ # 1.2.0
117
+
118
+ - formatting changed from tabs to spaces
119
+ - logged requests now include generated headers
120
+ - accept and content-type headers can now be specified using extentions: RestClient.post "http://example.com/resource", { 'x' => 1 }.to_json, :content_type => :json, :accept => :json
121
+ - should be 1.1.1 but renamed to 1.2.0 because 1.1.X versions has already been packaged on Debian
122
+
123
+ # 1.1.0
124
+
125
+ - new maintainer: Archiloque, the working repo is now at http://github.com/archiloque/rest-client
126
+ - a mailing list has been created at rest.client@librelist.com and an freenode irc channel #rest-client
127
+ - François Beausoleil' multipart code from http://github.com/francois/rest-client has been merged
128
+ - ability to use hash in hash as payload
129
+ - the mime-type code now rely on the mime-types gem http://mime-types.rubyforge.org/ instead of an internal partial list
130
+ - 204 response returns a Response instead of nil (patch provided by Elliott Draper)
131
+
132
+ All changes exept the last one should be fully compatible with the previous version.
133
+
134
+ NOTE: due to a dependency problem and to the last change, heroku users should update their heroku gem to >= 1.5.3 to be able to use this version.
@@ -0,0 +1,2 @@
1
+ # More logical way to require 'rest-client'
2
+ require File.dirname(__FILE__) + '/restclient'
@@ -0,0 +1,2 @@
1
+ # This file exists for backward compatbility with require 'rest_client'
2
+ require File.dirname(__FILE__) + '/restclient'
@@ -0,0 +1,106 @@
1
+ require 'cgi'
2
+
3
+ module RestClient
4
+
5
+ module AbstractResponse
6
+
7
+ attr_reader :net_http_res, :args
8
+
9
+ # HTTP status code
10
+ def code
11
+ @code ||= @net_http_res.code.to_i
12
+ end
13
+
14
+ # A hash of the headers, beautified with symbols and underscores.
15
+ # e.g. "Content-type" will become :content_type.
16
+ def headers
17
+ @headers ||= AbstractResponse.beautify_headers(@net_http_res.to_hash)
18
+ end
19
+
20
+ # The raw headers.
21
+ def raw_headers
22
+ @raw_headers ||= @net_http_res.to_hash
23
+ end
24
+
25
+ # Hash of cookies extracted from response headers
26
+ def cookies
27
+ @cookies ||= (self.headers[:set_cookie] || {}).inject({}) do |out, cookie_content|
28
+ out.merge parse_cookie(cookie_content)
29
+ end
30
+ end
31
+
32
+ # Return the default behavior corresponding to the response code:
33
+ # the response itself for code in 200..206, redirection for 301, 302 and 307 in get and head cases, redirection for 303 and an exception in other cases
34
+ def return! request = nil, result = nil, & block
35
+ if (200..207).include? code
36
+ self
37
+ elsif [301, 302, 307].include? code
38
+ unless [:get, :head].include? args[:method]
39
+ raise Exceptions::EXCEPTIONS_MAP[code].new(self, code)
40
+ else
41
+ follow_redirection(request, result, & block)
42
+ end
43
+ elsif code == 303
44
+ args[:method] = :get
45
+ args.delete :payload
46
+ follow_redirection(request, result, & block)
47
+ elsif Exceptions::EXCEPTIONS_MAP[code]
48
+ raise Exceptions::EXCEPTIONS_MAP[code].new(self, code)
49
+ else
50
+ raise RequestFailed.new(self, code)
51
+ end
52
+ end
53
+
54
+ def to_i
55
+ code
56
+ end
57
+
58
+ def description
59
+ "#{code} #{STATUSES[code]} | #{(headers[:content_type] || '').gsub(/;.*$/, '')} #{size} bytes\n"
60
+ end
61
+
62
+ # Follow a redirection
63
+ def follow_redirection request = nil, result = nil, & block
64
+ url = headers[:location]
65
+ if url !~ /^http/
66
+ url = URI.parse(args[:url]).merge(url).to_s
67
+ end
68
+ args[:url] = url
69
+ if request
70
+ if request.max_redirects == 0
71
+ raise MaxRedirectsReached
72
+ end
73
+ args[:password] = request.password
74
+ args[:user] = request.user
75
+ args[:headers] = request.headers
76
+ args[:max_redirects] = request.max_redirects - 1
77
+ # pass any cookie set in the result
78
+ if result && result['set-cookie']
79
+ args[:headers][:cookies] = (args[:headers][:cookies] || {}).merge(parse_cookie(result['set-cookie']))
80
+ end
81
+ end
82
+ Request.execute args, &block
83
+ end
84
+
85
+ def AbstractResponse.beautify_headers(headers)
86
+ headers.inject({}) do |out, (key, value)|
87
+ out[key.gsub(/-/, '_').downcase.to_sym] = %w{ set-cookie }.include?(key.downcase) ? value : value.first
88
+ out
89
+ end
90
+ end
91
+
92
+ private
93
+
94
+ # Parse a cookie value and return its content in an Hash
95
+ def parse_cookie cookie_content
96
+ out = {}
97
+ CGI::Cookie::parse(cookie_content).each do |key, cookie|
98
+ unless ['expires', 'path'].include? key
99
+ out[CGI::escape(key)] = cookie.value[0] ? (CGI::escape(cookie.value[0]) || '') : ''
100
+ end
101
+ end
102
+ out
103
+ end
104
+ end
105
+
106
+ end
@@ -0,0 +1,198 @@
1
+ module RestClient
2
+
3
+ STATUSES = {100 => 'Continue',
4
+ 101 => 'Switching Protocols',
5
+ 102 => 'Processing', #WebDAV
6
+
7
+ 200 => 'OK',
8
+ 201 => 'Created',
9
+ 202 => 'Accepted',
10
+ 203 => 'Non-Authoritative Information', # http/1.1
11
+ 204 => 'No Content',
12
+ 205 => 'Reset Content',
13
+ 206 => 'Partial Content',
14
+ 207 => 'Multi-Status', #WebDAV
15
+
16
+ 300 => 'Multiple Choices',
17
+ 301 => 'Moved Permanently',
18
+ 302 => 'Found',
19
+ 303 => 'See Other', # http/1.1
20
+ 304 => 'Not Modified',
21
+ 305 => 'Use Proxy', # http/1.1
22
+ 306 => 'Switch Proxy', # no longer used
23
+ 307 => 'Temporary Redirect', # http/1.1
24
+
25
+ 400 => 'Bad Request',
26
+ 401 => 'Unauthorized',
27
+ 402 => 'Payment Required',
28
+ 403 => 'Forbidden',
29
+ 404 => 'Resource Not Found',
30
+ 405 => 'Method Not Allowed',
31
+ 406 => 'Not Acceptable',
32
+ 407 => 'Proxy Authentication Required',
33
+ 408 => 'Request Timeout',
34
+ 409 => 'Conflict',
35
+ 410 => 'Gone',
36
+ 411 => 'Length Required',
37
+ 412 => 'Precondition Failed',
38
+ 413 => 'Request Entity Too Large',
39
+ 414 => 'Request-URI Too Long',
40
+ 415 => 'Unsupported Media Type',
41
+ 416 => 'Requested Range Not Satisfiable',
42
+ 417 => 'Expectation Failed',
43
+ 418 => 'I\'m A Teapot',
44
+ 421 => 'Too Many Connections From This IP',
45
+ 422 => 'Unprocessable Entity', #WebDAV
46
+ 423 => 'Locked', #WebDAV
47
+ 424 => 'Failed Dependency', #WebDAV
48
+ 425 => 'Unordered Collection', #WebDAV
49
+ 426 => 'Upgrade Required',
50
+ 449 => 'Retry With', #Microsoft
51
+ 450 => 'Blocked By Windows Parental Controls', #Microsoft
52
+
53
+ 500 => 'Internal Server Error',
54
+ 501 => 'Not Implemented',
55
+ 502 => 'Bad Gateway',
56
+ 503 => 'Service Unavailable',
57
+ 504 => 'Gateway Timeout',
58
+ 505 => 'HTTP Version Not Supported',
59
+ 506 => 'Variant Also Negotiates',
60
+ 507 => 'Insufficient Storage', #WebDAV
61
+ 509 => 'Bandwidth Limit Exceeded', #Apache
62
+ 510 => 'Not Extended'}
63
+
64
+ # Compatibility : make the Response act like a Net::HTTPResponse when needed
65
+ module ResponseForException
66
+ def method_missing symbol, *args
67
+ if net_http_res.respond_to? symbol
68
+ warn "[warning] The response contained in an RestClient::Exception is now a RestClient::Response instead of a Net::HTTPResponse, please update your code"
69
+ net_http_res.send symbol, *args
70
+ else
71
+ super
72
+ end
73
+ end
74
+ end
75
+
76
+ # This is the base RestClient exception class. Rescue it if you want to
77
+ # catch any exception that your request might raise
78
+ # You can get the status code by e.http_code, or see anything about the
79
+ # response via e.response.
80
+ # For example, the entire result body (which is
81
+ # probably an HTML error page) is e.response.
82
+ class Exception < RuntimeError
83
+ attr_accessor :response
84
+ attr_writer :message
85
+
86
+ def initialize response = nil, initial_response_code = nil
87
+ @response = response
88
+ @message = nil
89
+ @initial_response_code = initial_response_code
90
+
91
+ # compatibility: this make the exception behave like a Net::HTTPResponse
92
+ response.extend ResponseForException if response
93
+ end
94
+
95
+ def http_code
96
+ # return integer for compatibility
97
+ if @response
98
+ @response.code.to_i
99
+ else
100
+ @initial_response_code
101
+ end
102
+ end
103
+
104
+ def http_body
105
+ @response.body if @response
106
+ end
107
+
108
+ def inspect
109
+ "#{message}: #{http_body}"
110
+ end
111
+
112
+ def to_s
113
+ inspect
114
+ end
115
+
116
+ def message
117
+ @message || self.class.name
118
+ end
119
+
120
+ end
121
+
122
+ # Compatibility
123
+ class ExceptionWithResponse < Exception
124
+ end
125
+
126
+ # The request failed with an error code not managed by the code
127
+ class RequestFailed < ExceptionWithResponse
128
+
129
+ def message
130
+ "HTTP status code #{http_code}"
131
+ end
132
+
133
+ def to_s
134
+ message
135
+ end
136
+ end
137
+
138
+ # We will a create an exception for each status code, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
139
+ module Exceptions
140
+ # Map http status codes to the corresponding exception class
141
+ EXCEPTIONS_MAP = {}
142
+ end
143
+
144
+ STATUSES.each_pair do |code, message|
145
+
146
+ # Compatibility
147
+ superclass = ([304, 401, 404].include? code) ? ExceptionWithResponse : RequestFailed
148
+ klass = Class.new(superclass) do
149
+ send(:define_method, :message) {"#{http_code ? "#{http_code} " : ''}#{message}"}
150
+ end
151
+ klass_constant = const_set message.delete(' \-\''), klass
152
+ Exceptions::EXCEPTIONS_MAP[code] = klass_constant
153
+ end
154
+
155
+ # A redirect was encountered; caught by execute to retry with the new url.
156
+ class Redirect < Exception
157
+
158
+ def message
159
+ 'Redirect'
160
+ end
161
+
162
+ attr_accessor :url
163
+
164
+ def initialize(url)
165
+ @url = url
166
+ end
167
+ end
168
+
169
+ class MaxRedirectsReached < Exception
170
+ def message
171
+ 'Maximum number of redirect reached'
172
+ end
173
+ end
174
+
175
+ # The server broke the connection prior to the request completing. Usually
176
+ # this means it crashed, or sometimes that your network connection was
177
+ # severed before it could complete.
178
+ class ServerBrokeConnection < Exception
179
+ def initialize(message = 'Server broke connection')
180
+ super nil, nil
181
+ self.message = message
182
+ end
183
+ end
184
+
185
+ class SSLCertificateNotVerified < Exception
186
+ def initialize(message)
187
+ super nil, nil
188
+ self.message = message
189
+ end
190
+ end
191
+ end
192
+
193
+ # backwards compatibility
194
+ class RestClient::Request
195
+ Redirect = RestClient::Redirect
196
+ Unauthorized = RestClient::Unauthorized
197
+ RequestFailed = RestClient::RequestFailed
198
+ end
@@ -0,0 +1,55 @@
1
+ module Net
2
+ class HTTP
3
+
4
+ # Adding the patch method if it doesn't exist (rest-client issue: https://github.com/archiloque/rest-client/issues/79)
5
+ if !defined?(Net::HTTP::Patch)
6
+ # Code taken from this commit: https://github.com/ruby/ruby/commit/ab70e53ac3b5102d4ecbe8f38d4f76afad29d37d#lib/net/http.rb
7
+ class Protocol
8
+ # Sends a PATCH request to the +path+ and gets a response,
9
+ # as an HTTPResponse object.
10
+ def patch(path, data, initheader = nil, dest = nil, &block) # :yield: +body_segment+
11
+ send_entity(path, data, initheader, dest, Patch, &block)
12
+ end
13
+
14
+ # Executes a request which uses a representation
15
+ # and returns its body.
16
+ def send_entity(path, data, initheader, dest, type, &block)
17
+ res = nil
18
+ request(type.new(path, initheader), data) {|r|
19
+ r.read_body dest, &block
20
+ res = r
21
+ }
22
+ unless @newimpl
23
+ res.value
24
+ return res, res.body
25
+ end
26
+ res
27
+ end
28
+ end
29
+
30
+ class Patch < HTTPRequest
31
+ METHOD = 'PATCH'
32
+ REQUEST_HAS_BODY = true
33
+ RESPONSE_HAS_BODY = true
34
+ end
35
+ end
36
+
37
+ #
38
+ # Replace the request method in Net::HTTP to sniff the body type
39
+ # and set the stream if appropriate
40
+ #
41
+ # Taken from:
42
+ # http://www.missiondata.com/blog/ruby/29/streaming-data-to-s3-with-ruby/
43
+
44
+ alias __request__ request
45
+
46
+ def request(req, body=nil, &block)
47
+ if body != nil && body.respond_to?(:read)
48
+ req.body_stream = body
49
+ return __request__(req, nil, &block)
50
+ else
51
+ return __request__(req, body, &block)
52
+ end
53
+ end
54
+ end
55
+ end