Skip to content

Commit 48c61ae

Browse files
author
Francisco Liberal
committed
more
1 parent 02c7d8f commit 48c61ae

File tree

4 files changed

+181
-168
lines changed

4 files changed

+181
-168
lines changed
Lines changed: 160 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,160 @@
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+

src/ArcadeDotnet/ArcadeClient.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public async Task<ArcadeResponse> Execute<TParams>(ArcadeRequest<TParams> reques
125125
public ArcadeClient() : this(new ArcadeClientOptions
126126
{
127127
ApiKey = Environment.GetEnvironmentVariable(ArcadeClientOptions.ApiKeyEnvironmentVariable),
128-
BaseUrl = TryParseBaseUrl(Environment.GetEnvironmentVariable(ArcadeClientOptions.BaseUrlEnvironmentVariable))
128+
BaseUrl = ArcadeClientOptions.TryParseBaseUrl(Environment.GetEnvironmentVariable(ArcadeClientOptions.BaseUrlEnvironmentVariable))
129129
})
130130
{
131131
}
@@ -161,7 +161,4 @@ public ArcadeClient(ArcadeClientOptions options)
161161
Tools = new ToolService(this);
162162
Workers = new WorkerService(this);
163163
}
164-
165-
private static Uri? TryParseBaseUrl(string? url) =>
166-
string.IsNullOrEmpty(url) ? null : new Uri(url);
167164
}

src/ArcadeDotnet/ArcadeClientFactory.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static ArcadeClient Create()
1919
return new ArcadeClient(new ArcadeClientOptions
2020
{
2121
ApiKey = Environment.GetEnvironmentVariable(ArcadeClientOptions.ApiKeyEnvironmentVariable),
22-
BaseUrl = TryParseBaseUrl(Environment.GetEnvironmentVariable(ArcadeClientOptions.BaseUrlEnvironmentVariable)),
22+
BaseUrl = ArcadeClientOptions.TryParseBaseUrl(Environment.GetEnvironmentVariable(ArcadeClientOptions.BaseUrlEnvironmentVariable)),
2323
HttpClient = _sharedHttpClient.Value
2424
});
2525
}
@@ -37,8 +37,5 @@ public static ArcadeClient Create(string apiKey)
3737
HttpClient = _sharedHttpClient.Value
3838
});
3939
}
40-
41-
private static Uri? TryParseBaseUrl(string? url) =>
42-
string.IsNullOrEmpty(url) ? null : new Uri(url);
4340
}
4441

src/ArcadeDotnet/ArcadeClientOptions.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,24 @@ public sealed record ArcadeClientOptions
3939
/// </summary>
4040
public HttpClient? HttpClient { get; init; }
4141

42-
43-
private static Uri? TryParseBaseUrl(string? url) =>
44-
string.IsNullOrEmpty(url) ? null : new Uri(url);
42+
/// <summary>
43+
/// Attempts to parse a URL string into a Uri, returning null if parsing fails.
44+
/// </summary>
45+
/// <param name="url">The URL string to parse.</param>
46+
/// <returns>A Uri if parsing succeeds, null otherwise.</returns>
47+
internal static Uri? TryParseBaseUrl(string? url)
48+
{
49+
if (string.IsNullOrEmpty(url))
50+
return null;
51+
52+
try
53+
{
54+
return new Uri(url);
55+
}
56+
catch (UriFormatException)
57+
{
58+
return null; // Invalid URL, will use default
59+
}
60+
}
4561
}
4662

0 commit comments

Comments
 (0)