Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WithResponseHeaders and WithContentHeaders overloads

See original GitHub issue

Is 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:

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) =>
                item = content[counter.Value - 1];
                await callback(counter.Value, item, cancellationToken);
            .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;


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:closed
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

martincostellocommented, Nov 15, 2022

This change is now available from

martincostellocommented, Sep 22, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript http.get Error: No overload matches this call
I am trying to get a text file from the server so I have done this: const httpOptions = { headers: new HttpHeaders({...
Read more >
Back to Basics: Custom HTTP Response Header Manipulation ...
This post is specifically about the response data, but headers are both ... overloads that let you set and retrieve single string values....
Read more >
HttpHeaders.Add Method (System.Net.Http.Headers)
Inserts a new header and its values into the HttpHeaders collection. ... response headers with HttpResponseMessage, and content headers with HttpContent ...
Read more >
Support for multiple request/response models based on ...
Method overloading which is not breaking JSON and YAML semantics; treats operations that act differently based on Content-Type as different ...
Read more >
HttpHeaders.TryAddWithoutValidation Method (System.Net ...
Returns a value that indicates whether a new header and its values were added to the HttpHeaders collection without validating the provided information....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found