Something went wrong. Try again.
.NET ATProtocol Library - Built for Source Gen
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426using System;using System.Collections.Generic;using System.Net;using System.Net.Http;using System.Text;using System.Threading;using System.Threading.Tasks;using CarpaNet;using CarpaNet.Auth;using CarpaNet.Cbor;using Xunit;
namespace CarpaNet.UnitTests;
public class ATProtoClientTests{ // Sample JWT with exp claim (expires 2030-01-01 00:00:00 UTC) private const string SampleJwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6cGxjOnRlc3QiLCJleHAiOjE4OTM0NTYwMDB9.signature";
private static ATProtoClientOptions CreateDefaultOptions(HttpClient? httpClient = null) { var options = new ATProtoClientOptions { JsonOptions = TestHelpers.CreateJsonOptions(), CborContext = TestHelpers.CreateCborContext() }; if (httpClient != null) { options.HttpClient = httpClient; } return options; }
[Fact] public void Create_ReturnsPublicClient() { using var client = ATProtoClient.Create(CreateDefaultOptions());
Assert.False(client.IsAuthenticated); Assert.Null(client.AuthenticatedDid); Assert.Equal(new Uri(BlueskyServices.PublicAppView), client.BaseUrl); }
[Fact] public void Create_WithOptions_AppliesOptions() { var customUrl = new Uri("https://custom.api.example"); var options = CreateDefaultOptions(); options.BaseUrl = customUrl;
using var client = ATProtoClient.Create(options);
Assert.Equal(customUrl, client.BaseUrl); }
[Fact] public void Create_HasIdentityResolver() { using var client = ATProtoClient.Create(CreateDefaultOptions());
Assert.NotNull(client.IdentityResolver); }
[Fact] public void Create_WithoutIdentityResolver_RespectsOption() { var options = CreateDefaultOptions(); options.CreateIdentityResolver = false;
using var client = ATProtoClient.Create(options);
Assert.Null(client.IdentityResolver); }
[Fact] public async Task PostAsync_OnPublicClient_ThrowsATProtoException() { using var client = ATProtoClient.Create(CreateDefaultOptions());
await Assert.ThrowsAsync<ATProtoException>( () => client.PostAsync<object, object>("com.atproto.repo.createRecord", new { })); }
[Fact] public async Task GetAsync_OnPublicClient_Works() { var handler = new MockHttpMessageHandler((request, ct) => { return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{}", Encoding.UTF8, "application/json") }); });
using var httpClient = new HttpClient(handler); using var client = ATProtoClient.Create(CreateDefaultOptions(httpClient));
await client.GetAsync<object>("app.bsky.feed.getTimeline"); // No exception = success }
[Fact] public void CreateWithRestoredSession_CreatesAuthenticatedClient() { var pdsUrl = new Uri("https://pds.example.com");
using var client = ATProtoClient.CreateWithRestoredSession( SampleJwt, "refresh-token", "did:plc:test", "alice.bsky.social", pdsUrl, CreateDefaultOptions());
Assert.True(client.IsAuthenticated); Assert.Equal("did:plc:test", client.AuthenticatedDid); Assert.Equal(pdsUrl, client.BaseUrl); }
[Fact] public async Task CreateWithSessionAsync_AuthenticatesClient() { var handler = new MockHttpMessageHandler((request, ct) => { if (request.RequestUri!.ToString().Contains("createSession")) { return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent( $@"{{ ""accessJwt"": ""{SampleJwt}"", ""refreshJwt"": ""refresh-token"", ""did"": ""did:plc:test"", ""handle"": ""alice.bsky.social"" }}", Encoding.UTF8, "application/json") }); }
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{}", Encoding.UTF8, "application/json") }); });
using var httpClient = new HttpClient(handler);
using var client = await ATProtoClient.CreateWithSessionAsync( "alice.bsky.social", "app-password", options: CreateDefaultOptions(httpClient));
Assert.True(client.IsAuthenticated); Assert.Equal("did:plc:test", client.AuthenticatedDid); }
[Fact] public async Task CreateWithSessionAsync_FailedLogin_ThrowsAndCleansUp() { var handler = new MockHttpMessageHandler((request, ct) => { return Task.FromResult(new HttpResponseMessage(HttpStatusCode.Unauthorized) { Content = new StringContent( @"{""error"": ""AuthenticationRequired"", ""message"": ""Invalid credentials""}", Encoding.UTF8, "application/json") }); });
using var httpClient = new HttpClient(handler);
await Assert.ThrowsAsync<AuthenticationException>( () => ATProtoClient.CreateWithSessionAsync("alice.bsky.social", "wrong-password", options: CreateDefaultOptions(httpClient))); }
[Fact] public async Task GetAsync_WithAuth_IncludesAuthHeader() { string? authHeader = null; var handler = new MockHttpMessageHandler((request, ct) => { if (request.RequestUri!.ToString().Contains("createSession")) { return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent( $@"{{ ""accessJwt"": ""{SampleJwt}"", ""refreshJwt"": ""refresh-token"", ""did"": ""did:plc:test"", ""handle"": ""alice.bsky.social"" }}", Encoding.UTF8, "application/json") }); }
if (request.Headers.Authorization != null) { authHeader = request.Headers.Authorization.ToString(); }
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{}", Encoding.UTF8, "application/json") }); });
using var httpClient = new HttpClient(handler);
using var client = await ATProtoClient.CreateWithSessionAsync("alice.bsky.social", "app-password", options: CreateDefaultOptions(httpClient)); await client.GetAsync<object>("app.bsky.feed.getTimeline");
Assert.NotNull(authHeader); Assert.Contains("Bearer", authHeader); }
[Fact] public async Task GetAsync_WithLabelers_IncludesHeader() { string? labelerHeader = null; var handler = new MockHttpMessageHandler((request, ct) => { if (request.Headers.TryGetValues("atproto-accept-labelers", out var values)) { labelerHeader = string.Join(",", values); }
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{}", Encoding.UTF8, "application/json") }); });
using var httpClient = new HttpClient(handler); var options = CreateDefaultOptions(httpClient); options.LabelerDids = new[] { "did:plc:labeler1", "did:plc:labeler2" };
using var client = ATProtoClient.Create(options); await client.GetAsync<object>("app.bsky.feed.getTimeline");
Assert.Equal("did:plc:labeler1,did:plc:labeler2", labelerHeader); }
[Fact] public void ATProtoClientOptions_Clone_CreatesIndependentCopy() { var original = new ATProtoClientOptions { JsonOptions = TestHelpers.CreateJsonOptions(), CborContext = TestHelpers.CreateCborContext(), BaseUrl = new Uri("https://example.com"), AutoRetryOnAuthFailure = false, CreateIdentityResolver = false };
var clone = original.Clone();
Assert.Equal(original.BaseUrl, clone.BaseUrl); Assert.Equal(original.AutoRetryOnAuthFailure, clone.AutoRetryOnAuthFailure); Assert.Equal(original.CreateIdentityResolver, clone.CreateIdentityResolver);
// Modify clone, original should be unchanged clone.AutoRetryOnAuthFailure = true; Assert.False(original.AutoRetryOnAuthFailure); }
[Fact] public void Dispose_CanBeCalledMultipleTimes() { var client = ATProtoClient.Create(CreateDefaultOptions());
client.Dispose(); client.Dispose(); // Should not throw }
[Fact] public async Task GetAsync_AfterDispose_ThrowsObjectDisposedException() { var client = ATProtoClient.Create(CreateDefaultOptions()); client.Dispose();
await Assert.ThrowsAsync<ObjectDisposedException>( () => client.GetAsync<object>("app.bsky.feed.getTimeline")); }}
/// <summary>/// Integration tests for ATProtoClient./// Set BLUESKY_TEST_HANDLE and BLUESKY_TEST_PASSWORD environment variables to run./// </summary>public class ATProtoClientIntegrationTests{ private readonly string? _testHandle; private readonly string? _testPassword;
public ATProtoClientIntegrationTests() { _testHandle = Environment.GetEnvironmentVariable("BLUESKY_TEST_HANDLE"); _testPassword = Environment.GetEnvironmentVariable("BLUESKY_TEST_PASSWORD"); }
private bool CanRunIntegrationTests => !string.IsNullOrEmpty(_testHandle) && !string.IsNullOrEmpty(_testPassword);
private static ATProtoClientOptions CreateDefaultOptions(HttpClient? httpClient = null) { var options = new ATProtoClientOptions { JsonOptions = TestHelpers.CreateJsonOptions(), CborContext = TestHelpers.CreateCborContext() }; if (httpClient != null) { options.HttpClient = httpClient; } return options; }
[Fact] public async Task PublicClient_CanFetchPublicProfile() { using var client = ATProtoClient.Create(CreateDefaultOptions());
var parameters = new Dictionary<string, string> { { "actor", "bsky.app" } };
var profile = await client.GetAsync<TestProfileResponse>("app.bsky.actor.getProfile", parameters);
Assert.NotNull(profile); Assert.Equal("bsky.app", profile.Handle); }
[Fact] public async Task AuthenticatedClient_CanFetchTimeline() { if (!CanRunIntegrationTests) { return; }
using var client = await ATProtoClient.CreateWithSessionAsync(_testHandle!, _testPassword!, options: CreateDefaultOptions());
var parameters = new Dictionary<string, string> { { "limit", "5" } };
var timeline = await client.GetAsync<TestTimelineResponse>("app.bsky.feed.getTimeline", parameters);
Assert.NotNull(timeline); Assert.NotNull(timeline.Feed); }
[Fact] public async Task RestoredSession_CanMakeRequests() { if (!CanRunIntegrationTests) { return; }
// First, login to get tokens using var initialClient = await ATProtoClient.CreateWithSessionAsync(_testHandle!, _testPassword!, options: CreateDefaultOptions()); var tokenProvider = initialClient.TokenProvider as SessionTokenProvider; Assert.NotNull(tokenProvider);
var accessToken = await tokenProvider!.GetAccessTokenAsync(); var pdsUrl = tokenProvider.PdsUrl; var did = tokenProvider.CurrentDid; var handle = tokenProvider.Handle;
Assert.NotNull(accessToken); Assert.NotNull(pdsUrl); Assert.NotNull(did);
// Now create a new client with restored session // Note: In real usage, you'd store the refresh token too // For this test, we just verify the client can be created using var restoredClient = ATProtoClient.CreateWithRestoredSession( accessToken!, "fake-refresh-token", // Would need real refresh token for actual refresh did!, handle, pdsUrl!, CreateDefaultOptions());
Assert.True(restoredClient.IsAuthenticated); Assert.Equal(did, restoredClient.AuthenticatedDid); }
private class TestProfileResponse { public string? Did { get; set; } public string? Handle { get; set; } public string? DisplayName { get; set; } }
private class TestTimelineResponse { public object[]? Feed { get; set; } public string? Cursor { get; set; } }}
/// <summary>/// Simple mock HTTP message handler for testing./// </summary>internal class MockHttpMessageHandler : HttpMessageHandler{ private readonly Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> _handler;
public MockHttpMessageHandler(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handler) { _handler = handler; }
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { return _handler(request, cancellationToken); }}