rest-client 1.7.3 → 1.8.0.rc1

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

Potentially problematic release.


This version of rest-client might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8807a06ea9680179c83ab3cc8e93c13b517c04a4
4
- data.tar.gz: bdaa911803deedb6cc50870ecc438f98fbd6ba8f
3
+ metadata.gz: 419720d1763901902f6efe24e29884563f8cc6c8
4
+ data.tar.gz: 70b0abae33d0974fe015314b892759d40d882eea
5
5
  SHA512:
6
- metadata.gz: 40873fc7011e525bcec7460323c7256397783232d16f01eea880a45c6f7ad4c454edb0e28ac4150a76101f9ab48ca62ea63fb740297168760b75743bf565d896
7
- data.tar.gz: 88be594b35a8c7eca659b553fd79c09ee6aec9411f1eaa176c9bf1bba95822c7283fb1cba78c5c9528ad97d7581475ee59d9dccd93cc06f5c2d1a53c4a8f3ecb
6
+ metadata.gz: 8974511fde1bd1c91ac376c402eb74bc4934c067036877a88804e375a9a2ee837ee66f78ec5d4b9518e7aae8cda198e4ae04ebc1a6fbb44b32036d70b3d4ba33
7
+ data.tar.gz: cd594fe5b477132e1430c7bfd96fadbd44c0cf379c048dec4e7de8eee283da86baab278678e32e3d9c16d54a733ba37d056b72025063e4cb558bd03b739cac50
@@ -1,10 +1,11 @@
1
1
  require 'cgi'
2
+ require 'http-cookie'
2
3
 
3
4
  module RestClient
4
5
 
5
6
  module AbstractResponse
6
7
 
7
- attr_reader :net_http_res, :args
8
+ attr_reader :net_http_res, :args, :request
8
9
 
9
10
  # HTTP status code
10
11
  def code
@@ -22,11 +23,36 @@ module RestClient
22
23
  @raw_headers ||= @net_http_res.to_hash
23
24
  end
24
25
 
26
+ def response_set_vars(net_http_res, args, request)
27
+ @net_http_res = net_http_res
28
+ @args = args
29
+ @request = request
30
+ end
31
+
25
32
  # Hash of cookies extracted from response headers
26
33
  def cookies
27
- @cookies ||= (self.headers[:set_cookie] || {}).inject({}) do |out, cookie_content|
28
- out.merge parse_cookie(cookie_content)
34
+ hash = {}
35
+
36
+ cookie_jar.cookies.each do |cookie|
37
+ hash[cookie.name] = cookie.value
29
38
  end
39
+
40
+ hash
41
+ end
42
+
43
+ # Cookie jar extracted from response headers.
44
+ #
45
+ # @return [HTTP::CookieJar]
46
+ #
47
+ def cookie_jar
48
+ return @cookie_jar if @cookie_jar
49
+
50
+ jar = HTTP::CookieJar.new
51
+ headers.fetch(:set_cookie, []).each do |cookie|
52
+ jar.parse(cookie, @request.url)
53
+ end
54
+
55
+ @cookie_jar = jar
30
56
  end
31
57
 
32
58
  # Return the default behavior corresponding to the response code:
@@ -61,25 +87,28 @@ module RestClient
61
87
 
62
88
  # Follow a redirection
63
89
  def follow_redirection request = nil, result = nil, & block
90
+ new_args = @args.dup
91
+
64
92
  url = headers[:location]
65
93
  if url !~ /^http/
66
- url = URI.parse(args[:url]).merge(url).to_s
94
+ url = URI.parse(request.url).merge(url).to_s
67
95
  end
68
- args[:url] = url
96
+ new_args[:url] = url
69
97
  if request
70
98
  if request.max_redirects == 0
71
99
  raise MaxRedirectsReached
72
100
  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
101
+ new_args[:password] = request.password
102
+ new_args[:user] = request.user
103
+ new_args[:headers] = request.headers
104
+ new_args[:max_redirects] = request.max_redirects - 1
105
+
106
+ # TODO: figure out what to do with original :cookie, :cookies values
107
+ new_args[:headers]['Cookie'] = HTTP::Cookie.cookie_value(
108
+ cookie_jar.cookies(new_args.fetch(:url)))
81
109
  end
82
- Request.execute args, &block
110
+
111
+ Request.execute(new_args, &block)
83
112
  end
84
113
 
85
114
  def self.beautify_headers(headers)
@@ -13,12 +13,13 @@ module RestClient
13
13
 
14
14
  include AbstractResponse
15
15
 
16
- attr_reader :file
16
+ attr_reader :file, :request
17
17
 
18
- def initialize tempfile, net_http_res, args
18
+ def initialize(tempfile, net_http_res, args, request)
19
19
  @net_http_res = net_http_res
20
20
  @args = args
21
21
  @file = tempfile
22
+ @request = request
22
23
  end
23
24
 
24
25
  def to_s
@@ -484,9 +484,9 @@ module RestClient
484
484
  def process_result res, & block
485
485
  if @raw_response
486
486
  # We don't decode raw requests
487
- response = RawResponse.new(@tf, res, args)
487
+ response = RawResponse.new(@tf, res, args, self)
488
488
  else
489
- response = Response.create(Request.decode(res['content-encoding'], res.body), res, args)
489
+ response = Response.create(Request.decode(res['content-encoding'], res.body), res, args, self)
490
490
  end
491
491
 
492
492
  if block_given?
@@ -6,17 +6,14 @@ module RestClient
6
6
 
7
7
  include AbstractResponse
8
8
 
9
- attr_accessor :args, :net_http_res
10
-
11
9
  def body
12
10
  self
13
11
  end
14
12
 
15
- def self.create body, net_http_res, args
13
+ def self.create body, net_http_res, args, request
16
14
  result = body || ''
17
15
  result.extend Response
18
- result.net_http_res = net_http_res
19
- result.args = args
16
+ result.response_set_vars(net_http_res, args, request)
20
17
  result
21
18
  end
22
19
 
@@ -1,5 +1,5 @@
1
1
  module RestClient
2
- VERSION = '1.7.3' unless defined?(self::VERSION)
2
+ VERSION = '1.8.0.rc1' unless defined?(self::VERSION)
3
3
 
4
4
  def self.version
5
5
  VERSION
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency('pry-doc')
23
23
  s.add_development_dependency('rdoc', '>= 2.4.2', '< 5.0')
24
24
 
25
+ s.add_dependency('http-cookie', '>= 1.0.2', '< 2.0')
25
26
  s.add_dependency('mime-types', '>= 1.16', '< 3.0')
26
27
  s.add_dependency('netrc', '~> 0.7')
27
28
 
@@ -8,16 +8,18 @@ describe RestClient::AbstractResponse do
8
8
 
9
9
  attr_accessor :size
10
10
 
11
- def initialize net_http_res, args
11
+ def initialize net_http_res, args, request
12
12
  @net_http_res = net_http_res
13
13
  @args = args
14
+ @request = request
14
15
  end
15
16
 
16
17
  end
17
18
 
18
19
  before do
19
20
  @net_http_res = double('net http response')
20
- @response = MyAbstractResponse.new(@net_http_res, {})
21
+ @request = double('restclient request', :url => 'http://example.com')
22
+ @response = MyAbstractResponse.new(@net_http_res, {}, @request)
21
23
  end
22
24
 
23
25
  it "fetches the numeric response code" do
@@ -53,7 +55,8 @@ describe RestClient::AbstractResponse do
53
55
 
54
56
  it "extract strange cookies" do
55
57
  @net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/'])
56
- @response.cookies.should eq({ 'session_id' => 'ZJ%2FHQVH6YE+rVkTpn0zvTQ%3D%3D' })
58
+ @response.headers.should eq({:set_cookie => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/']})
59
+ @response.cookies.should eq({ 'session_id' => 'ZJ/HQVH6YE+rVkTpn0zvTQ==' })
57
60
  end
58
61
 
59
62
  it "doesn't escape cookies" do
@@ -4,7 +4,8 @@ describe RestClient::RawResponse do
4
4
  before do
5
5
  @tf = double("Tempfile", :read => "the answer is 42", :open => true)
6
6
  @net_http_res = double('net http response')
7
- @response = RestClient::RawResponse.new(@tf, @net_http_res, {})
7
+ @request = double('http request')
8
+ @response = RestClient::RawResponse.new(@tf, @net_http_res, {}, @request)
8
9
  end
9
10
 
10
11
  it "behaves like string" do
@@ -3,8 +3,9 @@ require 'spec_helper'
3
3
  describe RestClient::Response do
4
4
  before do
5
5
  @net_http_res = double('net http response', :to_hash => {"Status" => ["200 OK"]}, :code => 200)
6
- @request = double('http request', :user => nil, :password => nil)
7
- @response = RestClient::Response.create('abc', @net_http_res, {})
6
+ @example_url = 'http://example.com'
7
+ @request = double('http request', :user => nil, :password => nil, :url => @example_url)
8
+ @response = RestClient::Response.create('abc', @net_http_res, {}, @request)
8
9
  end
9
10
 
10
11
  it "behaves like string" do
@@ -14,7 +15,7 @@ describe RestClient::Response do
14
15
  end
15
16
 
16
17
  it "accepts nil strings and sets it to empty for the case of HEAD" do
17
- RestClient::Response.create(nil, @net_http_res, {}).to_s.should eq ""
18
+ RestClient::Response.create(nil, @net_http_res, {}, @request).to_s.should eq ""
18
19
  end
19
20
 
20
21
  it "test headers and raw headers" do
@@ -24,16 +25,18 @@ describe RestClient::Response do
24
25
 
25
26
  describe "cookie processing" do
26
27
  it "should correctly deal with one Set-Cookie header with one cookie inside" do
27
- net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"]})
28
- response = RestClient::Response.create('abc', net_http_res, {})
29
- response.headers[:set_cookie].should eq ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"]
28
+ header_val = "main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT".freeze
29
+
30
+ net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => [header_val]})
31
+ response = RestClient::Response.create('abc', net_http_res, {}, @request)
32
+ response.headers[:set_cookie].should eq [header_val]
30
33
  response.cookies.should eq({ "main_page" => "main_page_no_rewrite" })
31
34
  end
32
35
 
33
36
  it "should correctly deal with multiple cookies [multiple Set-Cookie headers]" do
34
- net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT", "remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT", "user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]})
35
- response = RestClient::Response.create('abc', net_http_res, {})
36
- response.headers[:set_cookie].should eq ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT", "remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT", "user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]
37
+ net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT", "remember_me=; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT", "user=somebody; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT"]})
38
+ response = RestClient::Response.create('abc', net_http_res, {}, @request)
39
+ response.headers[:set_cookie].should eq ["main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT", "remember_me=; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT", "user=somebody; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT"]
37
40
  response.cookies.should eq({
38
41
  "main_page" => "main_page_no_rewrite",
39
42
  "remember_me" => "",
@@ -42,8 +45,8 @@ describe RestClient::Response do
42
45
  end
43
46
 
44
47
  it "should correctly deal with multiple cookies [one Set-Cookie header with multiple cookies]" do
45
- net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT, remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT, user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]})
46
- response = RestClient::Response.create('abc', net_http_res, {})
48
+ net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT, remember_me=; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT, user=somebody; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT"]})
49
+ response = RestClient::Response.create('abc', net_http_res, {}, @request)
47
50
  response.cookies.should eq({
48
51
  "main_page" => "main_page_no_rewrite",
49
52
  "remember_me" => "",
@@ -56,7 +59,7 @@ describe RestClient::Response do
56
59
  it "should return itself for normal codes" do
57
60
  (200..206).each do |code|
58
61
  net_http_res = double('net http response', :code => '200')
59
- response = RestClient::Response.create('abc', net_http_res, {})
62
+ response = RestClient::Response.create('abc', net_http_res, {}, @request)
60
63
  response.return! @request
61
64
  end
62
65
  end
@@ -65,7 +68,7 @@ describe RestClient::Response do
65
68
  RestClient::Exceptions::EXCEPTIONS_MAP.each_key do |code|
66
69
  unless (200..207).include? code
67
70
  net_http_res = double('net http response', :code => code.to_i)
68
- response = RestClient::Response.create('abc', net_http_res, {})
71
+ response = RestClient::Response.create('abc', net_http_res, {}, @request)
69
72
  lambda { response.return!}.should raise_error
70
73
  end
71
74
  end
@@ -88,32 +91,38 @@ describe RestClient::Response do
88
91
  end
89
92
 
90
93
  it "follows a redirection and keep the cookies" do
94
+ stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => 'Foo=Bar', 'Location' => 'http://some/new_resource', })
95
+ stub_request(:get, 'http://some/new_resource').with(:headers => {'Cookie' => 'Foo=Bar'}).to_return(:body => 'Qux')
96
+ RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Qux'
97
+ end
98
+
99
+ it 'does not keep cookies across domains' do
91
100
  stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => 'Foo=Bar', 'Location' => 'http://new/resource', })
92
- stub_request(:get, 'http://new/resource').with(:headers => {'Cookie' => 'Foo=Bar'}).to_return(:body => 'Qux')
101
+ stub_request(:get, 'http://new/resource').with(:headers => {'Cookie' => ''}).to_return(:body => 'Qux')
93
102
  RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Qux'
94
103
  end
95
104
 
96
105
  it "doesn't follow a 301 when the request is a post" do
97
106
  net_http_res = double('net http response', :code => 301)
98
- response = RestClient::Response.create('abc', net_http_res, {:method => :post})
107
+ response = RestClient::Response.create('abc', net_http_res, {:method => :post}, @request)
99
108
  lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
100
109
  end
101
110
 
102
111
  it "doesn't follow a 302 when the request is a post" do
103
112
  net_http_res = double('net http response', :code => 302)
104
- response = RestClient::Response.create('abc', net_http_res, {:method => :post})
113
+ response = RestClient::Response.create('abc', net_http_res, {:method => :post}, @request)
105
114
  lambda { response.return!(@request)}.should raise_error(RestClient::Found)
106
115
  end
107
116
 
108
117
  it "doesn't follow a 307 when the request is a post" do
109
118
  net_http_res = double('net http response', :code => 307)
110
- response = RestClient::Response.create('abc', net_http_res, {:method => :post})
119
+ response = RestClient::Response.create('abc', net_http_res, {:method => :post}, @request)
111
120
  lambda { response.return!(@request)}.should raise_error(RestClient::TemporaryRedirect)
112
121
  end
113
122
 
114
123
  it "doesn't follow a redirection when the request is a put" do
115
124
  net_http_res = double('net http response', :code => 301)
116
- response = RestClient::Response.create('abc', net_http_res, {:method => :put})
125
+ response = RestClient::Response.create('abc', net_http_res, {:method => :put}, @request)
117
126
  lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
118
127
  end
119
128
 
@@ -71,9 +71,9 @@ describe RestClient do
71
71
  end
72
72
 
73
73
  describe 'version' do
74
- it 'has a version ~> 1.7.0.alpha' do
74
+ it 'has a version ~> 1.8.0.alpha' do
75
75
  ver = Gem::Version.new(RestClient.version)
76
- Gem::Requirement.new('~> 1.7.0.alpha').should be_satisfied_by(ver)
76
+ Gem::Requirement.new('~> 1.8.0.alpha').should be_satisfied_by(ver)
77
77
  end
78
78
  end
79
79
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.3
4
+ version: 1.8.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - REST Client Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-20 00:00:00.000000000 Z
11
+ date: 2015-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: webmock
@@ -86,6 +86,26 @@ dependencies:
86
86
  - - "<"
87
87
  - !ruby/object:Gem::Version
88
88
  version: '5.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: http-cookie
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: 1.0.2
96
+ - - "<"
97
+ - !ruby/object:Gem::Version
98
+ version: '2.0'
99
+ type: :runtime
100
+ prerelease: false
101
+ version_requirements: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: 1.0.2
106
+ - - "<"
107
+ - !ruby/object:Gem::Version
108
+ version: '2.0'
89
109
  - !ruby/object:Gem::Dependency
90
110
  name: mime-types
91
111
  requirement: !ruby/object:Gem::Requirement
@@ -195,9 +215,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
195
215
  version: 1.9.2
196
216
  required_rubygems_version: !ruby/object:Gem::Requirement
197
217
  requirements:
198
- - - ">="
218
+ - - ">"
199
219
  - !ruby/object:Gem::Version
200
- version: '0'
220
+ version: 1.3.1
201
221
  requirements: []
202
222
  rubyforge_project:
203
223
  rubygems_version: 2.2.2