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

Commit ec9edda

Browse files
committed
Merge pull request #1723 from curit/overwrite-content-type
Overwrite Content-Type from headers
2 parents 9fb732c + 23e0e1b commit ec9edda

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

Diff for: src/Nancy.Tests/Unit/ResponseFixture.cs

+14
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ public void Should_set_content_type_to_text_html_when_implicitly_cast_from_strin
154154
response.ContentType.ShouldEqual("text/html");
155155
}
156156

157+
[Fact]
158+
public void Should_overwrite_content_type_from_headers()
159+
{
160+
// Given
161+
const string value = "test value";
162+
Response response = value;
163+
164+
// When
165+
response.Headers.Add("contenT-typE", "application/json");
166+
167+
// Then
168+
response.ContentType.ShouldEqual("application/json");
169+
}
170+
157171
[Fact]
158172
public void Should_set_a_cookie_with_name_and_value()
159173
{

Diff for: src/Nancy/Response.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace Nancy
33
using System;
44
using System.Collections.Generic;
55
using System.IO;
6+
using System.Linq;
67
using System.Threading.Tasks;
78

89
using Cookies;
@@ -19,14 +20,16 @@ public class Response: IDisposable
1920
/// </summary>
2021
public static Action<Stream> NoBody = s => { };
2122

23+
private string contentType;
24+
2225
/// <summary>
2326
/// Initializes a new instance of the <see cref="Response"/> class.
2427
/// </summary>
2528
public Response()
2629
{
2730
this.Contents = NoBody;
2831
this.ContentType = "text/html";
29-
this.Headers = new Dictionary<string, string>();
32+
this.Headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
3033
this.StatusCode = HttpStatusCode.OK;
3134
this.Cookies = new List<INancyCookie>(2);
3235
}
@@ -36,7 +39,11 @@ public Response()
3639
/// </summary>
3740
/// <value>The type of the content.</value>
3841
/// <remarks>The default value is <c>text/html</c>.</remarks>
39-
public string ContentType { get; set; }
42+
public string ContentType
43+
{
44+
get { return Headers.ContainsKey("content-type") ? Headers["content-type"] : this.contentType; }
45+
set { this.contentType = value; }
46+
}
4047

4148
/// <summary>
4249
/// Gets the delegate that will render contents to the response stream.

0 commit comments

Comments
 (0)