From fc5afc332a8e3802fee721a9640269203fba01ee Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Thu, 27 Mar 2014 11:17:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BAauthorize=5Fdownload=5Furl=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=B5=8B=E8=AF=95=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 | 4 +-- spec/qiniu/auth_spec.rb | 78 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100755 spec/qiniu/auth_spec.rb diff --git a/lib/qiniu/auth.rb b/lib/qiniu/auth.rb index c286bfe..19aeba1 100755 --- a/lib/qiniu/auth.rb +++ b/lib/qiniu/auth.rb @@ -57,8 +57,8 @@ module Qiniu ### 生成下载授权凭证 dntoken = "#{access_key}:#{encoded_sign}" - ### 返回下载授权URL及相关参数 - return "#{download_url}&token=#{dntoken}", e, dntoken + ### 返回下载授权URL + return "#{download_url}&token=#{dntoken}" end # authorize_download_url end # class << self diff --git a/spec/qiniu/auth_spec.rb b/spec/qiniu/auth_spec.rb new file mode 100755 index 0000000..0054e4f --- /dev/null +++ b/spec/qiniu/auth_spec.rb @@ -0,0 +1,78 @@ +# -*- encoding: utf-8 -*- + +require 'spec_helper' +require 'qiniu/auth' +require 'qiniu/storage' +require 'digest/sha1' + +module Qiniu + module Auth + describe Auth do + + before :all do + @bucket = 'RubySDK-Test-Private' + @bucket = make_unique_bucket(@bucket) + + ### 尝试创建Bucket + result = Qiniu::Storage.make_a_private_bucket(@bucket) + puts result.inspect + end + + after :all do + ### 不删除Bucket以备下次使用 + end + + ### 测试私有资源下载 + context ".download_private_file" do + it "should works" do + ### 生成Key + key = 'a_private_file' + key = make_unique_key_in_bucket(key) + puts "key=#{key}" + + ### 上传测试文件 + upopts = {:scope => @bucket, :expires_in => 3600, :endUser => "why404@gmail.com"} + uptoken = Qiniu.generate_upload_token(upopts) + + code, data, raw_headers = Qiniu::Storage.upload_with_token_2( + uptoken, + __FILE__, + key + ) + code.should == 200 + puts data.inspect + puts raw_headers.inspect + + ### 获取下载地址 + code, data = Qiniu::Storage.get(@bucket, key) + code.should == 200 + puts data.inspect + + url = data['url'] + + ### 授权下载地址(不带参数) + download_url = Qiniu::Auth.authorize_download_url(url) + puts "download_url=#{download_url}" + + result = RestClient.get(download_url) + result.code.should == 200 + result.body.should_not be_empty + + ### 授权下载地址(带参数) + download_url = Qiniu::Auth.authorize_download_url(url + '?download/a.m3u8') + puts "download_url=#{download_url}" + + result = RestClient.get(download_url) + result.code.should == 200 + result.body.should_not be_empty + + ### 删除文件 + code, data = Qiniu::Storage.delete(@bucket, key) + code.should == 200 + puts data.inspect + end + end + + end + end # module Storage +end # module Qiniu