Skip to content

Commit efd7056

Browse files
authored
[API-7676]: Validate apiKey is not null when creating a Client (#79)
* [API-7676]: Validate apiKey is not null when creating a Client
1 parent 58936b2 commit efd7056

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Sift/Core/Client.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,22 @@ public class Client : IDisposable
1515

1616
public Client(String apiKey)
1717
{
18+
if (string.IsNullOrWhiteSpace(apiKey))
19+
{
20+
throw new ArgumentNullException(nameof(apiKey));
21+
}
22+
1823
this.apiKey = apiKey;
1924
this.http = new HttpClient();
2025
}
2126

22-
public Client(String apiKey, HttpClient http) {
27+
public Client(String apiKey, HttpClient http)
28+
{
29+
if (string.IsNullOrWhiteSpace(apiKey))
30+
{
31+
throw new ArgumentNullException(nameof(apiKey));
32+
}
33+
2334
this.apiKey = apiKey;
2435
this.http = http;
2536
}

Test/ClientTest.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Sift;
2+
using Xunit;
3+
using System.Net.Http;
4+
5+
namespace Test
6+
{
7+
public class ClientTest
8+
{
9+
[Fact]
10+
public void TestFailsToCreateClientWithAPIKeyNull()
11+
{
12+
Assert.Throws<ArgumentNullException>(
13+
() => new Client(null)
14+
);
15+
Assert.Throws<ArgumentNullException>(
16+
() => new Client(null, new HttpClient())
17+
);
18+
}
19+
20+
[Fact]
21+
public void TestFailsToCreateClientWithEmptyAPIKey()
22+
{
23+
Assert.Throws<ArgumentNullException>(
24+
() => new Client("")
25+
);
26+
Assert.Throws<ArgumentNullException>(
27+
() => new Client("", new HttpClient())
28+
);
29+
}
30+
}
31+
32+
}

0 commit comments

Comments
 (0)