-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathresponding_test.js
More file actions
149 lines (120 loc) · 4.27 KB
/
responding_test.js
File metadata and controls
149 lines (120 loc) · 4.27 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
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
149
var xhr, xmlDocumentConstructor;
module("responding", {
setup: function(){
xhr = new FakeXMLHttpRequest();
xmlDocumentConstructor = makeXMLDocument().constructor;
},
teardown: function(){
xhr = undefined;
xmlDocumentConstructor = undefined;
}
});
// Different browsers report different constructors for XML Documents.
// Chrome 45.0.2454 and Firefox 40.0.0 report `XMLDocument`,
// PhantomJS 1.9.8 reports `Document`.
// Make a dummy xml document to determine what constructor to
// compare against in the tests below.
// This function is taken from `parseXML` in the src/
function makeXMLDocument() {
var xmlDoc, text = "<some>xml</some>";
if (typeof DOMParser != "undefined") {
var parser = new DOMParser();
xmlDoc = parser.parseFromString(text, "text/xml");
} else {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(text);
}
return xmlDoc;
}
test("defaults responseHeaders to {} if not passed", function(){
xhr.respond(200);
deepEqual(xhr.responseHeaders, {});
});
test("sets responseHeaders", function(){
xhr.respond(200, {"Content-Type":"application/json"});
deepEqual(xhr.responseHeaders, {"Content-Type":"application/json"});
});
test("sets body", function(){
xhr.respond(200, {"Content-Type":"application/json"}, JSON.stringify({a: 'key'}));
equal(xhr.responseText, '{"a":"key"}');
});
test("parses the body if it's XML and no content-type is set", function(){
xhr.respond(200, {}, "<key>value</key>");
equal(xhr.responseXML.constructor, xmlDocumentConstructor);
});
test("parses the body if it's XML and xml content type is set", function(){
xhr.respond(200, {'Content-Type':'application/xml'}, "<key>value</key>");
equal(xhr.responseXML.constructor, xmlDocumentConstructor);
});
test("does not parse the body if it's XML and another content type is set", function(){
xhr.respond(200, {'Content-Type':'application/json'}, "<key>value</key>");
equal(xhr.responseXML, undefined);
});
test("parses the body if it's JSON and the json content type is set", function(){
const body = { key: 'value' };
xhr.respond(200, {'Content-Type':'application/json'}, JSON.stringify(body));
deepEqual(xhr.response, body);
});
test("does not parse the JSON body when another content type is set", function() {
xhr.respond(200, {'Content-Type':'application/xml'}, '{"a":"key"}');
equal(xhr.response, undefined);
});
test("calls the onload callback once", function(){
var wasCalled = 0;
xhr.onload = function(ev){
wasCalled += 1;
};
xhr.respond(200, {}, "");
strictEqual(wasCalled, 1);
});
test("passes event target as context to onload", function() {
var context;
var event;
xhr.onload = function(ev){
event = ev;
context = this;
};
xhr.respond(200, {}, "");
deepEqual(context, event.target);
});
test("calls onreadystatechange for each state change", function() {
var states = [];
xhr.onreadystatechange = function() {
states.push(this.readyState);
};
xhr.open('get', '/some/url');
xhr.respond(200, {}, "");
var expectedStates = [
FakeXMLHttpRequest.OPENED,
FakeXMLHttpRequest.HEADERS_RECEIVED,
FakeXMLHttpRequest.LOADING,
FakeXMLHttpRequest.DONE
];
deepEqual(states, expectedStates);
});
test("passes event to onreadystatechange", function() {
var event = null;
xhr.onreadystatechange = function(e) {
event = e;
};
xhr.open('get', '/some/url');
xhr.respond(200, {}, "");
ok(event && event.type === 'readystatechange',
'passes event with type "readystatechange"');
});
test("overrideMimeType overrides content-type responseHeader", function(){
xhr.overrideMimeType('text/plain');
xhr.respond(200, {"Content-Type":"application/json"});
deepEqual(xhr.responseHeaders, {"Content-Type":"text/plain"});
});
test("parses the body if it's XML and overrideMimeType is set to xml", function(){
xhr.overrideMimeType('application/xml');
xhr.respond(200, {'Content-Type':'text/plain'}, "<key>value</key>");
equal(xhr.responseXML.constructor, xmlDocumentConstructor);
});
test("does not parse the body if it's XML and overrideMimeType is set to another content type", function(){
xhr.overrideMimeType('text/plain');
xhr.respond(200, {'Content-Type':'application/xml'}, "<key>value</key>");
equal(xhr.responseXML, undefined);
});