Skip to content

Commit

Permalink
Finish tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 committed Aug 23, 2024
1 parent 6e9ef0e commit 196cb8c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 35 deletions.
51 changes: 37 additions & 14 deletions tests/Transports.AspNetCore.Tests/Middleware/GetTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net;
using System.Net.Http.Headers;
using GraphQL.PersistedDocuments;
using GraphQL.Server.Transports.AspNetCore.Errors;
using GraphQL.Validation;

Expand All @@ -10,6 +11,7 @@ public class GetTests : IDisposable
private GraphQLHttpMiddlewareOptions _options = null!;
private GraphQLHttpMiddlewareOptions _options2 = null!;
private Action<ExecutionOptions> _configureExecution = _ => { };
private bool _enablePersistedDocuments = true;
private readonly TestServer _server;

public GetTests()
Expand All @@ -18,19 +20,35 @@ public GetTests()
hostBuilder.ConfigureServices(services =>
{
services.AddSingleton<Chat.IChat, Chat.Chat>();
services.AddGraphQL(b => b
.AddAutoSchema<Chat.Query>(s => s
.WithMutation<Chat.Mutation>()
.WithSubscription<Chat.Subscription>())
.AddSchema<Schema2>()
.AddSystemTextJson()
.UsePersistedDocuments(o =>
services.AddGraphQL(b =>
{
b
.AddAutoSchema<Chat.Query>(s => s
.WithMutation<Chat.Mutation>()
.WithSubscription<Chat.Subscription>())
.AddSchema<Schema2>()
.AddSystemTextJson()
.ConfigureExecution((options, next) =>
{
if (_enablePersistedDocuments)
{
var handler = options.RequestServices!.GetRequiredService<PersistedDocumentHandler>();
return handler.ExecuteAsync(options, next);
}
return next(options);
})
.ConfigureExecutionOptions(o => _configureExecution(o));
b.Services.Configure<PersistedDocumentOptions>(o =>
{
o.AllowOnlyPersistedDocuments = false;
o.AllowedPrefixes.Add("test");
o.GetQueryDelegate = (options, prefix, payload) => prefix == "test" && payload == "abc" ? new("{count}") : default;
})
.ConfigureExecutionOptions(o => _configureExecution(o)));
o.GetQueryDelegate = (options, prefix, payload) =>
prefix == "test" && payload == "abc" ? new("{count}") :
prefix == "test" && payload == "form" ? new("query op1{ext} query op2($test:String!){ext var(test:$test)}") :
default;
});
});
services.AddSingleton<PersistedDocumentHandler>();
#if NETCOREAPP2_1 || NET48
services.AddHostApplicationLifetime();
#endif
Expand Down Expand Up @@ -360,14 +378,19 @@ public async Task QueryParseError(bool badRequest)
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task NoQuery(bool badRequest)
[InlineData(false, false)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(true, true)]
public async Task NoQuery(bool badRequest, bool usePersistedDocumentHandler)
{
_enablePersistedDocuments = usePersistedDocumentHandler;
_options.ValidationErrorsReturnBadRequest = badRequest;
var client = _server.CreateClient();
using var response = await client.GetAsync("/graphql");
await response.ShouldBeAsync(badRequest, """{"errors":[{"message":"GraphQL query is missing.","extensions":{"code":"QUERY_MISSING","codes":["QUERY_MISSING"]}}]}""");
await response.ShouldBeAsync(badRequest, usePersistedDocumentHandler
? """{"errors":[{"message":"The request must have a documentId parameter.","extensions":{"code":"DOCUMENT_ID_MISSING","codes":["DOCUMENT_ID_MISSING"]}}]}"""
: """{"errors":[{"message":"GraphQL query is missing.","extensions":{"code":"QUERY_MISSING","codes":["QUERY_MISSING"]}}]}""");
}

[Theory]
Expand Down
63 changes: 42 additions & 21 deletions tests/Transports.AspNetCore.Tests/Middleware/PostTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net;
using System.Net.Http.Headers;
using GraphQL.PersistedDocuments;
using GraphQL.Server.Transports.AspNetCore.Errors;
using GraphQL.Validation;

Expand All @@ -10,6 +11,7 @@ public class PostTests : IDisposable
private GraphQLHttpMiddlewareOptions _options = null!;
private GraphQLHttpMiddlewareOptions _options2 = null!;
private Action<ExecutionOptions> _configureExecution = _ => { };
private bool _enablePersistedDocuments = true;
private readonly TestServer _server;

public PostTests()
Expand All @@ -18,24 +20,37 @@ public PostTests()
hostBuilder.ConfigureServices(services =>
{
services.AddSingleton<Chat.IChat, Chat.Chat>();
services.AddGraphQL(b => b
.AddAutoSchema<Chat.Query>(s => s
.WithMutation<Chat.Mutation>()
.WithSubscription<Chat.Subscription>())
.AddSchema<Schema2>()
.AddAutoClrMappings()
.AddFormFileGraphType()
.AddSystemTextJson()
.UsePersistedDocuments(o =>
services.AddGraphQL(b =>
{
b
.AddAutoSchema<Chat.Query>(s => s
.WithMutation<Chat.Mutation>()
.WithSubscription<Chat.Subscription>())
.AddSchema<Schema2>()
.AddAutoClrMappings()
.AddFormFileGraphType()
.AddSystemTextJson()
.ConfigureExecution((options, next) =>
{
if (_enablePersistedDocuments)
{
var handler = options.RequestServices!.GetRequiredService<PersistedDocumentHandler>();
return handler.ExecuteAsync(options, next);
}
return next(options);
})
.ConfigureExecutionOptions(o => _configureExecution(o));
b.Services.Configure<PersistedDocumentOptions>(o =>
{
o.AllowOnlyPersistedDocuments = false;
o.AllowedPrefixes.Add("test");
o.GetQueryDelegate = (options, prefix, payload) =>
prefix == "test" && payload == "abc" ? new("{count}") :
prefix == "test" && payload == "form" ? new("query op1{ext} query op2($test:String!){ext var(test:$test)}") :
default;
})
.ConfigureExecutionOptions(o => _configureExecution(o)));
});
});
services.AddSingleton<PersistedDocumentHandler>();
#if NETCOREAPP2_1 || NET48
services.AddHostApplicationLifetime();
#endif
Expand Down Expand Up @@ -335,17 +350,17 @@ public async Task FormMultipart_Upload(bool requireCsrf, bool supplyCsrf, bool u
400, "{\"errors\":[{\"message\":\"Invalid map path. Map target cannot be null.\",\"extensions\":{\"code\":\"INVALID_MAP\",\"codes\":[\"INVALID_MAP\"]}}]}")]
// invalid map keys
[InlineData(40, null, "{\"\":[\"0.variables.arg\"]}", false, false,
400, "{\"errors\":[{\"message\":\"Invalid map path. Map key cannot be query, operationName, variables, extensions, operations or map.\",\"extensions\":{\"code\":\"INVALID_MAP\",\"codes\":[\"INVALID_MAP\"]}}]}")]
400, "{\"errors\":[{\"message\":\"Invalid map path. Map key cannot be query, operationName, variables, extensions, documentId, operations or map.\",\"extensions\":{\"code\":\"INVALID_MAP\",\"codes\":[\"INVALID_MAP\"]}}]}")]
[InlineData(41, null, "{\"query\":[\"0.variables.arg\"]}", false, false,
400, "{\"errors\":[{\"message\":\"Invalid map path. Map key cannot be query, operationName, variables, extensions, operations or map.\",\"extensions\":{\"code\":\"INVALID_MAP\",\"codes\":[\"INVALID_MAP\"]}}]}")]
400, "{\"errors\":[{\"message\":\"Invalid map path. Map key cannot be query, operationName, variables, extensions, documentId, operations or map.\",\"extensions\":{\"code\":\"INVALID_MAP\",\"codes\":[\"INVALID_MAP\"]}}]}")]
[InlineData(42, null, "{\"variables\":[\"0.variables.arg\"]}", false, false,
400, "{\"errors\":[{\"message\":\"Invalid map path. Map key cannot be query, operationName, variables, extensions, operations or map.\",\"extensions\":{\"code\":\"INVALID_MAP\",\"codes\":[\"INVALID_MAP\"]}}]}")]
400, "{\"errors\":[{\"message\":\"Invalid map path. Map key cannot be query, operationName, variables, extensions, documentId, operations or map.\",\"extensions\":{\"code\":\"INVALID_MAP\",\"codes\":[\"INVALID_MAP\"]}}]}")]
[InlineData(43, null, "{\"extensions\":[\"0.variables.arg\"]}", false, false,
400, "{\"errors\":[{\"message\":\"Invalid map path. Map key cannot be query, operationName, variables, extensions, operations or map.\",\"extensions\":{\"code\":\"INVALID_MAP\",\"codes\":[\"INVALID_MAP\"]}}]}")]
400, "{\"errors\":[{\"message\":\"Invalid map path. Map key cannot be query, operationName, variables, extensions, documentId, operations or map.\",\"extensions\":{\"code\":\"INVALID_MAP\",\"codes\":[\"INVALID_MAP\"]}}]}")]
[InlineData(44, null, "{\"operationName\":[\"0.variables.arg\"]}", false, false,
400, "{\"errors\":[{\"message\":\"Invalid map path. Map key cannot be query, operationName, variables, extensions, operations or map.\",\"extensions\":{\"code\":\"INVALID_MAP\",\"codes\":[\"INVALID_MAP\"]}}]}")]
400, "{\"errors\":[{\"message\":\"Invalid map path. Map key cannot be query, operationName, variables, extensions, documentId, operations or map.\",\"extensions\":{\"code\":\"INVALID_MAP\",\"codes\":[\"INVALID_MAP\"]}}]}")]
[InlineData(45, null, "{\"map\":[\"0.variables.arg\"]}", false, false,
400, "{\"errors\":[{\"message\":\"Invalid map path. Map key cannot be query, operationName, variables, extensions, operations or map.\",\"extensions\":{\"code\":\"INVALID_MAP\",\"codes\":[\"INVALID_MAP\"]}}]}")]
400, "{\"errors\":[{\"message\":\"Invalid map path. Map key cannot be query, operationName, variables, extensions, documentId, operations or map.\",\"extensions\":{\"code\":\"INVALID_MAP\",\"codes\":[\"INVALID_MAP\"]}}]}")]
// missing referenced file
[InlineData(50, null, "{\"file0\":[\"0.variables.arg\"]}", false, false,
400, "{\"errors\":[{\"message\":\"Invalid map path. Map key does not refer to an uploaded file.\",\"extensions\":{\"code\":\"INVALID_MAP\",\"codes\":[\"INVALID_MAP\"]}}]}")]
Expand Down Expand Up @@ -682,20 +697,26 @@ public async Task QueryParseError(bool badRequest)
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task NoQuery(bool badRequest)
[InlineData(false, false)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(true, true)]
public async Task NoQuery(bool badRequest, bool usePersistedDocumentHandler)
{
_enablePersistedDocuments = usePersistedDocumentHandler;
_options.ValidationErrorsReturnBadRequest = badRequest;
using var response = await PostJsonAsync("{}");
await response.ShouldBeAsync(badRequest, """{"errors":[{"message":"GraphQL query is missing.","extensions":{"code":"QUERY_MISSING","codes":["QUERY_MISSING"]}}]}""");
await response.ShouldBeAsync(badRequest, usePersistedDocumentHandler
? """{"errors":[{"message":"The request must have a documentId parameter.","extensions":{"code":"DOCUMENT_ID_MISSING","codes":["DOCUMENT_ID_MISSING"]}}]}"""
: """{"errors":[{"message":"GraphQL query is missing.","extensions":{"code":"QUERY_MISSING","codes":["QUERY_MISSING"]}}]}""");
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task NullRequest(bool badRequest)
{
_enablePersistedDocuments = false;
_options.ValidationErrorsReturnBadRequest = badRequest;
using var response = await PostJsonAsync("null");
await response.ShouldBeAsync(badRequest, """{"errors":[{"message":"GraphQL query is missing.","extensions":{"code":"QUERY_MISSING","codes":["QUERY_MISSING"]}}]}""");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ public async Task ExecuteRequestAsync()
Variables = new Inputs(new Dictionary<string, object?>()),
Extensions = new Inputs(new Dictionary<string, object?>()),
OperationName = "def",
DocumentId = "ghi",
};
_mockSerializer.Setup(x => x.ReadNode<GraphQLRequest>(payload))
.Returns(request)
Expand All @@ -381,6 +382,7 @@ public async Task ExecuteRequestAsync()
options.Query.ShouldBe(request.Query);
options.Variables.ShouldBe(request.Variables);
options.Extensions.ShouldBe(request.Extensions);
options.DocumentId.ShouldBe(request.DocumentId);
options.OperationName.ShouldBe(request.OperationName);
options.UserContext.ShouldBe(mockUserContext.Object);
options.RequestServices.ShouldBe(mockServiceProvider.Object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ public async Task ExecuteRequestAsync()
Variables = new Inputs(new Dictionary<string, object?>()),
Extensions = new Inputs(new Dictionary<string, object?>()),
OperationName = "def",
DocumentId = "ghi",
};
_mockSerializer.Setup(x => x.ReadNode<GraphQLRequest>(payload))
.Returns(request)
Expand All @@ -335,6 +336,7 @@ public async Task ExecuteRequestAsync()
options.Query.ShouldBe(request.Query);
options.Variables.ShouldBe(request.Variables);
options.Extensions.ShouldBe(request.Extensions);
options.DocumentId.ShouldBe(request.DocumentId);
options.OperationName.ShouldBe(request.OperationName);
options.UserContext.ShouldBe(mockUserContext.Object);
options.RequestServices.ShouldBe(mockServiceProvider.Object);
Expand Down

0 comments on commit 196cb8c

Please sign in to comment.