Skip to content

Commit c6036f9

Browse files
committed
add unauthenticated test
1 parent 7049fd8 commit c6036f9

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

Modrinth.RestClient.Test/EndpointTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Modrinth.RestClient.Test;
66
public class EndpointTests
77
{
88
protected IModrinthApi _client = null!;
9+
protected IModrinthApi _noAuthClient = null!;
910

1011
[OneTimeSetUp]
1112
public void SetUp()
@@ -16,6 +17,7 @@ public void SetUp()
1617
throw new Exception("MODRINTH_TOKEN environment variable is not set.");
1718
}
1819
var userAgent = $"Zechiax/Modrinth.RestClient.Test/{Assembly.GetExecutingAssembly().GetName().Version}";
19-
_client = new ModrinthApi(url: ModrinthApi.StagingBaseUrl, userAgent: userAgent , token: token);
20+
_client = new ModrinthApi(url: ModrinthApi.StagingBaseUrl, userAgent: userAgent, token: token);
21+
_noAuthClient = new ModrinthApi(url: ModrinthApi.StagingBaseUrl, userAgent: userAgent);
2022
}
2123
}

Modrinth.RestClient.Test/TestUserEndpoint.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Modrinth.RestClient.Test;
1+
using Flurl.Http;
2+
3+
namespace Modrinth.RestClient.Test;
24

35
[TestFixture]
46
public class TestUserEndpoint : EndpointTests
@@ -40,4 +42,11 @@ public async Task GetUserAsync_WithValidId_ShouldReturnUser()
4042
Assert.That(user, Is.Not.Null);
4143
Assert.That(user.Id, Is.EqualTo(currentUser.Id));
4244
}
45+
46+
// Test for current user with unauthenticated client
47+
[Test]
48+
public void TestGetCurrentUser_Unauthenticated()
49+
{
50+
Assert.ThrowsAsync<FlurlHttpException>(async () => await _noAuthClient.User.GetCurrentAsync());
51+
}
4352
}

Modrinth.RestClient/Modrinth.RestClient.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Flurl.Http;
1+
using System.Net;
2+
using Flurl.Http;
23
using Modrinth.RestClient.Endpoints.Project;
34
using Modrinth.RestClient.Endpoints.Tag;
45
using Modrinth.RestClient.Endpoints.Team;
@@ -67,6 +68,11 @@ public ModrinthApi(string? userAgent = null, string? token = null, string url =
6768
.WithHeader("Accept", "application/json")
6869
.WithHeader("Content-Type", "application/json");
6970

71+
_client.Configure(settings =>
72+
{
73+
settings.OnErrorAsync = HandleFlurlErrorAsync;
74+
});
75+
7076
if (!string.IsNullOrEmpty(token))
7177
{
7278
_client.WithHeader("Authorization", token);
@@ -79,4 +85,8 @@ public ModrinthApi(string? userAgent = null, string? token = null, string url =
7985
Version = new VersionApi(_client);
8086
VersionFile = new VersionFileApi(_client);
8187
}
88+
89+
private static async Task HandleFlurlErrorAsync(FlurlCall call) {
90+
91+
}
8292
}

0 commit comments

Comments
 (0)