Skip to content
Browse files

adding more xml converter tests

  • Loading branch information...
1 parent ce2c4ec commit c0e8ab1bbb0748ca252225f3a7a8051ffbb50e97 @pmariano pmariano committed Nov 12, 2010
Showing with 83 additions and 13 deletions.
  1. +12 −8 client/lib/converters.js
  2. +71 −5 client/tests/convertersTest.html
View
20 client/lib/converters.js
@@ -21,33 +21,37 @@ var XmlConverter = {
return this.findAndBuildLinks(JSON.parse(json));
},
findAndBuildLinks: function(resource){
- if (!resource) return resource;
if (typeof(resource) != 'object') return resource;
+
for (var attribute in resource){
resource[attribute] = this.findAndBuildLinks(resource[attribute]);
}
+
if (!resource.link) return resource;
- if (!resource.link.length){
+ if (!(resource.link instanceof Array)){
var link = resource.link;
resource.link = [];
resource.link[0] = link;
}
- resource.links = [];
- for (var i=0;i<resource.link.length;i++){
- var rel = resource.link[i]["@rel"],
- href = resource.link[i]["@href"],
- accept = resource.link[i]["@type"];
- var linkResource = href;
+ resource.links = {};
+
+ for (var i=0;i < resource.link.length;i++){
+ var rel = resource.link[i]["rel"],
+ href = resource.link[i]["href"],
+ accept = resource.link[i]["type"];
+
var linkResource = Restfulie.at(href);
if(accept) {
linkResource.accepts(accept);
}
resource.links[rel] = linkResource;
}
+
delete resource.link;
+
return resource;
}
};
View
76 client/tests/convertersTest.html
@@ -9,12 +9,14 @@
<script type="text/javascript" src="../lib/json2xml.js"></script>
<script type="text/javascript" src="../lib/xml2json.js"></script>
<script type="text/javascript" src="../lib/json2.js"></script>
+<script type="text/javascript" src="../lib/restfulie.js"></script>
+
<script>
$(document).ready(function(){
- module("Register Converters")
+ module("Registering Converters")
test("should register a new converter", function() {
var MyConverter = {};
Converters.register('application/mytype', MyConverter);
@@ -28,6 +30,7 @@
same(PlainConverter, converter, "the default converter must be a PlainConverter" );
});
+
module("XMLConverter")
test("should marshall my object", function() {
@@ -52,21 +55,84 @@
same(obj, myObject);
});
- test("should build links from a resource ", function() {
+
+ test("should unmarshall my object with links", function() {
var myObject = { user: {
name: "Pedro",
- age: 33
+ age: 33,
+ links: {
+ payment: Restfulie.at("http://localhost:8080")
+ }
}
}
- var obj = XmlConverter.findAndBuildLinks(myObject);
+ var obj = XmlConverter.unmarshal({responseText : "<user><name>Pedro</name><age>33</age><link><rel>payment</rel><href>http://localhost:8080</href></link></user>"})
same(obj, myObject);
});
+ test("should return the resource if the resource isn't an object ", function() {
+ equals(XmlConverter.findAndBuildLinks(undefined), undefined);
+ });
- module("XMLConverter")
+ test("should build one link", function() {
+
+ var objWithLink = {
+ link: {
+ rel: "payment",
+ href: "http://payment.com",
+ type: "application/paymenttype"
+ }
+ };
+
+ resource = XmlConverter.findAndBuildLinks(objWithLink);
+ equals(resource.links["payment"].uri, "http://payment.com");
+ equals(resource.links["payment"].headers.Accept, "application/paymenttype");
+
+ });
+
+ test("should build link with deep objects", function() {
+
+ var deepObject = { user: {
+ creditCard:{
+ link: {
+ rel: "refund",
+ href: "http://myCreditCard.com",
+ type: "application/refundtype"
+ }
+ }
+ }
+ };
+
+ resource = XmlConverter.findAndBuildLinks(deepObject);
+ equals(resource.user.creditCard.links["refund"].headers.Accept, "application/refundtype");
+ equals(resource.user.creditCard.links["refund"].uri, "http://myCreditCard.com");
+ });
+ test("should build more than one links ", function() {
+
+ var objWithLinks = {
+ link: [{
+ rel: "payment",
+ href: "http://payment.com",
+ type: "application/paymenttype"
+ },
+
+ {
+ rel: "deliver",
+ href: "http://deliver.com",
+ type: "application/delivertype"
+ }]
+ };
+
+ resource = XmlConverter.findAndBuildLinks(objWithLinks);
+ equals(resource.links["payment"].uri, "http://payment.com");
+ equals(resource.links["payment"].headers.Accept, "application/paymenttype");
+ equals(resource.links["deliver"].uri, "http://deliver.com");
+ equals(resource.links["deliver"].headers.Accept, "application/delivertype");
+
+ });
+
});
</script>

0 comments on commit c0e8ab1

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