Skip to content

Commit 0abd677

Browse files
committed
add missing tests in adnroid-test. fix signing tests in android-test. be more specific with exception class in http42 Cloudinary tests.
1 parent cebb620 commit 0abd677

File tree

3 files changed

+152
-11
lines changed

3 files changed

+152
-11
lines changed

cloudinary-android-test/src/main/java/com/cloudinary/test/CloudinaryTest.java

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.cloudinary.test;
22

33
import java.util.Map;
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
46

57
import android.test.AndroidTestCase;
68

@@ -345,12 +347,12 @@ public void testRecommendedIdentifierFormat() {
345347

346348
public void testSignedUrl() {
347349
// should correctly sign a url
348-
String expected = "http://res.cloudinary.com/test123/image/upload/s--MaRXzoEC--/c_crop,h_20,w_10/v1234/image.jpg";
350+
String expected = "http://res.cloudinary.com/test123/image/upload/s--Ai4Znfl3--/c_crop,h_20,w_10/v1234/image.jpg";
349351
String actual = cloudinary.url().version(1234).transformation(new Transformation().crop("crop").width(10).height(20)).signed(true)
350352
.generate("image.jpg");
351353
assertEquals(expected, actual);
352354

353-
expected = "http://res.cloudinary.com/test123/image/upload/s--ZlgFLQcO--/v1234/image.jpg";
355+
expected = "http://res.cloudinary.com/test123/image/upload/s----SjmNDA--/v1234/image.jpg";
354356
actual = cloudinary.url().version(1234).signed(true).generate("image.jpg");
355357
assertEquals(expected, actual);
356358

@@ -359,4 +361,129 @@ public void testSignedUrl() {
359361
assertEquals(expected, actual);
360362
}
361363

364+
public void testDisallowUrlSuffixInSharedDistribution() {
365+
boolean thrown = false;
366+
try {
367+
cloudinary.url().suffix("hello").generate("test");
368+
} catch (IllegalArgumentException e) {
369+
assertEquals(e.getMessage(), "URL Suffix only supported in private CDN");
370+
thrown = true;
371+
}
372+
assertTrue(thrown);
373+
}
374+
375+
public void testDisallowUrlSuffixInNonUploadTypes() {
376+
boolean thrown = false;
377+
try {
378+
cloudinary.url().suffix("hello").privateCdn(true).type("facebook").generate("test");
379+
} catch (IllegalArgumentException e) {
380+
assertEquals(e.getMessage(), "URL Suffix only supported for image/upload and raw/upload");
381+
}
382+
}
383+
384+
public void testDisallowUrlSuffixWithSlash() {
385+
boolean thrown = false;
386+
try {
387+
cloudinary.url().suffix("hello/world").privateCdn(true).generate("test");
388+
} catch (IllegalArgumentException e) {
389+
assertEquals(e.getMessage(), "url_suffix should not include . or /");
390+
}
391+
}
392+
393+
public void testDisallowUrlSuffixWithDot() {
394+
boolean thrown = false;
395+
try {
396+
cloudinary.url().suffix("hello.world").privateCdn(true).generate("test");
397+
} catch (IllegalArgumentException e) {
398+
assertEquals(e.getMessage(), "url_suffix should not include . or /");
399+
thrown = true;
400+
}
401+
assertTrue(thrown);
402+
}
403+
404+
public void testSupportUrlSuffixForPrivateCdn() {
405+
String actual = cloudinary.url().suffix("hello").privateCdn(true).generate("test");
406+
assertEquals("http://test123-res.cloudinary.com/images/test/hello", actual);
407+
408+
actual = cloudinary.url().suffix("hello").privateCdn(true).transformation(new Transformation().angle(0)).generate("test");
409+
assertEquals("http://test123-res.cloudinary.com/images/a_0/test/hello", actual);
410+
}
411+
412+
public void testPutFormatAfterUrlSuffix() {
413+
String actual = cloudinary.url().suffix("hello").privateCdn(true).format("jpg").generate("test");
414+
assertEquals("http://test123-res.cloudinary.com/images/test/hello.jpg", actual);
415+
}
416+
417+
public void testNotSignTheUrlSuffix() {
418+
419+
Pattern pattern = Pattern.compile("s--[0-9A-Za-z_-]{8}--");
420+
String url = cloudinary.url().format("jpg").signed(true).generate("test");
421+
Matcher matcher = pattern.matcher(url);
422+
matcher.find();
423+
String expectedSignature = url.substring(matcher.start(), matcher.end());
424+
425+
String actual = cloudinary.url().format("jpg").privateCdn(true).signed(true).suffix("hello").generate("test");
426+
assertEquals("http://test123-res.cloudinary.com/images/" + expectedSignature + "/test/hello.jpg", actual);
427+
428+
url = cloudinary.url().format("jpg").signed(true).transformation(new Transformation().angle(0)).generate("test");
429+
matcher = pattern.matcher(url);
430+
matcher.find();
431+
expectedSignature = url.substring(matcher.start(), matcher.end());
432+
433+
actual = cloudinary.url().format("jpg").privateCdn(true).signed(true).suffix("hello").transformation(new Transformation().angle(0)).generate("test");
434+
435+
assertEquals("http://test123-res.cloudinary.com/images/" + expectedSignature + "/a_0/test/hello.jpg", actual);
436+
}
437+
438+
public void testSupportUrlSuffixForRawUploads() {
439+
String actual = cloudinary.url().suffix("hello").privateCdn(true).resourceType("raw").generate("test");
440+
assertEquals("http://test123-res.cloudinary.com/files/test/hello", actual);
441+
}
442+
443+
public void testDisllowUseRootPathInSharedDistribution() {
444+
boolean thrown = false;
445+
try {
446+
cloudinary.url().useRootPath(true).generate("test");
447+
} catch (IllegalArgumentException e) {
448+
assertEquals(e.getMessage(), "Root path only supported in private CDN");
449+
thrown = true;
450+
}
451+
assertTrue(thrown);
452+
}
453+
454+
public void testSupportUseRootPathForPrivateCdn() {
455+
String actual = cloudinary.url().privateCdn(true).useRootPath(true).generate("test");
456+
assertEquals("http://test123-res.cloudinary.com/test", actual);
457+
458+
actual = cloudinary.url().privateCdn(true).transformation(new Transformation().angle(0)).useRootPath(true).generate("test");
459+
assertEquals("http://test123-res.cloudinary.com/a_0/test", actual);
460+
}
461+
462+
public void testSupportUseRootPathTogetherWithUrlSuffixForPrivateCdn() {
463+
String actual = cloudinary.url().privateCdn(true).suffix("hello").useRootPath(true).generate("test");
464+
assertEquals("http://test123-res.cloudinary.com/test/hello", actual);
465+
}
466+
467+
public void testDisllowUseRootPathIfNotImageUploadForFacebook() {
468+
boolean thrown = false;
469+
try {
470+
cloudinary.url().useRootPath(true).privateCdn(true).type("facebook").generate("test");
471+
} catch (IllegalArgumentException e) {
472+
assertEquals(e.getMessage(), "Root path only supported for image/upload");
473+
thrown = true;
474+
}
475+
assertTrue(thrown);
476+
}
477+
478+
public void testDisllowUseRootPathIfNotImageUploadForRaw() {
479+
boolean thrown = false;
480+
try {
481+
cloudinary.url().useRootPath(true).privateCdn(true).resourceType("raw").generate("test");
482+
} catch (IllegalArgumentException e) {
483+
assertEquals(e.getMessage(), "Root path only supported for image/upload");
484+
thrown = true;
485+
}
486+
assertTrue(thrown);
487+
}
488+
362489
}

cloudinary-android-test/src/main/java/com/cloudinary/test/UploaderTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ public void testMulti() throws Exception {
202202
}
203203

204204
public void testUniqueFilename() throws Exception {
205+
if (cloudinary.config.apiSecret == null)
206+
return;
205207

206208
File f = new File(getInstrumentation().getContext().getCacheDir() + "/logo.png");
207209

@@ -223,6 +225,8 @@ public void testUniqueFilename() throws Exception {
223225

224226
public void testFaceCoordinates() throws Exception {
225227
// should allow sending face coordinates
228+
if (cloudinary.config.apiSecret == null)
229+
return;
226230
Coordinates coordinates = new Coordinates();
227231
Rectangle rect1 = new Rectangle(121, 31, 231, 182);
228232
Rectangle rect2 = new Rectangle(120, 30, 229, 270);
@@ -250,20 +254,26 @@ public void testFaceCoordinates() throws Exception {
250254

251255
public void testContext() throws Exception {
252256
// should allow sending context
257+
if (cloudinary.config.apiSecret == null)
258+
return;
253259
@SuppressWarnings("rawtypes")
254260
Map context = ObjectUtils.asMap("caption", "some caption", "alt", "alternative");
255261
cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("context", context));
256262
}
257263

258264
public void testModerationRequest() throws Exception {
259265
// should support requesting manual moderation
266+
if (cloudinary.config.apiSecret == null)
267+
return;
260268
JSONObject result = new JSONObject(cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("moderation", "manual")));
261269
assertEquals("manual", result.getJSONArray("moderation").getJSONObject(0).getString("kind"));
262270
assertEquals("pending", result.getJSONArray("moderation").getJSONObject(0).getString("status"));
263271
}
264272

265273
public void testRawConvertRequest() {
266274
// should support requesting raw conversion
275+
if (cloudinary.config.apiSecret == null)
276+
return;
267277
try {
268278
cloudinary.uploader().upload(getAssetStream("docx.docx"), ObjectUtils.asMap("raw_convert", "illegal", "resource_type", "raw"));
269279
} catch (Exception e) {
@@ -273,6 +283,8 @@ public void testRawConvertRequest() {
273283

274284
public void testCategorizationRequest() {
275285
// should support requesting categorization
286+
if (cloudinary.config.apiSecret == null)
287+
return;
276288
try {
277289
cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("categorization", "illegal"));
278290
} catch (Exception e) {
@@ -282,6 +294,8 @@ public void testCategorizationRequest() {
282294

283295
public void testDetectionRequest() {
284296
// should support requesting detection
297+
if (cloudinary.config.apiSecret == null)
298+
return;
285299
try {
286300
cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("detection", "illegal"));
287301
} catch (Exception e) {
@@ -291,15 +305,15 @@ public void testDetectionRequest() {
291305

292306
public void testAutoTaggingRequest() {
293307
// should support requesting auto tagging
308+
if (cloudinary.config.apiSecret == null)
309+
return;
294310

295311
try {
296312
cloudinary.uploader().upload(getAssetStream("images/logo.png"), ObjectUtils.asMap("auto_tagging", 0.5f));
297313
} catch (Exception e) {
298314
for (int i = 0; i < e.getStackTrace().length; i++) {
299315
StackTraceElement x = e.getStackTrace()[i];
300316
}
301-
302-
303317
assertTrue(e.getMessage().matches("^Must use(.*)"));
304318
}
305319
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -473,23 +473,23 @@ public void testResponsiveWidth() {
473473
Transformation.setResponsiveWidthTransformation(null);
474474
}
475475

476-
@Test(expected = RuntimeException.class)
476+
@Test(expected = IllegalArgumentException.class)
477477
public void testDisallowUrlSuffixInSharedDistribution() {
478478
cloudinary.url().suffix("hello").generate("test");
479479
}
480480

481-
@Test(expected = RuntimeException.class)
481+
@Test(expected = IllegalArgumentException.class)
482482
public void testDisallowUrlSuffixInNonUploadTypes() {
483483
cloudinary.url().suffix("hello").privateCdn(true).type("facebook").generate("test");
484484

485485
}
486486

487-
@Test(expected = RuntimeException.class)
487+
@Test(expected = IllegalArgumentException.class)
488488
public void testDisallowUrlSuffixWithSlash() {
489489
cloudinary.url().suffix("hello/world").privateCdn(true).generate("test");
490490
}
491491

492-
@Test(expected = RuntimeException.class)
492+
@Test(expected = IllegalArgumentException.class)
493493
public void testDisallowUrlSuffixWithDot() {
494494
cloudinary.url().suffix("hello.world").privateCdn(true).generate("test");
495495
}
@@ -538,7 +538,7 @@ public void testSupportUrlSuffixForRawUploads() {
538538
assertEquals("http://test123-res.cloudinary.com/files/test/hello", actual);
539539
}
540540

541-
@Test(expected = RuntimeException.class)
541+
@Test(expected = IllegalArgumentException.class)
542542
public void testDisllowUseRootPathInSharedDistribution() {
543543
cloudinary.url().useRootPath(true).generate("test");
544544
}
@@ -560,12 +560,12 @@ public void testSupportUseRootPathTogetherWithUrlSuffixForPrivateCdn() {
560560

561561
}
562562

563-
@Test(expected = RuntimeException.class)
563+
@Test(expected = IllegalArgumentException.class)
564564
public void testDisllowUseRootPathIfNotImageUploadForFacebook() {
565565
cloudinary.url().useRootPath(true).privateCdn(true).type("facebook").generate("test");
566566
}
567567

568-
@Test(expected = RuntimeException.class)
568+
@Test(expected = IllegalArgumentException.class)
569569
public void testDisllowUseRootPathIfNotImageUploadForRaw() {
570570
cloudinary.url().useRootPath(true).privateCdn(true).resourceType("raw").generate("test");
571571
}

0 commit comments

Comments
 (0)