Permalink
Browse files

removed applicationtype from content type and added http header entry…

… that's independent of contenttype
  • Loading branch information...
1 parent d1c0884 commit 76b132f4f1ef33eb72fb172503327022abe7502c @amirrajan committed Nov 7, 2011
@@ -31,7 +31,7 @@ public UrlHelper UrlHelper
public IHyperMedia HyperMedia { get; set; }
- public dynamic ViewBag { get; set; }
+ public dynamic Metadata { get; set; }
/// <summary>
/// If you used forHyperMedia in any of your IHyperMedia implementations. Use this dictionary to stage dependencies.
@@ -50,7 +50,7 @@ public void TestInitialize()
{
var hyperMedia = Registry[name];
hyperMedia.UrlHelper = UrlHelper;
- hyperMedia.ViewBag = ViewBag;
+ hyperMedia.Metadata = Metadata;
return hyperMedia.ToHyperMedia(entity, returnHyperMedia);
};
@@ -19,7 +19,7 @@ public dynamic ToHyperMedia(dynamic data, Func<string, dynamic, dynamic> hyperMe
public UrlHelper UrlHelper { get; set; }
- public dynamic ViewBag { get; set; }
+ public dynamic Metadata { get; set; }
public string ApplicationType
{
@@ -25,7 +25,7 @@ public dynamic ToHyperMedia(dynamic data, Func<string, dynamic, dynamic> hyperMe
public UrlHelper UrlHelper { get; set; }
- public dynamic ViewBag { get; set; }
+ public dynamic Metadata { get; set; }
public string ApplicationType
{
@@ -24,7 +24,7 @@ public dynamic ToHyperMedia(dynamic data, Func<string, dynamic, dynamic> hyperMe
public UrlHelper UrlHelper { get; set; }
- public dynamic ViewBag { get; set; }
+ public dynamic Metadata { get; set; }
public string ApplicationType
{
@@ -23,7 +23,7 @@ static void Main(string[] args)
(JsonString.Parse(root) as string).Write();
- var createUser = (root as object).Link("CreateUser");
+ var createUser = (root.Links as object).Link("CreateUser");
"this is the rel that was given for CreateUser:".Write();
(JsonString.Parse(createUser) as string).Write();
@@ -25,7 +25,7 @@ public virtual dynamic ToHyperMedia(dynamic data, Func<string, dynamic, dynamic>
public UrlHelper UrlHelper { get; set; }
- public dynamic ViewBag { get; set; }
+ public dynamic Metadata { get; set; }
public string ApplicationType
{
@@ -28,7 +28,7 @@ public dynamic ToHyperMedia(dynamic data, Func<string, dynamic, dynamic> hyperMe
public UrlHelper UrlHelper { get; set; }
- public dynamic ViewBag { get; set; }
+ public dynamic Metadata { get; set; }
public string ApplicationType
{
@@ -11,15 +11,18 @@ public class New : IHyperMedia
{
public dynamic ToHyperMedia(dynamic data, Func<string, dynamic, dynamic> hyperMedia)
{
- return new Links(UrlHelper)
+ return new
{
- { "CreateUser", new { controller = "Users", action = "Create" } }
+ Links = new Links(UrlHelper)
+ {
+ { "CreateUser", new { controller = "Users", action = "Create" } }
+ }
};
}
public UrlHelper UrlHelper { get; set; }
- public dynamic ViewBag { get; set; }
+ public dynamic Metadata { get; set; }
public string ApplicationType
{
@@ -23,7 +23,7 @@ public dynamic ToHyperMedia(dynamic data, Func<string, dynamic, dynamic> hyperMe
public UrlHelper UrlHelper { get; set; }
- public dynamic ViewBag { get; set; }
+ public dynamic Metadata { get; set; }
public string ApplicationType
{
@@ -13,7 +13,7 @@ public void debug()
{
//the specification class you want to test
//this can be a regular expression
- var testClassYouWantToDebug = "describe_SomeTest";
+ var testClassYouWantToDebug = "describe_RestFacilitator";
//initialize NSpec's specfinder
var finder = new SpecFinder(
@@ -16,7 +16,7 @@ public dynamic ToHyperMedia(dynamic data, Func<string, dynamic, dynamic> hyperMe
public UrlHelper UrlHelper { get; set; }
- public dynamic ViewBag { get; set; }
+ public dynamic Metadata { get; set; }
public string ApplicationType
{
@@ -30,5 +30,21 @@ void requesting_haikus_from_twitter()
it["gives application type"] = () => (json.ApplicationType as string).should_be("application/json");
}
+
+ void requesting_root_from_sample_blog_service()
+ {
+ context["HyperMediaControl.Service is running on http://localhost:3000/"] = () =>
+ {
+ before = () => facilitator = new RestFacilitator("http://localhost:3000", "nonexistantuser@example.com");
+
+ act = () =>
+ {
+ json = facilitator.Get("/");
+ };
+
+ it["gives application type based on Application-Type http header"] = () =>
+ (json.ApplicationType as string).should_be("application/user.new");
+ };
+ }
}
}
@@ -25,7 +25,8 @@ class describe_HyperMediaResult : nspec
Mock<HttpResponseBase> response;
Mock<HttpRequestBase> request;
Mock<RequestContext> requestContext;
- NameValueCollection headers;
+ NameValueCollection requestHeaders;
+ NameValueCollection responseHeaders;
Mock<TextWriter> output;
string contentType;
string winningFormatter;
@@ -38,7 +39,9 @@ void before_each()
ApplicationFormatterRegistry.Instance.FindAllIn<Index>();
- headers = new NameValueCollection();
+ requestHeaders = new NameValueCollection();
+
+ responseHeaders = new NameValueCollection();
resourceName = "Root";
@@ -54,9 +57,11 @@ void before_each()
response.Setup(s => s.Output).Returns(output.Object);
+ response.Setup(s => s.Headers).Returns(responseHeaders);
+
request = new Mock<HttpRequestBase>();
- request.Setup(s => s.Headers).Returns(headers);
+ request.Setup(s => s.Headers).Returns(requestHeaders);
httpContext = new Mock<HttpContextBase>();
@@ -82,30 +87,34 @@ void when_executing_hypermedia_result()
context["given a null accept header"] = () =>
{
- before = () => headers.Add("Accept", null);
+ before = () => requestHeaders.Add("Accept", null);
it["the response's winning formatter is application/json"] = () => winningFormatter.should_be("application/json");
};
context["given a application/xml accept header which exists in the application formatters folder for this project"] = () =>
{
- before = () => headers.Add("Accept", "application/foobar");
+ before = () => requestHeaders.Add("Accept", "application/foobar");
it["the response's winning formatter is application/json"] = () => winningFormatter.should_be("application/foobar");
};
context["given application/vcard accept header which doesn't exist in the application formatters folder for this project"] = () =>
{
- before = () => headers.Add("Accept", "application/vcard");
+ before = () => requestHeaders.Add("Accept", "application/vcard");
it["the response's winning formatter is application/json"] = () => winningFormatter.should_be("application/json");
};
context["given root hypermedia as content type set to application/root"] = () =>
{
- before = () => headers.Add("Accept", "application/json");
+ before = () => requestHeaders.Add("Accept", "application/json");
+
+ it["the content type matches the content type of the accept headers"] = () =>
+ contentType.should_be("application/json");
- it["the content type matches the content type of the hypermedia"] = () => contentType.should_be("application/root");
+ it["an http header called Application-Type matches the application type of the hypermedia"] = () =>
+ responseHeaders["Application-Type"].should_be("application/root");
};
}
}
@@ -19,7 +19,7 @@ public static dynamic Parse(string jsonString, string applicationType)
var result = new JsonToDynamic(JsonConvert.DeserializeObject<JObject>(jsonString));
result.Add("ApplicationType", applicationType);
-
+
return result;
}
catch
@@ -4,6 +4,7 @@
using System.Net;
using System.IO;
using Newtonsoft.Json;
+using System.Collections.Specialized;
namespace RestfulClient
{
@@ -42,21 +43,33 @@ public dynamic Get(string url)
public dynamic Get(string url, object queryStringData)
{
+ var response = GetRaw(url, queryStringData);
+
+ var stream = response.GetResponseStream();
+
+ return Parse(stream, response.ContentType, response.Headers);
+ }
+
+ public WebResponse GetRaw(string url)
+ {
+ return GetRaw(url, null);
+ }
+
+ public WebResponse GetRaw(string url, object queryStringData)
+ {
string uriPath = GenerateUrl(url, queryStringData);
var uri = new Uri(uriPath, UriKind.Absolute);
var request = CreateRequest(uri, Authorization);
- var response = request.GetResponse();
-
- var stream = response.GetResponseStream();
-
- return Parse(stream, response.ContentType);
+ return request.GetResponse();
}
- string ScrubbedContentType(string contentType)
+ string ScrubbedContentType(string contentType, WebHeaderCollection headers)
{
+ if (headers["Application-Type"] != null) return headers["Application-Type"];
+
var scrubbedContentType = contentType ?? "";
if (scrubbedContentType.Contains(";"))
@@ -67,9 +80,9 @@ string ScrubbedContentType(string contentType)
return scrubbedContentType;
}
- dynamic Parse(Stream stream, string contentType)
+ dynamic Parse(Stream stream, string contentType, WebHeaderCollection headers)
{
- return JsonToDynamic.Parse(new StreamReader(stream).ReadToEnd(), ScrubbedContentType(contentType));
+ return JsonToDynamic.Parse(new StreamReader(stream).ReadToEnd(), ScrubbedContentType(contentType, headers));
}
private static HttpWebRequest CreateRequest(Uri uri, string authorization)
@@ -140,7 +153,7 @@ private dynamic ExecuteRequest(string href, string method, object payload)
var stream = response.GetResponseStream();
- return Parse(stream, response.ContentType);
+ return Parse(stream, response.ContentType, response.Headers);
}
private string GenerateUrl(string url)
@@ -9,6 +9,11 @@ namespace RestfulMvc
{
public class HyperMediaController : Controller
{
+ public dynamic Metadata
+ {
+ get { return ViewBag; }
+ }
+
public ActionResult HyperMedia(string name)
{
return new HyperMediaResult(ResourceName(), name, Url, ViewBag);
@@ -36,7 +36,8 @@ public override void ExecuteResult(ControllerContext context)
throw new InvalidOperationException(string.Format("Cannot find a class that implements IHyperMedia with namespace and name {0}.{1}. Make sure you have the following line in Global.asax.cs's Application_Start() method: HyperMediaRegistry.Instance.FindAllIn<MvcApplication>();", HyperMediaRegistry.Instance.NamespaceFilter, HyperMediaName()));
}
- context.HttpContext.Response.ContentType = hyperMedia.ApplicationType;
+ context.HttpContext.Response.ContentType = WinningFormatter(context);
+ context.HttpContext.Response.Headers.Add("Application-Type", hyperMedia.ApplicationType);
context.HttpContext.Response.Output.Write(Format(context, hyperMedia));
context.HttpContext.Response.Output.Flush();
}
@@ -45,7 +46,7 @@ string Format(ControllerContext context, IHyperMedia hyperMedia)
{
hyperMedia.UrlHelper = urlHelper;
- hyperMedia.ViewBag = viewBag;
+ hyperMedia.Metadata = viewBag;
return Serialize(context, hyperMedia);
}
@@ -64,7 +65,7 @@ private string Serialize(ControllerContext context, IHyperMedia hyperMedia)
var relativeHyperMedia = HyperMediaRegistry.Instance.FindFirst(fullName);
relativeHyperMedia.UrlHelper = urlHelper;
- relativeHyperMedia.ViewBag = viewBag;
+ relativeHyperMedia.Metadata = viewBag;
if (relativeHyperMedia == null)
{
@@ -5,9 +5,27 @@ namespace RestfulMvc
{
public interface IHyperMedia
{
+ /// <summary>
+ /// Return an object that will get serialized using ApplicationFormatters.
+ /// </summary>
+ /// <param name="data">The "Model"</param>
+ /// <param name="hyperMedia">Hooks to "Render Partial"</param>
+ /// <returns></returns>
dynamic ToHyperMedia(dynamic data, Func<string, dynamic, dynamic> hyperMedia);
+
+ /// <summary>
+ /// ASP.NET MVC UrlHelper.
+ /// </summary>
UrlHelper UrlHelper { get; set; }
- dynamic ViewBag { get; set; }
+
+ /// <summary>
+ /// Think of this as your ViewBag.
+ /// </summary>
+ dynamic Metadata { get; set; }
+
+ /// <summary>
+ /// The request header with key Application-Type will be populated with the value you provide here.
+ /// </summary>
string ApplicationType { get; }
}
}

0 comments on commit 76b132f

Please sign in to comment.