|
1 | | -using System; |
2 | | -using System.Net.Http; |
3 | | -using ArcadeDotnet.Exceptions; |
4 | | - |
5 | | -namespace ArcadeDotnet.Tests; |
6 | | - |
7 | | -public class ArcadeClientEdgeCasesTest |
8 | | -{ |
9 | | - [Fact] |
10 | | - public void Constructor_WithNullOptions_ShouldThrow() |
11 | | - { |
12 | | - // Act & Assert |
13 | | - Assert.Throws<ArgumentNullException>(() => new ArcadeClient(null!)); |
14 | | - } |
15 | | - |
16 | | - [Fact] |
17 | | - public void Constructor_CreatesMultipleClients_ShouldHaveIndependentHttpClients() |
18 | | - { |
19 | | - // Arrange |
20 | | - var httpClient1 = new HttpClient(); |
21 | | - var httpClient2 = new HttpClient(); |
22 | | - |
23 | | - // Act |
24 | | - var client1 = new ArcadeClient(new ArcadeClientOptions |
25 | | - { |
26 | | - ApiKey = "key1", |
27 | | - HttpClient = httpClient1 |
28 | | - }); |
29 | | - |
30 | | - var client2 = new ArcadeClient(new ArcadeClientOptions |
31 | | - { |
32 | | - ApiKey = "key2", |
33 | | - HttpClient = httpClient2 |
34 | | - }); |
35 | | - |
36 | | - // Assert - Different configurations |
37 | | - Assert.Equal("key1", client1.APIKey); |
38 | | - Assert.Equal("key2", client2.APIKey); |
39 | | - Assert.NotSame(client1, client2); |
40 | | - } |
41 | | - |
42 | | - [Fact] |
43 | | - public void BaseUrl_WithTrailingSlash_ShouldNormalize() |
44 | | - { |
45 | | - // Arrange |
46 | | - var options = new ArcadeClientOptions |
47 | | - { |
48 | | - ApiKey = "test", |
49 | | - BaseUrl = new Uri("https://api.test.com/") |
50 | | - }; |
51 | | - |
52 | | - // Act |
53 | | - var client = new ArcadeClient(options); |
54 | | - |
55 | | - // Assert |
56 | | - Assert.Equal("https://api.test.com/", client.BaseUrl.ToString()); |
57 | | - } |
58 | | - |
59 | | - [Fact] |
60 | | - public void Constructor_WithVeryLongApiKey_ShouldWork() |
61 | | - { |
62 | | - // Arrange |
63 | | - var longKey = new string('a', 1000); // 1000 character key |
64 | | - var options = new ArcadeClientOptions |
65 | | - { |
66 | | - ApiKey = longKey, |
67 | | - HttpClient = new HttpClient() |
68 | | - }; |
69 | | - |
70 | | - // Act |
71 | | - var client = new ArcadeClient(options); |
72 | | - |
73 | | - // Assert |
74 | | - Assert.Equal(longKey, client.APIKey); |
75 | | - } |
76 | | - |
77 | | - [Fact] |
78 | | - public void Constructor_WithSpecialCharactersInApiKey_ShouldWork() |
79 | | - { |
80 | | - // Arrange |
81 | | - var specialKey = "key!@#$%^&*()_+-=[]{}|;':\",./<>?"; |
82 | | - var options = new ArcadeClientOptions |
83 | | - { |
84 | | - ApiKey = specialKey, |
85 | | - HttpClient = new HttpClient() |
86 | | - }; |
87 | | - |
88 | | - // Act |
89 | | - var client = new ArcadeClient(options); |
90 | | - |
91 | | - // Assert |
92 | | - Assert.Equal(specialKey, client.APIKey); |
93 | | - } |
94 | | - |
95 | | - [Theory] |
96 | | - [InlineData("https://api.arcade.dev")] |
97 | | - [InlineData("https://staging.arcade.dev")] |
98 | | - [InlineData("http://localhost:3000")] |
99 | | - [InlineData("https://custom-domain.com:8080")] |
100 | | - public void Constructor_WithDifferentBaseUrls_ShouldAcceptAll(string baseUrl) |
101 | | - { |
102 | | - // Arrange |
103 | | - var options = new ArcadeClientOptions |
104 | | - { |
105 | | - ApiKey = "test", |
106 | | - BaseUrl = new Uri(baseUrl), |
107 | | - HttpClient = new HttpClient() |
108 | | - }; |
109 | | - |
110 | | - // Act |
111 | | - var client = new ArcadeClient(options); |
112 | | - |
113 | | - // Assert |
114 | | - Assert.StartsWith(baseUrl, client.BaseUrl.ToString()); |
115 | | - } |
116 | | - |
117 | | - [Fact] |
118 | | - public void Services_CalledMultipleTimes_ShouldReturnSameInstance() |
119 | | - { |
120 | | - // Arrange |
121 | | - var client = new ArcadeClient(new ArcadeClientOptions |
122 | | - { |
123 | | - ApiKey = "test", |
124 | | - HttpClient = new HttpClient() |
125 | | - }); |
126 | | - |
127 | | - // Act |
128 | | - var admin1 = client.Admin; |
129 | | - var admin2 = client.Admin; |
130 | | - var auth1 = client.Auth; |
131 | | - var auth2 = client.Auth; |
132 | | - |
133 | | - // Assert - Should return same instances (not create new each time) |
134 | | - Assert.Same(admin1, admin2); |
135 | | - Assert.Same(auth1, auth2); |
136 | | - } |
137 | | - |
138 | | - [Fact] |
139 | | - public void Constructor_Parameterless_WithInvalidEnvironmentBaseUrl_ShouldUseDefault() |
140 | | - { |
141 | | - // Arrange |
142 | | - Environment.SetEnvironmentVariable("ARCADE_API_KEY", "test-key"); |
143 | | - Environment.SetEnvironmentVariable("ARCADE_BASE_URL", "not-a-valid-url"); |
144 | | - |
145 | | - try |
146 | | - { |
147 | | - // Act & Assert - Should throw because URL parsing fails |
148 | | - Assert.Throws<UriFormatException>(() => new ArcadeClient()); |
149 | | - } |
150 | | - finally |
151 | | - { |
152 | | - Environment.SetEnvironmentVariable("ARCADE_API_KEY", null); |
153 | | - Environment.SetEnvironmentVariable("ARCADE_BASE_URL", null); |
154 | | - } |
155 | | - } |
156 | | -} |
157 | | - |
| 1 | +using System; |
| 2 | +using System.Net.Http; |
| 3 | +using ArcadeDotnet.Exceptions; |
| 4 | + |
| 5 | +namespace ArcadeDotnet.Tests; |
| 6 | + |
| 7 | +public class ArcadeClientEdgeCasesTest |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public void Constructor_WithNullOptions_ShouldThrow() |
| 11 | + { |
| 12 | + // Act & Assert |
| 13 | + Assert.Throws<ArgumentNullException>(() => new ArcadeClient(null!)); |
| 14 | + } |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public void Constructor_CreatesMultipleClients_ShouldHaveIndependentHttpClients() |
| 18 | + { |
| 19 | + // Arrange |
| 20 | + var httpClient1 = new HttpClient(); |
| 21 | + var httpClient2 = new HttpClient(); |
| 22 | + |
| 23 | + // Act |
| 24 | + var client1 = new ArcadeClient(new ArcadeClientOptions |
| 25 | + { |
| 26 | + ApiKey = "key1", |
| 27 | + HttpClient = httpClient1 |
| 28 | + }); |
| 29 | + |
| 30 | + var client2 = new ArcadeClient(new ArcadeClientOptions |
| 31 | + { |
| 32 | + ApiKey = "key2", |
| 33 | + HttpClient = httpClient2 |
| 34 | + }); |
| 35 | + |
| 36 | + // Assert - Different configurations |
| 37 | + Assert.Equal("key1", client1.APIKey); |
| 38 | + Assert.Equal("key2", client2.APIKey); |
| 39 | + Assert.NotSame(client1, client2); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void BaseUrl_WithTrailingSlash_ShouldNormalize() |
| 44 | + { |
| 45 | + // Arrange |
| 46 | + var options = new ArcadeClientOptions |
| 47 | + { |
| 48 | + ApiKey = "test", |
| 49 | + BaseUrl = new Uri("https://api.test.com/") |
| 50 | + }; |
| 51 | + |
| 52 | + // Act |
| 53 | + var client = new ArcadeClient(options); |
| 54 | + |
| 55 | + // Assert |
| 56 | + Assert.Equal("https://api.test.com/", client.BaseUrl.ToString()); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void Constructor_WithVeryLongApiKey_ShouldWork() |
| 61 | + { |
| 62 | + // Arrange |
| 63 | + var longKey = new string('a', 1000); // 1000 character key |
| 64 | + var options = new ArcadeClientOptions |
| 65 | + { |
| 66 | + ApiKey = longKey, |
| 67 | + HttpClient = new HttpClient() |
| 68 | + }; |
| 69 | + |
| 70 | + // Act |
| 71 | + var client = new ArcadeClient(options); |
| 72 | + |
| 73 | + // Assert |
| 74 | + Assert.Equal(longKey, client.APIKey); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public void Constructor_WithSpecialCharactersInApiKey_ShouldWork() |
| 79 | + { |
| 80 | + // Arrange |
| 81 | + var specialKey = "key!@#$%^&*()_+-=[]{}|;':\",./<>?"; |
| 82 | + var options = new ArcadeClientOptions |
| 83 | + { |
| 84 | + ApiKey = specialKey, |
| 85 | + HttpClient = new HttpClient() |
| 86 | + }; |
| 87 | + |
| 88 | + // Act |
| 89 | + var client = new ArcadeClient(options); |
| 90 | + |
| 91 | + // Assert |
| 92 | + Assert.Equal(specialKey, client.APIKey); |
| 93 | + } |
| 94 | + |
| 95 | + [Theory] |
| 96 | + [InlineData("https://api.arcade.dev")] |
| 97 | + [InlineData("https://staging.arcade.dev")] |
| 98 | + [InlineData("http://localhost:3000")] |
| 99 | + [InlineData("https://custom-domain.com:8080")] |
| 100 | + public void Constructor_WithDifferentBaseUrls_ShouldAcceptAll(string baseUrl) |
| 101 | + { |
| 102 | + // Arrange |
| 103 | + var options = new ArcadeClientOptions |
| 104 | + { |
| 105 | + ApiKey = "test", |
| 106 | + BaseUrl = new Uri(baseUrl), |
| 107 | + HttpClient = new HttpClient() |
| 108 | + }; |
| 109 | + |
| 110 | + // Act |
| 111 | + var client = new ArcadeClient(options); |
| 112 | + |
| 113 | + // Assert |
| 114 | + Assert.StartsWith(baseUrl, client.BaseUrl.ToString()); |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public void Services_CalledMultipleTimes_ShouldReturnSameInstance() |
| 119 | + { |
| 120 | + // Arrange |
| 121 | + var client = new ArcadeClient(new ArcadeClientOptions |
| 122 | + { |
| 123 | + ApiKey = "test", |
| 124 | + HttpClient = new HttpClient() |
| 125 | + }); |
| 126 | + |
| 127 | + // Act |
| 128 | + var admin1 = client.Admin; |
| 129 | + var admin2 = client.Admin; |
| 130 | + var auth1 = client.Auth; |
| 131 | + var auth2 = client.Auth; |
| 132 | + |
| 133 | + // Assert - Should return same instances (not create new each time) |
| 134 | + Assert.Same(admin1, admin2); |
| 135 | + Assert.Same(auth1, auth2); |
| 136 | + } |
| 137 | + |
| 138 | + [Fact] |
| 139 | + public void Constructor_Parameterless_WithInvalidEnvironmentBaseUrl_ShouldUseDefault() |
| 140 | + { |
| 141 | + // Arrange |
| 142 | + Environment.SetEnvironmentVariable("ARCADE_API_KEY", "test-key"); |
| 143 | + Environment.SetEnvironmentVariable("ARCADE_BASE_URL", "not-a-valid-url"); |
| 144 | + |
| 145 | + try |
| 146 | + { |
| 147 | + // Act - Invalid URL should be ignored and default used |
| 148 | + var client = new ArcadeClient(); |
| 149 | + |
| 150 | + // Assert - Should use default base URL |
| 151 | + Assert.Equal(new Uri(ArcadeClientOptions.DefaultBaseUrl), client.BaseUrl); |
| 152 | + } |
| 153 | + finally |
| 154 | + { |
| 155 | + Environment.SetEnvironmentVariable("ARCADE_API_KEY", null); |
| 156 | + Environment.SetEnvironmentVariable("ARCADE_BASE_URL", null); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
0 commit comments