Skip to content

Commit fa562f4

Browse files
author
Daniel Cohen
committed
added tests
1 parent 324bd58 commit fa562f4

File tree

2 files changed

+126
-28
lines changed

2 files changed

+126
-28
lines changed

cloudinary-core/src/main/java/com/cloudinary/Url.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,15 @@ public String generate(String source) {
208208
}
209209
}
210210

211+
if (source.contains("/") && !source.matches("v[0-9]+.*") && !source.matches("https?:/.*") && StringUtils.isEmpty(version)) {
212+
version = "1";
213+
}
214+
215+
if (version == null)
216+
version = "";
217+
else
218+
version = "v" + version;
219+
211220
if (type!=null && type.equals("fetch") && !StringUtils.isEmpty(format)) {
212221
transformation().fetchFormat(format);
213222
this.format = null;
@@ -221,14 +230,6 @@ public String generate(String source) {
221230
sourceToSign = finalizedSource[1];
222231

223232

224-
if (source.contains("/") && !source.matches("v[0-9]+.*") && !source.matches("https?:/.*") && StringUtils.isEmpty(version)) {
225-
version = "1";
226-
}
227-
228-
if (version == null)
229-
version = "";
230-
else
231-
version = "v" + version;
232233

233234
if (signUrl) {
234235
MessageDigest md = null;
@@ -258,7 +259,7 @@ private String[] finalizeSource(String source, String format, String urlSuffix)
258259
String[] result = new String[2];
259260
source = source.replaceAll("([^:])//", "\1/");
260261

261-
if (source.toLowerCase().matches("^https?:/")) {
262+
if (source.toLowerCase().matches("^https?:/.*")) {
262263
source = SmartUrlEncoder.encode(source);
263264
sourceToSign = source;
264265
} else {
@@ -269,7 +270,7 @@ private String[] finalizeSource(String source, String format, String urlSuffix)
269270
}
270271
sourceToSign = source;
271272
if (StringUtils.isNotBlank(urlSuffix)) {
272-
if (urlSuffix.matches("([\\./]))")) {
273+
if (urlSuffix.matches("\\w*[\\./]\\w*")) {
273274
throw new RuntimeException("url_suffix should not include . or /");
274275
}
275276
source = source + "/" + urlSuffix;

cloudinary-http42/src/test/java/com/cloudinary/test/CloudinaryTest.java

Lines changed: 115 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.net.URLDecoder;
1010
import java.util.HashMap;
1111
import java.util.Map;
12+
import java.util.regex.Matcher;
13+
import java.util.regex.Pattern;
1214

1315
import org.junit.Before;
1416
import org.junit.Rule;
@@ -23,14 +25,15 @@ public class CloudinaryTest {
2325

2426
private Cloudinary cloudinary;
2527

26-
@Rule public TestName currentTest = new TestName();
28+
@Rule
29+
public TestName currentTest = new TestName();
2730

2831
@Before
2932
public void setUp() {
30-
System.out.println("Running " +this.getClass().getName()+"."+ currentTest.getMethodName());
33+
System.out.println("Running " + this.getClass().getName() + "." + currentTest.getMethodName());
3134
this.cloudinary = new Cloudinary("cloudinary://a:b@test123");
3235
}
33-
36+
3437
@Test
3538
public void testCloudName() {
3639
// should use cloud_name from config
@@ -71,8 +74,8 @@ public void testSecureDistibution() {
7174
public void testSecureAkamai() {
7275
// should default to akamai if secure is given with private_cdn and no
7376
// secure_distribution
74-
cloudinary.config.secure = true;
75-
cloudinary.config.privateCdn =true;
77+
cloudinary.config.secure = true;
78+
cloudinary.config.privateCdn = true;
7679
String result = cloudinary.url().generate("test");
7780
assertEquals("https://test123-res.cloudinary.com/image/upload/test", result);
7881
}
@@ -81,8 +84,8 @@ public void testSecureAkamai() {
8184
public void testSecureNonAkamai() {
8285
// should not add cloud_name if private_cdn and secure non akamai
8386
// secure_distribution
84-
cloudinary.config.secure = true;
85-
cloudinary.config.privateCdn =true;
87+
cloudinary.config.secure = true;
88+
cloudinary.config.privateCdn = true;
8689
cloudinary.config.secureDistribution = "something.cloudfront.net";
8790
String result = cloudinary.url().generate("test");
8891
assertEquals("https://something.cloudfront.net/image/upload/test", result);
@@ -91,7 +94,7 @@ public void testSecureNonAkamai() {
9194
@Test
9295
public void testHttpPrivateCdn() {
9396
// should not add cloud_name if private_cdn and not secure
94-
cloudinary.config.privateCdn =true;
97+
cloudinary.config.privateCdn = true;
9598
String result = cloudinary.url().generate("test");
9699
assertEquals("http://test123-res.cloudinary.com/image/upload/test", result);
97100
}
@@ -151,8 +154,7 @@ public void testBaseTransformations() {
151154
@Test
152155
public void testBaseTransformationArray() {
153156
// should support array of base transformations
154-
Transformation transformation = new Transformation().x(100).y(100).width(200).crop("fill").chain().radius(10).chain().crop("crop")
155-
.width(100);
157+
Transformation transformation = new Transformation().x(100).y(100).width(200).crop("fill").chain().radius(10).chain().crop("crop").width(100);
156158
String result = cloudinary.url().transformation(transformation).generate("test");
157159
assertEquals("100", transformation.getHtmlWidth().toString());
158160
assertEquals("http://res.cloudinary.com/test123/image/upload/c_fill,w_200,x_100,y_100/r_10/c_crop,w_100/test", result);
@@ -339,7 +341,7 @@ public void testFlags() {
339341
result = cloudinary.url().transformation(transformation).generate("test");
340342
assertEquals("http://res.cloudinary.com/test123/image/upload/fl_abc.def/test", result);
341343
}
342-
344+
343345
@Test
344346
public void testOpacity() {
345347
// should support opacity
@@ -353,9 +355,7 @@ public void testOpacity() {
353355
public void testImageTag() {
354356
Transformation transformation = new Transformation().width(100).height(101).crop("crop");
355357
String result = cloudinary.url().transformation(transformation).imageTag("test", ObjectUtils.asMap("alt", "my image"));
356-
assertEquals(
357-
"<img src='http://res.cloudinary.com/test123/image/upload/c_crop,h_101,w_100/test' alt='my image' height='101' width='100'/>",
358-
result);
358+
assertEquals("<img src='http://res.cloudinary.com/test123/image/upload/c_crop,h_101,w_100/test' alt='my image' height='101' width='100'/>", result);
359359
transformation = new Transformation().width(0.9).height(0.9).crop("crop").responsiveWidth(true);
360360
result = cloudinary.url().transformation(transformation).imageTag("test", ObjectUtils.asMap("alt", "my image"));
361361
assertEquals(
@@ -457,22 +457,119 @@ public void testSignedUrl() {
457457
actual = cloudinary.url().transformation(new Transformation().crop("crop").width(10).height(20)).signed(true).generate("image.jpg");
458458
assertEquals(expected, actual);
459459
}
460-
460+
461461
@Test
462462
public void testResponsiveWidth() {
463463
// should support responsive width
464464
Transformation trans = new Transformation().width(100).height(100).crop("crop").responsiveWidth(true);
465465
String result = cloudinary.url().transformation(trans).generate("test");
466466
assertTrue(trans.isResponsive());
467-
assertEquals("http://res.cloudinary.com/test123/image/upload/c_crop,h_100,w_100/c_limit,w_auto/test", result);
468-
Transformation.setResponsiveWidthTransformation(ObjectUtils.asMap("width", "auto", "crop", "pad"));
467+
assertEquals("http://res.cloudinary.com/test123/image/upload/c_crop,h_100,w_100/c_limit,w_auto/test", result);
468+
Transformation.setResponsiveWidthTransformation(ObjectUtils.asMap("width", "auto", "crop", "pad"));
469469
trans = new Transformation().width(100).height(100).crop("crop").responsiveWidth(true);
470470
result = cloudinary.url().transformation(trans).generate("test");
471471
assertTrue(trans.isResponsive());
472472
assertEquals("http://res.cloudinary.com/test123/image/upload/c_crop,h_100,w_100/c_pad,w_auto/test", result);
473473
Transformation.setResponsiveWidthTransformation(null);
474474
}
475-
475+
476+
@Test(expected = RuntimeException.class)
477+
public void testDisallowUrlSuffixInSharedDistribution() {
478+
cloudinary.url().urlSuffix("hello").generate("test");
479+
}
480+
481+
@Test(expected = RuntimeException.class)
482+
public void testDisallowUrlSuffixInNonUploadTypes() {
483+
cloudinary.url().urlSuffix("hello").privateCdn(true).type("facebook").generate("test");
484+
485+
}
486+
487+
@Test(expected = RuntimeException.class)
488+
public void testDisallowUrlSuffixWithSlash() {
489+
cloudinary.url().urlSuffix("hello/world").privateCdn(true).generate("test");
490+
}
491+
492+
@Test(expected = RuntimeException.class)
493+
public void testDisallowUrlSuffixWithDot() {
494+
cloudinary.url().urlSuffix("hello.world").privateCdn(true).generate("test");
495+
}
496+
497+
@Test
498+
public void testSupportUrlSuffixForPrivateCdn() {
499+
String actual = cloudinary.url().urlSuffix("hello").privateCdn(true).generate("test");
500+
assertEquals("http://test123-res.cloudinary.com/images/test/hello", actual);
501+
502+
actual = cloudinary.url().urlSuffix("hello").privateCdn(true).transformation(new Transformation().angle(0)).generate("test");
503+
assertEquals("http://test123-res.cloudinary.com/images/a_0/test/hello", actual);
504+
505+
}
506+
507+
@Test
508+
public void testPutFormatAfterUrlSuffix() {
509+
String actual = cloudinary.url().urlSuffix("hello").privateCdn(true).format("jpg").generate("test");
510+
assertEquals("http://test123-res.cloudinary.com/images/test/hello.jpg", actual);
511+
}
512+
513+
@Test
514+
public void testNotSignTheUrlSuffix() {
515+
516+
Pattern pattern = Pattern.compile("s--[0-9A-Za-z_-]{8}--");
517+
String url = cloudinary.url().format("jpg").signed(true).generate("test");
518+
Matcher matcher = pattern.matcher(url);
519+
matcher.find();
520+
String expectedSignature = url.substring(matcher.start(), matcher.end());
521+
522+
String actual = cloudinary.url().format("jpg").privateCdn(true).signed(true).urlSuffix("hello").generate("test");
523+
assertEquals("http://test123-res.cloudinary.com/images/" + expectedSignature + "/test/hello.jpg", actual);
524+
525+
url = cloudinary.url().format("jpg").signed(true).transformation(new Transformation().angle(0)).generate("test");
526+
matcher = pattern.matcher(url);
527+
matcher.find();
528+
expectedSignature = url.substring(matcher.start(), matcher.end());
529+
530+
actual = cloudinary.url().format("jpg").privateCdn(true).signed(true).urlSuffix("hello").transformation(new Transformation().angle(0)).generate("test");
531+
532+
assertEquals("http://test123-res.cloudinary.com/images/" + expectedSignature + "/a_0/test/hello.jpg", actual);
533+
}
534+
535+
@Test
536+
public void testSupportUrlSuffixForRawUploads() {
537+
String actual = cloudinary.url().urlSuffix("hello").privateCdn(true).resourceType("raw").generate("test");
538+
assertEquals("http://test123-res.cloudinary.com/files/test/hello", actual);
539+
}
540+
541+
@Test(expected = RuntimeException.class)
542+
public void testDisllowUseRootPathInSharedDistribution() {
543+
cloudinary.url().useRootPath(true).generate("test");
544+
}
545+
546+
@Test
547+
public void testSupportUseRootPathForPrivateCdn() {
548+
String actual = cloudinary.url().privateCdn(true).useRootPath(true).generate("test");
549+
assertEquals("http://test123-res.cloudinary.com/test", actual);
550+
551+
actual = cloudinary.url().privateCdn(true).transformation(new Transformation().angle(0)).useRootPath(true).generate("test");
552+
assertEquals("http://test123-res.cloudinary.com/a_0/test", actual);
553+
}
554+
555+
@Test
556+
public void testSupportUseRootPathTogetherWithUrlSuffixForPrivateCdn() {
557+
558+
String actual = cloudinary.url().privateCdn(true).urlSuffix("hello").useRootPath(true).generate("test");
559+
assertEquals("http://test123-res.cloudinary.com/test/hello", actual);
560+
561+
}
562+
563+
@Test(expected = RuntimeException.class)
564+
public void testDisllowUseRootPathIfNotImageUploadForFacebook() {
565+
cloudinary.url().useRootPath(true).privateCdn(true).type("facebook").generate("test");
566+
}
567+
568+
@Test(expected = RuntimeException.class)
569+
public void testDisllowUseRootPathIfNotImageUploadForRaw() {
570+
cloudinary.url().useRootPath(true).privateCdn(true).resourceType("raw").generate("test");
571+
}
572+
476573
public void testUtils() {
477574
assertEquals(ObjectUtils.asBoolean(true, null), true);
478575
assertEquals(ObjectUtils.asBoolean(false, null), false);

0 commit comments

Comments
 (0)