|
| 1 | +#if NET8_0_OR_GREATER |
| 2 | +using System; |
| 3 | +using System.Net.Http; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Mockolate.Parameters; |
| 7 | +using Mockolate.Setup; |
| 8 | + |
| 9 | +namespace Mockolate.Web; |
| 10 | + |
| 11 | +#pragma warning disable S2325 // Methods and properties that don't access instance data should be static |
| 12 | +/// <summary> |
| 13 | +/// Extensions for mocking <see cref="HttpClient" />. |
| 14 | +/// </summary> |
| 15 | +public static partial class HttpClientExtensions |
| 16 | +{ |
| 17 | + /// <inheritdoc cref="HttpClientExtensions" /> |
| 18 | + extension(IMockMethodSetup<HttpClient> setup) |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// Setup for the method |
| 22 | + /// <see cref="System.Net.Http.HttpClient.PatchAsync(string?, HttpContent?)" /> |
| 23 | + /// with the given <paramref name="requestUri" />. |
| 24 | + /// </summary> |
| 25 | + public IReturnMethodSetup<Task<HttpResponseMessage>, HttpRequestMessage, CancellationToken> PatchAsync( |
| 26 | + IParameter<string?>? requestUri, |
| 27 | + IParameter<HttpContent?>? content) |
| 28 | + => setup.PatchAsync(requestUri, content, It.IsAny<CancellationToken>()); |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Setup for the method |
| 32 | + /// <see cref="System.Net.Http.HttpClient.PatchAsync(Uri?, HttpContent?)" /> |
| 33 | + /// with the given <paramref name="requestUri" />. |
| 34 | + /// </summary> |
| 35 | + public IReturnMethodSetup<Task<HttpResponseMessage>, HttpRequestMessage, CancellationToken> PatchAsync( |
| 36 | + IParameter<Uri?>? requestUri, |
| 37 | + IParameter<HttpContent?>? content) |
| 38 | + => setup.PatchAsync(requestUri, content, It.IsAny<CancellationToken>()); |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Setup for the method |
| 42 | + /// <see cref="System.Net.Http.HttpClient.PatchAsync(string?, HttpContent?, System.Threading.CancellationToken)" /> |
| 43 | + /// with the given <paramref name="requestUri" /> and <paramref name="cancellationToken" />. |
| 44 | + /// </summary> |
| 45 | + public IReturnMethodSetup<Task<HttpResponseMessage>, HttpRequestMessage, CancellationToken> PatchAsync( |
| 46 | + IParameter<string?>? requestUri, |
| 47 | + IParameter<HttpContent?>? content, |
| 48 | + IParameter<CancellationToken> cancellationToken) |
| 49 | + { |
| 50 | + if (setup is Mock<HttpClient> httpClientMock && |
| 51 | + httpClientMock.ConstructorParameters[0] is IMockSubject<HttpMessageHandler> httpMessageHandlerMock && |
| 52 | + httpMessageHandlerMock.Mock is IMockMethodSetup<HttpMessageHandler> httpMessageHandlerSetup) |
| 53 | + { |
| 54 | + ReturnMethodSetup<Task<HttpResponseMessage>, HttpRequestMessage, CancellationToken> methodSetup = |
| 55 | + new("System.Net.Http.HttpMessageHandler.SendAsync", |
| 56 | + new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Patch, |
| 57 | + new HttpStringUriParameter(requestUri), |
| 58 | + new HttpRequestMessageParameter<HttpContent?>(r => r.Content, content))), |
| 59 | + new NamedParameter("cancellationToken", (IParameter)cancellationToken)); |
| 60 | + CastToMockRegistrationOrThrow(httpMessageHandlerSetup).SetupMethod(methodSetup); |
| 61 | + return methodSetup; |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + ReturnMethodSetup<Task<HttpResponseMessage>, HttpRequestMessage, CancellationToken> methodSetup = |
| 66 | + new("System.Net.Http.HttpMessageInvoker.SendAsync", |
| 67 | + new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Patch, |
| 68 | + new HttpStringUriParameter(requestUri), |
| 69 | + new HttpRequestMessageParameter<HttpContent?>(r => r.Content, content))), |
| 70 | + new NamedParameter("cancellationToken", (IParameter)cancellationToken)); |
| 71 | + CastToMockRegistrationOrThrow(setup).SetupMethod(methodSetup); |
| 72 | + return methodSetup; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Setup for the method |
| 78 | + /// <see cref="System.Net.Http.HttpClient.PatchAsync(Uri?, HttpContent?, System.Threading.CancellationToken)" /> |
| 79 | + /// with the given <paramref name="requestUri" /> and <paramref name="cancellationToken" />. |
| 80 | + /// </summary> |
| 81 | + public IReturnMethodSetup<Task<HttpResponseMessage>, HttpRequestMessage, CancellationToken> PatchAsync( |
| 82 | + IParameter<Uri?>? requestUri, |
| 83 | + IParameter<HttpContent?>? content, |
| 84 | + IParameter<CancellationToken> cancellationToken) |
| 85 | + { |
| 86 | + if (setup is Mock<HttpClient> httpClientMock && |
| 87 | + httpClientMock.ConstructorParameters[0] is IMockSubject<HttpMessageHandler> httpMessageHandlerMock && |
| 88 | + httpMessageHandlerMock.Mock is IMockMethodSetup<HttpMessageHandler> httpMessageHandlerSetup) |
| 89 | + { |
| 90 | + ReturnMethodSetup<Task<HttpResponseMessage>, HttpRequestMessage, CancellationToken> methodSetup = |
| 91 | + new("System.Net.Http.HttpMessageHandler.SendAsync", |
| 92 | + new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Patch, |
| 93 | + new HttpRequestMessageParameter<Uri?>(r => r.RequestUri, requestUri), |
| 94 | + new HttpRequestMessageParameter<HttpContent?>(r => r.Content, content))), |
| 95 | + new NamedParameter("cancellationToken", (IParameter)cancellationToken)); |
| 96 | + CastToMockRegistrationOrThrow(httpMessageHandlerSetup).SetupMethod(methodSetup); |
| 97 | + return methodSetup; |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + ReturnMethodSetup<Task<HttpResponseMessage>, HttpRequestMessage, CancellationToken> methodSetup = |
| 102 | + new("System.Net.Http.HttpMessageInvoker.SendAsync", |
| 103 | + new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Patch, |
| 104 | + new HttpRequestMessageParameter<Uri?>(r => r.RequestUri, requestUri), |
| 105 | + new HttpRequestMessageParameter<HttpContent?>(r => r.Content, content))), |
| 106 | + new NamedParameter("cancellationToken", (IParameter)cancellationToken)); |
| 107 | + CastToMockRegistrationOrThrow(setup).SetupMethod(methodSetup); |
| 108 | + return methodSetup; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | +#pragma warning restore S2325 // Methods and properties that don't access instance data should be static |
| 114 | +#endif |
0 commit comments