Something went wrong. Try again.
.NET ATProtocol Library - Built for Source Gen
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374using System;using System.Collections.Generic;using System.Net;using System.Net.Http;using System.Text;using System.Text.Json;using System.Threading.Tasks;using CarpaNet;using CarpaNet.Cbor;using Xunit;
namespace CarpaNet.UnitTests.Http;
/// <summary>/// Tests for URL building and XRPC handling, tested through the public client./// </summary>public class XrpcUrlBuildingTests{ private static ATProtoClient CreateClient(HttpClient httpClient, Uri baseUrl) { return ATProtoClient.Create(new ATProtoClientOptions { HttpClient = httpClient, JsonOptions = TestHelpers.CreateJsonOptions(), CborContext = TestHelpers.CreateCborContext(), BaseUrl = baseUrl }); }
[Fact] public async Task GetAsync_WithNsidOnly_BuildsCorrectUrl() { string? capturedUrl = null; var handler = new MockHttpMessageHandler((request, ct) => { capturedUrl = request.RequestUri!.ToString(); return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{}", Encoding.UTF8, "application/json") }); });
using var httpClient = new HttpClient(handler) { BaseAddress = new Uri("https://bsky.social") }; using var client = CreateClient(httpClient, new Uri("https://bsky.social"));
await client.GetAsync<object>("app.bsky.feed.getTimeline");
Assert.Equal("https://bsky.social/xrpc/app.bsky.feed.getTimeline", capturedUrl); }
[Fact] public async Task GetAsync_WithParameters_BuildsCorrectQueryString() { string? capturedUrl = null; var handler = new MockHttpMessageHandler((request, ct) => { capturedUrl = request.RequestUri!.ToString(); return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{}", Encoding.UTF8, "application/json") }); });
using var httpClient = new HttpClient(handler) { BaseAddress = new Uri("https://bsky.social") }; using var client = CreateClient(httpClient, new Uri("https://bsky.social"));
var parameters = new Dictionary<string, string> { { "limit", "50" }, { "cursor", "abc123" } };
await client.GetAsync<object>("app.bsky.feed.getTimeline", parameters);
// Check both parameters are present Assert.Contains("limit=50", capturedUrl); Assert.Contains("cursor=abc123", capturedUrl); Assert.Contains("&", capturedUrl); }
[Fact] public async Task GetAsync_WithSpecialCharacters_EncodesCorrectly() { Uri? capturedUri = null; var handler = new MockHttpMessageHandler((request, ct) => { capturedUri = request.RequestUri; return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{}", Encoding.UTF8, "application/json") }); });
using var httpClient = new HttpClient(handler); using var client = CreateClient(httpClient, new Uri("https://bsky.social"));
var parameters = new Dictionary<string, string> { { "query", "hello world" } };
await client.GetAsync<object>("app.bsky.feed.searchPosts", parameters);
// The query string should contain our value - check via Query property which preserves encoding // or via the decoded string value Assert.NotNull(capturedUri); var query = capturedUri!.Query; // Query includes the '?' prefix Assert.Contains("query=hello", query.TrimStart('?')); }
[Fact] public async Task GetAsync_WithRepeatedKeys_EmitsRepeatedQueryParams() { Uri? capturedUri = null; var handler = new MockHttpMessageHandler((request, ct) => { capturedUri = request.RequestUri; return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{}", Encoding.UTF8, "application/json") }); });
using var httpClient = new HttpClient(handler) { BaseAddress = new Uri("https://bsky.social") }; using var client = CreateClient(httpClient, new Uri("https://bsky.social"));
var parameters = new List<KeyValuePair<string, string>> { new("uris", "at://did:plc:abc/app.bsky.feed.post/1"), new("uris", "at://did:plc:abc/app.bsky.feed.post/2"), };
await client.GetAsync<object>("app.bsky.feed.getPosts", parameters);
Assert.NotNull(capturedUri); var query = capturedUri!.Query.TrimStart('?'); var urisCount = 0; foreach (var pair in query.Split('&')) { if (pair.StartsWith("uris=", StringComparison.Ordinal)) urisCount++; } Assert.Equal(2, urisCount); Assert.Contains("uris=at%3A%2F%2Fdid%3Aplc%3Aabc%2Fapp.bsky.feed.post%2F1", query); Assert.Contains("uris=at%3A%2F%2Fdid%3Aplc%3Aabc%2Fapp.bsky.feed.post%2F2", query); }
[Fact] public async Task GetAsync_WithEmptyParameterValue_SkipsParameter() { string? capturedUrl = null; var handler = new MockHttpMessageHandler((request, ct) => { capturedUrl = request.RequestUri!.ToString(); return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{}", Encoding.UTF8, "application/json") }); });
using var httpClient = new HttpClient(handler); using var client = CreateClient(httpClient, new Uri("https://bsky.social"));
var parameters = new Dictionary<string, string> { { "limit", "50" }, { "cursor", "" } // Empty value };
await client.GetAsync<object>("app.bsky.feed.getTimeline", parameters);
Assert.Equal("https://bsky.social/xrpc/app.bsky.feed.getTimeline?limit=50", capturedUrl); }}
public class RateLimitInfoTests{ [Fact] public void FromHeaders_ParsesAllHeaders() { var headers = new Dictionary<string, string> { { "RateLimit-Limit", "3000" }, { "RateLimit-Remaining", "2500" }, { "RateLimit-Reset", "1704067200" }, { "RateLimit-Policy", "3000;w=300" } };
var info = RateLimitInfo.FromHeaders(name => headers.TryGetValue(name, out var value) ? value : null);
Assert.NotNull(info); Assert.Equal(3000, info!.Limit); Assert.Equal(2500, info.Remaining); Assert.Equal(1704067200L, info.ResetUnixTime); Assert.Equal("3000;w=300", info.Policy); }
[Fact] public void FromHeaders_ParsesRetryAfter() { var headers = new Dictionary<string, string> { { "Retry-After", "60" } };
var info = RateLimitInfo.FromHeaders(name => headers.TryGetValue(name, out var value) ? value : null);
Assert.NotNull(info); Assert.Equal(60, info!.RetryAfterSeconds); }
[Fact] public void FromHeaders_ReturnsNullForNoHeaders() { var info = RateLimitInfo.FromHeaders(_ => null);
Assert.Null(info); }
[Fact] public void ResetTime_ConvertsUnixTimestamp() { var info = new RateLimitInfo { ResetUnixTime = 1704067200 };
var resetTime = info.ResetTime;
Assert.NotNull(resetTime); Assert.Equal(2024, resetTime!.Value.Year); Assert.Equal(1, resetTime.Value.Month); Assert.Equal(1, resetTime.Value.Day); }
[Fact] public void GetRetryAfter_PrefersRetryAfterHeader() { var info = new RateLimitInfo { RetryAfterSeconds = 60, ResetUnixTime = DateTimeOffset.UtcNow.AddMinutes(5).ToUnixTimeSeconds() };
var retry = info.GetRetryAfter();
Assert.NotNull(retry); Assert.Equal(TimeSpan.FromSeconds(60), retry); }
[Fact] public void GetRetryAfter_FallsBackToResetTime() { var resetTime = DateTimeOffset.UtcNow.AddMinutes(2); var info = new RateLimitInfo { ResetUnixTime = resetTime.ToUnixTimeSeconds() };
var retry = info.GetRetryAfter();
Assert.NotNull(retry); // Should be approximately 2 minutes (allow some tolerance) Assert.True(retry!.Value.TotalMinutes >= 1.9 && retry.Value.TotalMinutes <= 2.1); }
[Fact] public void GetRetryAfter_ReturnsZeroForPastResetTime() { var info = new RateLimitInfo { ResetUnixTime = DateTimeOffset.UtcNow.AddMinutes(-5).ToUnixTimeSeconds() };
var retry = info.GetRetryAfter();
Assert.NotNull(retry); Assert.Equal(TimeSpan.Zero, retry); }
[Fact] public void GetRetryAfter_ReturnsNullForNoData() { var info = new RateLimitInfo();
var retry = info.GetRetryAfter();
Assert.Null(retry); }}
public class ATProtoExceptionTests{ [Fact] public void ATProtoException_SetsAllProperties() { var ex = new ATProtoException("Test message", "InvalidRequest", HttpStatusCode.BadRequest);
Assert.Equal("Test message", ex.Message); Assert.Equal("InvalidRequest", ex.ErrorCode); Assert.Equal(HttpStatusCode.BadRequest, ex.StatusCode); }
[Fact] public void ATProtoException_WithInnerException_SetsInnerException() { var inner = new Exception("Inner"); var ex = new ATProtoException("Outer", inner, "Error", HttpStatusCode.InternalServerError);
Assert.Same(inner, ex.InnerException); }
[Fact] public void AuthenticationException_ShouldRefresh_TrueForExpiredToken() { var ex = new AuthenticationException("Token expired", "ExpiredToken");
Assert.True(ex.ShouldRefresh); }
[Fact] public void AuthenticationException_ShouldRefresh_FalseForOtherErrors() { var ex = new AuthenticationException("Invalid token", "InvalidToken");
Assert.False(ex.ShouldRefresh); }
[Fact] public void AuthenticationException_HasCorrectStatusCode() { var ex = new AuthenticationException("Unauthorized");
Assert.Equal(HttpStatusCode.Unauthorized, ex.StatusCode); }
[Fact] public void RateLimitException_HasCorrectStatusCode() { var ex = new RateLimitException("Rate limited");
Assert.Equal((HttpStatusCode)429, ex.StatusCode); }
[Fact] public void RateLimitException_StoresRateLimitInfo() { var info = new RateLimitInfo { Limit = 100, Remaining = 0 }; var ex = new RateLimitException("Rate limited", info);
Assert.Same(info, ex.RateLimitInfo); }
[Fact] public void RateLimitException_RetryAfter_DelegatesToRateLimitInfo() { var info = new RateLimitInfo { RetryAfterSeconds = 30 }; var ex = new RateLimitException("Rate limited", info);
Assert.Equal(TimeSpan.FromSeconds(30), ex.RetryAfter); }
[Fact] public void ValidationException_HasCorrectStatusCode() { var ex = new ValidationException("Invalid request");
Assert.Equal(HttpStatusCode.BadRequest, ex.StatusCode); }}