From cf964054f3a93c23354f321c98ddc553d46cfb41 Mon Sep 17 00:00:00 2001 From: Adrian Hall Date: Sun, 18 May 2025 13:57:32 -0700 Subject: [PATCH] (#346) Swapped FluentAssertions for AwesomeAssertions --- Directory.Packages.props | 2 +- .../GenericAuthenticationProvider_Tests.cs | 8 +- .../Live/SampleServerTests.cs | 2 +- .../Operations/DeleteOperation_Tests.cs | 8 +- .../Operations/ReplaceOperation_Tests.cs | 9 +- .../Service/DatasyncServiceClient_Tests.cs | 24 ++-- .../Service/Create_Tests.cs | 12 +- .../Service/Delete_Tests.cs | 10 +- .../Service/Query_Tests.cs | 10 +- .../Service/Read_Tests.cs | 18 +-- .../Service/Replace_Tests.cs | 20 +-- .../FluentExtensions/ObjectAssertions.cs | 120 ++++-------------- .../FluentExtensions/StringAssertions.cs | 6 +- tests/Directory.Build.props | 2 +- 14 files changed, 91 insertions(+), 160 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index ac7e9527..955d848e 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -5,8 +5,8 @@ + - diff --git a/tests/CommunityToolkit.Datasync.Client.Test/Authentication/GenericAuthenticationProvider_Tests.cs b/tests/CommunityToolkit.Datasync.Client.Test/Authentication/GenericAuthenticationProvider_Tests.cs index 1e5bf2e5..2ec180a6 100644 --- a/tests/CommunityToolkit.Datasync.Client.Test/Authentication/GenericAuthenticationProvider_Tests.cs +++ b/tests/CommunityToolkit.Datasync.Client.Test/Authentication/GenericAuthenticationProvider_Tests.cs @@ -289,7 +289,7 @@ public async Task SendAsync_AddsHeader_BearerAuth() response.Should().NotBeNull(); handler.Requests.Should().ContainSingle(); - handler.Requests[0].Should().HaveHeader("Authorization", $"Bearer {ValidAuthenticationToken.Token}"); + handler.Requests[0].Headers.Should().ContainSingle(x => x.Key == "Authorization" && x.Value.First() == $"Bearer {ValidAuthenticationToken.Token}"); } [Fact] @@ -308,7 +308,7 @@ public async Task SendAsync_NoHeader_WhenExpired() response.Should().NotBeNull(); handler.Requests.Should().ContainSingle(); - handler.Requests[0].Should().NotHaveHeader("Authorization"); + handler.Requests[0].Headers.Should().NotContain(x => x.Key == "Authorization"); } [Fact] @@ -328,7 +328,7 @@ public async Task SendAsync_RemoveHeader_WhenExpired() response.Should().NotBeNull(); handler.Requests.Should().ContainSingle(); - handler.Requests[0].Should().NotHaveHeader("Authorization"); + handler.Requests[0].Headers.Should().NotContain(x => x.Key == "Authorization"); } [Fact] @@ -348,7 +348,7 @@ public async Task SendAsync_OverwritesHeader_WhenNotExpired() response.Should().NotBeNull(); handler.Requests.Should().ContainSingle(); - handler.Requests[0].Should().HaveHeader("Authorization", $"Bearer {ValidAuthenticationToken.Token}"); + handler.Requests[0].Headers.Should().ContainSingle(x => x.Key == "Authorization" && x.Value.First() == $"Bearer {ValidAuthenticationToken.Token}"); } /// diff --git a/tests/CommunityToolkit.Datasync.Client.Test/Live/SampleServerTests.cs b/tests/CommunityToolkit.Datasync.Client.Test/Live/SampleServerTests.cs index 92a827a5..7e97f7f7 100644 --- a/tests/CommunityToolkit.Datasync.Client.Test/Live/SampleServerTests.cs +++ b/tests/CommunityToolkit.Datasync.Client.Test/Live/SampleServerTests.cs @@ -24,7 +24,7 @@ public async Task Metadata_GetsSetByServer() TodoItem source = new() { Title = "Test item" }; HttpResponseMessage response = await client.PostAsJsonAsync($"{this.serviceEndpoint}/tables/TodoItem", source); - response.Should().HaveHttpStatusCode(HttpStatusCode.Created); + response.StatusCode.Should().Be(HttpStatusCode.Created); TodoItem result = await response.Content.ReadFromJsonAsync(); result.Id.Should().NotBeNullOrEmpty(); diff --git a/tests/CommunityToolkit.Datasync.Client.Test/Offline/Operations/DeleteOperation_Tests.cs b/tests/CommunityToolkit.Datasync.Client.Test/Offline/Operations/DeleteOperation_Tests.cs index 15824c8f..255b4655 100644 --- a/tests/CommunityToolkit.Datasync.Client.Test/Offline/Operations/DeleteOperation_Tests.cs +++ b/tests/CommunityToolkit.Datasync.Client.Test/Offline/Operations/DeleteOperation_Tests.cs @@ -21,7 +21,7 @@ public async Task DeleteOperation_ExecuteAsync() { MockDelegatingHandler handler = new(); HttpClient client = new(handler) { BaseAddress = new Uri("https://test.zumo.net") }; - string itemJson = """{"id":"123"}"""; + const string itemJson = """{"id":"123"}"""; DatasyncOperation op = new() { Id = Guid.NewGuid().ToString(), @@ -52,7 +52,7 @@ public async Task DeleteOperation_ExecuteAsync() request.Should().NotBeNull(); request.Method.Should().Be(HttpMethod.Delete); request.RequestUri.ToString().Should().Be("https://test.zumo.net/tables/movies/123"); - request.Should().NotHaveHeader("If-Match"); + request.Headers.Should().NotContain(x => x.Key == "If-Match"); response.Should().NotBeNull(); response.HasContent.Should().BeFalse(); @@ -67,7 +67,7 @@ public async Task DeleteOperation_ExecuteAsync_WithVersion() { MockDelegatingHandler handler = new(); HttpClient client = new(handler) { BaseAddress = new Uri("https://test.zumo.net") }; - string itemJson = """{"id":"123"}"""; + const string itemJson = """{"id":"123"}"""; DatasyncOperation op = new() { Id = Guid.NewGuid().ToString(), @@ -98,7 +98,7 @@ public async Task DeleteOperation_ExecuteAsync_WithVersion() request.Should().NotBeNull(); request.Method.Should().Be(HttpMethod.Delete); request.RequestUri.ToString().Should().Be("https://test.zumo.net/tables/movies/123"); - request.Should().HaveHeader("If-Match", "\"abcdefg\""); + request.Headers.Should().Contain(x => x.Key == "If-Match" && x.Value.First() == "\"abcdefg\""); response.Should().NotBeNull(); response.HasContent.Should().BeFalse(); diff --git a/tests/CommunityToolkit.Datasync.Client.Test/Offline/Operations/ReplaceOperation_Tests.cs b/tests/CommunityToolkit.Datasync.Client.Test/Offline/Operations/ReplaceOperation_Tests.cs index bd9cfe2a..ad570726 100644 --- a/tests/CommunityToolkit.Datasync.Client.Test/Offline/Operations/ReplaceOperation_Tests.cs +++ b/tests/CommunityToolkit.Datasync.Client.Test/Offline/Operations/ReplaceOperation_Tests.cs @@ -17,13 +17,12 @@ namespace CommunityToolkit.Datasync.Client.Test.Offline.Operations; [ExcludeFromCodeCoverage] public class ReplaceOperation_Tests { - [Fact] public async Task ReplaceOperation_ExecuteAsync() { MockDelegatingHandler handler = new(); HttpClient client = new(handler) { BaseAddress = new Uri("https://test.zumo.net") }; - string itemJson = """{"id":"123"}"""; + const string itemJson = """{"id":"123"}"""; DatasyncOperation op = new() { Id = Guid.NewGuid().ToString(), @@ -55,7 +54,7 @@ public async Task ReplaceOperation_ExecuteAsync() request.Should().NotBeNull(); request.Method.Should().Be(HttpMethod.Put); request.RequestUri.ToString().Should().Be("https://test.zumo.net/tables/movies/123"); - request.Should().NotHaveHeader("If-Match"); + request.Headers.Should().NotContain(x => x.Key == "If-Match"); (await request.Content.ReadAsStringAsync()).Should().Be(itemJson); response.Should().NotBeNull(); @@ -73,7 +72,7 @@ public async Task ReplaceOperation_ExecuteAsync_WithVersion() { MockDelegatingHandler handler = new(); HttpClient client = new(handler) { BaseAddress = new Uri("https://test.zumo.net") }; - string itemJson = """{"id":"123"}"""; + const string itemJson = """{"id":"123"}"""; DatasyncOperation op = new() { Id = Guid.NewGuid().ToString(), @@ -105,7 +104,7 @@ public async Task ReplaceOperation_ExecuteAsync_WithVersion() request.Should().NotBeNull(); request.Method.Should().Be(HttpMethod.Put); request.RequestUri.ToString().Should().Be("https://test.zumo.net/tables/movies/123"); - request.Should().HaveHeader("If-Match", "\"abcdefg\""); + request.Headers.Should().Contain(x => x.Key == "If-Match" && x.Value.First() == "\"abcdefg\""); (await request.Content.ReadAsStringAsync()).Should().Be(itemJson); response.Should().NotBeNull(); diff --git a/tests/CommunityToolkit.Datasync.Client.Test/Service/DatasyncServiceClient_Tests.cs b/tests/CommunityToolkit.Datasync.Client.Test/Service/DatasyncServiceClient_Tests.cs index b8c8bb49..36290fc2 100644 --- a/tests/CommunityToolkit.Datasync.Client.Test/Service/DatasyncServiceClient_Tests.cs +++ b/tests/CommunityToolkit.Datasync.Client.Test/Service/DatasyncServiceClient_Tests.cs @@ -56,7 +56,7 @@ internal class NamedSelectClass /// /// A test evaluator that ensures DateTimeOffset, DateTime, and TimeOnly values are msec resolution. /// - private readonly Func, EquivalencyAssertionOptions> entityEquivalentOptions = (options) => + private readonly Func, EquivalencyOptions> entityEquivalentOptions = (options) => { options.Using(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, 1.Milliseconds())).WhenTypeIs(); options.Using(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, 1.Milliseconds())).WhenTypeIs(); @@ -267,7 +267,7 @@ public async Task AddAsync_Conflict(HttpStatusCode code) { this.mockHandler.Responses.Add(GetSuccessfulResponse(this.successfulKitchenSink, code)); ClientKitchenSink entity = new() { Id = "1", StringValue = "abc" }; - string expected = """{"stringValue":"abc","id":"1"}"""; + const string expected = """{"stringValue":"abc","id":"1"}"""; DatasyncServiceClient client = GetMockClient(); Func act = async () => _ = await client.AddAsync(entity, new DatasyncServiceOptions()); @@ -1204,7 +1204,7 @@ public async Task Query_OnePageOfItems() [Fact] public async Task Query_TwoPagesOfItems() { - Page + Page page1 = CreatePage(5, null, "$skip=5"), page2 = CreatePage(5); @@ -1436,7 +1436,7 @@ public async Task RemoveAsync_SetsVersion() request.Should().NotBeNull(); request.Method.Should().Be(HttpMethod.Delete); request.RequestUri.ToString().Should().Be($"http://localhost/tables/kitchensink/{id}"); - request.Should().HaveHeader("If-Match", "\"abcdefg1234\""); + request.Headers.Should().Contain(x => x.Key == "If-Match" && x.Value.First() == "\"abcdefg1234\""); response.Should().NotBeNull(); response.HasContent.Should().BeFalse(); @@ -1458,7 +1458,7 @@ public async Task RemoveAsync_Extn_Unforced_SetsVersion() request.Should().NotBeNull(); request.Method.Should().Be(HttpMethod.Delete); request.RequestUri.ToString().Should().Be($"http://localhost/tables/kitchensink/{entity.Id}"); - request.Should().HaveHeader("If-Match", "\"abcdefg1234\""); + request.Headers.Should().Contain(x => x.Key == "If-Match" && x.Value.First() == "\"abcdefg1234\""); response.Should().NotBeNull(); response.HasContent.Should().BeFalse(); @@ -1480,7 +1480,7 @@ public async Task RemoveAsync_Extn2_SetsVersion() request.Should().NotBeNull(); request.Method.Should().Be(HttpMethod.Delete); request.RequestUri.ToString().Should().Be($"http://localhost/tables/kitchensink/{entity.Id}"); - request.Should().HaveHeader("If-Match", "\"abcdefg1234\""); + request.Headers.Should().Contain(x => x.Key == "If-Match" && x.Value.First() == "\"abcdefg1234\""); response.Should().NotBeNull(); response.HasContent.Should().BeFalse(); @@ -1502,7 +1502,7 @@ public async Task RemoveAsync_Extn_Forced_SetsVersion() request.Should().NotBeNull(); request.Method.Should().Be(HttpMethod.Delete); request.RequestUri.ToString().Should().Be($"http://localhost/tables/kitchensink/{entity.Id}"); - request.Should().NotHaveHeader("If-Match"); + request.Headers.Should().NotContain(x => x.Key == "If-Match"); response.Should().NotBeNull(); response.HasContent.Should().BeFalse(); @@ -1745,7 +1745,7 @@ public async Task ReplaceAsync_SetsVersion() request.Should().NotBeNull(); request.Method.Should().Be(HttpMethod.Put); request.RequestUri.ToString().Should().Be($"http://localhost/tables/kitchensink/{entity.Id}"); - request.Should().HaveHeader("If-Match", "\"abcdefg1234\""); + request.Headers.Should().Contain(x => x.Key == "If-Match" && x.Value.First() == "\"abcdefg1234\""); (await request.Content.ReadAsStringAsync()).Should().Be(expected); response.Should().NotBeNull(); @@ -1771,7 +1771,7 @@ public async Task ReplaceAsync_Extn_Forced_SetsVersion() request.Should().NotBeNull(); request.Method.Should().Be(HttpMethod.Put); request.RequestUri.ToString().Should().Be($"http://localhost/tables/kitchensink/{entity.Id}"); - request.Should().NotHaveHeader("If-Match"); + request.Headers.Should().NotContain(x => x.Key == "If-Match"); (await request.Content.ReadAsStringAsync()).Should().Be(expected); response.Should().NotBeNull(); @@ -1797,7 +1797,7 @@ public async Task ReplaceAsync_Extn_Unforced_SetsVersion() request.Should().NotBeNull(); request.Method.Should().Be(HttpMethod.Put); request.RequestUri.ToString().Should().Be($"http://localhost/tables/kitchensink/{entity.Id}"); - request.Should().HaveHeader("If-Match", "\"abcdefg1234\""); + request.Headers.Should().Contain(x => x.Key == "If-Match" && x.Value.First() == "\"abcdefg1234\""); (await request.Content.ReadAsStringAsync()).Should().Be(expected); response.Should().NotBeNull(); @@ -1823,7 +1823,7 @@ public async Task ReplaceAsync_Extn_Default_SetsVersion() request.Should().NotBeNull(); request.Method.Should().Be(HttpMethod.Put); request.RequestUri.ToString().Should().Be($"http://localhost/tables/kitchensink/{entity.Id}"); - request.Should().HaveHeader("If-Match", "\"abcdefg1234\""); + request.Headers.Should().Contain(x => x.Key == "If-Match" && x.Value.First() == "\"abcdefg1234\""); (await request.Content.ReadAsStringAsync()).Should().Be(expected); response.Should().NotBeNull(); @@ -3561,10 +3561,12 @@ public void Linq_Where_String_Contains() [Fact] public void Linq_Where_EndsWith_ToUpper() { +#pragma warning disable RCS1155 // Use StringComparison when comparing strings ExecuteQueryTest( x => x.Where(m => m.StringValue.ToUpperInvariant() == "ER"), "$filter=(toupper(stringValue) eq 'ER')" ); +#pragma warning restore RCS1155 // Use StringComparison when comparing strings } [Fact] diff --git a/tests/CommunityToolkit.Datasync.Server.Test/Service/Create_Tests.cs b/tests/CommunityToolkit.Datasync.Server.Test/Service/Create_Tests.cs index 022f3173..9b90cb0a 100644 --- a/tests/CommunityToolkit.Datasync.Server.Test/Service/Create_Tests.cs +++ b/tests/CommunityToolkit.Datasync.Server.Test/Service/Create_Tests.cs @@ -25,7 +25,7 @@ public async Task Create_WithValidInput_Returns201(string id) ClientMovie source = new(TestData.Movies.BlackPanther) { Id = id }; HttpResponseMessage response = await this.client.PostAsJsonAsync(this.factory.MovieEndpoint, source, this.serializerOptions); - response.Should().HaveStatusCode(HttpStatusCode.Created); + response.StatusCode.Should().Be(HttpStatusCode.Created); ClientMovie clientMovie = await response.Content.ReadFromJsonAsync(this.serializerOptions); clientMovie.Should().NotBeNull().And.HaveChangedMetadata(id, this.StartTime).And.BeEquivalentTo(source); @@ -44,7 +44,7 @@ public async Task Create_ExistingId_Returns409() ClientMovie source = new(TestData.Movies.BlackPanther) { Id = existingMovie.Id }; HttpResponseMessage response = await this.client.PostAsJsonAsync(this.factory.MovieEndpoint, source, this.serializerOptions); - response.Should().HaveStatusCode(HttpStatusCode.Conflict); + response.StatusCode.Should().Be(HttpStatusCode.Conflict); ClientMovie clientMovie = await response.Content.ReadFromJsonAsync(this.serializerOptions); clientMovie.Should().NotBeNull().And.HaveEquivalentMetadataTo(existingMovie).And.BeEquivalentTo(existingMovie); @@ -60,7 +60,7 @@ public async Task Create_SoftDeleted_Returns409() ClientMovie source = new(TestData.Movies.BlackPanther) { Id = existingMovie.Id }; HttpResponseMessage response = await this.client.PostAsJsonAsync(this.factory.MovieEndpoint, source, this.serializerOptions); - response.Should().HaveStatusCode(HttpStatusCode.Conflict); + response.StatusCode.Should().Be(HttpStatusCode.Conflict); ClientMovie clientMovie = await response.Content.ReadFromJsonAsync(this.serializerOptions); clientMovie.Should().NotBeNull().And.HaveEquivalentMetadataTo(existingMovie).And.BeEquivalentTo(existingMovie); @@ -83,7 +83,7 @@ public async Task Create_CanRoundtrip_Types() }; HttpResponseMessage response = await this.client.PostAsJsonAsync(this.factory.KitchenSinkEndpoint, source, this.serializerOptions); - response.Should().HaveStatusCode(HttpStatusCode.Created); + response.StatusCode.Should().Be(HttpStatusCode.Created); ClientKitchenSink clientKitchenSink = await response.Content.ReadFromJsonAsync(this.serializerOptions); clientKitchenSink.Should().NotBeNull().And.HaveChangedMetadata(id, this.StartTime).And.BeEquivalentTo(source); @@ -97,7 +97,7 @@ public async Task Create_NonJsonData_Returns415() { const string content = "

Not JSON

"; HttpResponseMessage response = await this.client.PostAsync(this.factory.MovieEndpoint, new StringContent(content, Encoding.UTF8, "text/html")); - response.Should().HaveStatusCode(HttpStatusCode.UnsupportedMediaType); + response.StatusCode.Should().Be(HttpStatusCode.UnsupportedMediaType); } [Theory] @@ -134,6 +134,6 @@ public async Task Create_ValidationError_Returns400(string propName, object prop } HttpResponseMessage response = await this.client.PostAsJsonAsync(this.factory.MovieEndpoint, source, this.serializerOptions); - response.Should().HaveStatusCode(HttpStatusCode.BadRequest); + response.StatusCode.Should().Be(HttpStatusCode.BadRequest); } } diff --git a/tests/CommunityToolkit.Datasync.Server.Test/Service/Delete_Tests.cs b/tests/CommunityToolkit.Datasync.Server.Test/Service/Delete_Tests.cs index 453a7db9..04b14ff4 100644 --- a/tests/CommunityToolkit.Datasync.Server.Test/Service/Delete_Tests.cs +++ b/tests/CommunityToolkit.Datasync.Server.Test/Service/Delete_Tests.cs @@ -19,7 +19,7 @@ public async Task Delete_ById_Returns204() InMemoryMovie existingMovie = this.factory.GetRandomMovie(); HttpResponseMessage response = await this.client.DeleteAsync($"{this.factory.MovieEndpoint}/{existingMovie.Id}"); - response.Should().HaveStatusCode(HttpStatusCode.NoContent); + response.StatusCode.Should().Be(HttpStatusCode.NoContent); InMemoryMovie serverEntity = this.factory.GetServerEntityById(existingMovie.Id); serverEntity.Should().BeNull(); @@ -39,7 +39,7 @@ public async Task Delete_WithVersioning_Works(string headerName, string value, H request.Headers.Add(headerName, etag); HttpResponseMessage response = await this.client.SendAsync(request); - response.Should().HaveStatusCode(expectedStatusCode); + response.StatusCode.Should().Be(expectedStatusCode); if (expectedStatusCode == HttpStatusCode.PreconditionFailed) { @@ -53,7 +53,7 @@ public async Task Delete_WithVersioning_Works(string headerName, string value, H public async Task Delete_MissingId_Returns404() { HttpResponseMessage response = await this.client.DeleteAsync($"{this.factory.MovieEndpoint}/missing"); - response.Should().HaveStatusCode(HttpStatusCode.NotFound); + response.StatusCode.Should().Be(HttpStatusCode.NotFound); } [Fact] @@ -64,7 +64,7 @@ public async Task Delete_NotSoftDeleted_Returns204() DateTimeOffset existingUpdatedAt = (DateTimeOffset)existingMovie.UpdatedAt; HttpResponseMessage response = await this.client.DeleteAsync($"{this.factory.SoftDeletedMovieEndpoint}/{existingMovie.Id}"); - response.Should().HaveStatusCode(HttpStatusCode.NoContent); + response.StatusCode.Should().Be(HttpStatusCode.NoContent); InMemoryMovie serverEntity = this.factory.GetServerEntityById(existingMovie.Id); serverEntity.Should().NotBeNull(); @@ -80,7 +80,7 @@ public async Task Delete_SoftDeletedId_Returns410() this.factory.SoftDelete(existingMovie); HttpResponseMessage response = await this.client.DeleteAsync($"{this.factory.SoftDeletedMovieEndpoint}/{existingMovie.Id}"); - response.Should().HaveStatusCode(HttpStatusCode.Gone); + response.StatusCode.Should().Be(HttpStatusCode.Gone); InMemoryMovie serverEntity = this.factory.GetServerEntityById(existingMovie.Id); serverEntity.Should().NotBeNull(); diff --git a/tests/CommunityToolkit.Datasync.Server.Test/Service/Query_Tests.cs b/tests/CommunityToolkit.Datasync.Server.Test/Service/Query_Tests.cs index 90e48bda..f0bbb67f 100644 --- a/tests/CommunityToolkit.Datasync.Server.Test/Service/Query_Tests.cs +++ b/tests/CommunityToolkit.Datasync.Server.Test/Service/Query_Tests.cs @@ -30,7 +30,7 @@ public class Query_Tests(ServiceApplicationFactory factory) : ServiceTest(factor public async Task FailedQueryTest(string query, HttpStatusCode expectedStatusCode) { HttpResponseMessage response = await this.client.GetAsync($"{this.factory.MovieEndpoint}?{query}"); - response.Should().HaveStatusCode(expectedStatusCode); + response.StatusCode.Should().Be(expectedStatusCode); } [Fact] @@ -3904,7 +3904,7 @@ public async Task SelectQueryTest(bool sId, bool sUpdatedAt, bool sVersion, bool string query = $"{this.factory.MovieEndpoint}?$top=5&$skip=5&$select={string.Join(',', selection)}"; HttpResponseMessage response = await this.client.GetAsync(query); - response.Should().HaveStatusCode(HttpStatusCode.OK); + response.StatusCode.Should().Be(HttpStatusCode.OK); string content = await response.Content.ReadAsStringAsync(); PageOfItems result = JsonSerializer.Deserialize>(content, this.serializerOptions); result.Should().NotBeNull(); @@ -3930,7 +3930,7 @@ private async Task MovieQueryTest(string pathAndQuery, int itemCount, string nex { HttpResponseMessage response = await this.client.GetAsync(pathAndQuery); string content = await response.Content.ReadAsStringAsync(); - response.Should().HaveStatusCode(HttpStatusCode.OK); + response.StatusCode.Should().Be(HttpStatusCode.OK); PageOfItems result = JsonSerializer.Deserialize>(content, this.serializerOptions); @@ -3961,7 +3961,7 @@ private async Task KitchenSinkQueryTest(string pathAndQuery, int itemCount, stri { HttpResponseMessage response = await this.client.GetAsync(pathAndQuery); string content = await response.Content.ReadAsStringAsync(); - response.Should().HaveStatusCode(HttpStatusCode.OK); + response.StatusCode.Should().Be(HttpStatusCode.OK); PageOfItems result = JsonSerializer.Deserialize>(content, this.serializerOptions); @@ -4000,7 +4000,7 @@ private async Task PagingTest(string startQuery, int expectedCount, int expected loops++; HttpResponseMessage response = await this.client.GetAsync(query); string content = await response.Content.ReadAsStringAsync(); - response.Should().HaveStatusCode(HttpStatusCode.OK); + response.StatusCode.Should().Be(HttpStatusCode.OK); PageOfItems result = JsonSerializer.Deserialize>(content, this.serializerOptions); result.Items.Should().NotBeNull(); result.Items.ToList().ForEach(x => items.Add(x.Id, x)); diff --git a/tests/CommunityToolkit.Datasync.Server.Test/Service/Read_Tests.cs b/tests/CommunityToolkit.Datasync.Server.Test/Service/Read_Tests.cs index 52deda27..a83f6734 100644 --- a/tests/CommunityToolkit.Datasync.Server.Test/Service/Read_Tests.cs +++ b/tests/CommunityToolkit.Datasync.Server.Test/Service/Read_Tests.cs @@ -19,7 +19,7 @@ public async Task Read_Returns200() { InMemoryMovie existingMovie = this.factory.GetRandomMovie(); HttpResponseMessage response = await this.client.GetAsync($"{this.factory.MovieEndpoint}/{existingMovie.Id}"); - response.Should().HaveStatusCode(HttpStatusCode.OK); + response.StatusCode.Should().Be(HttpStatusCode.OK); ClientMovie clientMovie = await response.Content.ReadFromJsonAsync(this.serializerOptions); clientMovie.Should().NotBeNull().And.HaveEquivalentMetadataTo(existingMovie).And.BeEquivalentTo(existingMovie); @@ -37,7 +37,7 @@ public async Task Read_WithVersioning_Works(string headerName, string headerValu HttpRequestMessage request = new(HttpMethod.Get, $"{this.factory.MovieEndpoint}/{existingMovie.Id}"); request.Headers.Add(headerName, headerValue ?? $"\"{Convert.ToBase64String(existingMovie.Version)}\""); HttpResponseMessage response = await this.client.SendAsync(request); - response.Should().HaveStatusCode(expectedStatusCode); + response.StatusCode.Should().Be(expectedStatusCode); if (expectedStatusCode == HttpStatusCode.OK) { @@ -64,7 +64,7 @@ public async Task Read_CanRoundtripTypes() this.factory.Store(storedEntity); HttpResponseMessage response = await this.client.GetAsync($"{this.factory.KitchenSinkEndpoint}/{storedEntity.Id}"); - response.Should().HaveStatusCode(HttpStatusCode.OK); + response.StatusCode.Should().Be(HttpStatusCode.OK); ClientKitchenSink clientEntity = await response.Content.ReadFromJsonAsync(this.serializerOptions); clientEntity.Should().NotBeNull().And.HaveEquivalentMetadataTo(storedEntity).And.BeEquivalentTo(storedEntity); @@ -117,7 +117,7 @@ public async Task Read_SerializationTests() this.factory.Store(entity2); HttpResponseMessage response = await this.client.GetAsync($"{this.factory.KitchenSinkEndpoint}/{entity1.Id}"); - response.Should().HaveStatusCode(HttpStatusCode.OK); + response.StatusCode.Should().Be(HttpStatusCode.OK); ClientObject actual = await response.Content.ReadFromJsonAsync(this.serializerOptions); @@ -143,7 +143,7 @@ public async Task Read_SerializationTests() actual.Data["timeOnlyValue"].Should().BeJsonElement("09:52:35.321"); HttpResponseMessage response2 = await this.client.GetAsync($"{this.factory.KitchenSinkEndpoint}/{entity2.Id}"); - response2.Should().HaveStatusCode(HttpStatusCode.OK); + response2.StatusCode.Should().Be(HttpStatusCode.OK); ClientObject actual2 = await response2.Content.ReadFromJsonAsync(); @@ -155,7 +155,7 @@ public async Task Read_SerializationTests() public async Task Read_MissingId_Returns404() { HttpResponseMessage response = await this.client.GetAsync($"{this.factory.MovieEndpoint}/missing"); - response.Should().HaveStatusCode(HttpStatusCode.NotFound); + response.StatusCode.Should().Be(HttpStatusCode.NotFound); } [Fact] @@ -163,7 +163,7 @@ public async Task Read_SoftDeleted_NotDeleted_Returns200() { InMemoryMovie existingMovie = this.factory.GetRandomMovie(); HttpResponseMessage response = await this.client.GetAsync($"{this.factory.SoftDeletedMovieEndpoint}/{existingMovie.Id}"); - response.Should().HaveStatusCode(HttpStatusCode.OK); + response.StatusCode.Should().Be(HttpStatusCode.OK); ClientMovie clientMovie = await response.Content.ReadFromJsonAsync(this.serializerOptions); clientMovie.Should().NotBeNull().And.HaveEquivalentMetadataTo(existingMovie).And.BeEquivalentTo(existingMovie); @@ -176,7 +176,7 @@ public async Task Read_SoftDeleted_Deleted_Returns410() InMemoryMovie existingMovie = this.factory.GetRandomMovie(); this.factory.SoftDelete(existingMovie); HttpResponseMessage response = await this.client.GetAsync($"{this.factory.SoftDeletedMovieEndpoint}/{existingMovie.Id}"); - response.Should().HaveStatusCode(HttpStatusCode.Gone); + response.StatusCode.Should().Be(HttpStatusCode.Gone); } [Fact] @@ -185,7 +185,7 @@ public async Task Read_SoftDeleted_Deleted_WithIncludeDeleted_Returns200() InMemoryMovie existingMovie = this.factory.GetRandomMovie(); this.factory.SoftDelete(existingMovie); HttpResponseMessage response = await this.client.GetAsync($"{this.factory.SoftDeletedMovieEndpoint}/{existingMovie.Id}?__includedeleted=true"); - response.Should().HaveStatusCode(HttpStatusCode.OK); + response.StatusCode.Should().Be(HttpStatusCode.OK); ClientMovie clientMovie = await response.Content.ReadFromJsonAsync(this.serializerOptions); clientMovie.Should().NotBeNull().And.HaveEquivalentMetadataTo(existingMovie).And.BeEquivalentTo(existingMovie); diff --git a/tests/CommunityToolkit.Datasync.Server.Test/Service/Replace_Tests.cs b/tests/CommunityToolkit.Datasync.Server.Test/Service/Replace_Tests.cs index 5f0a0add..8c5a297d 100644 --- a/tests/CommunityToolkit.Datasync.Server.Test/Service/Replace_Tests.cs +++ b/tests/CommunityToolkit.Datasync.Server.Test/Service/Replace_Tests.cs @@ -22,7 +22,7 @@ public async Task Replace_Returns200() { ClientMovie existingMovie = new(this.factory.GetRandomMovie()) { Title = "New Title" }; HttpResponseMessage response = await this.client.PutAsJsonAsync($"{this.factory.MovieEndpoint}/{existingMovie.Id}", existingMovie, this.serializerOptions); - response.Should().HaveStatusCode(HttpStatusCode.OK); + response.StatusCode.Should().Be(HttpStatusCode.OK); #if CAPTURE_STRING_JSON string jsonContent = await response.Content.ReadAsStringAsync(); @@ -52,7 +52,7 @@ public async Task Replace_WithVersioning_Works(string headerName, string headerV request.Headers.Add(headerName, etag); HttpResponseMessage response = await this.client.SendAsync(request); - response.Should().HaveStatusCode(expectedStatusCode); + response.StatusCode.Should().Be(expectedStatusCode); if (expectedStatusCode == HttpStatusCode.OK) { @@ -71,7 +71,7 @@ public async Task Replace_MissingId_Returns404() InMemoryMovie existingMovie = this.factory.GetRandomMovie(); ClientMovie source = new(existingMovie) { Id = "missing" }; HttpResponseMessage response = await this.client.PutAsJsonAsync($"{this.factory.MovieEndpoint}/missing", source, this.serializerOptions); - response.Should().HaveStatusCode(HttpStatusCode.NotFound, "Movie = {0}", existingMovie.Id); + response.StatusCode.Should().Be(HttpStatusCode.NotFound, "Movie = {0}", existingMovie.Id); } [Fact] @@ -79,7 +79,7 @@ public async Task Replace_IdMismatch_Returns400() { ClientMovie existingMovie = new(this.factory.GetRandomMovie()) { Title = "New Title" }; HttpResponseMessage response = await this.client.PutAsJsonAsync($"{this.factory.MovieEndpoint}/different-id", existingMovie, this.serializerOptions); - response.Should().HaveStatusCode(HttpStatusCode.BadRequest); + response.StatusCode.Should().Be(HttpStatusCode.BadRequest); } [Fact] @@ -87,7 +87,7 @@ public async Task Replace_NotSoftDeleted_Works() { InMemoryMovie existingMovie = this.factory.GetRandomMovie(); HttpResponseMessage response = await this.client.PutAsJsonAsync($"{this.factory.SoftDeletedMovieEndpoint}/{existingMovie.Id}", existingMovie, this.serializerOptions); - response.Should().HaveStatusCode(HttpStatusCode.OK); + response.StatusCode.Should().Be(HttpStatusCode.OK); ClientMovie clientMovie = await response.Content.ReadFromJsonAsync(this.serializerOptions); clientMovie.Should().NotBeNull().And.HaveChangedMetadata(existingMovie, this.StartTime).And.BeEquivalentTo(existingMovie); @@ -103,7 +103,7 @@ public async Task Replace_SoftDeleted_NotUndeleting_Returns410() InMemoryMovie existingMovie = this.factory.GetRandomMovie(); this.factory.SoftDelete(existingMovie); HttpResponseMessage response = await this.client.PutAsJsonAsync($"{this.factory.SoftDeletedMovieEndpoint}/{existingMovie.Id}", existingMovie, this.serializerOptions); - response.Should().HaveStatusCode(HttpStatusCode.Gone); + response.StatusCode.Should().Be(HttpStatusCode.Gone); } [Fact] @@ -112,7 +112,7 @@ public async Task Replace_SoftDeleted_Undeleting_Returns410() InMemoryMovie existingMovie = this.factory.GetRandomMovie(); this.factory.SoftDelete(existingMovie); HttpResponseMessage response = await this.client.PutAsJsonAsync($"{this.factory.SoftDeletedMovieEndpoint}/{existingMovie.Id}", existingMovie, this.serializerOptions); - response.Should().HaveStatusCode(HttpStatusCode.Gone); + response.StatusCode.Should().Be(HttpStatusCode.Gone); } [Fact] @@ -122,7 +122,7 @@ public async Task Replace_SoftDeleted_Undeleting_WithShowDeleted_Returns200() this.factory.SoftDelete(existingMovie); existingMovie.Deleted = false; HttpResponseMessage response = await this.client.PutAsJsonAsync($"{this.factory.SoftDeletedMovieEndpoint}/{existingMovie.Id}?__includedeleted=true", existingMovie, this.serializerOptions); - response.Should().HaveStatusCode(HttpStatusCode.OK); + response.StatusCode.Should().Be(HttpStatusCode.OK); ClientMovie clientMovie = await response.Content.ReadFromJsonAsync(this.serializerOptions); clientMovie.Should().NotBeNull().And.HaveChangedMetadata(existingMovie, this.StartTime).And.BeEquivalentTo(existingMovie); @@ -138,7 +138,7 @@ public async Task Replace_NonJsonData_Returns415() { const string content = "

Not JSON

"; HttpResponseMessage response = await this.client.PutAsync($"{this.factory.MovieEndpoint}/1", new StringContent(content, Encoding.UTF8, "text/html")); - response.Should().HaveStatusCode(HttpStatusCode.UnsupportedMediaType); + response.StatusCode.Should().Be(HttpStatusCode.UnsupportedMediaType); } /// @@ -154,7 +154,7 @@ public async Task Replace_Unauthorized_Returns401() InMemoryMovie inMemoryMovie = this.factory.GetRandomMovie(); ClientMovie existingMovie = new(inMemoryMovie) { Title = "New Title" }; HttpResponseMessage response = await this.client.PutAsJsonAsync($"{this.factory.AuthorizedMovieEndpoint}/{existingMovie.Id}", existingMovie, this.serializerOptions); - response.Should().HaveStatusCode(HttpStatusCode.Unauthorized); + response.StatusCode.Should().Be(HttpStatusCode.Unauthorized); // Ensure that the access provider was called with the existing movie, not the new movie InMemoryMovie lastEntity = this.factory.GetAuthorizedEntity(); diff --git a/tests/CommunityToolkit.Datasync.TestCommon/FluentExtensions/ObjectAssertions.cs b/tests/CommunityToolkit.Datasync.TestCommon/FluentExtensions/ObjectAssertions.cs index 9fae44d9..3033b7cf 100644 --- a/tests/CommunityToolkit.Datasync.TestCommon/FluentExtensions/ObjectAssertions.cs +++ b/tests/CommunityToolkit.Datasync.TestCommon/FluentExtensions/ObjectAssertions.cs @@ -7,6 +7,7 @@ using FluentAssertions; using FluentAssertions.Execution; using FluentAssertions.Primitives; +using Microsoft.AspNetCore.Mvc.Filters; using System.Net.Http.Headers; using System.Text.Json; @@ -20,7 +21,7 @@ public static class FluentObjectAssertions /// public static AndConstraint BeETag(this ObjectAssertions current, string value, string because = "", params object[] becauseArgs) { - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(current.Subject is EntityTagHeaderValue) .FailWith("Expected object to be an EntityTagHeaderValue", current.Subject) @@ -30,41 +31,22 @@ public static AndConstraint BeETag(this ObjectAssertions curre return new AndConstraint(current); } - public static AndConstraint BeHttpGet(this ObjectAssertions current, string uri, string because = "", params object[] becauseArgs) - { - Execute.Assertion - .BecauseOf(because, becauseArgs) - .ForCondition(current.Subject is HttpRequestMessage) - .FailWith("Expected object to be an HttpRequestMessage", current.Subject); - HttpRequestMessage sut = (HttpRequestMessage)current.Subject; - Execute.Assertion - .BecauseOf(because, becauseArgs) - .ForCondition(sut.Method == HttpMethod.Get) - .FailWith("Expected HttpRequestMessage to have Method == GET, but found {0}", sut.Method); - Execute.Assertion - .BecauseOf(because, becauseArgs) - .ForCondition(sut.RequestUri.ToString() == uri) - .FailWith("Expected HttpRequestMessage to have RequestUri == {0}, but found {1}", uri, sut.RequestUri.ToString()); - - return new AndConstraint(current); - } - /// /// Checks that the current object is a that is a boolean with the specified value. /// public static AndConstraint BeJsonElement(this ObjectAssertions current, bool value, string because = "", params object[] becauseArgs) { - Execute.Assertion - .BecauseOf(because, becauseArgs) + current.CurrentAssertionChain + .BecauseOf(because, becauseArgs) .ForCondition(current.Subject is JsonElement) .FailWith("Expected object to be a JsonElement", current.Subject); JsonElement jsonElement = (JsonElement)current.Subject; - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(jsonElement.ValueKind is JsonValueKind.False or JsonValueKind.True) .FailWith("Expected object to be a boolean, but found {0}", jsonElement.ValueKind); bool elementValue = jsonElement.GetBoolean(); - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(elementValue == value) .FailWith("Expected object to be a boolean with value {0}, but found {1}", value, elementValue); @@ -77,17 +59,17 @@ public static AndConstraint BeJsonElement(this ObjectAssertion ///
public static AndConstraint BeJsonElement(this ObjectAssertions current, double value, string because = "", params object[] becauseArgs) { - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(current.Subject is JsonElement) .FailWith("Expected object to be a JsonElement", current.Subject); JsonElement jsonElement = (JsonElement)current.Subject; - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(jsonElement.ValueKind == JsonValueKind.Number) .FailWith("Expected object to be a number, but found {0}", jsonElement.ValueKind); double elementValue = jsonElement.GetDouble(); - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(elementValue == value) .FailWith("Expected object to be a double with value {0}, but found {1}", value, elementValue); @@ -100,17 +82,17 @@ public static AndConstraint BeJsonElement(this ObjectAssertion /// public static AndConstraint BeJsonElement(this ObjectAssertions current, int value, string because = "", params object[] becauseArgs) { - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(current.Subject is JsonElement) .FailWith("Expected object to be a JsonElement", current.Subject); JsonElement jsonElement = (JsonElement)current.Subject; - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(jsonElement.ValueKind == JsonValueKind.Number) .FailWith("Expected object to be a number, but found {0}", jsonElement.ValueKind); int elementValue = jsonElement.GetInt32(); - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(elementValue == value) .FailWith("Expected object to be an int with value {0}, but found {1}", value, elementValue); @@ -123,18 +105,18 @@ public static AndConstraint BeJsonElement(this ObjectAssertion /// public static AndConstraint BeJsonElement(this ObjectAssertions current, string value, string because = "", params object[] becauseArgs) { - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(current.Subject is JsonElement) .FailWith("Expected object to be a JsonElement", current.Subject); JsonElement jsonElement = (JsonElement)current.Subject; - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(jsonElement.ValueKind == JsonValueKind.String) .FailWith("Expected object to be a string, but found {0}", jsonElement.ValueKind); string elementValue = jsonElement.GetString(); - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(elementValue == value) .FailWith("Expected object to be a string with value {0}, but found {1}", value, elementValue); @@ -147,13 +129,13 @@ public static AndConstraint BeJsonElement(this ObjectAssertion /// public static AndConstraint BeNullJsonElement(this ObjectAssertions current, string because = "", params object[] becauseArgs) { - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(current.Subject is null or JsonElement) .FailWith("Expected object to be a JsonElement", current.Subject); if (current.Subject is JsonElement jsonElement) { - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(jsonElement.ValueKind == JsonValueKind.Null) .FailWith("Expected object to be a NULL, but found {0}", jsonElement.ValueKind); @@ -167,18 +149,18 @@ public static AndConstraint BeNullJsonElement(this ObjectAsser /// public static AndConstraint BeJsonObject(this ObjectAssertions current, string value, string because = "", params object[] becauseArgs) { - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(current.Subject is JsonElement) .FailWith("Expected object to be a JsonElement", current.Subject); JsonElement jsonElement = (JsonElement)current.Subject; - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(jsonElement.ValueKind == JsonValueKind.Object) .FailWith("Expected object to be a string, but found {0}", jsonElement.ValueKind); string elementValue = jsonElement.ToString(); - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(elementValue == value) .FailWith("Expected object to be a string with value {0}, but found {1}", value, elementValue); @@ -194,13 +176,13 @@ public static AndConstraint HaveChangedMetadata(this ObjectAss // Round the start time to the nearest lowest millisecond. DateTimeOffset st = DateTimeOffset.FromUnixTimeMilliseconds(startTime.ToUnixTimeMilliseconds()); - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(current.Subject is ClientTableData) .FailWith("Expected object to be derived from ClientTableData"); ClientTableData metadata = (ClientTableData)current.Subject; - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(id == null ? !string.IsNullOrEmpty(metadata.Id) : metadata.Id == id) .FailWith(id == null ? "Expected Id to be set" : "Expected Id to be {0}, but found {1}", id, metadata.Id) @@ -222,7 +204,7 @@ public static AndConstraint HaveChangedMetadata(this ObjectAss // Round the start time to the nearest lowest millisecond. DateTimeOffset st = DateTimeOffset.FromUnixTimeMilliseconds(startTime.ToUnixTimeMilliseconds()); - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(current.Subject is ClientTableData) .FailWith("Expected object to be derived from ClientTableData") @@ -231,7 +213,7 @@ public static AndConstraint HaveChangedMetadata(this ObjectAss .FailWith("Expected source to be derived from ClientTableData or ITableData"); ClientTableData metadata = (ClientTableData)current.Subject; ClientTableData sourceMetadata = source is ClientTableData data ? data : new ClientTableData(source); - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(metadata.Id == sourceMetadata.Id) .FailWith("Exepcted Id to be {0}, but found {1}", sourceMetadata.Id, metadata.Id) @@ -251,7 +233,7 @@ public static AndConstraint HaveEquivalentMetadataTo(this Obje { const string dateFormat = "yyyy-MM-ddTHH:mm:ss.fffK"; - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(current.Subject is ITableData or ClientTableData) .FailWith("Expected object to be derived from ITableData or ClientTableData", current.Subject); @@ -259,7 +241,7 @@ public static AndConstraint HaveEquivalentMetadataTo(this Obje ClientTableData metadata = current.Subject is ClientTableData data ? data : new ClientTableData(current.Subject); bool updatedAtEquals = source.UpdatedAt == metadata.UpdatedAt; bool updatedAtClose = source.UpdatedAt != null && metadata.UpdatedAt != null && (source.UpdatedAt - metadata.UpdatedAt) < TimeSpan.FromMilliseconds(1); - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(metadata.Id == source.Id) .FailWith("Expected Id to be {0}, but found {1}", source.Id, metadata.Id) @@ -275,54 +257,4 @@ public static AndConstraint HaveEquivalentMetadataTo(this Obje return new AndConstraint(current); } - - /// - /// Checks that a has a specific header with a specific value. - /// - /// The current assertion. - /// The name of the header that is expected. - /// The expected value in the header. - /// A reason to use. - /// Any arguments to the reason. - /// A chaining construct. - public static AndConstraint HaveHeader(this ObjectAssertions current, string headerName, string expectedValue, string because = "", params object[] becauseArgs) - { - Execute.Assertion - .BecauseOf(because, becauseArgs) - .ForCondition(current.Subject is HttpRequestMessage) - .FailWith("Expected object to be a HttpRequestMessage", current.Subject); - - HttpRequestMessage request = (HttpRequestMessage)current.Subject; - Execute.Assertion - .BecauseOf(because, becauseArgs) - .ForCondition(request.Headers.TryGetValues(headerName, out IEnumerable values)) - .FailWith("Expected header {0} to be present", headerName) - .Then - .ForCondition(values.Contains(expectedValue)) - .FailWith("Exepcted header {0} to have value {1}", headerName, expectedValue); - return new AndConstraint(current); - } - - /// - /// Checks that a does not have a specific header. - /// - /// The current assertion. - /// The name of the header that is not expected. - /// A reason to use. - /// Any arguments to the reason. - /// A chaining construct. - public static AndConstraint NotHaveHeader(this ObjectAssertions current, string headerName, string because = "", params object[] becauseArgs) - { - Execute.Assertion - .BecauseOf(because, becauseArgs) - .ForCondition(current.Subject is HttpRequestMessage) - .FailWith("Expected object to be a HttpRequestMessage", current.Subject); - - HttpRequestMessage request = (HttpRequestMessage)current.Subject; - Execute.Assertion - .BecauseOf(because, becauseArgs) - .ForCondition(!request.Headers.TryGetValues(headerName, out IEnumerable _)) - .FailWith("Expected header {0} to not be present", headerName); - return new AndConstraint(current); - } } diff --git a/tests/CommunityToolkit.Datasync.TestCommon/FluentExtensions/StringAssertions.cs b/tests/CommunityToolkit.Datasync.TestCommon/FluentExtensions/StringAssertions.cs index dc071fb5..e891d433 100644 --- a/tests/CommunityToolkit.Datasync.TestCommon/FluentExtensions/StringAssertions.cs +++ b/tests/CommunityToolkit.Datasync.TestCommon/FluentExtensions/StringAssertions.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using FluentAssertions.Execution; using FluentAssertions.Primitives; using FluentAssertions; using Microsoft.AspNetCore.WebUtilities; @@ -18,7 +17,7 @@ public static class FluentStringAssertions /// public static AndConstraint BeAGuid(this StringAssertions current, string because = "", params object[] becauseArgs) { - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(Guid.TryParse(current.Subject, out _)) .FailWith("Expected object to be a Guid, but found {0}", current.Subject); @@ -31,11 +30,10 @@ public static AndConstraint MatchQueryString(this StringAssert Dictionary q2 = QueryHelpers.ParseNullableQuery(current.Subject) ?? []; bool isEquivalent = q1.Count == q2.Count && !q1.Except(q2).Any(); - Execute.Assertion + current.CurrentAssertionChain .BecauseOf(because, becauseArgs) .ForCondition(isEquivalent) .FailWith("Expected query string to match '{0}', but found '{1}'", queryString, current.Subject); return new AndConstraint(current); - } } diff --git a/tests/Directory.Build.props b/tests/Directory.Build.props index 48e94bbc..3ccbcf97 100644 --- a/tests/Directory.Build.props +++ b/tests/Directory.Build.props @@ -34,7 +34,7 @@ - +