WithResponseHeaders and WithContentHeaders overloads
See original GitHub issueIs your feature request related to a problem? Please describe.
I have an extension method which allows me to register responses for intercepted requests such that each subsequent request that matches, returns a different response content. I do this by using a closure of a Counter
object, which gets incremented using WithInterceptionCallback
, as follows:
[EditorBrowsable(EditorBrowsableState.Never)]
static class HttpRequestInterceptionBuilderExtensions
{
public static HttpRequestInterceptionBuilder WithJsonResponses<T>(
this HttpRequestInterceptionBuilder builder,
Func<int, T, CancellationToken, Task> callback,
T[] content,
JsonSerializerOptions? options = null)
{
var counter = new Counter(content.Length);
T item = default!;
return builder
.WithInterceptionCallback(async (_, cancellationToken) =>
{
counter.Increment();
item = content[counter.Value - 1];
await callback(counter.Value, item, cancellationToken);
})
.WithMediaType("application/json")
.WithContent(() =>
{
if (item is Exception exception)
throw exception;
return JsonSerializer.SerializeToUtf8Bytes(item, options);
});
}
class Counter
{
readonly int _maxValue;
public Counter(int maxValue) => _maxValue = maxValue;
public int Value { get; private set; }
public void Increment()
{
if (Value == _maxValue) return;
Value++;
}
}
}
I would like to do the same for the response headers and content headers as well. This requires new overloads for WithResponseHeaders
and WithContentHeaders
, and some associated code changes.
Describe the solution you’d like
New overloads for WithResponseHeaders
and WithContentHeaders
, which allow passing a Func<IDictionary<string, ICollection<string>>>
.
Describe alternatives you’ve considered
No alternatives exist.
Additional context
The reason for requiring different responses to the same request is that the API being “mocked” is one that returns data which changes over time. My test suite is set up so that I can verify this behaviour.
Issue Analytics
- State:
- Created a year ago
- Comments:7 (5 by maintainers)
This change is now available from NuGet.org.
Thanks - I’ll come back to finishing this off and prepping a release including the change next week when I’m back in the office.