qiniu_jxb_fix 6.4.2
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.
- data/.gitignore +18 -0
- data/.rspec +1 -0
- data/.travis.yml +12 -0
- data/CHANGELOG.md +146 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +44 -0
- data/LICENSE +22 -0
- data/README.md +47 -0
- data/Rakefile +21 -0
- data/docs/README.md +790 -0
- data/lib/qiniu/abstract.rb +22 -0
- data/lib/qiniu/adt.rb +46 -0
- data/lib/qiniu/auth.rb +255 -0
- data/lib/qiniu/config.rb +60 -0
- data/lib/qiniu/exceptions.rb +120 -0
- data/lib/qiniu/fop.rb +4 -0
- data/lib/qiniu/http.rb +137 -0
- data/lib/qiniu/image.rb +38 -0
- data/lib/qiniu/log.rb +15 -0
- data/lib/qiniu/management.rb +166 -0
- data/lib/qiniu/misc.rb +35 -0
- data/lib/qiniu/pfop.rb +124 -0
- data/lib/qiniu/resumable_upload.rb +319 -0
- data/lib/qiniu/storage.rb +5 -0
- data/lib/qiniu/tokens/access_token.rb +23 -0
- data/lib/qiniu/tokens/download_token.rb +36 -0
- data/lib/qiniu/tokens/qbox_token.rb +38 -0
- data/lib/qiniu/tokens/upload_token.rb +57 -0
- data/lib/qiniu/upload.rb +134 -0
- data/lib/qiniu/utils.rb +109 -0
- data/lib/qiniu/version.rb +17 -0
- data/lib/qiniu-rs.rb +2 -0
- data/lib/qiniu.rb +209 -0
- data/qiniu.gemspec +29 -0
- data/rails3/Gemfile +4 -0
- data/rails3/qiniu.gemspec +29 -0
- data/spec/qiniu/abstract_spec.rb +30 -0
- data/spec/qiniu/auth_spec.rb +74 -0
- data/spec/qiniu/image_logo_for_test.png +0 -0
- data/spec/qiniu/image_spec.rb +80 -0
- data/spec/qiniu/management_spec.rb +163 -0
- data/spec/qiniu/misc_spec.rb +53 -0
- data/spec/qiniu/pfop_spec.rb +80 -0
- data/spec/qiniu/qiniu_spec.rb +321 -0
- data/spec/qiniu/tokens/qbox_token_spec.rb +29 -0
- data/spec/qiniu/upload_spec.rb +301 -0
- data/spec/qiniu/utils_spec.rb +49 -0
- data/spec/qiniu/version_spec.rb +10 -0
- data/spec/spec_helper.rb +19 -0
- metadata +222 -0
@@ -0,0 +1,321 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'digest/sha1'
|
4
|
+
require 'spec_helper'
|
5
|
+
require 'qiniu'
|
6
|
+
require 'qiniu/exceptions'
|
7
|
+
|
8
|
+
module Qiniu
|
9
|
+
describe Qiniu do
|
10
|
+
|
11
|
+
before :all do
|
12
|
+
@bucket = 'rubysdk'
|
13
|
+
|
14
|
+
@test_image_bucket = @bucket
|
15
|
+
|
16
|
+
@key = Digest::SHA1.hexdigest Time.now.to_s
|
17
|
+
@key = make_unique_key_in_bucket(@key)
|
18
|
+
|
19
|
+
@key2 = @key + rand(100).to_s
|
20
|
+
#@domain = @bucket + '.dn.qbox.me'
|
21
|
+
|
22
|
+
@test_image_key = 'image_logo_for_test.png'
|
23
|
+
local_file = File.expand_path('./' + @test_image_key, File.dirname(__FILE__))
|
24
|
+
puts local_file.inspect
|
25
|
+
upopts = {:scope => @test_image_bucket, :expires_in => 3600, :customer => "why404@gmail.com"}
|
26
|
+
uptoken = Qiniu.generate_upload_token(upopts)
|
27
|
+
data = Qiniu.upload_file :uptoken => uptoken, :file => local_file, :bucket => @test_image_bucket, :key => @test_image_key
|
28
|
+
puts data.inspect
|
29
|
+
end
|
30
|
+
|
31
|
+
after :all do
|
32
|
+
end
|
33
|
+
|
34
|
+
context "get_url" do
|
35
|
+
it 'should works' do
|
36
|
+
key = 'Fg8ne_KBJpTrCqlvaZ9cPl68DK4N'
|
37
|
+
primitive_url = Qiniu.get(@bucket, key)['url'] rescue nil
|
38
|
+
return nil if primitive_url.nil?
|
39
|
+
#puts Qiniu::Auth.authorize_download_url(primitive_url)
|
40
|
+
puts Qiniu::Auth.authorize_download_url_2('testqiniu.jiaoxuebang.cn', key)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context ".buckets" do
|
45
|
+
it "should works" do
|
46
|
+
result = Qiniu.buckets
|
47
|
+
expect(result).to_not be_empty
|
48
|
+
puts result.inspect
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context ".set_protected" do
|
53
|
+
it "should works" do
|
54
|
+
result = Qiniu.set_protected(@bucket, 1)
|
55
|
+
expect(result).to eq(true)
|
56
|
+
puts result.inspect
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context ".set_separator" do
|
61
|
+
it "should works" do
|
62
|
+
result = Qiniu.set_separator(@bucket, "-")
|
63
|
+
expect(result).to eq(true)
|
64
|
+
puts result.inspect
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context ".set_style" do
|
69
|
+
it "should works" do
|
70
|
+
result = Qiniu.set_style(@bucket, "small.jpg", "imageMogr/auto-orient/thumbnail/!120x120r/gravity/center/crop/!120x120/quality/80")
|
71
|
+
expect(result).to eq(true)
|
72
|
+
puts result.inspect
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context ".unset_style" do
|
77
|
+
it "should works" do
|
78
|
+
result = Qiniu.unset_style(@bucket, "small.jpg")
|
79
|
+
expect(result).to eq(true)
|
80
|
+
puts result.inspect
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context ".upload_file" do
|
85
|
+
it "should works" do
|
86
|
+
uptoken_opts = {:scope => @bucket, :escape => 0}
|
87
|
+
upload_opts = {
|
88
|
+
:uptoken => Qiniu.generate_upload_token(uptoken_opts),
|
89
|
+
:file => __FILE__,
|
90
|
+
:bucket => @bucket,
|
91
|
+
:key => @key,
|
92
|
+
:enable_crc32_check => true
|
93
|
+
}
|
94
|
+
result = Qiniu.upload_file(upload_opts)
|
95
|
+
expect(result).to_not be_empty
|
96
|
+
puts result.inspect
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should raise MissingArgsError" do
|
100
|
+
uptoken_opts = {:scope => @bucket, :escape => 0}
|
101
|
+
upload_opts = {
|
102
|
+
:uptoken => Qiniu.generate_upload_token(uptoken_opts),
|
103
|
+
:file => __FILE__,
|
104
|
+
:key => @key,
|
105
|
+
:enable_crc32_check => true
|
106
|
+
}
|
107
|
+
lambda { Qiniu.upload_file(upload_opts) }.should raise_error(MissingArgsError)
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should raise NoSuchFileError" do
|
111
|
+
uptoken_opts = {:scope => @bucket, :escape => 0}
|
112
|
+
upload_opts = {
|
113
|
+
:uptoken => Qiniu.generate_upload_token(uptoken_opts),
|
114
|
+
:file => 'no_this_file',
|
115
|
+
:bucket => @bucket,
|
116
|
+
:key => @key,
|
117
|
+
:enable_crc32_check => true
|
118
|
+
}
|
119
|
+
lambda { Qiniu.upload_file(upload_opts) }.should raise_error(NoSuchFileError)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context ".resumable_upload_file" do
|
124
|
+
it "should works" do
|
125
|
+
# generate bigfile for testing
|
126
|
+
localfile = "test_bigfile"
|
127
|
+
File.open(localfile, "w"){|f| 5242888.times{f.write(rand(9).to_s)}}
|
128
|
+
key = Digest::SHA1.hexdigest(localfile+Time.now.to_s)
|
129
|
+
# generate the upload token
|
130
|
+
uptoken_opts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com", :escape => 0}
|
131
|
+
uptoken = Qiniu.generate_upload_token(uptoken_opts)
|
132
|
+
# uploading
|
133
|
+
upload_opts = {
|
134
|
+
:uptoken => uptoken,
|
135
|
+
:file => localfile,
|
136
|
+
:bucket => @bucket,
|
137
|
+
:key => key
|
138
|
+
}
|
139
|
+
#uploading
|
140
|
+
result1 = Qiniu.upload_file(upload_opts)
|
141
|
+
#drop the bigfile
|
142
|
+
File.unlink(localfile) if File.exists?(localfile)
|
143
|
+
#expect
|
144
|
+
puts result1.inspect
|
145
|
+
expect(result1).to_not be_empty
|
146
|
+
#stat
|
147
|
+
result2 = Qiniu.stat(@bucket, key)
|
148
|
+
puts result2.inspect
|
149
|
+
expect(result2).to_not be_empty
|
150
|
+
#delete
|
151
|
+
result3 = Qiniu.delete(@bucket, key)
|
152
|
+
puts result3.inspect
|
153
|
+
expect(result3).to eq(true)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
context ".stat" do
|
158
|
+
it "should works" do
|
159
|
+
result = Qiniu.stat(@bucket, @key)
|
160
|
+
expect(result).to_not be_empty
|
161
|
+
puts result.inspect
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
context ".get" do
|
166
|
+
it "should works" do
|
167
|
+
result = Qiniu.get(@bucket, @key, "qiniu_spec.rb", 10)
|
168
|
+
expect(result).to_not be_empty
|
169
|
+
puts result.inspect
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
context ".download" do
|
174
|
+
it "should works" do
|
175
|
+
result = Qiniu.download(@bucket, @key, "rs_spec.rb", 10)
|
176
|
+
expect(result).to_not be_empty
|
177
|
+
puts result.inspect
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
context ".batch" do
|
182
|
+
it "should works" do
|
183
|
+
result = Qiniu.batch("stat", @bucket, [@key])
|
184
|
+
expect(result).to_not be_empty
|
185
|
+
puts result.inspect
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
context ".batch_stat" do
|
190
|
+
it "should works" do
|
191
|
+
result = Qiniu.batch_stat(@bucket, [@key])
|
192
|
+
expect(result).to_not be_empty
|
193
|
+
puts result.inspect
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
context ".batch_get" do
|
198
|
+
it "should works" do
|
199
|
+
result = Qiniu.batch_get(@bucket, [@key])
|
200
|
+
expect(result).to_not be_empty
|
201
|
+
puts result.inspect
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
context ".batch_download" do
|
206
|
+
it "should works" do
|
207
|
+
result = Qiniu.batch_download(@bucket, [@key])
|
208
|
+
expect(result).to_not be_empty
|
209
|
+
puts result.inspect
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
=begin
|
214
|
+
context ".batch_copy" do
|
215
|
+
it "should works" do
|
216
|
+
result = Qiniu.batch_copy [@bucket, @key, @bucket, @key2]
|
217
|
+
result.should_not be_false
|
218
|
+
|
219
|
+
#result2 = Qiniu.stat(@bucket, @key2)
|
220
|
+
#result2.should_not be_false
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
context ".batch_move" do
|
225
|
+
it "should works" do
|
226
|
+
result = Qiniu.batch_move [@bucket, @key, @bucket, @key2]
|
227
|
+
result.should_not be_false
|
228
|
+
|
229
|
+
#result2 = Qiniu.stat(@bucket, @key2)
|
230
|
+
#result2.should_not be_false
|
231
|
+
|
232
|
+
result3 = Qiniu.batch_move [@bucket, @key2, @bucket, @key]
|
233
|
+
result3.should_not be_false
|
234
|
+
end
|
235
|
+
end
|
236
|
+
=end
|
237
|
+
|
238
|
+
context ".move" do
|
239
|
+
it "should works" do
|
240
|
+
result = Qiniu.move(@bucket, @key, @bucket, @key2)
|
241
|
+
expect(result).to eq(true)
|
242
|
+
|
243
|
+
result2 = Qiniu.stat(@bucket, @key2)
|
244
|
+
expect(result2).to_not be_empty
|
245
|
+
|
246
|
+
result3 = Qiniu.move(@bucket, @key2, @bucket, @key)
|
247
|
+
expect(result3).to eq(true)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
context ".copy" do
|
252
|
+
it "should works" do
|
253
|
+
result = Qiniu.copy(@bucket, @key, @bucket, @key2)
|
254
|
+
expect(result).to eq(true)
|
255
|
+
|
256
|
+
result3 = Qiniu.delete(@bucket, @key2)
|
257
|
+
expect(result3).to eq(true)
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
context ".delete" do
|
262
|
+
it "should works" do
|
263
|
+
result = Qiniu.delete(@bucket, @key)
|
264
|
+
expect(result).to eq(true)
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
context ".image_info" do
|
269
|
+
it "should works" do
|
270
|
+
data = Qiniu.get(@test_image_bucket, @test_image_key)
|
271
|
+
expect(data).to_not be_empty
|
272
|
+
puts data.inspect
|
273
|
+
result = Qiniu.image_info(data["url"])
|
274
|
+
expect(result).to_not be_empty
|
275
|
+
puts result.inspect
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
context ".image_mogrify_save_as" do
|
280
|
+
it "should works" do
|
281
|
+
data = Qiniu.get(@test_image_bucket, @test_image_key)
|
282
|
+
expect(data).to_not be_empty
|
283
|
+
puts data.inspect
|
284
|
+
|
285
|
+
dest_key = "cropped-" + @test_image_key
|
286
|
+
src_img_url = data["url"]
|
287
|
+
mogrify_options = {
|
288
|
+
:thumbnail => "!120x120>",
|
289
|
+
:gravity => "center",
|
290
|
+
:crop => "!120x120a0a0",
|
291
|
+
:quality => 85,
|
292
|
+
:rotate => 45,
|
293
|
+
:format => "jpg",
|
294
|
+
:auto_orient => true
|
295
|
+
}
|
296
|
+
result2 = Qiniu.image_mogrify_save_as(@test_image_bucket, dest_key, src_img_url, mogrify_options)
|
297
|
+
expect(result2).to_not be_empty
|
298
|
+
puts result2.inspect
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
context ".generate_upload_token" do
|
303
|
+
it "should works" do
|
304
|
+
data = Qiniu.generate_upload_token({:scope => @bucket, :expires_in => 3600, :escape => 0})
|
305
|
+
data.should_not be_empty
|
306
|
+
puts data.inspect
|
307
|
+
data.split(":").length.should == 3
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
context ".generate_download_token" do
|
312
|
+
it "should works" do
|
313
|
+
data = Qiniu.generate_download_token({:expires_in => 1, :pattern => 'http://*.dn.qbox.me/*'})
|
314
|
+
data.should_not be_empty
|
315
|
+
puts data.inspect
|
316
|
+
data.split(":").length.should == 3
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
end
|
321
|
+
end # module Qiniu
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'qiniu/tokens/qbox_token'
|
5
|
+
|
6
|
+
module Qiniu
|
7
|
+
module RS
|
8
|
+
describe QboxToken do
|
9
|
+
before :all do
|
10
|
+
@qbox_token = QboxToken.new(:url => 'www.qiniu.com?key1=value1',
|
11
|
+
:params => { :key2 => 'value2' })
|
12
|
+
@qbox_token.access_key = 'access_key'
|
13
|
+
@qbox_token.secret_key = 'secret_key'
|
14
|
+
end
|
15
|
+
|
16
|
+
context "#generate_token" do
|
17
|
+
it "should generate token" do
|
18
|
+
@qbox_token.generate_token.should_not be_empty
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "#generate_signature" do
|
23
|
+
it "should generate signature" do
|
24
|
+
@qbox_token.generate_signature.should == "www.qiniu.com?key1=value1\nkey2=value2"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,301 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# vim: sw=2 ts=2
|
3
|
+
|
4
|
+
require 'spec_helper'
|
5
|
+
require 'qiniu/auth'
|
6
|
+
require 'qiniu/storage'
|
7
|
+
require 'digest/sha1'
|
8
|
+
|
9
|
+
module Qiniu
|
10
|
+
module Storage
|
11
|
+
describe Storage do
|
12
|
+
|
13
|
+
before :all do
|
14
|
+
@bucket = 'rubysdk'
|
15
|
+
|
16
|
+
@key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s)
|
17
|
+
@key = make_unique_key_in_bucket(@key)
|
18
|
+
puts "key=#{@key}"
|
19
|
+
|
20
|
+
@localfile_5m = "5M.txt"
|
21
|
+
File.open(@localfile_5m, "w"){|f| 5242888.times{ f.write(rand(9).to_s) }}
|
22
|
+
@key_5m = Digest::SHA1.hexdigest(@localfile_5m+Time.now.to_s)
|
23
|
+
@key_5m = make_unique_key_in_bucket(@key_5m)
|
24
|
+
puts "key_5m=#{@key_5m}"
|
25
|
+
|
26
|
+
@localfile_4m = "4M.txt"
|
27
|
+
File.open(@localfile_4m, "w"){|f| (1 << 22).times{ f.write(rand(9).to_s) }}
|
28
|
+
@key_4m = Digest::SHA1.hexdigest(@localfile_4m+Time.now.to_s)
|
29
|
+
@key_4m = make_unique_key_in_bucket(@key_4m)
|
30
|
+
puts "key_4m=#{@key_4m}"
|
31
|
+
|
32
|
+
@localfile_8m = "8M.txt"
|
33
|
+
File.open(@localfile_8m, "w"){|f| (1 << 23).times{ f.write(rand(9).to_s) }}
|
34
|
+
@key_8m = Digest::SHA1.hexdigest(@localfile_8m+Time.now.to_s)
|
35
|
+
@key_8m = make_unique_key_in_bucket(@key_8m)
|
36
|
+
puts "key_8m=#{@key_8m}"
|
37
|
+
|
38
|
+
@localfile_1m = "1M.txt"
|
39
|
+
File.open(@localfile_1m, "w"){|f| (1 << 20).times{ f.write(rand(9).to_s) }}
|
40
|
+
@key_1m = Digest::SHA1.hexdigest(@localfile_1m+Time.now.to_s)
|
41
|
+
@key_1m = make_unique_key_in_bucket(@key_1m)
|
42
|
+
puts "key_1m=#{@key_1m}"
|
43
|
+
end
|
44
|
+
|
45
|
+
after :all do
|
46
|
+
### 清除本地临时文件
|
47
|
+
File.unlink(@localfile_5m) if File.exists?(@localfile_5m)
|
48
|
+
File.unlink(@localfile_4m) if File.exists?(@localfile_4m)
|
49
|
+
File.unlink(@localfile_8m) if File.exists?(@localfile_8m)
|
50
|
+
File.unlink(@localfile_1m) if File.exists?(@localfile_1m)
|
51
|
+
end
|
52
|
+
|
53
|
+
### 测试单文件直传
|
54
|
+
context ".upload_with_token" do
|
55
|
+
it "should works" do
|
56
|
+
upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"}
|
57
|
+
uptoken = Qiniu.generate_upload_token(upopts)
|
58
|
+
code, data, raw_headers = Qiniu::Storage.upload_with_token(
|
59
|
+
uptoken,
|
60
|
+
__FILE__,
|
61
|
+
@bucket,
|
62
|
+
@key,
|
63
|
+
nil,
|
64
|
+
nil,
|
65
|
+
nil,
|
66
|
+
true
|
67
|
+
)
|
68
|
+
code.should == 200
|
69
|
+
puts data.inspect
|
70
|
+
puts raw_headers.inspect
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context ".stat" do
|
75
|
+
it "should exists" do
|
76
|
+
code, data = Qiniu::Storage.stat(@bucket, @key)
|
77
|
+
puts data.inspect
|
78
|
+
code.should == 200
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context ".delete" do
|
83
|
+
it "should works" do
|
84
|
+
code, data = Qiniu::Storage.delete(@bucket, @key)
|
85
|
+
puts data.inspect
|
86
|
+
code.should == 200
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context ".upload_with_token_2" do
|
91
|
+
it "should works" do
|
92
|
+
upopts = {:scope => @bucket, :expires_in => 3600, :endUser => "why404@gmail.com"}
|
93
|
+
uptoken = Qiniu.generate_upload_token(upopts)
|
94
|
+
|
95
|
+
code, data, raw_headers = Qiniu::Storage.upload_with_token_2(
|
96
|
+
uptoken,
|
97
|
+
__FILE__,
|
98
|
+
@key
|
99
|
+
)
|
100
|
+
|
101
|
+
code.should == 200
|
102
|
+
puts data.inspect
|
103
|
+
puts raw_headers.inspect
|
104
|
+
end
|
105
|
+
end # .upload_with_token_2
|
106
|
+
|
107
|
+
context ".stat" do
|
108
|
+
it "should exists" do
|
109
|
+
code, data = Qiniu::Storage.stat(@bucket, @key)
|
110
|
+
puts data.inspect
|
111
|
+
code.should == 200
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context ".delete" do
|
116
|
+
it "should works" do
|
117
|
+
code, data = Qiniu::Storage.delete(@bucket, @key)
|
118
|
+
puts data.inspect
|
119
|
+
code.should == 200
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context ".upload_with_put_policy" do
|
124
|
+
it "should works" do
|
125
|
+
pp = Qiniu::Auth::PutPolicy.new(@bucket, @key)
|
126
|
+
pp.end_user = "why404@gmail.com"
|
127
|
+
puts 'put_policy=' + pp.to_json
|
128
|
+
|
129
|
+
code, data, raw_headers = Qiniu::Storage.upload_with_put_policy(
|
130
|
+
pp,
|
131
|
+
__FILE__,
|
132
|
+
@key + '-not-equal'
|
133
|
+
)
|
134
|
+
code.should_not == 200
|
135
|
+
puts data.inspect
|
136
|
+
puts raw_headers.inspect
|
137
|
+
|
138
|
+
code, data, raw_headers = Qiniu::Storage.upload_with_put_policy(
|
139
|
+
pp,
|
140
|
+
__FILE__,
|
141
|
+
@key
|
142
|
+
)
|
143
|
+
|
144
|
+
code.should == 200
|
145
|
+
puts data.inspect
|
146
|
+
puts raw_headers.inspect
|
147
|
+
end
|
148
|
+
end # .upload_with_put_policy
|
149
|
+
|
150
|
+
context ".stat" do
|
151
|
+
it "should exists" do
|
152
|
+
code, data = Qiniu::Storage.stat(@bucket, @key)
|
153
|
+
puts data.inspect
|
154
|
+
code.should == 200
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
context ".delete" do
|
159
|
+
it "should works" do
|
160
|
+
code, data = Qiniu::Storage.delete(@bucket, @key)
|
161
|
+
puts data.inspect
|
162
|
+
code.should == 200
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
### 测试断点续上传
|
167
|
+
context ".resumable_upload_with_token" do
|
168
|
+
it "should works" do
|
169
|
+
upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"}
|
170
|
+
uptoken = Qiniu.generate_upload_token(upopts)
|
171
|
+
code, data, raw_headers = Qiniu::Storage.resumable_upload_with_token(
|
172
|
+
uptoken,
|
173
|
+
@localfile_5m,
|
174
|
+
@bucket,
|
175
|
+
@key_5m
|
176
|
+
)
|
177
|
+
(code/100).should == 2
|
178
|
+
puts data.inspect
|
179
|
+
puts raw_headers.inspect
|
180
|
+
puts "key_5m=#{@key_5m}"
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
context ".stat" do
|
185
|
+
it "should exists" do
|
186
|
+
code, data = Qiniu::Storage.stat(@bucket, @key_5m)
|
187
|
+
puts data.inspect
|
188
|
+
code.should == 200
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
context ".delete" do
|
193
|
+
it "should works" do
|
194
|
+
code, data = Qiniu::Storage.delete(@bucket, @key_5m)
|
195
|
+
puts data.inspect
|
196
|
+
code.should == 200
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
context ".resumable_upload_with_token2" do
|
201
|
+
it "should works" do
|
202
|
+
upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"}
|
203
|
+
uptoken = Qiniu.generate_upload_token(upopts)
|
204
|
+
code, data, raw_headers = Qiniu::Storage.resumable_upload_with_token(
|
205
|
+
uptoken,
|
206
|
+
@localfile_4m,
|
207
|
+
@bucket,
|
208
|
+
@key_4m
|
209
|
+
)
|
210
|
+
(code/100).should == 2
|
211
|
+
puts data.inspect
|
212
|
+
puts raw_headers.inspect
|
213
|
+
puts "key_4m=#{@key_4m}"
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
context ".stat" do
|
218
|
+
it "should exists" do
|
219
|
+
code, data = Qiniu::Storage.stat(@bucket, @key_4m)
|
220
|
+
puts data.inspect
|
221
|
+
code.should == 200
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
context ".delete" do
|
226
|
+
it "should works" do
|
227
|
+
code, data = Qiniu::Storage.delete(@bucket, @key_4m)
|
228
|
+
puts data.inspect
|
229
|
+
code.should == 200
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
context ".resumable_upload_with_token3" do
|
234
|
+
it "should works" do
|
235
|
+
upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"}
|
236
|
+
uptoken = Qiniu.generate_upload_token(upopts)
|
237
|
+
code, data, raw_headers = Qiniu::Storage.resumable_upload_with_token(
|
238
|
+
uptoken,
|
239
|
+
@localfile_8m,
|
240
|
+
@bucket,
|
241
|
+
@key_8m
|
242
|
+
)
|
243
|
+
(code/100).should == 2
|
244
|
+
puts data.inspect
|
245
|
+
puts raw_headers.inspect
|
246
|
+
puts "key_8m=#{@key_8m}"
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
context ".stat" do
|
251
|
+
it "should exists" do
|
252
|
+
code, data = Qiniu::Storage.stat(@bucket, @key_8m)
|
253
|
+
puts data.inspect
|
254
|
+
code.should == 200
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
context ".delete" do
|
259
|
+
it "should works" do
|
260
|
+
code, data = Qiniu::Storage.delete(@bucket, @key_8m)
|
261
|
+
puts data.inspect
|
262
|
+
code.should == 200
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
context ".resumable_upload_with_token4" do
|
267
|
+
it "should works" do
|
268
|
+
upopts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com"}
|
269
|
+
uptoken = Qiniu.generate_upload_token(upopts)
|
270
|
+
code, data, raw_headers = Qiniu::Storage.resumable_upload_with_token(
|
271
|
+
uptoken,
|
272
|
+
@localfile_1m,
|
273
|
+
@bucket,
|
274
|
+
@key_1m
|
275
|
+
)
|
276
|
+
(code/100).should == 2
|
277
|
+
puts data.inspect
|
278
|
+
puts raw_headers.inspect
|
279
|
+
puts "key_1m=#{@key_1m}"
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
context ".stat" do
|
284
|
+
it "should exists" do
|
285
|
+
code, data = Qiniu::Storage.stat(@bucket, @key_1m)
|
286
|
+
puts data.inspect
|
287
|
+
code.should == 200
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
context ".delete" do
|
292
|
+
it "should works" do
|
293
|
+
code, data = Qiniu::Storage.delete(@bucket, @key_1m)
|
294
|
+
puts data.inspect
|
295
|
+
code.should == 200
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
end
|
300
|
+
end # module Storage
|
301
|
+
end # module Qiniu
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'fakeweb'
|
5
|
+
require 'qiniu/utils'
|
6
|
+
|
7
|
+
module Qiniu
|
8
|
+
module RS
|
9
|
+
describe Utils do
|
10
|
+
before :all do
|
11
|
+
Struct.new("Response", :code, :body)
|
12
|
+
end
|
13
|
+
|
14
|
+
after :each do
|
15
|
+
FakeWeb.clean_registry
|
16
|
+
FakeWeb.allow_net_connect = true
|
17
|
+
end
|
18
|
+
|
19
|
+
context "safe_json_parse" do
|
20
|
+
it "should works" do
|
21
|
+
Utils.safe_json_parse('{"foo": "bar"}').should == {"foo" => "bar"}
|
22
|
+
Utils.safe_json_parse('{}').should == {}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context ".send_request_with" do
|
27
|
+
it "should works" do
|
28
|
+
FakeWeb.allow_net_connect = false
|
29
|
+
FakeWeb.register_uri(:get, "http://develoepr.qiniu.com/", :body => {:abc => 123}.to_json)
|
30
|
+
res = Utils.send_request_with 'http://develoepr.qiniu.com/', nil, :method => :get
|
31
|
+
res.should == [200, {"abc" => 123}, {}]
|
32
|
+
end
|
33
|
+
|
34
|
+
[400, 500].each do |code|
|
35
|
+
context "upstream return http #{code}" do
|
36
|
+
it "should raise RestClient::RequestFailed" do
|
37
|
+
FakeWeb.allow_net_connect = false
|
38
|
+
FakeWeb.register_uri(:get, "http://develoepr.qiniu.com/", :status => code)
|
39
|
+
lambda {
|
40
|
+
res = Utils.send_request_with 'http://develoepr.qiniu.com/', nil, :method => :get
|
41
|
+
}.should raise_error RestClient::RequestFailed
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|