Skip to content

Commit

Permalink
Fixed more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Feb 29, 2024
1 parent b4cd935 commit c76dc26
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 46 deletions.
33 changes: 14 additions & 19 deletions src/HotChocolate/AspNetCore/src/AspNetCore/HttpPostMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class PersistedQueryCache
public PersistedQueryCache()
{
_cache.Add(
"60ddx/GGk4FDObSa6eK0sg==",
"60ddx_GGk4FDObSa6eK0sg",
Utf8GraphQLParser.Parse(@"{ hero { name } }"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ public async Task Query_No_Body(string? acceptHeader, HttpTransportVersion trans
{
Content = new ByteArrayContent(Array.Empty<byte>())
{
Headers = { ContentType = new("application/json") { CharSet = "utf-8", }, },
Headers =
{
ContentType = new("application/json")
{
CharSet = "utf-8",
},
},
},
};
AddAcceptHeader(request, acceptHeader);
Expand All @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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==");
Expand All @@ -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();
Expand All @@ -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[]
Expand All @@ -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));
Expand Down Expand Up @@ -750,12 +761,14 @@ public async Task Throw_Custom_GraphQL_Error()
await server.GetAsync(
new ClientQueryRequest
{
Query = @"
{
hero {
name
Query =
"""
{
hero {
name
}
}
}",
""",
});

// assert
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"ContentType": "application/graphql-response+json; charset=utf-8",
"StatusCode": "BadRequest",
"Data": null,
"Errors": [
{
"message": "Invalid query id format."
}
],
"Extensions": null
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
"message": "The GraphQL request is empty.",
"extensions": {
"code": "HC0012"
"code": "HC0009"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>.Shared.Rent(initialSize);
int written;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Text;
using CookieCrumble;

namespace HotChocolate.Language;
Expand Down

0 comments on commit c76dc26

Please sign in to comment.