diff --git a/spec/qiniu/image_spec.rb b/spec/qiniu/image_spec.rb index 986c9c0..b6d054a 100755 --- a/spec/qiniu/image_spec.rb +++ b/spec/qiniu/image_spec.rb @@ -11,14 +11,18 @@ module Qiniu before :all do - @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s - @key = "image_logo_for_test.png" + ### 复用RubySDK-Test-Storage空间 + @bucket = 'RubySDK-Test-Storage' + @bucket = make_unique_bucket(@bucket) + ### 尝试创建空间 result = Qiniu.mkbucket(@bucket) puts result.inspect - result.should be_true - local_file = File.expand_path('../' + @key, __FILE__) + pic_fname = "image_logo_for_test.png" + @key = make_unique_key_in_bucket(pic_fname) + + local_file = File.expand_path('../' + pic_fname, __FILE__) upopts = { :scope => @bucket, @@ -53,9 +57,7 @@ module Qiniu end after :all do - result = Qiniu.drop(@bucket) - puts result.inspect - result.should_not be_false + ### 不删除Bucket以备下次使用 end context ".info" do diff --git a/spec/qiniu/management_spec.rb b/spec/qiniu/management_spec.rb index 383c2c8..34ab775 100755 --- a/spec/qiniu/management_spec.rb +++ b/spec/qiniu/management_spec.rb @@ -11,21 +11,24 @@ module Qiniu describe Storage do before :all do - @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s - @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s) - @key2 = @key + rand(100).to_s + @bucket = 'RubySDK-Test-Management' + @bucket = make_unique_bucket(@bucket) - code, data = Storage.mkbucket(@bucket) - puts [code, data].inspect - code.should == 200 + ### 尝试创建Bucket + result = Qiniu.mkbucket(@bucket) + puts result.inspect + + @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s) + @key = make_unique_key_in_bucket(@key) + + @key2 = @key + rand(100).to_s end after :all do - code, data = Storage.drop(@bucket) - puts [code, data].inspect - code.should == 200 + ### 不删除Bucket以备下次使用 end + ### 准备数据 context ".put_file" do it "should works" do code, data = Storage.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true) @@ -34,6 +37,7 @@ module Qiniu end end + ### 列举Bucket context ".buckets" do it "should works" do code, data = Storage.buckets @@ -52,7 +56,7 @@ module Qiniu context ".get" do it "should works" do - code, data = Storage.get(@bucket, @key, "rs_spec.rb", 1) + code, data = Storage.get(@bucket, @key) code.should == 200 puts data.inspect end @@ -88,10 +92,6 @@ module Qiniu code.should == 200 puts data.inspect - #code2, data2 = Qiniu.stat(@bucket, @key2) - #code2.should == 200 - #puts data2.inspect - code, data = Storage.delete @bucket, @key2 code.should == 200 puts data.inspect @@ -104,34 +104,12 @@ module Qiniu code.should == 200 puts data.inspect - #code2, data2 = Qiniu.stat(@bucket, @key2) - #code2.should == 200 - #puts data2.inspect - code3, data3 = Storage.batch_move @bucket, @key2, @bucket, @key code3.should == 200 puts data3.inspect end end -=begin - context ".publish" do - it "should works" do - code, data = Qiniu.publish(@domain, @bucket) - code.should == 200 - puts data.inspect - end - end - - context ".unpublish" do - it "should works" do - code, data = Qiniu.unpublish(@domain) - code.should == 200 - puts data.inspect - end - end -=end - context ".move" do it "should works" do code, data = Storage.move(@bucket, @key, @bucket, @key2) @@ -154,9 +132,9 @@ module Qiniu code.should == 200 puts data.inspect - #code2, data2 = Qiniu.stat(@bucket, @key2) - #code2.should == 200 - #puts data2.inspect + code, data = Storage.delete(@bucket, @key2) + code.should == 200 + puts data.inspect end end @@ -168,14 +146,6 @@ module Qiniu end end - context ".drop" do - it "should works" do - code, data = Storage.drop(@bucket) - code.should == 200 - puts data.inspect - end - end - end end # module Storage end # module Qiniu diff --git a/spec/qiniu/misc_spec.rb b/spec/qiniu/misc_spec.rb index f9fa2c5..c49f818 100755 --- a/spec/qiniu/misc_spec.rb +++ b/spec/qiniu/misc_spec.rb @@ -10,16 +10,16 @@ module Qiniu describe Misc do before :all do - @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s + ### 复用RubySDK-Test-Management空间 + @bucket = 'RubySDK-Test-Management' + @bucket = make_unique_bucket(@bucket) + result = Qiniu.mkbucket(@bucket) puts result.inspect - result.should_not be_false end after :all do - result = Qiniu.drop(@bucket) - puts result.inspect - result.should_not be_false + ### 不删除Bucket以备下次使用 end context ".set_protected" do diff --git a/spec/qiniu/qiniu_spec.rb b/spec/qiniu/qiniu_spec.rb index 0c266b6..86a014e 100755 --- a/spec/qiniu/qiniu_spec.rb +++ b/spec/qiniu/qiniu_spec.rb @@ -9,19 +9,21 @@ module Qiniu describe Qiniu do before :all do - @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s + @bucket = 'RubySDK-Test' + @bucket = make_unique_bucket(@bucket) + + @test_image_bucket = @bucket + + ### 尝试创建Bucket + result = Qiniu.mkbucket(@bucket) + puts result.inspect + @key = Digest::SHA1.hexdigest Time.now.to_s + @key = make_unique_key_in_bucket(@key) + @key2 = @key + rand(100).to_s #@domain = @bucket + '.dn.qbox.me' - result = Qiniu.mkbucket(@bucket) - result.should_not be_false - - @test_image_bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s - result2 = Qiniu.mkbucket(@test_image_bucket) - puts result2.inspect - result2.should be_true - @test_image_key = 'image_logo_for_test.png' local_file = File.expand_path('./' + @test_image_key, File.dirname(__FILE__)) puts local_file.inspect @@ -32,16 +34,7 @@ module Qiniu end after :all do - #result = Qiniu.unpublish(@domain) - #result.should_not be_false - - result1 = Qiniu.drop(@bucket) - puts result1.inspect - result1.should_not be_false - - result2 = Qiniu.drop(@test_image_bucket) - puts result2.inspect - result2.should_not be_false + ### 不删除Bucket以备下次使用 end context ".put_file" do @@ -180,7 +173,7 @@ module Qiniu context ".get" do it "should works" do - result = Qiniu.get(@bucket, @key, "rs_spec.rb", 10) + result = Qiniu.get(@bucket, @key, "qiniu_spec.rb", 10) result.should_not be_false result.should_not be_empty puts result.inspect @@ -232,24 +225,6 @@ module Qiniu end end -=begin - context ".publish" do - it "should works" do - result = Qiniu.publish(@domain, @bucket) - result.should_not be_false - end - end -=end - -=begin - context ".unpublish" do - it "should works" do - result = Qiniu.unpublish(@domain) - result.should_not be_false - end - end -=end - =begin context ".batch_copy" do it "should works" do @@ -293,8 +268,8 @@ module Qiniu result = Qiniu.copy(@bucket, @key, @bucket, @key2) result.should_not be_false - #result2 = Qiniu.stat(@bucket, @key2) - #result2.should_not be_false + result3 = Qiniu.delete(@bucket, @key2) + result3.should_not be_false end end @@ -305,13 +280,6 @@ module Qiniu end end - context ".drop" do - it "should works" do - result = Qiniu.drop(@bucket) - result.should_not be_false - end - end - context ".image_info" do it "should works" do data = Qiniu.get(@test_image_bucket, @test_image_key) @@ -325,19 +293,6 @@ module Qiniu end end -=begin - context ".image_exif" do - it "should works" do - data = Qiniu.get(@test_image_bucket, @test_image_key) - data.should_not be_false - data.should_not be_empty - puts data.inspect - result = Qiniu.image_exif(data["url"]) - puts result.inspect - end - end -=end - context ".image_mogrify_save_as" do it "should works" do data = Qiniu.get(@test_image_bucket, @test_image_key) diff --git a/spec/qiniu/upload_spec.rb b/spec/qiniu/upload_spec.rb index 4739c31..895d54a 100755 --- a/spec/qiniu/upload_spec.rb +++ b/spec/qiniu/upload_spec.rb @@ -10,41 +10,48 @@ module Qiniu describe Storage do before :all do - @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s - @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s) - - @localfile1 = "bigfile.txt" - File.open(@localfile1, "w"){|f| 5242888.times{ f.write(rand(9).to_s) }} - @key1 = Digest::SHA1.hexdigest(@localfile1+Time.now.to_s) - - @localfile2 = "bigfile2.txt" - File.open(@localfile2, "w"){|f| (1 << 22).times{ f.write(rand(9).to_s) }} - @key2 = Digest::SHA1.hexdigest(@localfile2+Time.now.to_s) - - @localfile3 = "bigfile3.txt" - File.open(@localfile3, "w"){|f| (1 << 23).times{ f.write(rand(9).to_s) }} - @key3 = Digest::SHA1.hexdigest(@localfile3+Time.now.to_s) - - @localfile4 = "smallfile.txt" - File.open(@localfile4, "w"){|f| (1 << 20).times{ f.write(rand(9).to_s) }} - @key4 = Digest::SHA1.hexdigest(@localfile4+Time.now.to_s) + @bucket = 'RubySDK-Test-Storage' + @bucket = make_unique_bucket(@bucket) + ### 尝试创建Bucket result = Qiniu.mkbucket(@bucket) puts result.inspect - result.should be_true + + @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s) + @key = make_unique_key_in_bucket(@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) + + @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) + + @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) + + @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) end after :all do - File.unlink(@localfile1) if File.exists?(@localfile1) - File.unlink(@localfile2) if File.exists?(@localfile2) - File.unlink(@localfile3) if File.exists?(@localfile3) - File.unlink(@localfile4) if File.exists?(@localfile4) + ### 清除本地临时文件 + File.unlink(@localfile_5m) if File.exists?(@localfile_5m) + File.unlink(@localfile_4m) if File.exists?(@localfile_4m) + File.unlink(@localfile_8m) if File.exists?(@localfile_8m) + File.unlink(@localfile_1m) if File.exists?(@localfile_1m) - result = Qiniu.drop(@bucket) - puts result.inspect - result.should_not be_false + ### 不删除Bucket以备下次使用 end + ### 测试单文件直传 context ".put_file" do it "should works" do code, data = Qiniu::Storage.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true) @@ -53,6 +60,22 @@ module Qiniu end end + context ".stat" do + it "should exists" do + code, data = Qiniu::Storage.stat(@bucket, @key) + puts data.inspect + code.should == 200 + end + end + + context ".delete" do + it "should works" do + code, data = Qiniu::Storage.delete(@bucket, @key) + puts data.inspect + code.should == 200 + end + end + context ".upload_with_token" do it "should works" do upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"} @@ -63,6 +86,22 @@ module Qiniu end end + context ".stat" do + it "should exists" do + code, data = Qiniu::Storage.stat(@bucket, @key) + puts data.inspect + code.should == 200 + end + end + + context ".delete" do + it "should works" do + code, data = Qiniu::Storage.delete(@bucket, @key) + puts data.inspect + code.should == 200 + end + end + context ".upload_with_token_2" do it "should works" do upopts = {:scope => @bucket, :expires_in => 3600, :endUser => "why404@gmail.com"} @@ -75,19 +114,9 @@ module Qiniu end end # .upload_with_token_2 - context ".resumable_upload_with_token" do - 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, @localfile1, @bucket, @key1) - puts data.inspect - (code/100).should == 2 - end - end - context ".stat" do it "should exists" do - code, data = Qiniu::Storage.stat(@bucket, @key1) + code, data = Qiniu::Storage.stat(@bucket, @key) puts data.inspect code.should == 200 end @@ -95,7 +124,34 @@ module Qiniu context ".delete" do it "should works" do - code, data = Qiniu::Storage.delete(@bucket, @key1) + code, data = Qiniu::Storage.delete(@bucket, @key) + puts data.inspect + code.should == 200 + end + end + + ### 测试断点续上传 + context ".resumable_upload_with_token" do + 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/100).should == 2 + end + end + + context ".stat" do + it "should exists" do + code, data = Qiniu::Storage.stat(@bucket, @key_5m) + puts data.inspect + code.should == 200 + end + end + + context ".delete" do + it "should works" do + code, data = Qiniu::Storage.delete(@bucket, @key_5m) puts data.inspect code.should == 200 end @@ -105,7 +161,7 @@ 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, @localfile2, @bucket, @key2) + code, data = Qiniu::Storage.resumable_upload_with_token(uptoken, @localfile_4m, @bucket, @key_4m) puts data.inspect (code/100).should == 2 end @@ -113,7 +169,7 @@ module Qiniu context ".stat" do it "should exists" do - code, data = Qiniu::Storage.stat(@bucket, @key2) + code, data = Qiniu::Storage.stat(@bucket, @key_4m) puts data.inspect code.should == 200 end @@ -121,7 +177,7 @@ module Qiniu context ".delete" do it "should works" do - code, data = Qiniu::Storage.delete(@bucket, @key2) + code, data = Qiniu::Storage.delete(@bucket, @key_4m) puts data.inspect code.should == 200 end @@ -131,7 +187,7 @@ 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, @localfile3, @bucket, @key3) + code, data = Qiniu::Storage.resumable_upload_with_token(uptoken, @localfile_8m, @bucket, @key_8m) puts data.inspect (code/100).should == 2 end @@ -139,7 +195,7 @@ module Qiniu context ".stat" do it "should exists" do - code, data = Qiniu::Storage.stat(@bucket, @key3) + code, data = Qiniu::Storage.stat(@bucket, @key_8m) puts data.inspect code.should == 200 end @@ -147,7 +203,7 @@ module Qiniu context ".delete" do it "should works" do - code, data = Qiniu::Storage.delete(@bucket, @key3) + code, data = Qiniu::Storage.delete(@bucket, @key_8m) puts data.inspect code.should == 200 end @@ -157,7 +213,7 @@ 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, @localfile4, @bucket, @key4) + code, data = Qiniu::Storage.resumable_upload_with_token(uptoken, @localfile_1m, @bucket, @key_1m) puts data.inspect (code/100).should == 2 end @@ -165,7 +221,7 @@ module Qiniu context ".stat" do it "should exists" do - code, data = Qiniu::Storage.stat(@bucket, @key4) + code, data = Qiniu::Storage.stat(@bucket, @key_1m) puts data.inspect code.should == 200 end @@ -173,7 +229,7 @@ module Qiniu context ".delete" do it "should works" do - code, data = Qiniu::Storage.delete(@bucket, @key4) + code, data = Qiniu::Storage.delete(@bucket, @key_1m) puts data.inspect code.should == 200 end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e695e04..b5ec3ee 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,3 +9,11 @@ RSpec.configure do |config| Qiniu.establish_connection! :access_key => ENV["QINIU_ACCESS_KEY"], :secret_key => ENV["QINIU_SECRET_KEY"] end end + +def make_unique_bucket (bucket) + bucket + "-" + ENV["QINIU_ACCESS_KEY"][0, 8] +end # make_unique_bucket + +def make_unique_key_in_bucket (key) + "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}-" + key +end # make_unique_key_in_bucket