Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Add overloaded WithHeaders extension supporting IEnumerables #2971

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Nancy/ResponseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,30 @@ public static Response WithHeaders(this Response response, params Tuple<string,
return response;
}

/// <summary>
/// Adds headers to the response using anonymous types
/// </summary>
/// Enumerable of headers - each header should be a Tuple with two string elements
/// for header name and header value
/// </param>
/// <returns>Modified response</returns>
public static Response WithHeaders(this Response response, IEnumerable<Tuple<string, string>> headers)
{
return response.WithHeaders(headers.ToArray());
}

/// <summary>
/// Adds headers to the response using anonymous types
/// </summary>
/// Enumerable of headers - each header should be a Tuple with two string elements
/// for header name and header value
/// </param>
/// <returns>Modified response</returns>
public static Response WithHeaders(this Response response, IEnumerable<object> headers)
{
return response.WithHeaders(headers.ToArray());
}

/// <summary>
/// Sets the content type of the response
/// </summary>
Expand Down
36 changes: 35 additions & 1 deletion test/Nancy.Tests/Unit/ResponseExtensionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,40 @@ public void Should_be_able_to_supply_withHeaders_headers_as_anonymous_types()
result.Headers["test2"].ShouldEqual("test2value");
}

[Fact]
public void Should_be_able_to_add_headers_as_enumerable()
{
var headers = new List<Tuple<string, string>>
{
new Tuple<string, string>("test", "testvalue"),
new Tuple<string, string>("test2", "test2value")
};

var response = new Response();
var result = response.WithHeaders(headers);

result.Headers.ShouldNotBeNull();
result.Headers["test"].ShouldEqual("testvalue");
result.Headers["test2"].ShouldEqual("test2value");
}

[Fact]
public void Should_be_able_to_add_headers_as_enumerable_of_anonymous()
{
var headers = new List<object>
{
new { Header = "test", Value = "testvalue" },
new { Header = "test2", Value = "test2value" }
};

var response = new Response();
var result = response.WithHeaders(headers);

result.Headers.ShouldNotBeNull();
result.Headers["test"].ShouldEqual("testvalue");
result.Headers["test2"].ShouldEqual("test2value");
}

[Fact]
public void Should_be_able_to_chain_setting_single_headers()
{
Expand Down Expand Up @@ -193,4 +227,4 @@ private string GetFilePath()
#endif
}
}
}
}