Skip to content

Commit 942f3d4

Browse files
committed
Add responseHandler to WebApiTestBase methods to perform asserts on the HttpResponse<T>
1 parent 44bf1be commit 942f3d4

2 files changed

Lines changed: 33 additions & 18 deletions

File tree

src/Dibix.Testing/Http/WebApiServiceTestBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ namespace Dibix.Testing.Http
1515

1616
protected Task ExecuteTest<TContent>(Expression<Func<TService, Task<HttpResponse<TContent>>>> methodSelector) => ExecuteTest(x => InvokeApiAndAssertResponse(x.Service, methodSelector));
1717

18-
protected Task<TContent> InvokeApi<TContent>(HttpTestContext<TService> context, Expression<Func<TService, Task<HttpResponse<TContent>>>> methodSelector) => InvokeApi(context.Service, methodSelector);
18+
protected Task<TResponseContent> InvokeApi<TResponseContent>(HttpTestContext<TService> context, Expression<Func<TService, Task<HttpResponse<TResponseContent>>>> methodSelector, Action<HttpResponse<TResponseContent>> responseHandler = null) => InvokeApi(context.Service, methodSelector, responseHandler);
1919

20-
protected Task<TContent> InvokeApiAndAssertResponse<TContent>(HttpTestContext<TService> context, Expression<Func<TService, Task<HttpResponse<TContent>>>> methodSelector, string expectedText = null, string outputName = null, Action<JsonSerializerSettings> configureSerializer = null) => InvokeApiAndAssertResponse(context.Service, methodSelector, expectedText, outputName, configureSerializer);
20+
protected Task<TResponseContent> InvokeApiAndAssertResponse<TResponseContent>(HttpTestContext<TService> context, Expression<Func<TService, Task<HttpResponse<TResponseContent>>>> methodSelector, string expectedText = null, string outputName = null, Action<JsonSerializerSettings> configureSerializer = null, Action<HttpResponse<TResponseContent>> responseHandler = null) => InvokeApiAndAssertResponse(context.Service, methodSelector, expectedText, outputName, configureSerializer, responseHandler);
2121
#endregion
2222

2323
#region Private Methods

src/Dibix.Testing/Http/WebApiTestBase.cs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,28 @@ protected virtual void ConfigureOptions(HttpClientOptions options)
3838

3939
protected Task InvokeApi<TService>(HttpTestContext context, Expression<Func<TService, Task<HttpResponseMessage>>> methodSelector) => CreateServiceAndInvokeApi(context, methodSelector);
4040
protected Task InvokeApi<TService>(TService service, Expression<Func<TService, Task<HttpResponseMessage>>> methodSelector) => InvokeApiCore(service, methodSelector);
41-
protected async Task<TResponseContent> InvokeApi<TService, TResponseContent>(HttpTestContext context, Expression<Func<TService, Task<HttpResponse<TResponseContent>>>> methodSelector)
41+
protected async Task<TResponseContent> InvokeApi<TService, TResponseContent>(HttpTestContext context, Expression<Func<TService, Task<HttpResponse<TResponseContent>>>> methodSelector, Action<HttpResponse<TResponseContent>> responseHandler = null)
4242
{
43-
HttpResponse<TResponseContent> response = await CreateServiceAndInvokeApi(context, methodSelector).ConfigureAwait(false);
44-
return response.ResponseContent;
43+
TResponseContent responseContent = await CreateServiceAndInvokeApiContent(context, methodSelector, responseHandler).ConfigureAwait(false);
44+
return responseContent;
4545
}
46-
protected async Task<TResponseContent> InvokeApi<TService, TResponseContent>(TService service, Expression<Func<TService, Task<HttpResponse<TResponseContent>>>> methodSelector)
46+
protected async Task<TResponseContent> InvokeApi<TService, TResponseContent>(TService service, Expression<Func<TService, Task<HttpResponse<TResponseContent>>>> methodSelector, Action<HttpResponse<TResponseContent>> responseHandler = null)
4747
{
48-
HttpResponse<TResponseContent> response = await InvokeApiCore(service, methodSelector).ConfigureAwait(false);
49-
return response.ResponseContent;
48+
TResponseContent responseContent = await InvokeApiCoreContent(service, methodSelector, responseHandler).ConfigureAwait(false);
49+
return responseContent;
5050
}
5151

52-
protected async Task<TResponseContent> InvokeApiAndAssertResponse<TService, TResponseContent>(TService service, Expression<Func<TService, Task<HttpResponse<TResponseContent>>>> methodSelector, string expectedText = null, string outputName = null, Action<JsonSerializerSettings> configureSerializer = null)
52+
protected async Task<TResponseContent> InvokeApiAndAssertResponse<TService, TResponseContent>(TService service, Expression<Func<TService, Task<HttpResponse<TResponseContent>>>> methodSelector, string expectedText = null, string outputName = null, Action<JsonSerializerSettings> configureSerializer = null, Action<HttpResponse<TResponseContent>> responseHandler = null)
5353
{
54-
HttpResponse<TResponseContent> response = await InvokeApiCore(service, methodSelector).ConfigureAwait(false);
55-
AssertJsonResponse(response.ResponseContent, configureSerializer, outputName, expectedText);
56-
return response.ResponseContent;
54+
TResponseContent responseContent = await InvokeApiCoreContent(service, methodSelector, responseHandler).ConfigureAwait(false);
55+
AssertJsonResponse(responseContent, configureSerializer, outputName, expectedText);
56+
return responseContent;
5757
}
58-
protected async Task<TResponseContent> InvokeApiAndAssertResponse<TService, TResponseContent>(HttpTestContext context, Expression<Func<TService, Task<HttpResponse<TResponseContent>>>> methodSelector, string expectedText = null, string outputName = null, Action<JsonSerializerSettings> configureSerializer = null)
58+
protected async Task<TResponseContent> InvokeApiAndAssertResponse<TService, TResponseContent>(HttpTestContext context, Expression<Func<TService, Task<HttpResponse<TResponseContent>>>> methodSelector, string expectedText = null, string outputName = null, Action<JsonSerializerSettings> configureSerializer = null, Action<HttpResponse<TResponseContent>> responseHandler = null)
5959
{
60-
HttpResponse<TResponseContent> response = await CreateServiceAndInvokeApi(context, methodSelector).ConfigureAwait(false);
61-
AssertJsonResponse(response.ResponseContent, configureSerializer, outputName, expectedText);
62-
return response.ResponseContent;
60+
TResponseContent responseContent = await CreateServiceAndInvokeApiContent(context, methodSelector, responseHandler).ConfigureAwait(false);
61+
AssertJsonResponse(responseContent, configureSerializer, outputName, expectedText);
62+
return responseContent;
6363
}
6464

6565
protected virtual void ConfigureClient(TConfiguration configuration, IHttpClientBuilder builder) { }
@@ -68,6 +68,13 @@ protected virtual void ConfigureClient(TConfiguration configuration, IHttpClient
6868
#endregion
6969

7070
#region Private Methods
71+
private static async Task<TResponseContent> InvokeApiCoreContent<TService, TResponseContent>(TService service, Expression<Func<TService, Task<HttpResponse<TResponseContent>>>> methodSelector, Action<HttpResponse<TResponseContent>> responseHandler)
72+
{
73+
HttpResponse<TResponseContent> response = await InvokeApiCore(service, methodSelector).ConfigureAwait(false);
74+
responseHandler?.Invoke(response);
75+
return response.ResponseContent;
76+
}
77+
7178
private static async Task<TResponse> InvokeApiCore<TService, TResponse>(TService service, Expression<Func<TService, Task<TResponse>>> methodSelector)
7279
{
7380
Func<TService, Task<TResponse>> compiled = methodSelector.Compile();
@@ -76,10 +83,18 @@ private static async Task<TResponse> InvokeApiCore<TService, TResponse>(TService
7683
return response;
7784
}
7885

79-
private static Task<TResponse> CreateServiceAndInvokeApi<TService, TResponse>(HttpTestContext context, Expression<Func<TService, Task<TResponse>>> methodSelector)
86+
private static async Task<TResponseContent> CreateServiceAndInvokeApiContent<TService, TResponseContent>(HttpTestContext context, Expression<Func<TService, Task<HttpResponse<TResponseContent>>>> methodSelector, Action<HttpResponse<TResponseContent>> responseHandler)
87+
{
88+
TService service = HttpServiceFactory.CreateServiceInstance<TService>(context.HttpClientFactory, context.HttpClientOptions, context.HttpAuthorizationProvider);
89+
TResponseContent responseContent = await InvokeApiCoreContent(service, methodSelector, responseHandler).ConfigureAwait(false);
90+
return responseContent;
91+
}
92+
93+
private static async Task<TResponse> CreateServiceAndInvokeApi<TService, TResponse>(HttpTestContext context, Expression<Func<TService, Task<TResponse>>> methodSelector)
8094
{
8195
TService service = HttpServiceFactory.CreateServiceInstance<TService>(context.HttpClientFactory, context.HttpClientOptions, context.HttpAuthorizationProvider);
82-
return InvokeApiCore(service, methodSelector);
96+
TResponse response = await InvokeApiCore(service, methodSelector).ConfigureAwait(false);
97+
return response;
8398
}
8499

85100
private static HttpTestContext CreateTestContext(IHttpClientFactory httpClientFactory, HttpClientOptions httpClientOptions, IHttpAuthorizationProvider authorizationProvider)

0 commit comments

Comments
 (0)