From c49ea097b891926546d80163da2e7ab05df33e4a Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Mon, 24 Mar 2014 16:04:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A4http=5Frequest()=E8=BE=93=E5=87=BAHTTP?= =?UTF-8?q?=20Response=20Headers=EF=BC=8C=E5=B9=B6=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E6=89=80=E6=9C=89=E7=9A=84=E4=B8=8A=E4=BC=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/auth.rb | 14 +++---- lib/qiniu/utils.rb | 8 ++-- spec/qiniu/upload_spec.rb | 78 +++++++++++++++++++++++++++++++++------ 3 files changed, 79 insertions(+), 21 deletions(-) diff --git a/lib/qiniu/auth.rb b/lib/qiniu/auth.rb index 867e4a3..1d3075c 100755 --- a/lib/qiniu/auth.rb +++ b/lib/qiniu/auth.rb @@ -10,16 +10,16 @@ module Qiniu include Utils def call_with_signature(url, data, retry_times = 0, options = {}) - code, data = http_request url, data, options.merge({:qbox_signature_token => generate_qbox_signature(url, data, options[:mime])}) - [code, data] - end + code, data, raw_headers = http_request url, data, options.merge({:qbox_signature_token => generate_qbox_signature(url, data, options[:mime])}) + [code, data, raw_headers] + end # call_with_signature def request(url, data = nil, options = {}) - code, data = Auth.call_with_signature(url, data, 0, options) - [code, data] - end + code, data, raw_headers = Auth.call_with_signature(url, data, 0, options) + [code, data, raw_headers] + end # request - end + end # class << self end # module Auth end # module Qiniu diff --git a/lib/qiniu/utils.rb b/lib/qiniu/utils.rb index ba7a4d8..8e1d252 100755 --- a/lib/qiniu/utils.rb +++ b/lib/qiniu/utils.rb @@ -75,9 +75,10 @@ module Qiniu data = {} body = response.respond_to?(:body) ? response.body : {} data = safe_json_parse(body) unless body.empty? + raw_headers = response.raw_headers end - [code, data] - end + [code, data, raw_headers] + end # send_request_with def http_request url, data = nil, options = {} retry_times = 0 @@ -100,8 +101,9 @@ module Qiniu code = res.code.to_i if res.respond_to? :code body = res.respond_to?(:body) ? res.body : "" data = safe_json_parse(body) unless body.empty? + raw_headers = response.raw_headers end - [code, data] + [code, data, raw_headers] end end diff --git a/spec/qiniu/upload_spec.rb b/spec/qiniu/upload_spec.rb index 895d54a..7c886c4 100755 --- a/spec/qiniu/upload_spec.rb +++ b/spec/qiniu/upload_spec.rb @@ -19,26 +19,31 @@ module Qiniu @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s) @key = make_unique_key_in_bucket(@key) + puts "key=#{@key}" @localfile_5m = "5M.txt" File.open(@localfile_5m, "w"){|f| 5242888.times{ f.write(rand(9).to_s) }} @key_5m = Digest::SHA1.hexdigest(@localfile_5m+Time.now.to_s) @key_5m = make_unique_key_in_bucket(@key_5m) + puts "key_5m=#{@key_5m}" @localfile_4m = "4M.txt" File.open(@localfile_4m, "w"){|f| (1 << 22).times{ f.write(rand(9).to_s) }} @key_4m = Digest::SHA1.hexdigest(@localfile_4m+Time.now.to_s) @key_4m = make_unique_key_in_bucket(@key_4m) + puts "key_4m=#{@key_4m}" @localfile_8m = "8M.txt" File.open(@localfile_8m, "w"){|f| (1 << 23).times{ f.write(rand(9).to_s) }} @key_8m = Digest::SHA1.hexdigest(@localfile_8m+Time.now.to_s) @key_8m = make_unique_key_in_bucket(@key_8m) + puts "key_8m=#{@key_8m}" @localfile_1m = "1M.txt" File.open(@localfile_1m, "w"){|f| (1 << 20).times{ f.write(rand(9).to_s) }} @key_1m = Digest::SHA1.hexdigest(@localfile_1m+Time.now.to_s) @key_1m = make_unique_key_in_bucket(@key_1m) + puts "key_1m=#{@key_1m}" end after :all do @@ -54,9 +59,17 @@ module Qiniu ### 测试单文件直传 context ".put_file" do it "should works" do - code, data = Qiniu::Storage.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true) + code, data, raw_headers = Qiniu::Storage.put_file( + __FILE__, + @bucket, + @key, + 'application/x-ruby', + 'customMeta', + true, + ) code.should == 200 puts data.inspect + puts raw_headers.inspect end end @@ -80,9 +93,19 @@ module Qiniu it "should works" do upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} uptoken = Qiniu.generate_upload_token(upopts) - code, data = Qiniu::Storage.upload_with_token(uptoken, __FILE__, @bucket, @key, nil, nil, nil, true) + code, data, raw_headers = Qiniu::Storage.upload_with_token( + uptoken, + __FILE__, + @bucket, + @key, + nil, + nil, + nil, + true, + ) code.should == 200 puts data.inspect + puts raw_headers.inspect end end @@ -107,10 +130,15 @@ module Qiniu upopts = {:scope => @bucket, :expires_in => 3600, :endUser => "why404@gmail.com"} uptoken = Qiniu.generate_upload_token(upopts) - code, data = Qiniu::Storage.upload_with_token_2(uptoken, __FILE__, @key) + code, data, raw_headers = Qiniu::Storage.upload_with_token_2( + uptoken, + __FILE__, + @key, + ) code.should == 200 puts data.inspect + puts raw_headers.inspect end end # .upload_with_token_2 @@ -135,9 +163,16 @@ module Qiniu it "should works" do upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} uptoken = Qiniu.generate_upload_token(upopts) - code, data = Qiniu::Storage.resumable_upload_with_token(uptoken, @localfile_5m, @bucket, @key_5m) - puts data.inspect + code, data, raw_headers = Qiniu::Storage.resumable_upload_with_token( + uptoken, + @localfile_5m, + @bucket, + @key_5m, + ) (code/100).should == 2 + puts data.inspect + puts raw_headers.inspect + puts "key_5m=#{@key_5m}" end end @@ -161,9 +196,16 @@ module Qiniu it "should works" do upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} uptoken = Qiniu.generate_upload_token(upopts) - code, data = Qiniu::Storage.resumable_upload_with_token(uptoken, @localfile_4m, @bucket, @key_4m) - puts data.inspect + code, data, raw_headers = Qiniu::Storage.resumable_upload_with_token( + uptoken, + @localfile_4m, + @bucket, + @key_4m, + ) (code/100).should == 2 + puts data.inspect + puts raw_headers.inspect + puts "key_4m=#{@key_4m}" end end @@ -187,9 +229,16 @@ module Qiniu it "should works" do upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} uptoken = Qiniu.generate_upload_token(upopts) - code, data = Qiniu::Storage.resumable_upload_with_token(uptoken, @localfile_8m, @bucket, @key_8m) - puts data.inspect + code, data, raw_headers = Qiniu::Storage.resumable_upload_with_token( + uptoken, + @localfile_8m, + @bucket, + @key_8m, + ) (code/100).should == 2 + puts data.inspect + puts raw_headers.inspect + puts "key_8m=#{@key_8m}" end end @@ -213,9 +262,16 @@ module Qiniu it "should works" do upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} uptoken = Qiniu.generate_upload_token(upopts) - code, data = Qiniu::Storage.resumable_upload_with_token(uptoken, @localfile_1m, @bucket, @key_1m) - puts data.inspect + code, data, raw_headers = Qiniu::Storage.resumable_upload_with_token( + uptoken, + @localfile_1m, + @bucket, + @key_1m, + ) (code/100).should == 2 + puts data.inspect + puts raw_headers.inspect + puts "key_1m=#{@key_1m}" end end