From 16315c85d32e327605014573c9bf68d75b9e65e5 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Fri, 7 Jul 2017 20:27:03 -0700 Subject: [PATCH] Add UI.PublishJson to enable APIs Fixes #7 --- Ooui/UI.cs | 55 ++++++++++++++++++++++++++++++++++++++++++------ Tests/UITests.cs | 32 +++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 8 deletions(-) diff --git a/Ooui/UI.cs b/Ooui/UI.cs index e93ea7b..b461203 100644 --- a/Ooui/UI.cs +++ b/Ooui/UI.cs @@ -102,13 +102,24 @@ namespace Ooui PublishFile (path, filePath); } - public static void PublishFile (string path, string filePath, string contentType = null, string contentEncoding = null) + public static void PublishFile (string path, string filePath, string contentType = null) { var data = System.IO.File.ReadAllBytes (filePath); if (contentType == null) { contentType = GuessContentType (path, filePath); } - Publish (path, new DataHandler (data, contentType, contentEncoding)); + Publish (path, new DataHandler (data, contentType)); + } + + public static void PublishJson (string path, Func ctor) + { + Publish (path, new JsonHandler (ctor)); + } + + public static void PublishJson (string path, object value) + { + var data = JsonHandler.GetData (value); + Publish (path, new DataHandler (data, JsonHandler.ContentType)); } static string GuessContentType (string path, string filePath) @@ -284,13 +295,11 @@ namespace Ooui { readonly byte[] data; readonly string contentType; - readonly string contentEncoding; - public DataHandler (byte[] data, string contentType = null, string contentEncoding = null) + public DataHandler (byte[] data, string contentType = null) { this.data = data; this.contentType = contentType; - this.contentEncoding = contentEncoding; } public override void Respond (HttpListenerContext listenerContext, CancellationToken token) @@ -302,8 +311,40 @@ namespace Ooui response.StatusCode = 200; if (!string.IsNullOrEmpty (contentType)) response.ContentType = contentType; - if (!string.IsNullOrEmpty (contentEncoding)) - response.ContentType = contentEncoding; + response.ContentLength64 = data.LongLength; + + using (var s = response.OutputStream) { + s.Write (data, 0, data.Length); + } + response.Close (); + } + } + + class JsonHandler : RequestHandler + { + public const string ContentType = "application/json; charset=utf-8"; + + readonly Func ctor; + + public JsonHandler (Func ctor) + { + this.ctor = ctor; + } + + public static byte[] GetData (object obj) + { + var r = Newtonsoft.Json.JsonConvert.SerializeObject (obj); + return System.Text.Encoding.UTF8.GetBytes (r); + } + + public override void Respond (HttpListenerContext listenerContext, CancellationToken token) + { + var response = listenerContext.Response; + + var data = GetData (ctor ()); + + response.StatusCode = 200; + response.ContentType = ContentType; response.ContentLength64 = data.LongLength; using (var s = response.OutputStream) { diff --git a/Tests/UITests.cs b/Tests/UITests.cs index 11fffb5..0e14601 100644 --- a/Tests/UITests.cs +++ b/Tests/UITests.cs @@ -90,7 +90,7 @@ namespace Tests { var f = System.IO.Path.GetTempFileName (); System.IO.File.WriteAllText (f, "Test Ooui Text File", System.Text.Encoding.UTF8); - UI.PublishFile ("/text-file", f, "text/plain", "utf-8"); + UI.PublishFile ("/text-file", f, "text/plain; charset=utf-8"); UI.WaitUntilStarted (); var c = new System.Net.WebClient (); var r = c.DownloadString (UI.GetUrl ("/text-file")); @@ -108,5 +108,35 @@ namespace Tests var r = c.DownloadString (UI.GetUrl ("/" + System.IO.Path.GetFileName (f))); Assert.AreEqual ("Test Ooui Text File 2", r); } + + [TestMethod] + public void PublishJsonObject () + { + UI.PublishJson ("/json", new JsonTestObject ()); + UI.WaitUntilStarted (); + var c = new System.Net.WebClient (); + var r = c.DownloadString (UI.GetUrl ("/json")); + Assert.AreEqual ("{\"Name\":\"X\",\"Value\":null}", r); + } + + + [TestMethod] + public void PublishJsonCtor () + { + var i = 1; + UI.PublishJson ("/jsond", () => new JsonTestObject { Value = i++ }); + UI.WaitUntilStarted (); + var c = new System.Net.WebClient (); + var r1 = c.DownloadString (UI.GetUrl ("/jsond")); + var r2 = c.DownloadString (UI.GetUrl ("/jsond")); + Assert.AreEqual ("{\"Name\":\"X\",\"Value\":1}", r1); + Assert.AreEqual ("{\"Name\":\"X\",\"Value\":2}", r2); + } + + class JsonTestObject + { + public string Name = "X"; + public object Value; + } } }