11package com .cloudinary .test ;
22
33import java .util .Map ;
4+ import java .util .regex .Matcher ;
5+ import java .util .regex .Pattern ;
46
57import 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}
0 commit comments