From c76dc269bee667e7cacb3697472092d4741c99ea Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Thu, 29 Feb 2024 14:38:45 +0100 Subject: [PATCH] Fixed more tests --- .../src/AspNetCore/HttpPostMiddleware.cs | 33 ++++++++----------- .../PersistedQueryCache.cs | 2 +- .../GraphQLOverHttpSpecTests.cs | 10 ++++-- .../HttpGetMiddlewareTests.cs | 33 +++++++++++++------ ...ctivePersistedQuery_Invalid_Id_Format.snap | 11 +++++++ .../HttpPostMiddlewareTests.EmptyRequest.snap | 2 +- .../Pipeline/DocumentCacheMiddlewareTests.cs | 6 ++-- .../Language.Web/DocumentHashProviderBase.cs | 2 +- .../Parser/GraphQLRequestParserTests.cs | 24 +++++++++----- .../Sha256DocumentHashProviderTests.cs | 1 - 10 files changed, 78 insertions(+), 46 deletions(-) create mode 100644 src/HotChocolate/AspNetCore/test/AspNetCore.Tests/__snapshots__/HttpGetMiddlewareTests.Get_ActivePersistedQuery_Invalid_Id_Format.snap diff --git a/src/HotChocolate/AspNetCore/src/AspNetCore/HttpPostMiddleware.cs b/src/HotChocolate/AspNetCore/src/AspNetCore/HttpPostMiddleware.cs index 394d51bf5df..48a92470620 100644 --- a/src/HotChocolate/AspNetCore/src/AspNetCore/HttpPostMiddleware.cs +++ b/src/HotChocolate/AspNetCore/src/AspNetCore/HttpPostMiddleware.cs @@ -4,22 +4,17 @@ namespace HotChocolate.AspNetCore; -public sealed class HttpPostMiddleware : HttpPostMiddlewareBase -{ - public HttpPostMiddleware( - HttpRequestDelegate next, - IRequestExecutorResolver executorResolver, - IHttpResponseFormatter responseFormatter, - IHttpRequestParser requestParser, - IServerDiagnosticEvents diagnosticEvents, - string schemaName) - : base( - next, - executorResolver, - responseFormatter, - requestParser, - diagnosticEvents, - schemaName) - { - } -} +public sealed class HttpPostMiddleware( + HttpRequestDelegate next, + IRequestExecutorResolver executorResolver, + IHttpResponseFormatter responseFormatter, + IHttpRequestParser requestParser, + IServerDiagnosticEvents diagnosticEvents, + string schemaName) + : HttpPostMiddlewareBase( + next, + executorResolver, + responseFormatter, + requestParser, + diagnosticEvents, + schemaName); diff --git a/src/HotChocolate/AspNetCore/test/AspNetCore.Tests.Utilities/PersistedQueryCache.cs b/src/HotChocolate/AspNetCore/test/AspNetCore.Tests.Utilities/PersistedQueryCache.cs index 9d80cabbeeb..9a5d20954b4 100644 --- a/src/HotChocolate/AspNetCore/test/AspNetCore.Tests.Utilities/PersistedQueryCache.cs +++ b/src/HotChocolate/AspNetCore/test/AspNetCore.Tests.Utilities/PersistedQueryCache.cs @@ -12,7 +12,7 @@ public class PersistedQueryCache public PersistedQueryCache() { _cache.Add( - "60ddx/GGk4FDObSa6eK0sg==", + "60ddx_GGk4FDObSa6eK0sg", Utf8GraphQLParser.Parse(@"{ hero { name } }")); } diff --git a/src/HotChocolate/AspNetCore/test/AspNetCore.Tests/GraphQLOverHttpSpecTests.cs b/src/HotChocolate/AspNetCore/test/AspNetCore.Tests/GraphQLOverHttpSpecTests.cs index 89aa3b81b6e..941538841eb 100644 --- a/src/HotChocolate/AspNetCore/test/AspNetCore.Tests/GraphQLOverHttpSpecTests.cs +++ b/src/HotChocolate/AspNetCore/test/AspNetCore.Tests/GraphQLOverHttpSpecTests.cs @@ -144,7 +144,13 @@ public async Task Query_No_Body(string? acceptHeader, HttpTransportVersion trans { Content = new ByteArrayContent(Array.Empty()) { - Headers = { ContentType = new("application/json") { CharSet = "utf-8", }, }, + Headers = + { + ContentType = new("application/json") + { + CharSet = "utf-8", + }, + }, }, }; AddAcceptHeader(request, acceptHeader); @@ -162,7 +168,7 @@ public async Task Query_No_Body(string? acceptHeader, HttpTransportVersion trans Status Code: {expectedStatusCode} --------------------------> " + - @"{""errors"":[{""message"":""The GraphQL request is empty."",""extensions"":{""code"":""HC0012""}}]}"); + @"{""errors"":[{""message"":""The GraphQL request is empty."",""extensions"":{""code"":""HC0009""}}]}"); } [Theory] diff --git a/src/HotChocolate/AspNetCore/test/AspNetCore.Tests/HttpGetMiddlewareTests.cs b/src/HotChocolate/AspNetCore/test/AspNetCore.Tests/HttpGetMiddlewareTests.cs index 0dcdb9f9ca1..2f43988cc8c 100644 --- a/src/HotChocolate/AspNetCore/test/AspNetCore.Tests/HttpGetMiddlewareTests.cs +++ b/src/HotChocolate/AspNetCore/test/AspNetCore.Tests/HttpGetMiddlewareTests.cs @@ -661,6 +661,19 @@ public async Task Get_ActivePersistedQuery() // arrange var server = CreateStarWarsServer(); + // act + var result = await server.GetActivePersistedQueryAsync("md5Hash", "60ddx_GGk4FDObSa6eK0sg"); + + // assert + result.MatchSnapshot(); + } + + [Fact] + public async Task Get_ActivePersistedQuery_Invalid_Id_Format() + { + // arrange + var server = CreateStarWarsServer(); + // act var result = await server.GetActivePersistedQueryAsync("md5Hash", "60ddx/GGk4FDObSa6eK0sg=="); @@ -676,8 +689,7 @@ public async Task Get_ActivePersistedQuery_NotFound() var server = CreateStarWarsServer(); // act - var result = - await server.GetActivePersistedQueryAsync("md5Hash", "abc"); + var result = await server.GetActivePersistedQueryAsync("md5Hash", "abc"); // assert result.MatchSnapshot(); @@ -702,8 +714,7 @@ await server.GetStoreActivePersistedQueryAsync( "md5Hash", hash); - var resultB = - await server.GetActivePersistedQueryAsync("md5Hash", hash); + var resultB = await server.GetActivePersistedQueryAsync("md5Hash", hash); // assert new[] @@ -719,7 +730,7 @@ public async Task Get_ActivePersistedQuery_AddQuery_Unformatted() // arrange var server = CreateStarWarsServer(); - var query = "{__typename}"; + const string query = "{__typename}"; var hashProvider = new MD5DocumentHashProvider(HashFormat.Hex); var hash = hashProvider.ComputeHash(Encoding.UTF8.GetBytes(query)); @@ -750,12 +761,14 @@ public async Task Throw_Custom_GraphQL_Error() await server.GetAsync( new ClientQueryRequest { - Query = @" - { - hero { - name + Query = + """ + { + hero { + name + } } - }", + """, }); // assert diff --git a/src/HotChocolate/AspNetCore/test/AspNetCore.Tests/__snapshots__/HttpGetMiddlewareTests.Get_ActivePersistedQuery_Invalid_Id_Format.snap b/src/HotChocolate/AspNetCore/test/AspNetCore.Tests/__snapshots__/HttpGetMiddlewareTests.Get_ActivePersistedQuery_Invalid_Id_Format.snap new file mode 100644 index 00000000000..139e497ed4e --- /dev/null +++ b/src/HotChocolate/AspNetCore/test/AspNetCore.Tests/__snapshots__/HttpGetMiddlewareTests.Get_ActivePersistedQuery_Invalid_Id_Format.snap @@ -0,0 +1,11 @@ +{ + "ContentType": "application/graphql-response+json; charset=utf-8", + "StatusCode": "BadRequest", + "Data": null, + "Errors": [ + { + "message": "Invalid query id format." + } + ], + "Extensions": null +} \ No newline at end of file diff --git a/src/HotChocolate/AspNetCore/test/AspNetCore.Tests/__snapshots__/HttpPostMiddlewareTests.EmptyRequest.snap b/src/HotChocolate/AspNetCore/test/AspNetCore.Tests/__snapshots__/HttpPostMiddlewareTests.EmptyRequest.snap index d18b7fd6adc..1480b011f20 100644 --- a/src/HotChocolate/AspNetCore/test/AspNetCore.Tests/__snapshots__/HttpPostMiddlewareTests.EmptyRequest.snap +++ b/src/HotChocolate/AspNetCore/test/AspNetCore.Tests/__snapshots__/HttpPostMiddlewareTests.EmptyRequest.snap @@ -6,7 +6,7 @@ { "message": "The GraphQL request is empty.", "extensions": { - "code": "HC0012" + "code": "HC0009" } } ], diff --git a/src/HotChocolate/Core/test/Execution.Tests/Pipeline/DocumentCacheMiddlewareTests.cs b/src/HotChocolate/Core/test/Execution.Tests/Pipeline/DocumentCacheMiddlewareTests.cs index 6c7867ced76..374c9821b11 100644 --- a/src/HotChocolate/Core/test/Execution.Tests/Pipeline/DocumentCacheMiddlewareTests.cs +++ b/src/HotChocolate/Core/test/Execution.Tests/Pipeline/DocumentCacheMiddlewareTests.cs @@ -119,7 +119,7 @@ public async Task RetrieveItemFromCache_DocumentNotFoundOnCache() // assert Assert.Null(requestContext.Object.Document); Assert.Null(requestContext.Object.DocumentId); - Assert.Equal("1/4JnW9GhGu3YdhGeMefaA==", requestContext.Object.DocumentHash); + Assert.Equal("1_4JnW9GhGu3YdhGeMefaA", requestContext.Object.DocumentHash); } [Fact] @@ -159,7 +159,7 @@ public async Task AddItemToCacheWithDocumentId() // assert Assert.Equal(document, requestContext.Object.Document); Assert.Equal("a", requestContext.Object.DocumentId); - Assert.Equal("1/4JnW9GhGu3YdhGeMefaA==", requestContext.Object.DocumentHash); + Assert.Equal("1_4JnW9GhGu3YdhGeMefaA", requestContext.Object.DocumentHash); } [Fact] @@ -198,6 +198,6 @@ public async Task AddItemToCacheWithDocumentHash() // assert Assert.NotNull(requestContext.Object.Document); Assert.Equal(requestContext.Object.DocumentHash, requestContext.Object.DocumentId); - Assert.Equal("1/4JnW9GhGu3YdhGeMefaA==", requestContext.Object.DocumentHash); + Assert.Equal("1_4JnW9GhGu3YdhGeMefaA", requestContext.Object.DocumentHash); } } diff --git a/src/HotChocolate/Language/src/Language.Web/DocumentHashProviderBase.cs b/src/HotChocolate/Language/src/Language.Web/DocumentHashProviderBase.cs index a9add680fbe..a48e724378d 100644 --- a/src/HotChocolate/Language/src/Language.Web/DocumentHashProviderBase.cs +++ b/src/HotChocolate/Language/src/Language.Web/DocumentHashProviderBase.cs @@ -107,7 +107,7 @@ private static string ToBase64UrlSafeString( { byte[]? rented = null; var initialSize = hash.Length * 3; - var buffer = initialSize <= 256 + var buffer = initialSize <= GraphQLConstants.StackallocThreshold ? stackalloc byte[initialSize] : rented = ArrayPool.Shared.Rent(initialSize); int written; diff --git a/src/HotChocolate/Language/test/Language.Tests/Parser/GraphQLRequestParserTests.cs b/src/HotChocolate/Language/test/Language.Tests/Parser/GraphQLRequestParserTests.cs index b815f6f1b1e..b8f1c9f21e4 100644 --- a/src/HotChocolate/Language/test/Language.Tests/Parser/GraphQLRequestParserTests.cs +++ b/src/HotChocolate/Language/test/Language.Tests/Parser/GraphQLRequestParserTests.cs @@ -212,11 +212,13 @@ public void Parse_Kitchen_Sink_Query_With_Cache() var buffer = Encoding.UTF8.GetBytes(request.Query); var expectedHash = Convert.ToBase64String( - SHA1.Create().ComputeHash(buffer)); - + SHA1.Create().ComputeHash(buffer)) + .Replace("/", "_") + .Replace("+", "-") + .TrimEnd('='); + var source = Encoding.UTF8.GetBytes( - JsonConvert.SerializeObject(request - ).NormalizeLineBreaks()); + JsonConvert.SerializeObject(request).NormalizeLineBreaks()); var cache = new DocumentCache(); @@ -270,8 +272,11 @@ public void Parse_Skip_Custom_Property() var buffer = Encoding.UTF8.GetBytes(request.Query); var expectedHash = Convert.ToBase64String( - SHA1.Create().ComputeHash(buffer)); - + SHA1.Create().ComputeHash(buffer)) + .Replace("/", "_") + .Replace("+", "-") + .TrimEnd('='); + var cache = new DocumentCache(); var requestParser = new Utf8GraphQLRequestParser( @@ -313,8 +318,11 @@ public void Parse_Id_As_Name() var buffer = Encoding.UTF8.GetBytes(request.Query); var expectedHash = Convert.ToBase64String( - SHA1.Create().ComputeHash(buffer)); - + SHA1.Create().ComputeHash(buffer)) + .Replace("/", "_") + .Replace("+", "-") + .TrimEnd('='); + var cache = new DocumentCache(); var requestParser = new Utf8GraphQLRequestParser( diff --git a/src/HotChocolate/Language/test/Language.Web.Tests/Sha256DocumentHashProviderTests.cs b/src/HotChocolate/Language/test/Language.Web.Tests/Sha256DocumentHashProviderTests.cs index 5687057c44c..431c954b494 100644 --- a/src/HotChocolate/Language/test/Language.Web.Tests/Sha256DocumentHashProviderTests.cs +++ b/src/HotChocolate/Language/test/Language.Web.Tests/Sha256DocumentHashProviderTests.cs @@ -1,4 +1,3 @@ -using System.Text; using CookieCrumble; namespace HotChocolate.Language;