Skip to content
Browse files

added create/post support

  • Loading branch information...
1 parent 46f5ab0 commit 13e1904011344342eaf2b14a079e69a58d937aec @sergioazevedo sergioazevedo committed Feb 19, 2010
View
1 RestfulieClient/resources/DynamicXmlResource.cs
@@ -95,6 +95,7 @@ private object GetValueFromXmlElement(XElement element)
return null;
}
+
private XElement GetFirstElementWithName(string name)
{
XElement firstElement = XmlRepresentation.Descendants(name).FirstOrDefault();
View
36 RestfulieClient/resources/EntryPointService.cs
@@ -1,15 +1,17 @@
using System;
-using RestfulieClient.service;
using System.Dynamic;
using System.Net;
+using RestfulieClient.service;
+using System.Text;
+using System.IO;
namespace RestfulieClient.resources
{
public class EntryPointService : IRemoteResourceService
{
private string entryPointURI = "";
- private string contentType;
- private string accept;
+ private string contentType = "";
+ private string accepts = "";
private RestfulieHttpVerbDiscovery httpVerbDiscovery = new RestfulieHttpVerbDiscovery();
@@ -21,7 +23,7 @@ public EntryPointService(string uri)
public dynamic As(string contentType)
{
this.contentType = contentType;
- this.accept = contentType;
+ this.accepts = contentType;
return this;
}
@@ -32,13 +34,19 @@ public dynamic Get()
return this.FromXml(this.entryPointURI);
}
+
+ public dynamic Create(string content)
+ {
+ return InvokeRemoteUri(this.entryPointURI, "post", content);
+ }
+
private dynamic FromXml(string uri)
{
dynamic response = this.GetResourceFromWeb(uri);
//todo - criar um enum para MediaType
if (response.ContentType.Equals("application/xml"))
{
- return new DynamicXmlResource(response, this);
+ return new DynamicXmlResource(response, this);
}
else
{
@@ -57,17 +65,27 @@ public object GetResourceFromWeb(string uri)
return this.InvokeRemoteUri(uri, "get");
}
- private object InvokeRemoteUri(string uri, string httpVerb)
+
+ private object InvokeRemoteUri(string uri, string httpVerb, string content = "")
{
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
+ Uri requestUri = new Uri(this.entryPointURI);
+ HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
try
{
request.Method = httpVerb;
- if (!accept.Equals(""))
+ if (!accepts.Equals(""))
{
- request.Accept = accept;
+ request.Accept = accepts;
request.ContentType = contentType;
}
+ if (!content.Equals(""))
+ {
+ byte[] byteArray = Encoding.UTF8.GetBytes(content);
+ request.ContentLength = byteArray.Length;
+ Stream bodyStream = request.GetRequestStream();
+ bodyStream.Write(byteArray, 0, byteArray.Length);
+ bodyStream.Close();
+ }
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return HttpRemoteResponseFactory.GetRemoteResponse(response);
}
View
2 RestfulieClient/service/IRemoteResourceService.cs
@@ -10,5 +10,7 @@ public interface IRemoteResourceService
object Execute(string uri, string transitionName);
object GetResourceFromWeb(string uri);
dynamic Get();
+
+ dynamic Create(string content);
}
}
View
2 RestfulieClientTest/DynamicXmlResourceTest.cs
@@ -107,6 +107,7 @@ public void ShouldBePossibleToAccessResponseHeadersEasily()
public void ShouldBePossibleToAccessFieldsLikeUpdateAt()
{
dynamic order = this.GetDynamicResourceWithServiceFake("order.xml");
+ //DateTime date = new DateTime(2010, 01, 01);
Assert.AreEqual("01/01/2010", order.Update_At);
}
@@ -118,6 +119,7 @@ public void ShouldBePossibleToAccessInnerFieldsInAResource()
Assert.AreEqual("10", city.Growth);
}
+
[TestMethod]
public void ShouldBePossibleToAccessAOtherResourceByLink()
{
View
9 RestfulieClientTest/EntryPointTests.cs
@@ -104,6 +104,15 @@ public void ShouldBePossibleToGetTheResponseStatusCode()
Assert.IsNotNull(order.WebResponse.StatusCode);
}
+ [TestMethod]
+ public void ShouldBePossibleToPostSomeContent()
+ {
+ string content = "<order><date>19/02/2010</date><total>55.00</total></order>";
+ dynamic newOrder = Restfulie.At(GetEntryPointServiceForTests("http:\\localhost:3000\\orders")).Create(content);
+ Assert.IsNotNull(newOrder);
+ Assert.AreEqual("55.00",newOrder.total);
+ }
+
private IRemoteResourceService GetEntryPointServiceForTests(string uri)
{
return RemoteResourceFactory.GetRemoteResource(uri);
View
30 RestfulieClientTest/helpers/RemoteResourceFactory.cs
@@ -23,10 +23,11 @@ public class RemoteResourceMock : IRemoteResourceService
{
private string fileName;
- public RemoteResourceMock(string fileName){
+ public RemoteResourceMock(string fileName)
+ {
this.fileName = fileName;
}
-
+
public object Execute(string uri, string transitionName)
{
return GetMediaTypeXMLResponse(uri);
@@ -45,21 +46,40 @@ public dynamic Get()
return GetMediaTypeXMLResponse(fileName);
}
+ public dynamic Create(string content)
+ {
+ return new DynamicXmlResource(CreateRemoteResponse(content));
+ }
+
+
private static DynamicXmlResource GetMediaTypeXMLResponse(string uri)
{
return new DynamicXmlResource(GetHttpRemoteResponseFake(uri));
}
+
private static HttpRemoteResponse GetHttpRemoteResponseFake(string fileName)
{
- string content = "";
- if (fileName != "" && fileName != "")
- content = new LoadDocument().GetDocumentContent(fileName);
+ string content = GetContentFromFile(fileName);
+ return CreateRemoteResponse(content);
+ }
+ private static HttpRemoteResponse CreateRemoteResponse(string content)
+ {
HttpRemoteResponse response = new HttpRemoteResponse(HttpStatusCode.OK,
new Dictionary<string, string>(), content);
return response;
}
+
+ private static string GetContentFromFile(string fileName)
+ {
+ string content = "";
+ if (fileName != "" && fileName != "")
+ content = new LoadDocument().GetDocumentContent(fileName);
+ return content;
+ }
+
+
}
}
}

0 comments on commit 13e1904

Please sign in to comment.
Something went wrong with that request. Please try again.