forked from caelum/restfulie-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertersTest.html
148 lines (116 loc) · 4.48 KB
/
convertersTest.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<link rel="stylesheet" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>
<script type="text/javascript" src="../lib/converters.js"></script>
<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("Registering Converters")
test("should register a new converter", function() {
var MyConverter = {};
Converters.register('application/mytype', MyConverter);
var converter = Converters.getMediaType('application/mytype');
same(MyConverter, Converters.getMediaType('application/mytype'));
});
test("when my converter doesn't exists return the PlainConverter", function() {
var converter = Converters.getMediaType('nonexistent/type');
same(PlainConverter, converter, "the default converter must be a PlainConverter" );
});
module("XMLConverter")
test("should marshall my object", function() {
var myObject = {
name: "Pedro",
age: 33
}
var representationInXML = XmlConverter.marshal(myObject)
equal(representationInXML, "<name>Pedro</name><age>33</age>");
});
test("should unmarshall my object", function() {
var myObject = { user: {
name: "Pedro",
age: 33
}
}
var obj = XmlConverter.unmarshal({responseText : "<user><name>Pedro</name><age>33</age></user>"})
same(obj, myObject);
});
test("should unmarshall my object with links", function() {
var myObject = { user: {
name: "Pedro",
age: 33,
links: {
payment: Restfulie.at("http://localhost:8080")
}
}
}
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);
});
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>
</head>
<body>
<h1 id="qunit-header">Converters Test</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
</body>
</html>