qiniu 6.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'qiniu/abstract'
5
+
6
+ describe Qiniu::Abstract do
7
+ before(:each) do
8
+ @klass = Class.new do
9
+ include Qiniu::Abstract
10
+
11
+ abstract_methods :foo, :bar
12
+ end
13
+ end
14
+
15
+ it "raises NotImplementedError" do
16
+ proc {
17
+ @klass.new.foo
18
+ }.should raise_error(NotImplementedError)
19
+ end
20
+
21
+ it "can be overridden" do
22
+ subclass = Class.new(@klass) do
23
+ def foo
24
+ :overridden
25
+ end
26
+ end
27
+
28
+ subclass.new.foo.should == :overridden
29
+ end
30
+ end
@@ -0,0 +1,87 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'qiniu/auth'
5
+ require 'qiniu'
6
+ require 'qiniu/fop'
7
+
8
+ module Qiniu
9
+ module Fop
10
+ describe Fop do
11
+
12
+ before :all do
13
+
14
+ @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s
15
+ @key = "image_logo_for_test.png"
16
+
17
+ result = Qiniu.mkbucket(@bucket)
18
+ puts result.inspect
19
+ result.should be_true
20
+
21
+ local_file = File.expand_path('../' + @key, __FILE__)
22
+
23
+ upopts = {
24
+ :scope => @bucket,
25
+ :expires_in => 3600,
26
+ :customer => "why404@gmail.com",
27
+ :async_options => "imageView/1/w/120/h/120",
28
+ :return_body => '{"size":$(fsize), "hash":$(etag), "width":$(imageInfo.width), "height":$(imageInfo.height)}'
29
+ }
30
+ uptoken = Qiniu.generate_upload_token(upopts)
31
+ data = Qiniu.upload_file :uptoken => uptoken, :file => local_file, :bucket => @bucket, :key => @key
32
+ puts data.inspect
33
+
34
+ data["size"].should_not be_zero
35
+ data["hash"].should_not be_empty
36
+ data["width"].should_not be_zero
37
+ data["height"].should_not be_zero
38
+
39
+ result = Qiniu.get(@bucket, @key)
40
+ result["url"].should_not be_empty
41
+ puts result.inspect
42
+ @source_image_url = result["url"]
43
+
44
+ @mogrify_options = {
45
+ :thumbnail => "!120x120>",
46
+ :gravity => "center",
47
+ :crop => "!120x120a0a0",
48
+ :quality => 85,
49
+ :rotate => 45,
50
+ :format => "jpg",
51
+ :auto_orient => true
52
+ }
53
+ end
54
+
55
+ after :all do
56
+ result = Qiniu.drop(@bucket)
57
+ puts result.inspect
58
+ result.should_not be_false
59
+ end
60
+
61
+ context ".info" do
62
+ it "should works" do
63
+ code, data = Qiniu::Fop::Image.info(@source_image_url)
64
+ code.should == 200
65
+ puts data.inspect
66
+ end
67
+ end
68
+
69
+ =begin
70
+ context ".exif" do
71
+ it "should works" do
72
+ code, data = Qiniu::Fop::Image.exif(@source_image_url)
73
+ puts data.inspect
74
+ end
75
+ end
76
+ =end
77
+
78
+ context ".mogrify_preview_url" do
79
+ it "should works" do
80
+ mogrify_preview_url = Qiniu::Fop::Image.mogrify_preview_url(@source_image_url, @mogrify_options)
81
+ puts mogrify_preview_url.inspect
82
+ end
83
+ end
84
+
85
+ end
86
+ end # module Fop
87
+ end # module Qiniu
@@ -0,0 +1,181 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'digest/sha1'
4
+ require 'spec_helper'
5
+ require 'qiniu/auth'
6
+ require 'qiniu/management'
7
+ require 'qiniu'
8
+
9
+ module Qiniu
10
+ module Storage
11
+ describe Storage do
12
+
13
+ before :all do
14
+ @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s
15
+ @key = Digest::SHA1.hexdigest((Time.now.to_i+rand(100)).to_s)
16
+ @key2 = @key + rand(100).to_s
17
+
18
+ code, data = Storage.mkbucket(@bucket)
19
+ puts [code, data].inspect
20
+ code.should == 200
21
+ end
22
+
23
+ after :all do
24
+ code, data = Storage.drop(@bucket)
25
+ puts [code, data].inspect
26
+ code.should == 200
27
+ end
28
+
29
+ context ".put_file" do
30
+ it "should works" do
31
+ code, data = Storage.put_file(__FILE__, @bucket, @key, 'application/x-ruby', 'customMeta', true)
32
+ code.should == 200
33
+ puts data.inspect
34
+ end
35
+ end
36
+
37
+ context ".buckets" do
38
+ it "should works" do
39
+ code, data = Storage.buckets
40
+ code.should == 200
41
+ puts data.inspect
42
+ end
43
+ end
44
+
45
+ context ".stat" do
46
+ it "should works" do
47
+ code, data = Storage.stat(@bucket, @key)
48
+ code.should == 200
49
+ puts data.inspect
50
+ end
51
+ end
52
+
53
+ context ".get" do
54
+ it "should works" do
55
+ code, data = Storage.get(@bucket, @key, "rs_spec.rb", 1)
56
+ code.should == 200
57
+ puts data.inspect
58
+ end
59
+ end
60
+
61
+ context ".batch" do
62
+ it "should works" do
63
+ code, data = Storage.batch("stat", @bucket, [@key])
64
+ code.should == 200
65
+ puts data.inspect
66
+ end
67
+ end
68
+
69
+ context ".batch_stat" do
70
+ it "should works" do
71
+ code, data = Storage.batch_stat(@bucket, [@key])
72
+ code.should == 200
73
+ puts data.inspect
74
+ end
75
+ end
76
+
77
+ context ".batch_get" do
78
+ it "should works" do
79
+ code, data = Storage.batch_get(@bucket, [@key])
80
+ code.should == 200
81
+ puts data.inspect
82
+ end
83
+ end
84
+
85
+ context ".batch_copy" do
86
+ it "should works" do
87
+ code, data = Storage.batch_copy @bucket, @key, @bucket, @key2
88
+ code.should == 200
89
+ puts data.inspect
90
+
91
+ #code2, data2 = Qiniu.stat(@bucket, @key2)
92
+ #code2.should == 200
93
+ #puts data2.inspect
94
+
95
+ code, data = Storage.delete @bucket, @key2
96
+ code.should == 200
97
+ puts data.inspect
98
+ end
99
+ end
100
+
101
+ context ".batch_move" do
102
+ it "should works" do
103
+ code, data = Storage.batch_move @bucket, @key, @bucket, @key2
104
+ code.should == 200
105
+ puts data.inspect
106
+
107
+ #code2, data2 = Qiniu.stat(@bucket, @key2)
108
+ #code2.should == 200
109
+ #puts data2.inspect
110
+
111
+ code3, data3 = Storage.batch_move @bucket, @key2, @bucket, @key
112
+ code3.should == 200
113
+ puts data3.inspect
114
+ end
115
+ end
116
+
117
+ =begin
118
+ context ".publish" do
119
+ it "should works" do
120
+ code, data = Qiniu.publish(@domain, @bucket)
121
+ code.should == 200
122
+ puts data.inspect
123
+ end
124
+ end
125
+
126
+ context ".unpublish" do
127
+ it "should works" do
128
+ code, data = Qiniu.unpublish(@domain)
129
+ code.should == 200
130
+ puts data.inspect
131
+ end
132
+ end
133
+ =end
134
+
135
+ context ".move" do
136
+ it "should works" do
137
+ code, data = Storage.move(@bucket, @key, @bucket, @key2)
138
+ code.should == 200
139
+ puts data.inspect
140
+
141
+ code2, data2 = Storage.stat(@bucket, @key2)
142
+ code2.should == 200
143
+ puts data2.inspect
144
+
145
+ code3, data3 = Storage.move(@bucket, @key2, @bucket, @key)
146
+ code3.should == 200
147
+ puts data3.inspect
148
+ end
149
+ end
150
+
151
+ context ".copy" do
152
+ it "should works" do
153
+ code, data = Storage.copy(@bucket, @key, @bucket, @key2)
154
+ code.should == 200
155
+ puts data.inspect
156
+
157
+ #code2, data2 = Qiniu.stat(@bucket, @key2)
158
+ #code2.should == 200
159
+ #puts data2.inspect
160
+ end
161
+ end
162
+
163
+ context ".delete" do
164
+ it "should works" do
165
+ code, data = Storage.delete(@bucket, @key)
166
+ code.should == 200
167
+ puts data.inspect
168
+ end
169
+ end
170
+
171
+ context ".drop" do
172
+ it "should works" do
173
+ code, data = Storage.drop(@bucket)
174
+ code.should == 200
175
+ puts data.inspect
176
+ end
177
+ end
178
+
179
+ end
180
+ end # module Storage
181
+ end # module Qiniu
@@ -0,0 +1,59 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'spec_helper'
4
+ require 'qiniu/auth'
5
+ require 'qiniu'
6
+ require 'qiniu/misc'
7
+
8
+ module Qiniu
9
+ module Misc
10
+ describe Misc do
11
+
12
+ before :all do
13
+ @bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s
14
+ result = Qiniu.mkbucket(@bucket)
15
+ puts result.inspect
16
+ result.should_not be_false
17
+ end
18
+
19
+ after :all do
20
+ result = Qiniu.drop(@bucket)
21
+ puts result.inspect
22
+ result.should_not be_false
23
+ end
24
+
25
+ context ".set_protected" do
26
+ it "should works" do
27
+ code, data = Qiniu::Misc.set_protected(@bucket, 1)
28
+ code.should == 200
29
+ puts data.inspect
30
+ end
31
+ end
32
+
33
+ context ".set_separator" do
34
+ it "should works" do
35
+ code, data = Qiniu::Misc.set_separator(@bucket, "-")
36
+ code.should == 200
37
+ puts data.inspect
38
+ end
39
+ end
40
+
41
+ context ".set_style" do
42
+ it "should works" do
43
+ code, data = Qiniu::Misc.set_style(@bucket, "small.jpg", "imageMogr/auto-orient/thumbnail/!120x120r/gravity/center/crop/!120x120/quality/80")
44
+ code.should == 200
45
+ puts data.inspect
46
+ end
47
+ end
48
+
49
+ context ".unset_style" do
50
+ it "should works" do
51
+ code, data = Qiniu::Misc.unset_style(@bucket, "small.jpg")
52
+ code.should == 200
53
+ puts data.inspect
54
+ end
55
+ end
56
+
57
+ end
58
+ end # module Misc
59
+ end # module Qiniu
@@ -0,0 +1,385 @@
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 = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s
13
+ @key = Digest::SHA1.hexdigest Time.now.to_s
14
+ @key2 = @key + rand(100).to_s
15
+ #@domain = @bucket + '.dn.qbox.me'
16
+
17
+ result = Qiniu.mkbucket(@bucket)
18
+ result.should_not be_false
19
+
20
+ @test_image_bucket = 'RubySdkTest' + (Time.now.to_i+rand(1000)).to_s
21
+ result2 = Qiniu.mkbucket(@test_image_bucket)
22
+ puts result2.inspect
23
+ result2.should be_true
24
+
25
+ @test_image_key = 'image_logo_for_test.png'
26
+ local_file = File.expand_path('./' + @test_image_key, File.dirname(__FILE__))
27
+ puts local_file.inspect
28
+ upopts = {:scope => @test_image_bucket, :expires_in => 3600, :customer => "why404@gmail.com"}
29
+ uptoken = Qiniu.generate_upload_token(upopts)
30
+ data = Qiniu.upload_file :uptoken => uptoken, :file => local_file, :bucket => @test_image_bucket, :key => @test_image_key
31
+ puts data.inspect
32
+ end
33
+
34
+ after :all do
35
+ #result = Qiniu.unpublish(@domain)
36
+ #result.should_not be_false
37
+
38
+ result1 = Qiniu.drop(@bucket)
39
+ puts result1.inspect
40
+ result1.should_not be_false
41
+
42
+ result2 = Qiniu.drop(@test_image_bucket)
43
+ puts result2.inspect
44
+ result2.should_not be_false
45
+ end
46
+
47
+ context ".put_file" do
48
+ it "should works" do
49
+ result = Qiniu.put_file :file => __FILE__,
50
+ :bucket => @bucket,
51
+ :key => @key,
52
+ :mime_type => 'application/x-ruby',
53
+ :enable_crc32_check => true
54
+ result.should be_true
55
+ end
56
+ end
57
+
58
+ context ".buckets" do
59
+ it "should works" do
60
+ result = Qiniu.buckets
61
+ result.should_not be_false
62
+ puts result.inspect
63
+ end
64
+ end
65
+
66
+ context ".set_protected" do
67
+ it "should works" do
68
+ result = Qiniu.set_protected(@bucket, 1)
69
+ result.should_not be_false
70
+ puts result.inspect
71
+ end
72
+ end
73
+
74
+ context ".set_separator" do
75
+ it "should works" do
76
+ result = Qiniu.set_separator(@bucket, "-")
77
+ result.should_not be_false
78
+ puts result.inspect
79
+ end
80
+ end
81
+
82
+ context ".set_style" do
83
+ it "should works" do
84
+ result = Qiniu.set_style(@bucket, "small.jpg", "imageMogr/auto-orient/thumbnail/!120x120r/gravity/center/crop/!120x120/quality/80")
85
+ result.should_not be_false
86
+ puts result.inspect
87
+ end
88
+ end
89
+
90
+ context ".unset_style" do
91
+ it "should works" do
92
+ result = Qiniu.unset_style(@bucket, "small.jpg")
93
+ result.should_not be_false
94
+ puts result.inspect
95
+ end
96
+ end
97
+
98
+ context ".upload_file" do
99
+ it "should works" do
100
+ uptoken_opts = {:scope => @bucket, :escape => 0}
101
+ upload_opts = {
102
+ :uptoken => Qiniu.generate_upload_token(uptoken_opts),
103
+ :file => __FILE__,
104
+ :bucket => @bucket,
105
+ :key => @key,
106
+ :enable_crc32_check => true
107
+ }
108
+ result = Qiniu.upload_file(upload_opts)
109
+ result.should_not be_false
110
+ puts result.inspect
111
+ end
112
+
113
+ it "should raise MissingArgsError" do
114
+ uptoken_opts = {:scope => @bucket, :escape => 0}
115
+ upload_opts = {
116
+ :uptoken => Qiniu.generate_upload_token(uptoken_opts),
117
+ :file => __FILE__,
118
+ :key => @key,
119
+ :enable_crc32_check => true
120
+ }
121
+ lambda { Qiniu.upload_file(upload_opts) }.should raise_error(MissingArgsError)
122
+ end
123
+
124
+ it "should raise NoSuchFileError" do
125
+ uptoken_opts = {:scope => @bucket, :escape => 0}
126
+ upload_opts = {
127
+ :uptoken => Qiniu.generate_upload_token(uptoken_opts),
128
+ :file => 'no_this_file',
129
+ :bucket => @bucket,
130
+ :key => @key,
131
+ :enable_crc32_check => true
132
+ }
133
+ lambda { Qiniu.upload_file(upload_opts) }.should raise_error(NoSuchFileError)
134
+ end
135
+ end
136
+
137
+ context ".resumable_upload_file" do
138
+ it "should works" do
139
+ # generate bigfile for testing
140
+ localfile = "test_bigfile"
141
+ File.open(localfile, "w"){|f| 5242888.times{f.write(rand(9).to_s)}}
142
+ key = Digest::SHA1.hexdigest(localfile+Time.now.to_s)
143
+ # generate the upload token
144
+ uptoken_opts = {:scope => @bucket, :expires_in => 3600, :customer => "why404@gmail.com", :escape => 0}
145
+ uptoken = Qiniu.generate_upload_token(uptoken_opts)
146
+ # uploading
147
+ upload_opts = {
148
+ :uptoken => uptoken,
149
+ :file => localfile,
150
+ :bucket => @bucket,
151
+ :key => key
152
+ }
153
+ #uploading
154
+ result1 = Qiniu.upload_file(upload_opts)
155
+ #drop the bigfile
156
+ File.unlink(localfile) if File.exists?(localfile)
157
+ #expect
158
+ puts result1.inspect
159
+ result1.should_not be_false
160
+ result1.should_not be_empty
161
+ #stat
162
+ result2 = Qiniu.stat(@bucket, key)
163
+ puts result2.inspect
164
+ result2.should_not be_false
165
+ #delete
166
+ result3 = Qiniu.delete(@bucket, key)
167
+ puts result3.inspect
168
+ result3.should_not be_false
169
+ end
170
+ end
171
+
172
+ context ".stat" do
173
+ it "should works" do
174
+ result = Qiniu.stat(@bucket, @key)
175
+ result.should_not be_false
176
+ result.should_not be_empty
177
+ puts result.inspect
178
+ end
179
+ end
180
+
181
+ context ".get" do
182
+ it "should works" do
183
+ result = Qiniu.get(@bucket, @key, "rs_spec.rb", 10)
184
+ result.should_not be_false
185
+ result.should_not be_empty
186
+ puts result.inspect
187
+ end
188
+ end
189
+
190
+ context ".download" do
191
+ it "should works" do
192
+ result = Qiniu.download(@bucket, @key, "rs_spec.rb", 10)
193
+ result.should_not be_false
194
+ result.should_not be_empty
195
+ puts result.inspect
196
+ end
197
+ end
198
+
199
+ context ".batch" do
200
+ it "should works" do
201
+ result = Qiniu.batch("stat", @bucket, [@key])
202
+ result.should_not be_false
203
+ result.should_not be_empty
204
+ puts result.inspect
205
+ end
206
+ end
207
+
208
+ context ".batch_stat" do
209
+ it "should works" do
210
+ result = Qiniu.batch_stat(@bucket, [@key])
211
+ result.should_not be_false
212
+ result.should_not be_empty
213
+ puts result.inspect
214
+ end
215
+ end
216
+
217
+ context ".batch_get" do
218
+ it "should works" do
219
+ result = Qiniu.batch_get(@bucket, [@key])
220
+ result.should_not be_false
221
+ result.should_not be_empty
222
+ puts result.inspect
223
+ end
224
+ end
225
+
226
+ context ".batch_download" do
227
+ it "should works" do
228
+ result = Qiniu.batch_download(@bucket, [@key])
229
+ result.should_not be_false
230
+ result.should_not be_empty
231
+ puts result.inspect
232
+ end
233
+ end
234
+
235
+ =begin
236
+ context ".publish" do
237
+ it "should works" do
238
+ result = Qiniu.publish(@domain, @bucket)
239
+ result.should_not be_false
240
+ end
241
+ end
242
+ =end
243
+
244
+ =begin
245
+ context ".unpublish" do
246
+ it "should works" do
247
+ result = Qiniu.unpublish(@domain)
248
+ result.should_not be_false
249
+ end
250
+ end
251
+ =end
252
+
253
+ =begin
254
+ context ".batch_copy" do
255
+ it "should works" do
256
+ result = Qiniu.batch_copy [@bucket, @key, @bucket, @key2]
257
+ result.should_not be_false
258
+
259
+ #result2 = Qiniu.stat(@bucket, @key2)
260
+ #result2.should_not be_false
261
+ end
262
+ end
263
+
264
+ context ".batch_move" do
265
+ it "should works" do
266
+ result = Qiniu.batch_move [@bucket, @key, @bucket, @key2]
267
+ result.should_not be_false
268
+
269
+ #result2 = Qiniu.stat(@bucket, @key2)
270
+ #result2.should_not be_false
271
+
272
+ result3 = Qiniu.batch_move [@bucket, @key2, @bucket, @key]
273
+ result3.should_not be_false
274
+ end
275
+ end
276
+ =end
277
+
278
+ context ".move" do
279
+ it "should works" do
280
+ result = Qiniu.move(@bucket, @key, @bucket, @key2)
281
+ result.should_not be_false
282
+
283
+ result2 = Qiniu.stat(@bucket, @key2)
284
+ result2.should_not be_false
285
+
286
+ result3 = Qiniu.move(@bucket, @key2, @bucket, @key)
287
+ result3.should_not be_false
288
+ end
289
+ end
290
+
291
+ context ".copy" do
292
+ it "should works" do
293
+ result = Qiniu.copy(@bucket, @key, @bucket, @key2)
294
+ result.should_not be_false
295
+
296
+ #result2 = Qiniu.stat(@bucket, @key2)
297
+ #result2.should_not be_false
298
+ end
299
+ end
300
+
301
+ context ".delete" do
302
+ it "should works" do
303
+ result = Qiniu.delete(@bucket, @key)
304
+ result.should_not be_false
305
+ end
306
+ end
307
+
308
+ context ".drop" do
309
+ it "should works" do
310
+ result = Qiniu.drop(@bucket)
311
+ result.should_not be_false
312
+ end
313
+ end
314
+
315
+ context ".image_info" do
316
+ it "should works" do
317
+ data = Qiniu.get(@test_image_bucket, @test_image_key)
318
+ data.should_not be_false
319
+ data.should_not be_empty
320
+ puts data.inspect
321
+ result = Qiniu.image_info(data["url"])
322
+ result.should_not be_false
323
+ result.should_not be_empty
324
+ puts result.inspect
325
+ end
326
+ end
327
+
328
+ =begin
329
+ context ".image_exif" do
330
+ it "should works" do
331
+ data = Qiniu.get(@test_image_bucket, @test_image_key)
332
+ data.should_not be_false
333
+ data.should_not be_empty
334
+ puts data.inspect
335
+ result = Qiniu.image_exif(data["url"])
336
+ puts result.inspect
337
+ end
338
+ end
339
+ =end
340
+
341
+ context ".image_mogrify_save_as" do
342
+ it "should works" do
343
+ data = Qiniu.get(@test_image_bucket, @test_image_key)
344
+ data.should_not be_false
345
+ data.should_not be_empty
346
+ puts data.inspect
347
+
348
+ dest_key = "cropped-" + @test_image_key
349
+ src_img_url = data["url"]
350
+ mogrify_options = {
351
+ :thumbnail => "!120x120>",
352
+ :gravity => "center",
353
+ :crop => "!120x120a0a0",
354
+ :quality => 85,
355
+ :rotate => 45,
356
+ :format => "jpg",
357
+ :auto_orient => true
358
+ }
359
+ result2 = Qiniu.image_mogrify_save_as(@test_image_bucket, dest_key, src_img_url, mogrify_options)
360
+ result2.should_not be_false
361
+ result2.should_not be_empty
362
+ puts result2.inspect
363
+ end
364
+ end
365
+
366
+ context ".generate_upload_token" do
367
+ it "should works" do
368
+ data = Qiniu.generate_upload_token({:scope => @bucket, :expires_in => 3600, :escape => 0})
369
+ data.should_not be_empty
370
+ puts data.inspect
371
+ data.split(":").length.should == 3
372
+ end
373
+ end
374
+
375
+ context ".generate_download_token" do
376
+ it "should works" do
377
+ data = Qiniu.generate_download_token({:expires_in => 1, :pattern => 'http://*.dn.qbox.me/*'})
378
+ data.should_not be_empty
379
+ puts data.inspect
380
+ data.split(":").length.should == 3
381
+ end
382
+ end
383
+
384
+ end
385
+ end # module Qiniu