• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentRest / 11929962690

20 Nov 2024 08:57AM UTC coverage: 58.107%. Remained the same
11929962690

Pull #206

github

web-flow
Merge 7d96b20ca into dd4bfca3f
Pull Request #206: Bump Microsoft.NET.Test.Sdk from 17.11.1 to 17.12.0

264 of 594 branches covered (44.44%)

Branch coverage included in aggregate %.

829 of 1287 relevant lines covered (64.41%)

48.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.23
/src/FluentRest.Fake/MemoryMessageStore.cs
1
using System;
2
using System.Net;
3
using System.Net.Http;
4
using System.Threading.Tasks;
5

6
namespace FluentRest.Fake;
7

8
/// <summary>
9
/// A memory based fake message store.  The fake response messages are saved and loaded from a ResponseStore using a sh1 has of the URL as the key.
10
/// </summary>
11
public class MemoryMessageStore : FakeMessageStore
12
{
13
    private static readonly Lazy<MemoryMessageStore> _current = new Lazy<MemoryMessageStore>(() => new MemoryMessageStore());
4✔
14

15
    /// <summary>
16
    /// Gets the current singleton instance of <see cref="MemoryMessageStore"/>.
17
    /// </summary>
18
    /// <value>The current singleton instance <see cref="MemoryMessageStore"/>.</value>
19
    public static MemoryMessageStore Current => _current.Value;
22✔
20

21
    private readonly System.Collections.Concurrent.ConcurrentDictionary<string, FakeResponseContainer> _responseStore = new System.Collections.Concurrent.ConcurrentDictionary<string, FakeResponseContainer>();
2✔
22

23
    /// <summary>
24
    /// Gets the response store.
25
    /// </summary>
26
    /// <value>
27
    /// The response store.
28
    /// </value>
29
    public System.Collections.Generic.IDictionary<string, FakeResponseContainer> ResponseStore => _responseStore;
×
30

31
    /// <summary>
32
    /// The callback used to serialize fake response data objects into a byte array.
33
    /// If not specified, the default settings for the System.Text.Json.JsonSerializer will be used.
34
    ///</summary>
35
    public SerializeResponseContentCallback SerializeResponseContentCallback { get; set; }
12✔
36

37
    /// <summary>
38
    /// Saves the specified HTTP <paramref name="response" /> to the message store as an asynchronous operation.
39
    /// </summary>
40
    /// <param name="request">The HTTP request message that was sent.</param>
41
    /// <param name="response">The HTTP response messsage to save.</param>
42
    /// <returns>
43
    /// The task object representing the asynchronous operation.
44
    /// </returns>
45
    public override async Task SaveAsync(HttpRequestMessage request, HttpResponseMessage response)
46
    {
47
        // don't save content if not success
48
        if (!response.IsSuccessStatusCode || response.Content == null || response.StatusCode == HttpStatusCode.NoContent)
×
49
            return;
×
50

51
        var httpContent = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
×
52
        var fakeResponse = Convert(response);
×
53

54
        var key = GenerateKey(request);
×
55
        var container = new FakeResponseContainer
×
56
        {
×
57
            HttpContent = httpContent,
×
58
            ResponseMessage = fakeResponse
×
59
        };
×
60

61

62
        // save to store
63
        _responseStore.AddOrUpdate(key, container, (k, o) => container);
×
64
    }
×
65

66
    /// <summary>
67
    /// Loads an HTTP fake response message for the specified HTTP <paramref name="request" /> as an asynchronous operation.
68
    /// </summary>
69
    /// <param name="request">The HTTP request message to load response for.</param>
70
    /// <returns>
71
    /// The task object representing the asynchronous operation.
72
    /// </returns>
73
    public override Task<HttpResponseMessage> LoadAsync(HttpRequestMessage request)
74
    {
75
        var taskSource = new TaskCompletionSource<HttpResponseMessage>();
10✔
76
        var key = GenerateKey(request);
10✔
77

78
        FakeResponseContainer container;
79

80
        var found = _responseStore.TryGetValue(key, out container);
10✔
81
        if (!found)
10!
82
        {
83
            var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.NotFound);
×
84
            httpResponseMessage.RequestMessage = request;
×
85
            httpResponseMessage.ReasonPhrase = $"Response for key '{key}' not found";
×
86

87
            taskSource.SetResult(httpResponseMessage);
×
88
            return taskSource.Task;
×
89
        }
90

91
        var fakeResponse = container.ResponseMessage;
10✔
92
        var httpResponse = Convert(fakeResponse);
10✔
93

94
        taskSource.SetResult(httpResponse);
10✔
95

96
        if (container.HttpContent == null)
10!
97
            return taskSource.Task;
×
98

99
        var httpContent = new ByteArrayContent(container.HttpContent);
10✔
100

101
        // copy headers
102
        foreach (var header in fakeResponse.ResponseHeaders)
40✔
103
            httpContent.Headers.TryAddWithoutValidation(header.Key, header.Value);
10✔
104

105
        httpResponse.Content = httpContent;
10✔
106
        httpResponse.RequestMessage = request;
10✔
107

108
        return taskSource.Task;
10✔
109
    }
110

111

112
    /// <summary>
113
    /// Register a fake response using the specified fluent <paramref name="builder"/> action.
114
    /// </summary>
115
    /// <param name="builder">The fluent container builder.</param>
116
    /// <exception cref="ArgumentNullException"></exception>
117
    public void Register(Action<FakeContainerBuilder> builder)
118
    {
119
        if (builder == null)
10!
120
            throw new ArgumentNullException(nameof(builder));
×
121

122
        var container = new FakeResponseContainer
10✔
123
        {
10✔
124
            SerializeResponseContentCallback = SerializeResponseContentCallback
10✔
125
        };
10✔
126
        var containerBuilder = new FakeContainerBuilder(container);
10✔
127

128
        builder(containerBuilder);
10✔
129

130
        // save to store
131
        var key = container.RequestUri.ToString();
10✔
132

133
        _responseStore.AddOrUpdate(key, container, (k, o) => container);
16✔
134
    }
10✔
135
}
136

137
/// <summary>
138
/// Callback used to serialize fake response content data objects into a byte array.
139
///</summary>
140
public delegate byte[] SerializeResponseContentCallback(object content, Type contentType);
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc