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

Adds charset=utf8 to a json response Content-Type #1089

Merged
merged 6 commits into from
Jun 21, 2013
Merged
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
4 changes: 2 additions & 2 deletions src/Nancy.Tests.Functional/Tests/JsonpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void Ensure_that_Jsonp_hook_does_not_affect_a_normal_json_response()

Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Equal("true", result.Body.AsString());
Assert.Equal("application/json", result.Context.Response.ContentType);
Assert.Equal("application/json; charset=utf8", result.Context.Response.ContentType);
}

[Fact]
Expand All @@ -71,7 +71,7 @@ public void Ensure_that_Jsonp_hook_should_pad_a_json_response_when_callback_is_p

Assert.Equal(HttpStatusCode.OK, result.StatusCode);
Assert.Equal("myCallback(true);", result.Body.AsString());
Assert.Equal("application/javascript", result.Context.Response.ContentType);
Assert.Equal("application/javascript; charset=utf8", result.Context.Response.ContentType);
}
}
}
4 changes: 2 additions & 2 deletions src/Nancy.Tests/Unit/JsonFormatterExtensionsFixtures.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Nancy.Tests.Unit
{
using System;
using System.IO;
using System.Text;
using FakeItEasy;

using Nancy.Responses;
using Nancy.Tests.Fakes;
using Xunit;
Expand All @@ -25,7 +25,7 @@ public JsonFormatterExtensionsFixtures()
[Fact]
public void Should_return_a_response_with_the_standard_json_content_type()
{
response.ContentType.ShouldEqual("application/json");
response.ContentType.ShouldEqual("application/json; charset=utf8");
}

[Fact]
Expand Down
6 changes: 6 additions & 0 deletions src/Nancy/Json/JsonSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ public static class JsonSettings
/// </summary>
public static int MaxRecursions { get; set; }

/// <summary>
/// Default charset for json responses.
/// </summary>
public static string DefaultCharset { get; set; }

public static IList<JavaScriptConverter> Converters { get; set; }

static JsonSettings()
{
MaxJsonLength = 102400;
MaxRecursions = 100;
DefaultCharset = "utf8";
Converters = new List<JavaScriptConverter>
{
new TimeSpanConverter(),
Expand Down
18 changes: 9 additions & 9 deletions src/Nancy/Jsonp.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nancy.Bootstrapper;
using System.IO;

namespace Nancy
namespace Nancy
{
using System;
using System.IO;
using System.Linq;

using Nancy.Json;
using Nancy.Bootstrapper;

public static class Jsonp
{
static PipelineItem<Action<NancyContext>> JsonpItem;
Expand Down Expand Up @@ -56,7 +56,7 @@ private static void PrepareJsonp(NancyContext context)

// set content type to application/javascript so browsers can handle it by default
// http://stackoverflow.com/questions/111302/best-content-type-to-serve-jsonp
context.Response.ContentType = "application/javascript";
context.Response.ContentType = "application/javascript" + (String.IsNullOrWhiteSpace(JsonSettings.DefaultCharset) ? "" : "; charset=" + JsonSettings.DefaultCharset);
context.Response.Contents = stream =>
{
// disposing of stream is handled elsewhere
Expand Down
14 changes: 11 additions & 3 deletions src/Nancy/Responses/JsonResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

public class JsonResponse<TModel> : Response
{
private static string contentType
{
get
{
return "application/json" + (String.IsNullOrWhiteSpace(JsonSettings.DefaultCharset) ? "" : "; charset=" + JsonSettings.DefaultCharset);
}
}

public JsonResponse(TModel model, ISerializer serializer)
{
if (serializer == null)
Expand All @@ -14,13 +22,13 @@ public JsonResponse(TModel model, ISerializer serializer)
}

this.Contents = GetJsonContents(model, serializer);
this.ContentType = "application/json";
this.ContentType = contentType;
this.StatusCode = HttpStatusCode.OK;
}

private static Action<Stream> GetJsonContents(TModel model, ISerializer serializer)
{
return stream => serializer.Serialize("application/json", model, stream);
return stream => serializer.Serialize(contentType, model, stream);
}
}

Expand Down