Skip to content

Commit

Permalink
Added Connection Refused Stitching Tests (#672)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Mar 29, 2019
1 parent 71ec0ab commit 7a817a2
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,96 @@ public async Task StitchedMutation()
Snapshot.Match(result);
}

[Fact]
public async Task ConnectionLost()
{
// arrange
var connections = new Dictionary<string, HttpClient>();
IHttpClientFactory clientFactory = CreateRemoteSchemas(connections);

var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(clientFactory);
serviceCollection.AddStitchedSchema(builder =>
builder.AddSchemaFromHttp("contract")
.AddSchemaFromHttp("customer")
.RenameType("CreateCustomerInput", "CreateCustomerInput2")
.AddExtensionsFromString(
FileResource.Open("StitchingExtensions.graphql"))
.AddSchemaConfiguration(c =>
c.RegisterType<PaginationAmountType>())
.AddExecutionConfiguration(b =>
{
b.AddErrorFilter(error =>
{
if (error.Exception is Exception ex)
{
return ErrorBuilder.FromError(error)
.ClearExtensions()
.SetMessage(ex.GetType().FullName)
.SetException(null)
.Build();
};
return error;
});
}));

IServiceProvider services =
serviceCollection.BuildServiceProvider();

IQueryExecutor executor = services
.GetRequiredService<IQueryExecutor>();
IExecutionResult result = null;

using (IServiceScope scope = services.CreateScope())
{
var request = new QueryRequest(@"
mutation {
createCustomer(input: { name: ""a"" })
{
customer {
name
contracts {
id
}
}
}
}");
request.Services = scope.ServiceProvider;

result = await executor.ExecuteAsync(request);
}

var client = new HttpClient
{
BaseAddress = new Uri("http://127.0.0.1")
}; ;
connections["contract"] = client;
connections["customer"] = client;

// act
using (IServiceScope scope = services.CreateScope())
{
var request = new QueryRequest(@"
mutation {
createCustomer(input: { name: ""a"" })
{
customer {
name
contracts {
id
}
}
}
}");
request.Services = scope.ServiceProvider;

result = await executor.ExecuteAsync(request);
}

// assert
Snapshot.Match(result);
}

[Fact]
public async Task StitchedMutationWithRenamedInputType()
{
Expand Down Expand Up @@ -1028,6 +1118,12 @@ public async Task<IExecutionResult>
}

private IHttpClientFactory CreateRemoteSchemas()
{
return CreateRemoteSchemas(new Dictionary<string, HttpClient>());
}

private IHttpClientFactory CreateRemoteSchemas(
Dictionary<string, HttpClient> connections)
{
TestServer server_contracts = TestServerFactory.Create(
ContractSchemaFactory.ConfigureSchema,
Expand All @@ -1039,13 +1135,19 @@ private IHttpClientFactory CreateRemoteSchemas()
CustomerSchemaFactory.ConfigureServices,
new QueryMiddlewareOptions());

connections["contract"] = server_contracts.CreateClient();
connections["customer"] = server_customers.CreateClient();

var httpClientFactory = new Mock<IHttpClientFactory>();
httpClientFactory.Setup(t => t.CreateClient(It.IsAny<string>()))
.Returns(new Func<string, HttpClient>(n =>
{
return n.Equals("contract")
? server_contracts.CreateClient()
: server_customers.CreateClient();
if (connections.ContainsKey(n))
{
return connections[n];
}

throw new Exception();
}));
return httpClientFactory.Object;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Data": {
"createCustomer": null
},
"Extensions": {},
"Errors": [
{
"Message": "System.Net.Http.HttpRequestException",
"Code": null,
"Path": null,
"Locations": [],
"Exception": null,
"Extensions": {}
}
]
}

0 comments on commit 7a817a2

Please sign in to comment.