Skip to content

Commit 99afd4c

Browse files
Rename ExcludeVersion to ForceVersion
1 parent 3e60016 commit 99afd4c

File tree

3 files changed

+55
-41
lines changed

3 files changed

+55
-41
lines changed

Shared.Tests/ApiTest.cs

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public partial class ApiTest
1717

1818
private const string TestVersion = "1234";
1919
private const string TestVersionStr = "v1234";
20+
private const string DefaultVersionStr = "v1";
2021
private const string SOURCE_MOVIE = "movie";
22+
private const string TestFolder = "folder/test";
2123
private const string TestImageId = "image.jpg";
2224

2325
[OneTimeSetUp]
@@ -598,13 +600,13 @@ public void TestUrlClone()
598600
Url url2 = url1.Clone().Action("go");
599601
transformation.Angle(14);
600602
layer.FontSize(20);
601-
603+
602604
string result1 = url1.BuildUrl("test");
603605
string result2 = url2.BuildUrl("test");
604-
605-
Assert.AreEqual(m_defaultImgUpPath + "a_14,l_text:Arial_20:Hello/test", result1,
606+
607+
Assert.AreEqual(m_defaultImgUpPath + "a_14,l_text:Arial_20:Hello/test", result1,
606608
"Original Url should not be affected by changes to a cloned Url");
607-
Assert.AreEqual(m_defaultRootPath + "image/go/a_12,l_text:Arial_10:Hello/test", result2,
609+
Assert.AreEqual(m_defaultRootPath + "image/go/a_12,l_text:Arial_10:Hello/test", result2,
608610
"Cloned Url should not be affected by changes to source Url params and layers");
609611
}
610612

@@ -756,56 +758,51 @@ public void TestFolders()
756758
{
757759
// should add version if public_id contains /
758760

759-
string result = m_api.UrlImgUp.BuildUrl("folder/test");
760-
Assert.AreEqual(m_defaultImgUpPath + "v1/folder/test", result);
761-
result = m_api.UrlImgUp.Version("123").BuildUrl("folder/test");
762-
Assert.AreEqual(m_defaultImgUpPath + "v123/folder/test", result);
763-
result = m_api.UrlImgUp.BuildUrl("1/av1/test");
764-
Assert.AreEqual(m_defaultImgUpPath + "v1/1/av1/test", result);
761+
string result = m_api.UrlImgUp.BuildUrl(TestFolder);
762+
Assert.AreEqual(m_defaultImgUpPath + $"{DefaultVersionStr}/{TestFolder}", result);
763+
result = m_api.UrlImgUp.Version(TestVersion).BuildUrl(TestFolder);
764+
Assert.AreEqual(m_defaultImgUpPath + $"{TestVersionStr}/{TestFolder}", result);
765+
result = m_api.UrlImgUp.BuildUrl($"1/a{DefaultVersionStr}/{TestImageId}");
766+
Assert.AreEqual(m_defaultImgUpPath + $"{DefaultVersionStr}/1/a{DefaultVersionStr}/{TestImageId}", result);
765767
}
766768

767769
[Test]
768770
public void TestFoldersWithVersion()
769771
{
770772
// should not add version if public_id contains version already
771773

772-
string result = m_api.UrlImgUp.BuildUrl("v1234/test");
773-
Assert.AreEqual(m_defaultImgUpPath + "v1234/test", result);
774+
string result = m_api.UrlImgUp.BuildUrl($"{TestVersionStr}/{TestImageId}");
775+
Assert.AreEqual(m_defaultImgUpPath + $"{TestVersionStr}/{TestImageId}", result);
774776
}
775777

776778
[Test]
777-
public void TestExcludeVersion()
779+
public void TestForceVersion()
778780
{
779781
var api = new Api(m_api.Account);
780782

781-
// Should ignore the version parameter if ExcludeVersion is set to true
782-
var result = api.UrlImgUp.ExcludeVersion().BuildUrl(TestImageId);
783-
Assert.AreEqual($"{m_defaultImgUpPath}{TestImageId}", result);
783+
var result = api.UrlImgUp.BuildUrl(TestFolder);
784+
Assert.AreEqual($"{m_defaultImgUpPath}{DefaultVersionStr}/{TestFolder}", result);
784785

785-
result = api.UrlImgUp.Version(TestVersion).ExcludeVersion().BuildUrl(TestImageId);
786-
Assert.AreEqual($"{m_defaultImgUpPath}{TestImageId}", result);
786+
// Should not add default version if ForceVersion is set to false
787+
result = api.UrlImgUp.ForceVersion(false).BuildUrl(TestFolder);
788+
Assert.AreEqual($"{m_defaultImgUpPath}{TestFolder}", result);
787789

788-
result = api.UrlImgUp.ExcludeVersion(false).BuildUrl(TestImageId);
789-
Assert.AreEqual($"{m_defaultImgUpPath}{TestImageId}", result);
790+
// Explicitly set version is always passed
791+
result = api.UrlImgUp.Version(TestVersion).ForceVersion(false).BuildUrl(TestFolder);
792+
Assert.AreEqual($"{m_defaultImgUpPath}{TestVersionStr}/{TestFolder}", result);
790793

791-
result = api.UrlImgUp.Version(TestVersion).ExcludeVersion(false).BuildUrl(TestImageId);
794+
result = api.UrlImgUp.Version(TestVersion).ForceVersion(false).BuildUrl(TestImageId);
792795
Assert.AreEqual($"{m_defaultImgUpPath}{TestVersionStr}/{TestImageId}", result);
793796

794-
// Should use ExcludeVersion from Api instance
795-
api.ExcludeVersion = true;
796-
797-
result = api.UrlImgUp.BuildUrl(TestImageId);
798-
Assert.AreEqual($"{m_defaultImgUpPath}{TestImageId}", result);
797+
// Should use ForceVersion from Api instance
798+
api.ForceVersion = false;
799799

800-
result = api.UrlImgUp.Version(TestVersion).BuildUrl(TestImageId);
801-
Assert.AreEqual($"{m_defaultImgUpPath}{TestImageId}", result);
800+
result = api.UrlImgUp.BuildUrl(TestFolder);
801+
Assert.AreEqual($"{m_defaultImgUpPath}{TestFolder}", result);
802802

803-
// Should override ExcludeVersion from Api instance
804-
result = api.UrlImgUp.ExcludeVersion(false).BuildUrl(TestImageId);
805-
Assert.AreEqual($"{m_defaultImgUpPath}{TestImageId}", result);
806-
807-
result = api.UrlImgUp.Version(TestVersion).ExcludeVersion(false).BuildUrl(TestImageId);
808-
Assert.AreEqual($"{m_defaultImgUpPath}{TestVersionStr}/{TestImageId}", result);
803+
// Should override ForceVersion from Api instance
804+
result = api.UrlImgUp.ForceVersion().BuildUrl(TestFolder);
805+
Assert.AreEqual($"{m_defaultImgUpPath}{DefaultVersionStr}/{TestFolder}", result);
809806
}
810807

811808
[Test]

Shared/ApiShared.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,18 @@ public class ApiShared : ISignProvider
2929
public bool Secure;
3030
public string PrivateCdn;
3131
public string Suffix;
32-
public bool ExcludeVersion;
3332
public string UserPlatform;
3433

3534
public int Timeout = 0;
3635

36+
/// <summary>
37+
/// Indicates whether to add '/v1/' to the URL when the public ID includes folders and a 'version' value was
38+
/// not defined.
39+
/// When no version is explicitly specified and the public id contains folders, a default v1 version
40+
/// is added to the url. Set this boolean as false to prevent that behaviour.
41+
/// </summary>
42+
public bool ForceVersion = true;
43+
3744
/// <summary>
3845
/// Sets whether to use the use chunked encoding. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1 for further info.
3946
/// Server must support HTTP/1.1 in order to use the chunked encoding.
@@ -165,7 +172,7 @@ public Url Url
165172
.Shorten(ShortenUrl)
166173
.PrivateCdn(UsePrivateCdn)
167174
.Secure(Secure)
168-
.ExcludeVersion(ExcludeVersion)
175+
.ForceVersion(ForceVersion)
169176
.SecureDistribution(PrivateCdn);
170177
}
171178
}

Shared/Url.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class Url : Core.ICloneable
5555
protected string m_suffix;
5656
protected string m_privateCdn;
5757
protected string m_version;
58-
protected bool m_excludeVersion;
58+
protected bool m_forceVersion;
5959
protected string m_cName;
6060
protected string m_source;
6161
protected string m_fallbackContent;
@@ -145,9 +145,17 @@ public Url Version(string version)
145145
return this;
146146
}
147147

148-
public Url ExcludeVersion(bool excludeVersion = true)
148+
/// <summary>
149+
/// Indicates whether to add '/v1/' to the URL when the public ID includes folders and a 'version' value was
150+
/// not defined.
151+
/// When no version is explicitly specified and the public id contains folders, a default v1 version
152+
/// is added to the url. Set this boolean as false to prevent that behaviour.
153+
/// </summary>
154+
/// <param name="forceVersion">A boolean parameter indicating whether or not to add the version.</param>
155+
/// <returns>Url</returns>
156+
public Url ForceVersion(bool forceVersion = true)
149157
{
150-
m_excludeVersion = excludeVersion;
158+
m_forceVersion = forceVersion;
151159
return this;
152160
}
153161

@@ -643,12 +651,14 @@ public string BuildUrl(string source)
643651
urlParts.Add(m_action);
644652
urlParts.AddRange(m_customParts);
645653

646-
if (src.SourceToSign.Contains("/") && !Regex.IsMatch(src.SourceToSign, "^v[0-9]+/") && !Regex.IsMatch(src.SourceToSign, "https?:/.*") && String.IsNullOrEmpty(m_version))
654+
if (m_forceVersion &&
655+
src.SourceToSign.Contains("/") && !Regex.IsMatch(src.SourceToSign, "^v[0-9]+/") &&
656+
!Regex.IsMatch(src.SourceToSign, "https?:/.*") && string.IsNullOrEmpty(m_version))
647657
{
648658
m_version = "1";
649659
}
650660

651-
var version = string.IsNullOrEmpty(m_version) || m_excludeVersion ? string.Empty : $"v{m_version}";
661+
var version = string.IsNullOrEmpty(m_version) ? string.Empty : $"v{m_version}";
652662

653663
if (m_signed && (m_AuthToken == null && CloudinaryConfiguration.AuthToken == null))
654664
{

0 commit comments

Comments
 (0)