|
| 1 | +require 'spec_helper' |
| 2 | +require 'cloudinary' |
| 3 | + |
| 4 | +describe Cloudinary::Utils do |
| 5 | + before(:each) do |
| 6 | + Cloudinary.config do |
| 7 | + |config| |
| 8 | + config.cloud_name = "test123" |
| 9 | + config.secure_distribution = nil |
| 10 | + config.private_cdn = false |
| 11 | + end |
| 12 | + end |
| 13 | + |
| 14 | + it "should use cloud_name from config" do |
| 15 | + result = Cloudinary::Utils.cloudinary_url("test") |
| 16 | + result.should == "http://res.cloudinary.com/test123/image/upload/test" |
| 17 | + end |
| 18 | + |
| 19 | + it "should allow overriding cloud_name in options" do |
| 20 | + options = {:cloud_name=>"test321"} |
| 21 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 22 | + options.should == {} |
| 23 | + result.should == "http://res.cloudinary.com/test321/image/upload/test" |
| 24 | + end |
| 25 | + |
| 26 | + it "should use default secure distribution if secure=true" do |
| 27 | + options = {:secure=>true} |
| 28 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 29 | + options.should == {} |
| 30 | + result.should == "https://d3jpl91pxevbkh.cloudfront.net/test123/image/upload/test" |
| 31 | + end |
| 32 | + |
| 33 | + it "should allow overriding secure distribution if secure=true" do |
| 34 | + options = {:secure=>true, :secure_distribution=>"something.else.com"} |
| 35 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 36 | + options.should == {} |
| 37 | + result.should == "https://something.else.com/test123/image/upload/test" |
| 38 | + end |
| 39 | + |
| 40 | + it "should take secure distribution from config if secure=true" do |
| 41 | + Cloudinary.config.secure_distribution = "config.secure.distribution.com" |
| 42 | + options = {:secure=>true} |
| 43 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 44 | + options.should == {} |
| 45 | + result.should == "https://config.secure.distribution.com/test123/image/upload/test" |
| 46 | + end |
| 47 | + |
| 48 | + it "should raise exception if secure is given with private_cdn and no secure_distribution" do |
| 49 | + Cloudinary.config.private_cdn = true |
| 50 | + lambda {Cloudinary::Utils.cloudinary_url("test", :secure=>true)}.should raise_error |
| 51 | + end |
| 52 | + |
| 53 | + it "should use format from options" do |
| 54 | + options = {:format=>:jpg} |
| 55 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 56 | + options.should == {} |
| 57 | + result.should == "http://res.cloudinary.com/test123/image/upload/test.jpg" |
| 58 | + end |
| 59 | + |
| 60 | + it "should use width and height from options only if crop is given" do |
| 61 | + options = {:width=>100, :height=>100} |
| 62 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 63 | + result.should == "http://res.cloudinary.com/test123/image/upload/test" |
| 64 | + options.should == {:width=>100, :height=>100} |
| 65 | + options = {:width=>100, :height=>100, :crop=>:crop} |
| 66 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 67 | + options.should == {:width=>100, :height=>100} |
| 68 | + result.should == "http://res.cloudinary.com/test123/image/upload/c_crop,h_100,w_100/test" |
| 69 | + end |
| 70 | + |
| 71 | + it "should use x, y, radius, prefix, gravity and quality from options" do |
| 72 | + options = {:x=>1, :y=>2, :radius=>3, :gravity=>:center, :quality=>0.4, :prefix=>"a"} |
| 73 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 74 | + options.should == {} |
| 75 | + result.should == "http://res.cloudinary.com/test123/image/upload/g_center,p_a,q_0.4,r_3,x_1,y_2/test" |
| 76 | + end |
| 77 | + |
| 78 | + it "should support named tranformation" do |
| 79 | + options = {:transformation=>"blip"} |
| 80 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 81 | + options.should == {} |
| 82 | + result.should == "http://res.cloudinary.com/test123/image/upload/t_blip/test" |
| 83 | + end |
| 84 | + |
| 85 | + it "should support array of named tranformations" do |
| 86 | + options = {:transformation=>["blip", "blop"]} |
| 87 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 88 | + options.should == {} |
| 89 | + result.should == "http://res.cloudinary.com/test123/image/upload/t_blip.blop/test" |
| 90 | + end |
| 91 | + |
| 92 | + it "should support base tranformation" do |
| 93 | + options = {:transformation=>{:x=>100, :y=>100, :crop=>:fill}, :crop=>:crop, :width=>100} |
| 94 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 95 | + options.should == {:width=>100} |
| 96 | + result.should == "http://res.cloudinary.com/test123/image/upload/c_fill,x_100,y_100/c_crop,w_100/test" |
| 97 | + end |
| 98 | + |
| 99 | + it "should support array of base tranformations" do |
| 100 | + options = {:transformation=>[{:x=>100, :y=>100, :width=>200, :crop=>:fill}, {:radius=>10}], :crop=>:crop, :width=>100} |
| 101 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 102 | + options.should == {:width=>100} |
| 103 | + result.should == "http://res.cloudinary.com/test123/image/upload/c_fill,w_200,x_100,y_100/r_10/c_crop,w_100/test" |
| 104 | + end |
| 105 | + |
| 106 | + it "should not include empty tranformations" do |
| 107 | + options = {:transformation=>[{}, {:x=>100, :y=>100, :crop=>:fill}, {}]} |
| 108 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 109 | + options.should == {} |
| 110 | + result.should == "http://res.cloudinary.com/test123/image/upload/c_fill,x_100,y_100/test" |
| 111 | + end |
| 112 | + |
| 113 | + it "should support size" do |
| 114 | + options = {:size=>"10x10", :crop=>:crop} |
| 115 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 116 | + options.should == {:width=>"10", :height=>"10"} |
| 117 | + result.should == "http://res.cloudinary.com/test123/image/upload/c_crop,h_10,w_10/test" |
| 118 | + end |
| 119 | + |
| 120 | + it "should use type from options" do |
| 121 | + options = {:type=>:facebook} |
| 122 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 123 | + options.should == {} |
| 124 | + result.should == "http://res.cloudinary.com/test123/image/facebook/test" |
| 125 | + end |
| 126 | + |
| 127 | + it "should use resource_type from options" do |
| 128 | + options = {:resource_type=>:raw} |
| 129 | + result = Cloudinary::Utils.cloudinary_url("test", options) |
| 130 | + options.should == {} |
| 131 | + result.should == "http://res.cloudinary.com/test123/raw/upload/test" |
| 132 | + end |
| 133 | + |
| 134 | + it "should ignore http links only if type is not given or is asset" do |
| 135 | + options = {:type=>nil} |
| 136 | + result = Cloudinary::Utils.cloudinary_url("http://test", options) |
| 137 | + options.should == {} |
| 138 | + result.should == "http://test" |
| 139 | + options = {:type=>:asset} |
| 140 | + result = Cloudinary::Utils.cloudinary_url("http://test", options) |
| 141 | + options.should == {} |
| 142 | + result.should == "http://test" |
| 143 | + options = {:type=>:fetch} |
| 144 | + result = Cloudinary::Utils.cloudinary_url("http://test", options) |
| 145 | + options.should == {} |
| 146 | + result.should == "http://res.cloudinary.com/test123/image/fetch/http://test" |
| 147 | + end |
| 148 | + |
| 149 | + it "should use allow absolute links to /images" do |
| 150 | + options = {} |
| 151 | + result = Cloudinary::Utils.cloudinary_url("/images/test", options) |
| 152 | + options.should == {} |
| 153 | + result.should == "http://res.cloudinary.com/test123/image/upload/test" |
| 154 | + end |
| 155 | + |
| 156 | + it "should use ignore absolute links not to /images" do |
| 157 | + options = {} |
| 158 | + result = Cloudinary::Utils.cloudinary_url("/js/test", options) |
| 159 | + options.should == {} |
| 160 | + result.should == "/js/test" |
| 161 | + end |
| 162 | + |
| 163 | + it "should escape fetch urls" do |
| 164 | + options = {:type=>:fetch} |
| 165 | + result = Cloudinary::Utils.cloudinary_url("http://blah.com/hello?a=b", options) |
| 166 | + options.should == {} |
| 167 | + result.should == "http://res.cloudinary.com/test123/image/fetch/http://blah.com/hello%3Fa%3Db" |
| 168 | + end |
| 169 | +end |
0 commit comments