How can I create a stub that responds only for headers with multiple values? #458

Closed
opened 2025-12-29 15:24:18 +01:00 by adam · 1 comment
Owner

Originally created by @basdijkstra on GitHub (Oct 10, 2022).

So, I've got a request that contains a header with multiple values (in this case, 2), and I want to write a stub that responds only when that header has both these values:
unnamed

I've tried

private void CreateStubForMultipleHeaderValues()
        {
            this.server.Given(Request.Create().WithPath("/multiple-header-values").UsingGet()
                .WithHeader("my_header", new string[] { "my_header_value_1", "my_header_value_2" }))
                .RespondWith(Response.Create()
                .WithStatusCode(200));
        }

as well as

private void CreateStubForMultipleHeaderValues()
        {
            this.server.Given(Request.Create().WithPath("/multiple-header-values").UsingGet()
                .WithHeader("my_header", "my_header_value_1, my_header_value_2"))
                .RespondWith(Response.Create()
                .WithStatusCode(200));
        }

and also

private void CreateStubForMultipleHeaderValues()
        {
            this.server.Given(Request.Create().WithPath("/multiple-header-values").UsingGet()
                .WithHeader("my_header", "my_header_value_1")
                .WithHeader("my_header", "my_header_value_2"))
                .RespondWith(Response.Create()
                .WithStatusCode(200));
        }

but none of them work. Could someone please show me how to do this properly?

Originally created by @basdijkstra on GitHub (Oct 10, 2022). So, I've got a request that contains a header with multiple values (in this case, 2), and I want to write a stub that responds only when that header has both these values: ![unnamed](https://user-images.githubusercontent.com/10740451/194833716-d4cabc89-a44f-4254-99c8-869f8ae6a1d9.png) I've tried ```csharp private void CreateStubForMultipleHeaderValues() { this.server.Given(Request.Create().WithPath("/multiple-header-values").UsingGet() .WithHeader("my_header", new string[] { "my_header_value_1", "my_header_value_2" })) .RespondWith(Response.Create() .WithStatusCode(200)); } ``` as well as ```csharp private void CreateStubForMultipleHeaderValues() { this.server.Given(Request.Create().WithPath("/multiple-header-values").UsingGet() .WithHeader("my_header", "my_header_value_1, my_header_value_2")) .RespondWith(Response.Create() .WithStatusCode(200)); } ``` and also ```csharp private void CreateStubForMultipleHeaderValues() { this.server.Given(Request.Create().WithPath("/multiple-header-values").UsingGet() .WithHeader("my_header", "my_header_value_1") .WithHeader("my_header", "my_header_value_2")) .RespondWith(Response.Create() .WithStatusCode(200)); } ``` but none of them work. Could someone please show me how to do this properly?
adam added the question label 2025-12-29 15:24:18 +01:00
adam closed this issue 2025-12-29 15:24:18 +01:00
Author
Owner

@basdijkstra commented on GitHub (Oct 10, 2022):

Nevermind, I figured it out. Mistake on my end: I didn't call the code that added the mock response to the stub server.

For those who are interested, once I did that, this response definition worked:

private void CreateStubForMultipleHeaderValues()
        {
            this.server.Given(Request.Create().WithPath("/multiple-header-values").UsingGet()
                .WithHeader("my_header", "my_header_value_1, my_header_value_2"))
                .RespondWith(Response.Create()
                .WithStatusCode(200));
        }
@basdijkstra commented on GitHub (Oct 10, 2022): Nevermind, I figured it out. Mistake on my end: I didn't call the code that added the mock response to the stub server. For those who are interested, once I did that, this response definition worked: ```csharp private void CreateStubForMultipleHeaderValues() { this.server.Given(Request.Create().WithPath("/multiple-header-values").UsingGet() .WithHeader("my_header", "my_header_value_1, my_header_value_2")) .RespondWith(Response.Create() .WithStatusCode(200)); } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#458