|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using Contentstack.Utils.Endpoints; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace Contentstack.Utils.Tests |
| 8 | +{ |
| 9 | + public class EndpointTest : IDisposable |
| 10 | + { |
| 11 | + // Reset cache before each test so tests are isolated. |
| 12 | + public EndpointTest() |
| 13 | + { |
| 14 | + Endpoint.ResetCache(); |
| 15 | + } |
| 16 | + |
| 17 | + public void Dispose() |
| 18 | + { |
| 19 | + Endpoint.ResetCache(); |
| 20 | + } |
| 21 | + |
| 22 | + // ------------------------------------------------------------------ |
| 23 | + // Basic resolution |
| 24 | + // ------------------------------------------------------------------ |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void GetContentstackEndpoint_Na_ReturnsCorrectCdnUrl() |
| 28 | + { |
| 29 | + string url = Endpoint.GetContentstackEndpoint("na", "contentDelivery"); |
| 30 | + Assert.Equal("https://cdn.contentstack.io", url); |
| 31 | + } |
| 32 | + |
| 33 | + [Theory] |
| 34 | + [InlineData("na")] |
| 35 | + [InlineData("eu")] |
| 36 | + [InlineData("au")] |
| 37 | + [InlineData("azure-na")] |
| 38 | + [InlineData("azure-eu")] |
| 39 | + [InlineData("gcp-na")] |
| 40 | + [InlineData("gcp-eu")] |
| 41 | + public void GetContentstackEndpoint_AllRegionIds_Resolve(string regionId) |
| 42 | + { |
| 43 | + string url = Endpoint.GetContentstackEndpoint(regionId, "contentDelivery"); |
| 44 | + Assert.False(string.IsNullOrEmpty(url)); |
| 45 | + Assert.StartsWith("https://", url); |
| 46 | + } |
| 47 | + |
| 48 | + // ------------------------------------------------------------------ |
| 49 | + // Alias resolution (case-insensitive, dash/underscore variants) |
| 50 | + // ------------------------------------------------------------------ |
| 51 | + |
| 52 | + [Theory] |
| 53 | + [InlineData("na")] |
| 54 | + [InlineData("us")] |
| 55 | + [InlineData("NA")] |
| 56 | + [InlineData("US")] |
| 57 | + [InlineData("AWS-NA")] |
| 58 | + [InlineData("aws_na")] |
| 59 | + [InlineData("AWS_NA")] |
| 60 | + public void GetContentstackEndpoint_NaAliasVariants_AllResolveToSameCdn(string alias) |
| 61 | + { |
| 62 | + string url = Endpoint.GetContentstackEndpoint(alias, "contentDelivery"); |
| 63 | + Assert.Equal("https://cdn.contentstack.io", url); |
| 64 | + } |
| 65 | + |
| 66 | + [Theory] |
| 67 | + [InlineData("azure-na")] |
| 68 | + [InlineData("azure_na")] |
| 69 | + [InlineData("AZURE-NA")] |
| 70 | + [InlineData("AZURE_NA")] |
| 71 | + public void GetContentstackEndpoint_AzureNaAliasVariants_AllResolveToSameUrl(string alias) |
| 72 | + { |
| 73 | + string expected = Endpoint.GetContentstackEndpoint("azure-na", "contentDelivery"); |
| 74 | + string result = Endpoint.GetContentstackEndpoint(alias, "contentDelivery"); |
| 75 | + Assert.Equal(expected, result); |
| 76 | + } |
| 77 | + |
| 78 | + // ------------------------------------------------------------------ |
| 79 | + // omitHttps flag |
| 80 | + // ------------------------------------------------------------------ |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public void GetContentstackEndpoint_OmitHttps_StripsScheme() |
| 84 | + { |
| 85 | + string host = Endpoint.GetContentstackEndpoint("na", "contentDelivery", omitHttps: true); |
| 86 | + Assert.Equal("cdn.contentstack.io", host); |
| 87 | + } |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public void GetContentstackEndpoint_OmitHttps_EuRegion_StripsScheme() |
| 91 | + { |
| 92 | + string host = Endpoint.GetContentstackEndpoint("eu", "contentDelivery", omitHttps: true); |
| 93 | + Assert.Equal("eu-cdn.contentstack.com", host); |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public void GetContentstackEndpoint_OmitHttpsFalse_ReturnsFullUrl() |
| 98 | + { |
| 99 | + string url = Endpoint.GetContentstackEndpoint("na", "contentDelivery", omitHttps: false); |
| 100 | + Assert.StartsWith("https://", url); |
| 101 | + } |
| 102 | + |
| 103 | + // ------------------------------------------------------------------ |
| 104 | + // All-endpoints (dict) overload |
| 105 | + // ------------------------------------------------------------------ |
| 106 | + |
| 107 | + [Theory] |
| 108 | + [InlineData("na")] |
| 109 | + [InlineData("eu")] |
| 110 | + [InlineData("au")] |
| 111 | + [InlineData("azure-na")] |
| 112 | + [InlineData("azure-eu")] |
| 113 | + [InlineData("gcp-na")] |
| 114 | + [InlineData("gcp-eu")] |
| 115 | + public void GetContentstackEndpoint_AllEndpoints_ContainsExpectedKeys(string regionId) |
| 116 | + { |
| 117 | + var endpoints = Endpoint.GetContentstackEndpoint(regionId); |
| 118 | + Assert.True(endpoints.Count > 0); |
| 119 | + Assert.True(endpoints.ContainsKey("contentDelivery")); |
| 120 | + Assert.True(endpoints.ContainsKey("contentManagement")); |
| 121 | + Assert.True(endpoints.ContainsKey("auth")); |
| 122 | + } |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public void GetContentstackEndpoint_NaAllEndpoints_Has18Keys() |
| 126 | + { |
| 127 | + var endpoints = Endpoint.GetContentstackEndpoint("na"); |
| 128 | + Assert.Equal(18, endpoints.Count); |
| 129 | + } |
| 130 | + |
| 131 | + [Fact] |
| 132 | + public void GetContentstackEndpoint_AllEndpoints_OmitHttps_AllValuesStripped() |
| 133 | + { |
| 134 | + var endpoints = Endpoint.GetContentstackEndpoint("na", omitHttps: true); |
| 135 | + foreach (var url in endpoints.Values) |
| 136 | + Assert.DoesNotContain("https://", url); |
| 137 | + } |
| 138 | + |
| 139 | + // ------------------------------------------------------------------ |
| 140 | + // Error handling |
| 141 | + // ------------------------------------------------------------------ |
| 142 | + |
| 143 | + [Theory] |
| 144 | + [InlineData("")] |
| 145 | + [InlineData(" ")] |
| 146 | + public void GetContentstackEndpoint_EmptyRegion_ThrowsArgumentException(string emptyRegion) |
| 147 | + { |
| 148 | + Assert.Throws<ArgumentException>(() => |
| 149 | + Endpoint.GetContentstackEndpoint(emptyRegion, "contentDelivery")); |
| 150 | + } |
| 151 | + |
| 152 | + [Theory] |
| 153 | + [InlineData("")] |
| 154 | + [InlineData(" ")] |
| 155 | + public void GetContentstackEndpoint_EmptyRegion_DictOverload_ThrowsArgumentException(string emptyRegion) |
| 156 | + { |
| 157 | + Assert.Throws<ArgumentException>(() => |
| 158 | + Endpoint.GetContentstackEndpoint(emptyRegion)); |
| 159 | + } |
| 160 | + |
| 161 | + [Fact] |
| 162 | + public void GetContentstackEndpoint_UnknownRegion_ThrowsKeyNotFoundException() |
| 163 | + { |
| 164 | + Assert.Throws<KeyNotFoundException>(() => |
| 165 | + Endpoint.GetContentstackEndpoint("xyz", "contentDelivery")); |
| 166 | + } |
| 167 | + |
| 168 | + [Fact] |
| 169 | + public void GetContentstackEndpoint_UnknownService_ThrowsKeyNotFoundException() |
| 170 | + { |
| 171 | + var ex = Assert.Throws<KeyNotFoundException>(() => |
| 172 | + Endpoint.GetContentstackEndpoint("na", "nonExistentService")); |
| 173 | + Assert.Contains("nonExistentService", ex.Message); |
| 174 | + } |
| 175 | + |
| 176 | + // ------------------------------------------------------------------ |
| 177 | + // Cache behaviour |
| 178 | + // ------------------------------------------------------------------ |
| 179 | + |
| 180 | + [Fact] |
| 181 | + public void ResetCache_AllowsReload_NoError() |
| 182 | + { |
| 183 | + // First call populates cache |
| 184 | + Endpoint.GetContentstackEndpoint("na", "contentDelivery"); |
| 185 | + |
| 186 | + // Reset and call again — should reload without error |
| 187 | + Endpoint.ResetCache(); |
| 188 | + string url = Endpoint.GetContentstackEndpoint("na", "contentDelivery"); |
| 189 | + Assert.Equal("https://cdn.contentstack.io", url); |
| 190 | + } |
| 191 | + |
| 192 | + [Fact] |
| 193 | + public void GetContentstackEndpoint_ConsecutiveCalls_ReturnConsistentResults() |
| 194 | + { |
| 195 | + string first = Endpoint.GetContentstackEndpoint("eu", "contentManagement"); |
| 196 | + string second = Endpoint.GetContentstackEndpoint("eu", "contentManagement"); |
| 197 | + Assert.Equal(first, second); |
| 198 | + } |
| 199 | + |
| 200 | + // ------------------------------------------------------------------ |
| 201 | + // Utils proxy parity |
| 202 | + // ------------------------------------------------------------------ |
| 203 | + |
| 204 | + [Fact] |
| 205 | + public void Utils_Proxy_String_MatchesEndpoint() |
| 206 | + { |
| 207 | + string direct = Endpoint.GetContentstackEndpoint("na", "contentDelivery"); |
| 208 | + string proxy = Utils.GetContentstackEndpoint("na", "contentDelivery"); |
| 209 | + Assert.Equal(direct, proxy); |
| 210 | + } |
| 211 | + |
| 212 | + [Fact] |
| 213 | + public void Utils_Proxy_Dict_MatchesEndpoint() |
| 214 | + { |
| 215 | + var direct = Endpoint.GetContentstackEndpoint("eu"); |
| 216 | + var proxy = Utils.GetContentstackEndpoint("eu"); |
| 217 | + Assert.Equal(direct.Count, proxy.Count); |
| 218 | + foreach (var kvp in direct) |
| 219 | + Assert.Equal(kvp.Value, proxy[kvp.Key]); |
| 220 | + } |
| 221 | + |
| 222 | + [Fact] |
| 223 | + public void Utils_Proxy_OmitHttps_MatchesEndpoint() |
| 224 | + { |
| 225 | + string direct = Endpoint.GetContentstackEndpoint("eu", "contentDelivery", omitHttps: true); |
| 226 | + string proxy = Utils.GetContentstackEndpoint("eu", "contentDelivery", omitHttps: true); |
| 227 | + Assert.Equal(direct, proxy); |
| 228 | + } |
| 229 | + |
| 230 | + // ------------------------------------------------------------------ |
| 231 | + // Local file self-heal. |
| 232 | + // ------------------------------------------------------------------ |
| 233 | + |
| 234 | + [Fact] |
| 235 | + public void LocalFile_WhenWritten_IsReadOnNextCacheReset() |
| 236 | + { |
| 237 | + // First call: either reads from disk or downloads from CDN and writes to disk. |
| 238 | + Endpoint.GetContentstackEndpoint("na", "contentDelivery"); |
| 239 | + |
| 240 | + string localPath = Endpoint.GetLocalFilePath(); |
| 241 | + |
| 242 | + // If CDN download failed (no network in CI), skip this assertion — the |
| 243 | + // self-heal path is covered by the CDN download succeeding at call time. |
| 244 | + if (!File.Exists(localPath)) |
| 245 | + return; |
| 246 | + |
| 247 | + // Reset cache — next call must read from local file (step 2 in LoadRegions), |
| 248 | + // not re-download. Mirrors Python: os.path.exists(_REGIONS_FILE) → open(). |
| 249 | + Endpoint.ResetCache(); |
| 250 | + string url = Endpoint.GetContentstackEndpoint("na", "contentDelivery"); |
| 251 | + |
| 252 | + Assert.Equal("https://cdn.contentstack.io", url); |
| 253 | + } |
| 254 | + |
| 255 | + [Fact] |
| 256 | + public void LocalFilePath_IsNextToDll_NotSourceDirectory() |
| 257 | + { |
| 258 | + string localPath = Endpoint.GetLocalFilePath(); |
| 259 | + Assert.Contains("Assets", localPath); |
| 260 | + Assert.EndsWith("regions.json", localPath); |
| 261 | + } |
| 262 | + } |
| 263 | +} |
0 commit comments