handlers.js
1.67 KB
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
define([
"doh/main",
"dojo/request/handlers",
"dojo/has",
"dojo/_base/kernel",
"dojo/json"
], function(doh, handlers, has, kernel, JSON){
var tests = [
function textContentHandler(t){
var response = handlers({
text: "foo bar baz ",
options: {}
});
t.is("foo bar baz ", response.data);
},
function jsonContentHandler(t){
var jsonObj = {
foo: "bar",
baz: [
{ thonk: "blarg" },
"xyzzy!"
]
};
var responseData = handlers({
text: JSON.stringify(jsonObj),
options: {
handleAs: "json"
}
});
t.is(jsonObj, responseData.data);
},
function jsContentHandler(t){
var jsonObj = {
foo: "bar",
baz: [
{ thonk: "blarg" },
"xyzzy!"
]
};
var responseData = handlers({
text: "("+JSON.stringify(jsonObj)+")",
options: {
handleAs: "javascript"
}
});
t.is(jsonObj, responseData.data);
responseData = handlers({
text: "true;",
options: {
handleAs: "javascript"
}
});
t.t(responseData.data);
responseData = handlers({
text: "false;",
options: {
handleAs: "javascript"
}
});
t.f(responseData.data);
}
];
if(has("host-browser")){
tests.push(
function xmlContentHandler(t){
var responseData = {
text: "<foo><bar baz='thonk'>blarg</bar></foo>",
options: {
handleAs: "xml"
}
};
if("DOMParser" in kernel.global){
var parser = new DOMParser();
responseData.data = parser.parseFromString(responseData.text, "text/xml");
}
responseData = handlers(responseData);
t.is("foo", responseData.data.documentElement.tagName);
}
);
}
doh.register("tests.request.handlers", tests);
});