|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Net; |
| 6 | +using System.Text; |
| 7 | +using System.Text.Encodings.Web; |
| 8 | +using System.Text.Json; |
| 9 | +using System.Text.Json.Serialization; |
| 10 | + |
| 11 | +namespace CommunityToolkit.Datasync.Client.Test.Helpers; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// A delegating handler for mocking responses. |
| 15 | +/// </summary> |
| 16 | +[ExcludeFromCodeCoverage] |
| 17 | +public class MockDelegatingHandler : DelegatingHandler |
| 18 | +{ |
| 19 | + // For manipulating the request/response link - we need to surround it with a lock |
| 20 | + private readonly SemaphoreSlim requestLock = new(1, 1); |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Used for serializing objects to be returned as responses. |
| 24 | + /// </summary> |
| 25 | + private static readonly JsonSerializerOptions serializerOptions = new(JsonSerializerDefaults.Web) |
| 26 | + { |
| 27 | + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, |
| 28 | + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, |
| 29 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase |
| 30 | + }; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// List of requests that have been received. |
| 34 | + /// </summary> |
| 35 | + public List<HttpRequestMessage> Requests { get; } = []; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// List of responses that will be sent. |
| 39 | + /// </summary> |
| 40 | + public List<HttpResponseMessage> Responses { get; } = []; |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Handler for the request/response |
| 44 | + /// </summary> |
| 45 | + protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token = default) |
| 46 | + { |
| 47 | + await this.requestLock.WaitAsync(token).ConfigureAwait(false); |
| 48 | + try |
| 49 | + { |
| 50 | + Requests.Add(await CloneRequest(request).ConfigureAwait(false)); |
| 51 | + return Responses[Requests.Count - 1]; |
| 52 | + } |
| 53 | + finally |
| 54 | + { |
| 55 | + this.requestLock.Release(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Clone the <see cref="HttpRequestMessage"/>. |
| 61 | + /// </summary> |
| 62 | + public static async Task<HttpRequestMessage> CloneRequest(HttpRequestMessage request) |
| 63 | + { |
| 64 | + HttpRequestMessage clone = new(request.Method, request.RequestUri) |
| 65 | + { |
| 66 | + Version = request.Version |
| 67 | + }; |
| 68 | + request.Headers.ToList().ForEach(header => clone.Headers.TryAddWithoutValidation(header.Key, header.Value)); |
| 69 | + |
| 70 | + if (request.Content != null) |
| 71 | + { |
| 72 | + MemoryStream ms = new(); |
| 73 | + await request.Content.CopyToAsync(ms).ConfigureAwait(false); |
| 74 | + ms.Position = 0; |
| 75 | + clone.Content = new StreamContent(ms); |
| 76 | + |
| 77 | + request.Content.Headers?.ToList().ForEach(header => clone.Content.Headers.Add(header.Key, header.Value)); |
| 78 | + } |
| 79 | + |
| 80 | + return clone; |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Adds a response with no payload to the list of responses. |
| 85 | + /// </summary> |
| 86 | + /// <param name="statusCode"></param> |
| 87 | + /// <param name="headers"></param> |
| 88 | + public void AddResponse(HttpStatusCode statusCode, IDictionary<string, string> headers = null) |
| 89 | + => Responses.Add(CreateResponse(statusCode, headers)); |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Adds a response with a string payload. |
| 93 | + /// </summary> |
| 94 | + /// <param name="content">The JSON content</param> |
| 95 | + public void AddResponseContent(string content, HttpStatusCode statusCode = HttpStatusCode.OK) |
| 96 | + { |
| 97 | + HttpResponseMessage response = CreateResponse(statusCode); |
| 98 | + response.Content = new StringContent(content, Encoding.UTF8, "application/json"); |
| 99 | + Responses.Add(response); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Adds a response with a payload to the list of responses. |
| 104 | + /// </summary> |
| 105 | + /// <typeparam name="T"></typeparam> |
| 106 | + /// <param name="statusCode"></param> |
| 107 | + /// <param name="payload"></param> |
| 108 | + /// <param name="headers"></param> |
| 109 | + public void AddResponse<T>(HttpStatusCode statusCode, T payload, IDictionary<string, string> headers = null) |
| 110 | + { |
| 111 | + HttpResponseMessage response = CreateResponse(statusCode, headers); |
| 112 | + response.Content = new StringContent(JsonSerializer.Serialize(payload, serializerOptions), Encoding.UTF8, "application/json"); |
| 113 | + Responses.Add(response); |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Creates a <see cref="HttpResponseMessage"/> with no payload |
| 118 | + /// </summary> |
| 119 | + /// <param name="statusCode">The status code</param> |
| 120 | + /// <param name="headers">The headers (if any) to add</param> |
| 121 | + /// <returns>The <see cref="HttpResponseMessage"/></returns> |
| 122 | + private static HttpResponseMessage CreateResponse(HttpStatusCode statusCode, IDictionary<string, string> headers = null) |
| 123 | + { |
| 124 | + HttpResponseMessage response = new(statusCode); |
| 125 | + if (headers != null) |
| 126 | + { |
| 127 | + foreach (KeyValuePair<string, string> kv in headers) |
| 128 | + { |
| 129 | + if (!response.Content.Headers.TryAddWithoutValidation(kv.Key, kv.Value)) |
| 130 | + { |
| 131 | + response.Headers.Add(kv.Key, kv.Value); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return response; |
| 137 | + } |
| 138 | + |
| 139 | + protected override void Dispose(bool disposing) |
| 140 | + { |
| 141 | + if (disposing) |
| 142 | + { |
| 143 | + this.requestLock.Dispose(); |
| 144 | + } |
| 145 | + |
| 146 | + base.Dispose(disposing); |
| 147 | + } |
| 148 | +} |
0 commit comments