Skip to content

Commit e2701a2

Browse files
authored
Fix response factory to allow setting success (#54)
1 parent 0f7b402 commit e2701a2

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

src/Elastic.Transport/Responses/ResponseFactory.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
namespace Elastic.Transport;
6+
7+
/// <summary>
8+
/// A factory used to configure responses that can be used for unit testing.
9+
/// </summary>
10+
public static class TestableResponseFactory
11+
{
12+
/// <summary>
13+
/// Creates a response suitable for testing which includes the provided HTTP status code and is marked as successful.
14+
/// </summary>
15+
/// <param name="response">The <see cref="TransportResponse"/> to configure.</param>
16+
/// <param name="httpStatusCode">The HTTP status code to set on the final response.</param>
17+
/// <typeparam name="T">The response derived from <see cref="TransportResponse"/> that will be returned.</typeparam>
18+
/// <returns>The <typeparamref name="T"/> response configured as a successful server response with the provided status code.</returns>
19+
public static T CreateSuccessfulResponse<T>(T response, int httpStatusCode) where T : TransportResponse
20+
=> CreateResponse(response, httpStatusCode, true);
21+
22+
/// <summary>
23+
/// Creates a response suitable for testing which includes the provided HTTP status code and is marked as successful.
24+
/// </summary>
25+
/// <typeparam name="T">The response derived from <see cref="TransportResponse"/> that will be returned.</typeparam>
26+
/// <param name="response">The <see cref="TransportResponse"/> to configure.</param>
27+
/// <param name="httpStatusCode">The HTTP status code to set on the final response.</param>
28+
/// <param name="statusCodeRepresentsSuccess">Control whether the provided HTTP status code should be considered a success for this response.</param>
29+
/// <returns>The <typeparamref name="T"/> response configured with the provided status code where <see cref="ApiCallDetails.HasSuccessfulStatusCode"/> will be set using <paramref name="statusCodeRepresentsSuccess"/>.</returns>
30+
public static T CreateResponse<T>(T response, int httpStatusCode, bool statusCodeRepresentsSuccess) where T : TransportResponse
31+
{
32+
var apiCallDetails = new ApiCallDetails { HttpStatusCode = httpStatusCode, HasSuccessfulStatusCode = statusCodeRepresentsSuccess };
33+
return CreateResponse<T>(response, apiCallDetails);
34+
}
35+
36+
internal static T CreateResponse<T>(T response, ApiCallDetails apiCallDetails) where T : TransportResponse
37+
{
38+
response.ApiCallDetails = apiCallDetails;
39+
return response;
40+
}
41+
}

0 commit comments

Comments
 (0)