rest-client 2.0.2-x64-mingw32 → 2.1.0.rc1-x64-mingw32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.mailmap +10 -0
- data/.rubocop +2 -0
- data/.rubocop-disables.yml +27 -24
- data/.rubocop.yml +5 -0
- data/.travis.yml +2 -1
- data/AUTHORS +8 -0
- data/README.md +119 -7
- data/Rakefile +12 -4
- data/history.md +33 -0
- data/lib/restclient.rb +0 -1
- data/lib/restclient/abstract_response.rb +28 -2
- data/lib/restclient/exceptions.rb +3 -3
- data/lib/restclient/payload.rb +28 -3
- data/lib/restclient/raw_response.rb +17 -6
- data/lib/restclient/request.rb +89 -67
- data/lib/restclient/resource.rb +16 -6
- data/lib/restclient/response.rb +14 -4
- data/lib/restclient/utils.rb +47 -8
- data/lib/restclient/version.rb +2 -2
- data/rest-client.gemspec +1 -0
- data/spec/ISS.jpg +0 -0
- data/spec/helpers.rb +37 -5
- data/spec/integration/httpbin_spec.rb +41 -0
- data/spec/integration/integration_spec.rb +0 -7
- data/spec/unit/abstract_response_spec.rb +7 -7
- data/spec/unit/payload_spec.rb +51 -19
- data/spec/unit/raw_response_spec.rb +6 -2
- data/spec/unit/request2_spec.rb +8 -8
- data/spec/unit/request_spec.rb +51 -63
- data/spec/unit/resource_spec.rb +7 -7
- data/spec/unit/response_spec.rb +33 -22
- data/spec/unit/restclient_spec.rb +3 -2
- data/spec/unit/utils_spec.rb +10 -10
- metadata +29 -7
- data/spec/unit/master_shake.jpg +0 -0
@@ -2,9 +2,9 @@ require_relative '_lib'
|
|
2
2
|
|
3
3
|
describe RestClient::RawResponse do
|
4
4
|
before do
|
5
|
-
@tf = double("Tempfile", :read => "the answer is 42", :open => true)
|
5
|
+
@tf = double("Tempfile", :read => "the answer is 42", :open => true, :rewind => true)
|
6
6
|
@net_http_res = double('net http response')
|
7
|
-
@request = double('
|
7
|
+
@request = double('restclient request', :redirection_history => nil)
|
8
8
|
@response = RestClient::RawResponse.new(@tf, @net_http_res, @request)
|
9
9
|
end
|
10
10
|
|
@@ -15,4 +15,8 @@ describe RestClient::RawResponse do
|
|
15
15
|
it "exposes a Tempfile" do
|
16
16
|
expect(@response.file).to eq @tf
|
17
17
|
end
|
18
|
+
|
19
|
+
it "includes AbstractResponse" do
|
20
|
+
expect(RestClient::RawResponse.ancestors).to include(RestClient::AbstractResponse)
|
21
|
+
end
|
18
22
|
end
|
data/spec/unit/request2_spec.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
require_relative '_lib'
|
2
2
|
|
3
|
-
describe RestClient::Request do
|
3
|
+
describe RestClient::Request, :include_helpers do
|
4
4
|
|
5
5
|
context 'params for GET requests' do
|
6
6
|
it "manage params for get requests" do
|
7
|
-
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', '
|
7
|
+
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
|
8
8
|
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}).body).to eq 'foo'
|
9
9
|
|
10
|
-
stub_request(:get, 'http://some/resource').with(:headers => {'Accept'=>'*/*', '
|
10
|
+
stub_request(:get, 'http://some/resource').with(:headers => {'Accept'=>'*/*', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
|
11
11
|
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => :a}).body).to eq 'foo'
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'adds GET params when params are present in URL' do
|
15
|
-
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', '
|
15
|
+
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
|
16
16
|
expect(RestClient::Request.execute(:url => 'http://some/resource?a=b', :method => :get, :headers => {:foo => :bar, :params => {:c => 'd'}}).body).to eq 'foo'
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'encodes nested GET params' do
|
20
|
-
stub_request(:get, 'http://some/resource?a[foo][]=1&a[foo][]=2&a[bar]&b=foo+bar&math=2+%2B+2+%3D%3D+4').with(:headers => {'Accept'=>'*/*',
|
20
|
+
stub_request(:get, 'http://some/resource?a[foo][]=1&a[foo][]=2&a[bar]&b=foo+bar&math=2+%2B+2+%3D%3D+4').with(:headers => {'Accept'=>'*/*',}).to_return(:body => 'foo', :status => 200)
|
21
21
|
expect(RestClient::Request.execute(url: 'http://some/resource', method: :get, headers: {
|
22
22
|
params: {
|
23
23
|
a: {
|
@@ -37,15 +37,15 @@ describe RestClient::Request do
|
|
37
37
|
block = proc do |http_response|
|
38
38
|
response_value = http_response.body
|
39
39
|
end
|
40
|
-
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', '
|
40
|
+
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
|
41
41
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}, :block_response => block)
|
42
42
|
expect(response_value).to eq "foo"
|
43
43
|
end
|
44
44
|
|
45
45
|
it 'closes payload if not nil' do
|
46
|
-
test_file = File.new(
|
46
|
+
test_file = File.new(test_image_path)
|
47
47
|
|
48
|
-
stub_request(:post, 'http://some/resource').with(:headers => {'Accept'=>'*/*'
|
48
|
+
stub_request(:post, 'http://some/resource').with(:headers => {'Accept'=>'*/*'}).to_return(:body => 'foo', :status => 200)
|
49
49
|
RestClient::Request.execute(:url => 'http://some/resource', :method => :post, :payload => {:file => test_file})
|
50
50
|
|
51
51
|
expect(test_file.closed?).to be true
|
data/spec/unit/request_spec.rb
CHANGED
@@ -27,46 +27,22 @@ describe RestClient::Request, :include_helpers do
|
|
27
27
|
expect(@request.default_headers[:accept]).to eq '*/*'
|
28
28
|
end
|
29
29
|
|
30
|
-
describe "compression" do
|
31
|
-
|
32
|
-
it "decodes an uncompressed result body by passing it straight through" do
|
33
|
-
expect(RestClient::Request.decode(nil, 'xyz')).to eq 'xyz'
|
34
|
-
end
|
35
|
-
|
36
|
-
it "doesn't fail for nil bodies" do
|
37
|
-
expect(RestClient::Request.decode('gzip', nil)).to be_nil
|
38
|
-
end
|
39
|
-
|
40
|
-
|
41
|
-
it "decodes a gzip body" do
|
42
|
-
expect(RestClient::Request.decode('gzip', "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000")).to eq "i'm gziped\n"
|
43
|
-
end
|
44
|
-
|
45
|
-
it "ingores gzip for empty bodies" do
|
46
|
-
expect(RestClient::Request.decode('gzip', '')).to be_empty
|
47
|
-
end
|
48
|
-
|
49
|
-
it "decodes a deflated body" do
|
50
|
-
expect(RestClient::Request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363")).to eq "some deflated text"
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
30
|
it "processes a successful result" do
|
55
|
-
res =
|
31
|
+
res = res_double
|
56
32
|
allow(res).to receive(:code).and_return("200")
|
57
33
|
allow(res).to receive(:body).and_return('body')
|
58
34
|
allow(res).to receive(:[]).with('content-encoding').and_return(nil)
|
59
|
-
expect(@request.send(:process_result, res).body).to eq 'body'
|
60
|
-
expect(@request.send(:process_result, res).to_s).to eq 'body'
|
35
|
+
expect(@request.send(:process_result, res, Time.now).body).to eq 'body'
|
36
|
+
expect(@request.send(:process_result, res, Time.now).to_s).to eq 'body'
|
61
37
|
end
|
62
38
|
|
63
39
|
it "doesn't classify successful requests as failed" do
|
64
40
|
203.upto(207) do |code|
|
65
|
-
res =
|
41
|
+
res = res_double
|
66
42
|
allow(res).to receive(:code).and_return(code.to_s)
|
67
43
|
allow(res).to receive(:body).and_return("")
|
68
44
|
allow(res).to receive(:[]).with('content-encoding').and_return(nil)
|
69
|
-
expect(@request.send(:process_result, res)).to be_empty
|
45
|
+
expect(@request.send(:process_result, res, Time.now)).to be_empty
|
70
46
|
end
|
71
47
|
end
|
72
48
|
|
@@ -315,7 +291,7 @@ describe RestClient::Request, :include_helpers do
|
|
315
291
|
|
316
292
|
describe "user headers" do
|
317
293
|
it "merges user headers with the default headers" do
|
318
|
-
expect(@request).to receive(:default_headers).and_return({
|
294
|
+
expect(@request).to receive(:default_headers).and_return({:accept => '*/*'})
|
319
295
|
headers = @request.make_headers("Accept" => "application/json", :accept_encoding => 'gzip')
|
320
296
|
expect(headers).to have_key "Accept-Encoding"
|
321
297
|
expect(headers["Accept-Encoding"]).to eq "gzip"
|
@@ -472,12 +448,13 @@ describe RestClient::Request, :include_helpers do
|
|
472
448
|
end
|
473
449
|
|
474
450
|
it "does not attempt to send credentials if Authorization header is set" do
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
451
|
+
['Authorization', 'authorization', 'auTHORization', :authorization].each do |authorization|
|
452
|
+
headers = {authorization => 'Token abc123'}
|
453
|
+
request = RestClient::Request.new(method: :get, url: 'http://some/resource', headers: headers, user: 'joe', password: 'mypass')
|
454
|
+
req = double("net::http request")
|
455
|
+
expect(req).not_to receive(:basic_auth)
|
456
|
+
request.send(:setup_credentials, req)
|
457
|
+
end
|
481
458
|
end
|
482
459
|
end
|
483
460
|
|
@@ -535,25 +512,25 @@ describe RestClient::Request, :include_helpers do
|
|
535
512
|
|
536
513
|
describe "exception" do
|
537
514
|
it "raises Unauthorized when the response is 401" do
|
538
|
-
res =
|
539
|
-
expect { @request.send(:process_result, res) }.to raise_error(RestClient::Unauthorized)
|
515
|
+
res = res_double(:code => '401', :[] => ['content-encoding' => ''], :body => '' )
|
516
|
+
expect { @request.send(:process_result, res, Time.now) }.to raise_error(RestClient::Unauthorized)
|
540
517
|
end
|
541
518
|
|
542
519
|
it "raises ResourceNotFound when the response is 404" do
|
543
|
-
res =
|
544
|
-
expect { @request.send(:process_result, res) }.to raise_error(RestClient::ResourceNotFound)
|
520
|
+
res = res_double(:code => '404', :[] => ['content-encoding' => ''], :body => '' )
|
521
|
+
expect { @request.send(:process_result, res, Time.now) }.to raise_error(RestClient::ResourceNotFound)
|
545
522
|
end
|
546
523
|
|
547
524
|
it "raises RequestFailed otherwise" do
|
548
|
-
res =
|
549
|
-
expect { @request.send(:process_result, res) }.to raise_error(RestClient::InternalServerError)
|
525
|
+
res = res_double(:code => '500', :[] => ['content-encoding' => ''], :body => '' )
|
526
|
+
expect { @request.send(:process_result, res, Time.now) }.to raise_error(RestClient::InternalServerError)
|
550
527
|
end
|
551
528
|
end
|
552
529
|
|
553
530
|
describe "block usage" do
|
554
531
|
it "returns what asked to" do
|
555
|
-
res =
|
556
|
-
expect(@request.send(:process_result, res){|response, request| "foo"}).to eq "foo"
|
532
|
+
res = res_double(:code => '401', :[] => ['content-encoding' => ''], :body => '' )
|
533
|
+
expect(@request.send(:process_result, res, Time.now){|response, request| "foo"}).to eq "foo"
|
557
534
|
end
|
558
535
|
end
|
559
536
|
|
@@ -644,64 +621,74 @@ describe RestClient::Request, :include_helpers do
|
|
644
621
|
it "logs a get request" do
|
645
622
|
log = RestClient.log = []
|
646
623
|
RestClient::Request.new(:method => :get, :url => 'http://url', :headers => {:user_agent => 'rest-client'}).log_request
|
647
|
-
expect(log[0]).to eq %Q{RestClient.get "http://url", "Accept"=>"*/*", "
|
624
|
+
expect(log[0]).to eq %Q{RestClient.get "http://url", "Accept"=>"*/*", "User-Agent"=>"rest-client"\n}
|
648
625
|
end
|
649
626
|
|
650
627
|
it "logs a post request with a small payload" do
|
651
628
|
log = RestClient.log = []
|
652
629
|
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo', :headers => {:user_agent => 'rest-client'}).log_request
|
653
|
-
expect(log[0]).to eq %Q{RestClient.post "http://url", "foo", "Accept"=>"*/*", "
|
630
|
+
expect(log[0]).to eq %Q{RestClient.post "http://url", "foo", "Accept"=>"*/*", "Content-Length"=>"3", "User-Agent"=>"rest-client"\n}
|
654
631
|
end
|
655
632
|
|
656
633
|
it "logs a post request with a large payload" do
|
657
634
|
log = RestClient.log = []
|
658
635
|
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000), :headers => {:user_agent => 'rest-client'}).log_request
|
659
|
-
expect(log[0]).to eq %Q{RestClient.post "http://url", 1000 byte(s) length, "Accept"=>"*/*", "
|
636
|
+
expect(log[0]).to eq %Q{RestClient.post "http://url", 1000 byte(s) length, "Accept"=>"*/*", "Content-Length"=>"1000", "User-Agent"=>"rest-client"\n}
|
660
637
|
end
|
661
638
|
|
662
639
|
it "logs input headers as a hash" do
|
663
640
|
log = RestClient.log = []
|
664
641
|
RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain', :user_agent => 'rest-client' }).log_request
|
665
|
-
expect(log[0]).to eq %Q{RestClient.get "http://url", "Accept"=>"text/plain", "
|
642
|
+
expect(log[0]).to eq %Q{RestClient.get "http://url", "Accept"=>"text/plain", "User-Agent"=>"rest-client"\n}
|
666
643
|
end
|
667
644
|
|
668
645
|
it "logs a response including the status code, content type, and result body size in bytes" do
|
669
646
|
log = RestClient.log = []
|
670
|
-
res =
|
647
|
+
res = res_double(code: '200', class: Net::HTTPOK, body: 'abcd')
|
671
648
|
allow(res).to receive(:[]).with('Content-type').and_return('text/html')
|
672
|
-
@request
|
673
|
-
|
649
|
+
response = response_from_res_double(res, @request)
|
650
|
+
response.log_response
|
651
|
+
expect(log).to eq ["# => 200 OK | text/html 4 bytes, 1.00s\n"]
|
674
652
|
end
|
675
653
|
|
676
654
|
it "logs a response with a nil Content-type" do
|
677
655
|
log = RestClient.log = []
|
678
|
-
res =
|
656
|
+
res = res_double(code: '200', class: Net::HTTPOK, body: 'abcd')
|
679
657
|
allow(res).to receive(:[]).with('Content-type').and_return(nil)
|
680
|
-
@request
|
681
|
-
|
658
|
+
response = response_from_res_double(res, @request)
|
659
|
+
response.log_response
|
660
|
+
expect(log).to eq ["# => 200 OK | 4 bytes, 1.00s\n"]
|
682
661
|
end
|
683
662
|
|
684
663
|
it "logs a response with a nil body" do
|
685
664
|
log = RestClient.log = []
|
686
|
-
res =
|
665
|
+
res = res_double(code: '200', class: Net::HTTPOK, body: nil)
|
687
666
|
allow(res).to receive(:[]).with('Content-type').and_return('text/html; charset=utf-8')
|
688
|
-
@request
|
689
|
-
|
667
|
+
response = response_from_res_double(res, @request)
|
668
|
+
response.log_response
|
669
|
+
expect(log).to eq ["# => 200 OK | text/html 0 bytes, 1.00s\n"]
|
690
670
|
end
|
691
671
|
|
692
672
|
it 'does not log request password' do
|
693
673
|
log = RestClient.log = []
|
694
674
|
RestClient::Request.new(:method => :get, :url => 'http://user:password@url', :headers => {:user_agent => 'rest-client'}).log_request
|
695
|
-
expect(log[0]).to eq %Q{RestClient.get "http://user:REDACTED@url", "Accept"=>"*/*", "
|
675
|
+
expect(log[0]).to eq %Q{RestClient.get "http://user:REDACTED@url", "Accept"=>"*/*", "User-Agent"=>"rest-client"\n}
|
676
|
+
end
|
677
|
+
|
678
|
+
it 'logs to a passed logger, if provided' do
|
679
|
+
logger = double('logger', '<<' => true)
|
680
|
+
expect(logger).to receive(:<<)
|
681
|
+
RestClient::Request.new(:method => :get, :url => 'http://user:password@url', log: logger).log_request
|
696
682
|
end
|
697
683
|
end
|
698
684
|
|
699
685
|
it "strips the charset from the response content type" do
|
700
686
|
log = RestClient.log = []
|
701
|
-
res =
|
687
|
+
res = res_double(code: '200', class: Net::HTTPOK, body: 'abcd')
|
702
688
|
allow(res).to receive(:[]).with('Content-type').and_return('text/html; charset=utf-8')
|
703
|
-
@request
|
704
|
-
|
689
|
+
response = response_from_res_double(res, @request)
|
690
|
+
response.log_response
|
691
|
+
expect(log).to eq ["# => 200 OK | text/html 4 bytes, 1.00s\n"]
|
705
692
|
end
|
706
693
|
|
707
694
|
describe "timeout" do
|
@@ -1149,7 +1136,7 @@ describe RestClient::Request, :include_helpers do
|
|
1149
1136
|
)
|
1150
1137
|
net_http_res = Net::HTTPNoContent.new("", "204", "No Content")
|
1151
1138
|
allow(net_http_res).to receive(:read_body).and_return(nil)
|
1152
|
-
expect(@http).to receive(:request).and_return(
|
1139
|
+
expect(@http).to receive(:request).and_return(net_http_res)
|
1153
1140
|
response = @request.send(:transmit, @uri, 'req', 'payload')
|
1154
1141
|
expect(response).not_to be_nil
|
1155
1142
|
expect(response.code).to eq 204
|
@@ -1167,7 +1154,8 @@ describe RestClient::Request, :include_helpers do
|
|
1167
1154
|
|
1168
1155
|
net_http_res = Net::HTTPOK.new(nil, "200", "body")
|
1169
1156
|
allow(net_http_res).to receive(:read_body).and_return("body")
|
1170
|
-
@request.send(:
|
1157
|
+
received_tempfile = @request.send(:fetch_body_to_tempfile, net_http_res)
|
1158
|
+
expect(received_tempfile).to eq tempfile
|
1171
1159
|
end
|
1172
1160
|
end
|
1173
1161
|
|
data/spec/unit/resource_spec.rb
CHANGED
@@ -7,37 +7,37 @@ describe RestClient::Resource do
|
|
7
7
|
|
8
8
|
context "Resource delegation" do
|
9
9
|
it "GET" do
|
10
|
-
expect(RestClient::Request).to receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
10
|
+
expect(RestClient::Request).to receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass', :log => nil)
|
11
11
|
@resource.get
|
12
12
|
end
|
13
13
|
|
14
14
|
it "HEAD" do
|
15
|
-
expect(RestClient::Request).to receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
15
|
+
expect(RestClient::Request).to receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass', :log => nil)
|
16
16
|
@resource.head
|
17
17
|
end
|
18
18
|
|
19
19
|
it "POST" do
|
20
|
-
expect(RestClient::Request).to receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
20
|
+
expect(RestClient::Request).to receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass', :log => nil)
|
21
21
|
@resource.post 'abc', :content_type => 'image/jpg'
|
22
22
|
end
|
23
23
|
|
24
24
|
it "PUT" do
|
25
|
-
expect(RestClient::Request).to receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
25
|
+
expect(RestClient::Request).to receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass', :log => nil)
|
26
26
|
@resource.put 'abc', :content_type => 'image/jpg'
|
27
27
|
end
|
28
28
|
|
29
29
|
it "PATCH" do
|
30
|
-
expect(RestClient::Request).to receive(:execute).with(:method => :patch, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
30
|
+
expect(RestClient::Request).to receive(:execute).with(:method => :patch, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass', :log => nil)
|
31
31
|
@resource.patch 'abc', :content_type => 'image/jpg'
|
32
32
|
end
|
33
33
|
|
34
34
|
it "DELETE" do
|
35
|
-
expect(RestClient::Request).to receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
35
|
+
expect(RestClient::Request).to receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass', :log => nil)
|
36
36
|
@resource.delete
|
37
37
|
end
|
38
38
|
|
39
39
|
it "overrides resource headers" do
|
40
|
-
expect(RestClient::Request).to receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '2'}, :user => 'jane', :password => 'mypass')
|
40
|
+
expect(RestClient::Request).to receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '2'}, :user => 'jane', :password => 'mypass', :log => nil)
|
41
41
|
@resource.get 'X-Something' => '2'
|
42
42
|
end
|
43
43
|
end
|
data/spec/unit/response_spec.rb
CHANGED
@@ -2,10 +2,10 @@ require_relative '_lib'
|
|
2
2
|
|
3
3
|
describe RestClient::Response, :include_helpers do
|
4
4
|
before do
|
5
|
-
@net_http_res =
|
5
|
+
@net_http_res = res_double(to_hash: {'Status' => ['200 OK']}, code: '200', body: 'abc')
|
6
6
|
@example_url = 'http://example.com'
|
7
7
|
@request = request_double(url: @example_url, method: 'get')
|
8
|
-
@response =
|
8
|
+
@response = response_from_res_double(@net_http_res, @request, duration: 1)
|
9
9
|
end
|
10
10
|
|
11
11
|
it "behaves like string" do
|
@@ -17,6 +17,7 @@ describe RestClient::Response, :include_helpers do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
it "accepts nil strings and sets it to empty for the case of HEAD" do
|
20
|
+
# TODO
|
20
21
|
expect(RestClient::Response.create(nil, @net_http_res, @request).to_s).to eq ""
|
21
22
|
end
|
22
23
|
|
@@ -27,13 +28,13 @@ describe RestClient::Response, :include_helpers do
|
|
27
28
|
end
|
28
29
|
|
29
30
|
it 'handles multiple headers by joining with comma' do
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
net_http_res = res_double(to_hash: {'My-Header' => ['foo', 'bar']}, code: '200', body: nil)
|
32
|
+
example_url = 'http://example.com'
|
33
|
+
request = request_double(url: example_url, method: 'get')
|
34
|
+
response = response_from_res_double(net_http_res, request)
|
34
35
|
|
35
|
-
expect(
|
36
|
-
expect(
|
36
|
+
expect(response.raw_headers['My-Header']).to eq ['foo', 'bar']
|
37
|
+
expect(response.headers[:my_header]).to eq 'foo, bar'
|
37
38
|
end
|
38
39
|
end
|
39
40
|
|
@@ -72,7 +73,7 @@ describe RestClient::Response, :include_helpers do
|
|
72
73
|
describe "exceptions processing" do
|
73
74
|
it "should return itself for normal codes" do
|
74
75
|
(200..206).each do |code|
|
75
|
-
net_http_res =
|
76
|
+
net_http_res = res_double(:code => '200')
|
76
77
|
resp = RestClient::Response.create('abc', net_http_res, @request)
|
77
78
|
resp.return!
|
78
79
|
end
|
@@ -81,7 +82,7 @@ describe RestClient::Response, :include_helpers do
|
|
81
82
|
it "should throw an exception for other codes" do
|
82
83
|
RestClient::Exceptions::EXCEPTIONS_MAP.each_pair do |code, exc|
|
83
84
|
unless (200..207).include? code
|
84
|
-
net_http_res =
|
85
|
+
net_http_res = res_double(:code => code.to_i)
|
85
86
|
resp = RestClient::Response.create('abc', net_http_res, @request)
|
86
87
|
allow(@request).to receive(:max_redirects).and_return(5)
|
87
88
|
expect { resp.return! }.to raise_error(exc)
|
@@ -131,28 +132,27 @@ describe RestClient::Response, :include_helpers do
|
|
131
132
|
end
|
132
133
|
|
133
134
|
it "doesn't follow a 301 when the request is a post" do
|
134
|
-
net_http_res =
|
135
|
+
net_http_res = res_double(:code => 301)
|
136
|
+
response = response_from_res_double(net_http_res, request_double(method: 'post'))
|
135
137
|
|
136
|
-
response = RestClient::Response.create('abc', net_http_res,
|
137
|
-
request_double(method: 'post'))
|
138
138
|
expect {
|
139
139
|
response.return!
|
140
140
|
}.to raise_error(RestClient::MovedPermanently)
|
141
141
|
end
|
142
142
|
|
143
143
|
it "doesn't follow a 302 when the request is a post" do
|
144
|
-
net_http_res =
|
145
|
-
response =
|
146
|
-
|
144
|
+
net_http_res = res_double(:code => 302)
|
145
|
+
response = response_from_res_double(net_http_res, request_double(method: 'post'))
|
146
|
+
|
147
147
|
expect {
|
148
148
|
response.return!
|
149
149
|
}.to raise_error(RestClient::Found)
|
150
150
|
end
|
151
151
|
|
152
152
|
it "doesn't follow a 307 when the request is a post" do
|
153
|
-
net_http_res =
|
154
|
-
response =
|
155
|
-
|
153
|
+
net_http_res = res_double(:code => 307)
|
154
|
+
response = response_from_res_double(net_http_res, request_double(method: 'post'))
|
155
|
+
|
156
156
|
expect(response).not_to receive(:follow_redirection)
|
157
157
|
expect {
|
158
158
|
response.return!
|
@@ -160,9 +160,8 @@ describe RestClient::Response, :include_helpers do
|
|
160
160
|
end
|
161
161
|
|
162
162
|
it "doesn't follow a redirection when the request is a put" do
|
163
|
-
net_http_res =
|
164
|
-
response =
|
165
|
-
request_double(method: 'put'))
|
163
|
+
net_http_res = res_double(:code => 301)
|
164
|
+
response = response_from_res_double(net_http_res, request_double(method: 'put'))
|
166
165
|
expect {
|
167
166
|
response.return!
|
168
167
|
}.to raise_error(RestClient::MovedPermanently)
|
@@ -238,4 +237,16 @@ describe RestClient::Response, :include_helpers do
|
|
238
237
|
end
|
239
238
|
end
|
240
239
|
|
240
|
+
describe "logging" do
|
241
|
+
it "uses the request's logger" do
|
242
|
+
stub_request(:get, 'http://some/resource').to_return(body: 'potato', status: 200)
|
243
|
+
|
244
|
+
logger = double('logger', '<<' => true)
|
245
|
+
request = RestClient::Request.new(url: 'http://some/resource', method: :get, log: logger)
|
246
|
+
|
247
|
+
expect(logger).to receive(:<<)
|
248
|
+
|
249
|
+
request.execute
|
250
|
+
end
|
251
|
+
end
|
241
252
|
end
|