Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added a jsonp aware IJsonWriter #248

Merged
merged 2 commits into from

2 participants

@KevM

I wanted to start a conversation on supporting JSONP.

I added an implementation of an IJsonWriter that supports pulling the padding value fro IRequestData. Not sure if this is the correct way to do this. Also not sure how this fits in with the work slated to do on Conneg and the IMediaWriters.

Our interest here is that we are about to ship some web services to customers and we want to enable them in as may ways as possible to consume those services.

@jeremydmiller

@KevM : I'm going to hold off on this pull request for this round. I don't have any comments right now, and that's more or less the problem;) Let me look at this, Corey's view engine changes, and think through the conneg/view merging first.

@KevM

You could pull it in. It does not get used by default. If you don't I will copy it around to my projects. Same same.

@jeremydmiller jeremydmiller merged commit 98f9bc5 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
42 src/FubuMVC.Core/Runtime/IJsonWriter.cs
@@ -1,4 +1,4 @@
-using System;
+using FubuCore;
using FubuCore.Binding;
using HtmlTags;
@@ -65,4 +65,44 @@ public void Write(object output, string mimeType)
Write(output);
}
}
+
+ public class JsonpAwareWriter : IJsonWriter
+ {
+ private readonly IOutputWriter _outputWriter;
+ private readonly IRequestData _requestData;
+
+ public const string JsonPHttpRequest = "jsonp";
+
+ public JsonpAwareWriter(IOutputWriter outputWriter, IRequestData requestData)
+ {
+ _outputWriter = outputWriter;
+ _requestData = requestData;
+ }
+
+ public void Write(object output)
+ {
+ Write(output, MimeType.Json.ToString());
+ }
+
+ public void Write(object output, string mimeType)
+ {
+ var json = JsonUtil.ToJson(output);
+ var padding = GetJsonPadding(_requestData);
+
+ if (padding != null)
+ {
+ json = "{0}({1});".ToFormat(padding, json);
+ }
+
+ _outputWriter.Write(mimeType, json);
+ }
+
+ public static string GetJsonPadding(IRequestData requestInput)
+ {
+ string result = null;
+ requestInput.Value(JsonPHttpRequest, value => result = value.ToString());
+ return result;
+ }
+ }
+
}
View
1  src/FubuMVC.Tests/Behaviors/RenderJsonBehaviorTester.cs
@@ -1,5 +1,6 @@
using System;
using System.Net.Mime;
+using FubuCore;
using FubuCore.Binding;
using FubuMVC.Core.Behaviors;
using FubuMVC.Core.Runtime;
View
61 src/FubuMVC.Tests/Behaviors/RenderJsonpBehaviorTester.cs
@@ -0,0 +1,61 @@
+using FubuCore.Binding;
+using FubuMVC.Core.Runtime;
+using FubuTestingSupport;
+using HtmlTags;
+using NUnit.Framework;
+using InMemoryRequestData = FubuMVC.Core.Runtime.InMemoryRequestData;
+
+namespace FubuMVC.Tests.Behaviors
+{
+ [TestFixture]
+ public class when_rendering_json_from_a_model_object : InteractionContext<JsonpAwareWriter>
+ {
+ private JsonOutput output;
+ private InMemoryRequestData requestData;
+ private InMemoryOutputWriter theOutputWriter;
+
+ protected override void beforeEach()
+ {
+ output = new JsonOutput
+ {
+ Name = "Max",
+ Age = 6
+ };
+
+ requestData = new InMemoryRequestData();
+ Services.Inject<IRequestData>(requestData);
+
+ theOutputWriter = new InMemoryOutputWriter();
+ Services.Inject<IOutputWriter>(theOutputWriter);
+ }
+
+ [Test]
+ public void should_write_json_mimetype()
+ {
+ ClassUnderTest.Write(output);
+
+ theOutputWriter.ContentType.ShouldEqual(MimeType.Json.ToString());
+ }
+
+ [Test]
+ public void should_write_only_json_output_when_jsonp_request_parameter_is_not_present()
+ {
+ ClassUnderTest.Write(output);
+
+ var json = JsonUtil.ToJson(output);
+ theOutputWriter.ToString().TrimEnd().ShouldEqual(json);
+ }
+
+ [Test]
+ public void should_write_json_padded_output_when_jsonp_request_parameter_is_present()
+ {
+ const string padding = "parseResult";
+ requestData[JsonpAwareWriter.JsonPHttpRequest] = padding;
+
+ ClassUnderTest.Write(output);
+
+ var expectedResult = padding + "(" + JsonUtil.ToJson(output) + ");";
+ theOutputWriter.ToString().TrimEnd().ShouldEqual(expectedResult);
+ }
+ }
+}
View
1  src/FubuMVC.Tests/FubuMVC.Tests.csproj
@@ -178,6 +178,7 @@
<Compile Include="Behaviors\Conditional\when_condition_is_true.cs" />
<Compile Include="Behaviors\InterceptExceptionBehaviorTester.cs" />
<Compile Include="Behaviors\ITargetController.cs" />
+ <Compile Include="Behaviors\RenderJsonpBehaviorTester.cs" />
<Compile Include="Behaviors\RenderStatusCodeBehaviorTester.cs" />
<Compile Include="Bugs\DoubleActionRegistrationWhenDiagnosticsIsOnTester.cs" />
<Compile Include="Caching\OuputCachingNodeTester.cs" />
Something went wrong with that request. Please try again.