Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added dynamic JSON resource

  • Loading branch information...
commit 7beb58523ad27465546385bf27bbc95258069e9b 1 parent 6b9f8cd
@MrBretticus MrBretticus authored
Showing with 13,723 additions and 38 deletions.
  1. +4 −0 RestfulieClient/RestfulieClient.csproj
  2. +245 −0 RestfulieClient/resources/DynamicJsonResource.cs
  3. +37 −17 RestfulieClient/resources/DynamicXmlResource.cs
  4. +9 −1 RestfulieClient/resources/EmptyResource.cs
  5. +3 −0  RestfulieClient/resources/EntryPointService.cs
  6. +14 −0 RestfulieClient/resources/IResource.cs
  7. +87 −0 RestfulieClientTest/DynamicJsonResourceTest.cs
  8. +10 −10 RestfulieClientTest/DynamicXmlResourceTest.cs
  9. +1 −1  RestfulieClientTest/{EntryPointServiceTests.cs → EntryPointServiceTest.cs}
  10. +9 −1 RestfulieClientTest/RestfulieClientTests.csproj
  11. +2 −1  RestfulieClientTest/helpers/LoadDocument.cs
  12. +25 −7 RestfulieClientTest/helpers/TestHelper.cs
  13. +12 −0 RestfulieClientTest/jsons/city.json
  14. +13 −0 RestfulieClientTest/jsons/order.json
  15. +11 −0 RestfulieClientTest/jsons/orderWithoutLinks.json
  16. +12 −0 RestfulieClientTest/jsons/otherCity.json
  17. +6 −0 RestfulieSilverlightClient/RestfulieSilverlightClient.csproj
  18. BIN  RestfulieSilverlightClientTest.Web/ClientBin/RestfulieSilverlightClientTest.xap
  19. BIN  libs/Newtonsoft.Json-4.0/Newtonsoft.Json.Silverlight.dll
  20. +6,361 −0 libs/Newtonsoft.Json-4.0/Newtonsoft.Json.Silverlight.xml
  21. BIN  libs/Newtonsoft.Json-4.0/Newtonsoft.Json.dll
  22. +6,862 −0 libs/Newtonsoft.Json-4.0/Newtonsoft.Json.xml
View
4 RestfulieClient/RestfulieClient.csproj
@@ -31,6 +31,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
+ <Reference Include="Newtonsoft.Json">
+ <HintPath>..\libs\Newtonsoft.Json-4.0\Newtonsoft.Json.dll</HintPath>
+ </Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@@ -53,6 +56,7 @@
<Compile Include="request\RequestChain.cs" />
<Compile Include="request\RequestStack.cs" />
<Compile Include="request\ResponseChain.cs" />
+ <Compile Include="resources\DynamicJsonResource.cs" />
<Compile Include="resources\DynamicXmlResource.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="resources\EmptyResource.cs" />
View
245 RestfulieClient/resources/DynamicJsonResource.cs
@@ -0,0 +1,245 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Dynamic;
+using System.Globalization;
+using System.Linq;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+using RestfulieClient.service;
+
+namespace RestfulieClient.resources
+{
+ public class DynamicJsonResource : DynamicObject, IEnumerable, IResource
+ {
+ private readonly StringValueConverter _converter = new StringValueConverter();
+
+ private readonly IRemoteResourceService _service;
+ private readonly JContainer _root;
+ private readonly IDictionary<string, JToken> _rootIndex = new Dictionary<string, JToken>(StringComparer.OrdinalIgnoreCase);
+
+ public HttpRemoteResponse WebResponse { get; private set; }
+ public bool IsEmpty { get { return !_root.HasValues; } }
+ public NumberFormatInfo NumberFormatInfo { get; set; }
+
+ public DynamicJsonResource(HttpRemoteResponse webResponse) {
+ WebResponse = webResponse;
+ _root = String.IsNullOrWhiteSpace(webResponse.Content)
+ ? new JObject()
+ : JObject.Parse(webResponse.Content);
+
+ IndexRoot();
+ }
+
+ public DynamicJsonResource(HttpRemoteResponse webResponse, IRemoteResourceService service)
+ : this(webResponse) {
+ _service = service;
+ }
+
+ private DynamicJsonResource(HttpRemoteResponse webResponse, IRemoteResourceService service, JContainer root) {
+ WebResponse = webResponse;
+ _service = service;
+ _root = root;
+
+ IndexRoot();
+ }
+
+ private void IndexRoot() {
+ _rootIndex.Clear();
+
+ if (_root.Type != JTokenType.Object)
+ return;
+
+ var o = (JObject)_root;
+
+ ReadLinks();
+
+ foreach (var child in o.Properties())
+ _rootIndex.Add(child.Name, child.Value);
+ }
+
+ private object InvokeRemoteResource(string uri, string transitionName, string content) {
+ return String.IsNullOrWhiteSpace(content)
+ ? _service.Execute(uri, transitionName)
+ : _service.Execute(uri, transitionName, content);
+ }
+
+ private object GetValueFromToken(JToken token) {
+ if (token == null)
+ return null;
+
+ switch (token.Type) {
+ case JTokenType.Object:
+ case JTokenType.Array:
+ return new DynamicJsonResource(WebResponse, _service, (JContainer)token);
+ case JTokenType.Boolean:
+ return (bool)token;
+ case JTokenType.Date:
+ return (DateTime)token;
+ case JTokenType.Float:
+ return (float)token;
+ case JTokenType.Integer:
+ return (int)token;
+ case JTokenType.String:
+ return _converter.TransformText((string)token).WithNumberFormatInfo(NumberFormatInfo).ToValue();
+ default:
+ return token;
+ }
+ }
+
+ private readonly IDictionary<string, JObject> _links = new Dictionary<string, JObject>(StringComparer.OrdinalIgnoreCase);
+
+ private void ReadLinks() {
+ _links.Clear();
+
+ var single = _root["link"] as JObject;
+
+ if (single != null) {
+ _links.Add((string)single["rel"], single);
+ return;
+ }
+
+ var links = _root["links"] as JArray;
+
+ if (links == null)
+ return;
+
+ foreach (var link in links.Where(l => l is JObject).Cast<JObject>())
+ _links.Add((string)link["rel"], link);
+ }
+
+ private JObject GetLink(string rel) {
+ if (_root.Type != JTokenType.Object)
+ throw new InvalidOperationException(String.Format("Link scanning not supported for type: {0}", _root.Type));
+
+ JObject link;
+
+ return _links.TryGetValue(rel, out link) ? link : null;
+ }
+
+ private IResource FollowLink(JObject link, string content = null) {
+ var resource = (IResource)InvokeRemoteResource((string)link["href"], (string)link["rel"], content);
+ IResource result;
+
+ if (resource.WebResponse.HasNoContent()) {
+ result = WebResponse.HasNoContent() ? null : this;
+ WebResponse = resource.WebResponse;
+ }
+ else
+ result = resource;
+
+ return result;
+ }
+
+ private string Serialize(object o) {
+ return JsonConvert.SerializeObject(o);
+ }
+
+ public override bool TryGetMember(GetMemberBinder binder, out object result) {
+ if (_root.Type != JTokenType.Object)
+ throw new InvalidOperationException(String.Format("Cannot get member for {0}", _root.Type));
+
+ JToken token;
+
+ result = null;
+
+ if (_rootIndex.TryGetValue(binder.Name.Replace("_", "-"), out token))
+ result = GetValueFromToken(token);
+
+ return result != null;
+ }
+
+ public JToken GetTokenByIndex(JArray array, int index) {
+ if (index < 0 || index > array.Count - 1)
+ throw new ArgumentOutOfRangeException("index");
+
+ return array[index];
+ }
+
+ public JToken GetTokenById(JArray array, string id) {
+ if (String.IsNullOrWhiteSpace(id))
+ throw new ArgumentNullException("id");
+
+ return array
+ .Where(t => t.Type == JTokenType.Object)
+ .Cast<JObject>()
+ .FirstOrDefault(o =>
+ o.Property("id") != null &&
+ o.Property("id").Value.ToString().Equals(id, StringComparison.OrdinalIgnoreCase));
+ }
+
+ public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) {
+ if (_root.Type != JTokenType.Array)
+ throw new InvalidOperationException(String.Format("Cannot get index for {0}", _root.Type));
+ if (indexes == null)
+ throw new ArgumentNullException("indexes");
+ if (indexes.Length <= 0 || indexes.Length >= 2)
+ throw new IndexOutOfRangeException("Only one lookup index can be supplied");
+
+ var array = (JArray)_root;
+ var index = indexes.First();
+ var token = index is int
+ ? GetTokenByIndex(array, (int)index)
+ : GetTokenById(array, index.ToString());
+
+ result = GetValueFromToken(token);
+
+ return result != null;
+ }
+
+ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) {
+ if (binder.Name.StartsWith("has", StringComparison.OrdinalIgnoreCase) && binder.Name.Length > 3) {
+ result = HasLink(binder.Name.Substring(3));
+ return true;
+ }
+
+ var link = GetLink(binder.Name);
+
+ if (link == null)
+ throw new ArgumentException(string.Format("There is no link defined with rel: {0}", binder.Name));
+
+ string content = null;
+
+ if (args != null && args.Length == 1)
+ content = args.First() as string ?? Serialize(args.First());
+
+ result = FollowLink(link, content);
+
+ return result != null ? true : false;
+ }
+
+ public T As<T>() where T : class {
+ if (_root is JObject)
+ return new JsonSerializer().Deserialize<T>(new JTokenReader(_root));
+
+ throw new InvalidOperationException("As can only be called on objects");
+ }
+
+ public T[] AsMany<T>() where T : class {
+ if (_root is JArray)
+ return new JsonSerializer().Deserialize<T[]>(new JTokenReader(_root));
+
+ throw new InvalidOperationException("AsMany can only be called on arrays");
+ }
+
+ public bool HasLink(string rel) {
+ return _links.ContainsKey(rel);
+ }
+
+ public IResource Follow(string rel, string content = null) {
+ var link = GetLink(rel);
+
+ if (link == null)
+ throw new ArgumentException(String.Format("There is no link defined with rel: {0}", rel));
+
+ return FollowLink(link, content);
+ }
+
+ public IEnumerator GetEnumerator() {
+ if (_root.Type != JTokenType.Array)
+ throw new InvalidOperationException(String.Format("Cannot get index for {0}", _root.Type));
+
+ return _root.Select(GetValueFromToken).GetEnumerator();
+ }
+ }
+}
View
54 RestfulieClient/resources/DynamicXmlResource.cs
@@ -50,31 +50,43 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
return result != null ? true : false;
}
+ private object FollowLink(string rel, string href, string content = null) {
+ var resource = (IResource)InvokeRemoteResource(href, rel, content);
+ object result;
+
+ if (resource.WebResponse.HasNoContent()) {
+ result = XmlRepresentation;
+ WebResponse = resource.WebResponse;
+ }
+ else
+ result = resource;
+
+ return result;
+ }
+
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
object value = GetValueFromAttributeName(binder.Name, "href");
if (value == null)
throw new ArgumentException(string.Format("There is not method defined with name:", binder.Name));
- DynamicXmlResource resource = (DynamicXmlResource)InvokeRemoteResource(value.ToString(), binder.Name);
+ string content = null;
+
+ if (args != null && args.Length == 1)
+ content = args.First() as string;
+
+ result = FollowLink(binder.Name, (string)value, content);
- if (resource.WebResponse.HasNoContent())
- {
- result = XmlRepresentation;
- UpdateWebResponse(resource.WebResponse);
- }
- else
- {
- result = resource;
- }
return result != null ? true : false;
}
- private object InvokeRemoteResource(string url, string transitionName)
+ private object InvokeRemoteResource(string uri, string transitionName, string content)
{
try
{
- return RemoteResourceService.Execute(url, transitionName);
+ return String.IsNullOrWhiteSpace(content)
+ ? RemoteResourceService.Execute(uri, transitionName)
+ : RemoteResourceService.Execute(uri, transitionName, content);
}
catch (Exception ex)
{
@@ -120,16 +132,24 @@ private object GetValueFromAttributeName(string name, string attributeName)
return null;
}
- private void UpdateWebResponse(HttpRemoteResponse response)
- {
- WebResponse = response;
+ public bool HasLink(string rel) {
+ return GetValueFromAttributeName(rel, "href") != null;
}
- public bool HasLink(string rel) {
+ public IResource Follow(string rel, string content) {
+ var link = GetValueFromAttributeName(rel, "href");
+
+ if (link == null)
+ throw new ArgumentException(String.Format("There is no link defined with rel: {0}", rel));
+
+ return FollowLink(rel, (string)link, content) as IResource; // Note: need to fix up XElement result
+ }
+
+ public T As<T>() where T : class {
throw new NotImplementedException();
}
- public IResource Follow(string rel, string content) {
+ public T[] AsMany<T>() where T : class {
throw new NotImplementedException();
}
}
View
10 RestfulieClient/resources/EmptyResource.cs
@@ -18,7 +18,15 @@ public class EmptyResource : IResource
}
public IResource Follow(string rel, string content) {
- throw new NotImplementedException();
+ throw new NotImplementedException("Empty resource does not have anything to follow");
+ }
+
+ public T As<T>() where T : class {
+ throw new NotImplementedException("Empty resource cannot be converted");
+ }
+
+ public T[] AsMany<T>() where T : class {
+ throw new NotImplementedException("Empty resource cannot be converted");
}
}
}
View
3  RestfulieClient/resources/EntryPointService.cs
@@ -40,6 +40,9 @@ public class EntryPointService : IRemoteResourceService
if (contentType.IndexOf("application/xml", StringComparison.OrdinalIgnoreCase) > -1 ||
contentType.IndexOf("text/xml", StringComparison.OrdinalIgnoreCase) > -1)
return new DynamicXmlResource(response, this);
+ if (contentType.IndexOf("application/json", StringComparison.OrdinalIgnoreCase) > -1 ||
+ contentType.IndexOf("text/json", StringComparison.OrdinalIgnoreCase) > -1)
+ return new DynamicJsonResource(response, this);
return null;
}
View
14 RestfulieClient/resources/IResource.cs
@@ -26,5 +26,19 @@ public interface IResource
/// <param name="content">The content to include in the request body</param>
/// <returns>The resource result of the new request</returns>
IResource Follow(string rel, string content);
+
+ /// <summary>
+ /// Converts the dynamic resource to a typed object
+ /// </summary>
+ /// <typeparam name="T">The type of the class to convert to</typeparam>
+ /// <returns>The converted object</returns>
+ T As<T>() where T : class;
+
+ /// <summary>
+ /// Converts the dynamic collection resource to an array of typed objects
+ /// </summary>
+ /// <typeparam name="T">The type of the class to convert to</typeparam>
+ /// <returns>The converted objects</returns>
+ T[] AsMany<T>() where T : class;
}
}
View
87 RestfulieClientTest/DynamicJsonResourceTest.cs
@@ -0,0 +1,87 @@
+using System;
+using System.Globalization;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using RestfulieClientTests.helpers;
+
+namespace RestfulieClientTests
+{
+ [TestClass]
+ public class DynamicJsonResourceTest
+ {
+ [TestMethod]
+ public void ShouldBePossibleToLoadAJsonByTheDynamicObject()
+ {
+ dynamic order = TestHelper.GetDynamicJsonResourceWithServiceFake("order.json");
+ Assert.IsNotNull(order.date, "the attribute date is no expected");
+ Assert.IsNotNull(order.total, "the attribute total is no expected");
+ }
+
+ [TestMethod]
+ public void ShouldBePossibleToExecuteDynamicMethodsInResource()
+ {
+ dynamic order = TestHelper.GetDynamicJsonResourceWithServiceFake("order.json");
+ Assert.IsNotNull(order.Pay());
+ }
+
+
+ [Ignore]
+ public void ShouldBeAbleToAnswerToMethodRelName()
+ {
+ dynamic order = TestHelper.GetDynamicJsonResourceWithServiceFake("order.json");
+ Assert.IsNotNull(order.Update());
+
+ }
+
+ [TestMethod]
+ public void LearningToReadAAtomLinkInXml()
+ {
+ dynamic order = TestHelper.GetDynamicJsonResourceWithServiceFake("order.json");
+ Assert.IsNotNull(order.Pay());
+ }
+
+ [TestMethod]
+ public void ShouldBePossibleToAccessResponseHeadersEasily()
+ {
+ dynamic order = TestHelper.GetDynamicJsonResourceWithServiceFake("order.json");
+ Assert.AreEqual("application/json", order.WebResponse.Content_Type);
+ Assert.AreEqual("keep-alive", order.WebResponse.Connection);
+ }
+
+ [TestMethod]
+ public void ShouldBePossibleToAccessFieldsLikeUpdateAt()
+ {
+ dynamic order = TestHelper.GetDynamicJsonResourceWithServiceFake("order.json");
+ DateTime date = new DateTime(2010, 01, 01);
+ Assert.AreEqual(date, order.Update_At);
+ }
+
+ [TestMethod]
+ public void ShouldBePossibleToAccessInnerFieldsInAResource()
+ {
+ dynamic city = TestHelper.GetDynamicJsonResourceWithServiceFake("city.json");
+ Assert.AreEqual(18000000, city.Population.Size);
+ Assert.AreEqual(10, city.Population.Growth);
+ }
+
+ [TestMethod]
+ public void ShouldBePossibleToAccessInnerFieldsWithYourRealTypes()
+ {
+ dynamic order = TestHelper.GetDynamicJsonResourceWithServiceFake("order.json");
+ order.NumberFormatInfo = new CultureInfo("en-US", false).NumberFormat;
+ Assert.AreEqual(15.00, order.Total);
+ Assert.AreEqual(1, order.Id);
+ Assert.AreEqual(new DateTime(2010, 01, 01), order.Update_At);
+ Assert.AreEqual("unpaid", order.status);
+ }
+
+ [TestMethod]
+ public void ShouldBePossibleToAccessAnotherResourceByLink()
+ {
+ dynamic city = TestHelper.GetDynamicJsonResourceWithServiceFake("city.json");
+ dynamic otherCity = city.Next_Largest();
+ Assert.IsNotNull(otherCity);
+ Assert.AreEqual("Rio de Janeiro", otherCity.Name);
+ Assert.AreEqual("Sao Paulo", city.Name);
+ }
+ }
+}
View
20 RestfulieClientTest/DynamicXmlResourceTest.cs
@@ -9,9 +9,9 @@ namespace RestfulieClientTests
public class DynamicXmlResourceTest
{
[TestMethod]
- public void ShouldBePossibleToLoadAXmlByTheyDynamicObject()
+ public void ShouldBePossibleToLoadAXmlByTheDynamicObject()
{
- dynamic order = TestHelper.GetDynamicResourceWithServiceFake("order.xml");
+ dynamic order = TestHelper.GetDynamicXmlResourceWithServiceFake("order.xml");
Assert.IsNotNull(order.date, "the attribute date is no expected");
Assert.IsNotNull(order.total, "the attribute total is no expected");
}
@@ -19,7 +19,7 @@ public void ShouldBePossibleToLoadAXmlByTheyDynamicObject()
[TestMethod]
public void ShouldBePossibleToExecuteDynamicMethodsInResource()
{
- dynamic order = TestHelper.GetDynamicResourceWithServiceFake("order.xml");
+ dynamic order = TestHelper.GetDynamicXmlResourceWithServiceFake("order.xml");
Assert.IsNotNull(order.Pay());
}
@@ -27,7 +27,7 @@ public void ShouldBePossibleToExecuteDynamicMethodsInResource()
[Ignore]
public void ShouldBeAbleToAnswerToMethodRelName()
{
- dynamic order = TestHelper.GetDynamicResourceWithServiceFake("order.xml");
+ dynamic order = TestHelper.GetDynamicXmlResourceWithServiceFake("order.xml");
Assert.IsNotNull(order.Update());
}
@@ -35,14 +35,14 @@ public void ShouldBeAbleToAnswerToMethodRelName()
[TestMethod]
public void LearningToReadAAtomLinkInXml()
{
- dynamic order = TestHelper.GetDynamicResourceWithServiceFake("order.xml");
+ dynamic order = TestHelper.GetDynamicXmlResourceWithServiceFake("order.xml");
Assert.IsNotNull(order.Pay());
}
[TestMethod]
public void ShouldBePossibleToAccessResponseHeadersEasily()
{
- dynamic order = TestHelper.GetDynamicResourceWithServiceFake("order.xml");
+ dynamic order = TestHelper.GetDynamicXmlResourceWithServiceFake("order.xml");
Assert.AreEqual("application/xml", order.WebResponse.Content_Type);
Assert.AreEqual("keep-alive", order.WebResponse.Connection);
}
@@ -50,7 +50,7 @@ public void ShouldBePossibleToAccessResponseHeadersEasily()
[TestMethod]
public void ShouldBePossibleToAccessFieldsLikeUpdateAt()
{
- dynamic order = TestHelper.GetDynamicResourceWithServiceFake("order.xml");
+ dynamic order = TestHelper.GetDynamicXmlResourceWithServiceFake("order.xml");
DateTime date = new DateTime(2010, 01, 01);
Assert.AreEqual(date, order.Update_At);
}
@@ -58,7 +58,7 @@ public void ShouldBePossibleToAccessFieldsLikeUpdateAt()
[TestMethod]
public void ShouldBePossibleToAccessInnerFieldsInAResource()
{
- dynamic city = TestHelper.GetDynamicResourceWithServiceFake("city.xml");
+ dynamic city = TestHelper.GetDynamicXmlResourceWithServiceFake("city.xml");
Assert.AreEqual(18000000, city.Population.Size);
Assert.AreEqual(10, city.Growth);
}
@@ -66,7 +66,7 @@ public void ShouldBePossibleToAccessInnerFieldsInAResource()
[TestMethod]
public void ShouldBePossibleToAccessInnerFieldsWithYourRealTypes()
{
- dynamic order = TestHelper.GetDynamicResourceWithServiceFake("order.xml");
+ dynamic order = TestHelper.GetDynamicXmlResourceWithServiceFake("order.xml");
order.NumberFormatInfo = new CultureInfo("en-US", false).NumberFormat;
Assert.AreEqual(15.00, order.Total);
Assert.AreEqual(1, order.Id);
@@ -77,7 +77,7 @@ public void ShouldBePossibleToAccessInnerFieldsWithYourRealTypes()
[TestMethod]
public void ShouldBePossibleToAccessAOtherResourceByLink()
{
- dynamic city = TestHelper.GetDynamicResourceWithServiceFake("city.xml");
+ dynamic city = TestHelper.GetDynamicXmlResourceWithServiceFake("city.xml");
dynamic otherCity = city.Next_Largest();
Assert.IsNotNull(otherCity);
Assert.AreEqual("Rio de Janeiro", otherCity.Name);
View
2  ...fulieClientTest/EntryPointServiceTests.cs → RestfulieClientTest/EntryPointServiceTest.cs
@@ -12,7 +12,7 @@
namespace RestfulieClientTests
{
[TestClass]
- public class EntryPointServiceTests
+ public class EntryPointServiceTest
{
private IRemoteResourceService CreateService(HttpRemoteResponse response = null, IRequestDispatcher dispatcher = null) {
if (response == null)
View
10 RestfulieClientTest/RestfulieClientTests.csproj
@@ -48,13 +48,14 @@
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="DynamicJsonResourceTest.cs" />
<Compile Include="features\AutoRefreshTests.cs" />
<Compile Include="features\FollowRedirectsTests.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="helpers\TestHelper.cs" />
<Compile Include="DynamicXmlResourceTest.cs" />
- <Compile Include="EntryPointServiceTests.cs" />
+ <Compile Include="EntryPointServiceTest.cs" />
<Compile Include="helpers\EmbeddedFileResourceDispatcher.cs" />
<Compile Include="helpers\LoadDocument.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -79,6 +80,13 @@
<ItemGroup>
<EmbeddedResource Include="xmls\city.xml" />
</ItemGroup>
+ <ItemGroup />
+ <ItemGroup>
+ <EmbeddedResource Include="jsons\city.json" />
+ <EmbeddedResource Include="jsons\order.json" />
+ <EmbeddedResource Include="jsons\orderWithoutLinks.json" />
+ <EmbeddedResource Include="jsons\otherCity.json" />
+ </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
View
3  RestfulieClientTest/helpers/LoadDocument.cs
@@ -8,7 +8,8 @@ namespace RestfulieClientTests.helpers
public class LoadDocument
{
private static readonly IDictionary<string, string> ContentTypeNamespaces = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) {
- { "application/xml", "xmls" }
+ { "application/xml", "xmls" },
+ { "application/json", "jsons" }
};
private readonly string _contentType;
View
32 RestfulieClientTest/helpers/TestHelper.cs
@@ -21,11 +21,11 @@ public static class TestHelper
service.SetupGet(s => s.Headers).Returns(headers);
service.Setup(s => s.Execute(It.IsAny<string>()))
- .Returns<string>(u => GetDynamicResourceWithServiceFake(u, contentType));
+ .Returns<string>(u => GetDynamicXmlResourceWithServiceFake(u));
service.Setup(s => s.Execute(It.IsAny<string>(), It.IsAny<string>()))
- .Returns<string, string>((u, t) => GetDynamicResourceWithServiceFake(u, contentType));
+ .Returns<string, string>((u, t) => GetDynamicXmlResourceWithServiceFake(u));
service.Setup(s => s.Execute(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
- .Returns<string, string, object>((u, t, p) => GetDynamicResourceWithServiceFake(u, contentType));
+ .Returns<string, string, object>((u, t, p) => GetDynamicXmlResourceWithServiceFake(u));
return service;
}
@@ -64,12 +64,12 @@ public static class TestHelper
return responseFeature;
}
- public static DynamicXmlResource GetDynamicResourceWithServiceFake(string rawUri, string contentType = "application/xml")
+ private static HttpRemoteResponse GetDynamicResourceWithServiceFake(string rawUri, string contentType, out IRemoteResourceService service)
{
+ var serviceMock = CreateService(rawUri, contentType);
var uri = rawUri.StartsWith("http://") ? new Uri(rawUri) : new Uri(String.Format("file://{0}", rawUri));
var dispatcher = new EmbeddedFileRequestDispatcher(contentType);
- var service = CreateService(rawUri, contentType);
- var response = dispatcher.Process(service.Object, "GET", uri, null);
+ var response = dispatcher.Process(serviceMock.Object, "GET", uri, null);
response.Headers.Add("X-Runtime", "29");
response.Headers.Add("Connection", "keep-alive");
@@ -80,7 +80,25 @@ public static DynamicXmlResource GetDynamicResourceWithServiceFake(string rawUri
response.Headers.Add("Server", "nginx/0.6.39");
response.Headers.Add("Via", "1.1 varnish");
- return new DynamicXmlResource(response, service.Object);
+ service = serviceMock.Object;
+
+ return response;
+ }
+
+ public static DynamicXmlResource GetDynamicXmlResourceWithServiceFake(string rawUri) {
+ IRemoteResourceService service;
+
+ HttpRemoteResponse response = GetDynamicResourceWithServiceFake(rawUri, "application/xml", out service);
+
+ return new DynamicXmlResource(response, service);
+ }
+
+ public static DynamicJsonResource GetDynamicJsonResourceWithServiceFake(string rawUri) {
+ IRemoteResourceService service;
+
+ HttpRemoteResponse response = GetDynamicResourceWithServiceFake(rawUri, "application/json", out service);
+
+ return new DynamicJsonResource(response, service);
}
}
}
View
12 RestfulieClientTest/jsons/city.json
@@ -0,0 +1,12 @@
+{
+ "name": "Sao Paulo",
+ "population": {
+ "size": 18000000,
+ "growth": 10
+ },
+ "updated-at": "01/01/2010",
+ "link": {
+ "rel": "next_largest",
+ "href": "othercity.xml"
+ }
+}
View
13 RestfulieClientTest/jsons/order.json
@@ -0,0 +1,13 @@
+{
+ "id": 1,
+ "date": "26/11/2009",
+ "total": 15.00,
+ "status": "unpaid",
+ "update-at": "01/01/2010",
+ "links": [
+ { "rel": "refresh", "href": "http://localhost:3000/orders/1" },
+ { "rel": "update", "href": "http://localhost:3000/orders/1" },
+ { "rel": "pay", "href": "http://localhost:3000/orders/1" },
+ { "rel": "destroy", "href": "http://localhost:3000/orders/1" }
+ ]
+}
View
11 RestfulieClientTest/jsons/orderWithoutLinks.json
@@ -0,0 +1,11 @@
+{
+ "id": 1,
+ "date": "26/11/2009",
+ "total": 15.00,
+ "status": "unpaid",
+ "update-at": "01/01/2010",
+ "refresh": "http://localhost:3000/orders/1",
+ "update": "http://localhost:3000/orders/1",
+ "pay": "http://localhost:3000/orders/1",
+ "destroy": "http://localhost:3000/orders/1"
+}
View
12 RestfulieClientTest/jsons/otherCity.json
@@ -0,0 +1,12 @@
+{
+ "name": "Rio de Janeiro",
+ "population": {
+ "size": 18000000,
+ "growth": 10
+ },
+ "updated-at": "01/01/2010",
+ "link": {
+ "rel": "next_largest",
+ "href": "othercity.xml"
+ }
+}
View
6 RestfulieSilverlightClient/RestfulieSilverlightClient.csproj
@@ -48,6 +48,9 @@
<ItemGroup>
<Reference Include="Microsoft.CSharp, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="mscorlib" />
+ <Reference Include="Newtonsoft.Json.Silverlight">
+ <HintPath>..\libs\Newtonsoft.Json-4.0\Newtonsoft.Json.Silverlight.dll</HintPath>
+ </Reference>
<Reference Include="System.Windows" />
<Reference Include="system" />
<Reference Include="System.Core" />
@@ -93,6 +96,9 @@
<Compile Include="..\RestfulieClient\request\ResponseChain.cs">
<Link>request\ResponseChain.cs</Link>
</Compile>
+ <Compile Include="..\RestfulieClient\resources\DynamicJsonResource.cs">
+ <Link>resources\DynamicJsonResource.cs</Link>
+ </Compile>
<Compile Include="..\RestfulieClient\resources\DynamicXmlResource.cs">
<Link>resources\DynamicXmlResource.cs</Link>
</Compile>
View
BIN  RestfulieSilverlightClientTest.Web/ClientBin/RestfulieSilverlightClientTest.xap
Binary file not shown
View
BIN  libs/Newtonsoft.Json-4.0/Newtonsoft.Json.Silverlight.dll
Binary file not shown
View
6,361 libs/Newtonsoft.Json-4.0/Newtonsoft.Json.Silverlight.xml
6,361 additions, 0 deletions not shown
View
BIN  libs/Newtonsoft.Json-4.0/Newtonsoft.Json.dll
Binary file not shown
View
6,862 libs/Newtonsoft.Json-4.0/Newtonsoft.Json.xml
6,862 additions, 0 deletions not shown
Please sign in to comment.
Something went wrong with that request. Please try again.