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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const { httpRequest } = ChromeUtils.importESModule(
"resource://gre/modules/Http.sys.mjs"
);
const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
const BinaryInputStream = Components.Constructor(
"@mozilla.org/binaryinputstream;1",
"nsIBinaryInputStream",
"setInputStream"
);
var server;
const kDefaultServerPort = 9000;
const kSuccessPath = "/success";
const kBaseUrl = "http://localhost:" + kDefaultServerPort;
const kSuccessUrl = kBaseUrl + kSuccessPath;
const kPostPath = "/post";
const kPostUrl = kBaseUrl + kPostPath;
const kPostDataSent = [
["foo", "bar"],
["complex", "!*()@"],
];
const kPostDataReceived = "foo=bar&complex=%21%2A%28%29%40";
const kPostMimeTypeReceived =
"application/x-www-form-urlencoded; charset=utf-8";
const kJsonPostPath = "/json_post";
const kJsonPostUrl = kBaseUrl + kJsonPostPath;
const kJsonPostData = JSON.stringify(kPostDataSent);
const kJsonPostMimeType = "application/json";
const kPutPath = "/put";
const kPutUrl = kBaseUrl + kPutPath;
const kPutDataSent = [["P", "NP"]];
const kPutDataReceived = "P=NP";
const kGetPath = "/get";
const kGetUrl = kBaseUrl + kGetPath;
function successResult(aRequest, aResponse) {
aResponse.setStatusLine(null, 200, "OK");
aResponse.setHeader("Content-Type", "application/json");
aResponse.write("Success!");
}
function getDataChecker(
aExpectedMethod,
aExpectedData,
aExpectedMimeType = null
) {
return function (aRequest, aResponse) {
let body = new BinaryInputStream(aRequest.bodyInputStream);
let bytes = [];
let avail;
while ((avail = body.available()) > 0) {
Array.prototype.push.apply(bytes, body.readByteArray(avail));
}
Assert.equal(aRequest.method, aExpectedMethod);
// Checking if the Content-Type is as expected.
if (aExpectedMimeType) {
let contentType = aRequest.getHeader("Content-Type");
Assert.equal(contentType, aExpectedMimeType);
}
var data = String.fromCharCode.apply(null, bytes);
Assert.equal(data, aExpectedData);
aResponse.setStatusLine(null, 200, "OK");
aResponse.setHeader("Content-Type", "application/json");
aResponse.write("Success!");
};
}
add_test(function test_successCallback() {
do_test_pending();
let options = {
onLoad(aResponse) {
Assert.equal(aResponse, "Success!");
do_test_finished();
run_next_test();
},
onError(e) {
Assert.ok(false);
do_test_finished();
run_next_test();
},
};
httpRequest(kSuccessUrl, options);
});
add_test(function test_errorCallback() {
do_test_pending();
let options = {
onSuccess(aResponse) {
Assert.ok(false);
do_test_finished();
run_next_test();
},
onError(e, aResponse) {
Assert.equal(e.message, "404 - Not Found");
do_test_finished();
run_next_test();
},
};
httpRequest(kBaseUrl + "/failure", options);
});
add_test(function test_PostData() {
do_test_pending();
let options = {
onLoad(aResponse) {
Assert.equal(aResponse, "Success!");
do_test_finished();
run_next_test();
},
onError(e) {
Assert.ok(false);
do_test_finished();
run_next_test();
},
postData: kPostDataSent,
};
httpRequest(kPostUrl, options);
});
add_test(function test_PutData() {
do_test_pending();
let options = {
method: "PUT",
onLoad(aResponse) {
Assert.equal(aResponse, "Success!");
do_test_finished();
run_next_test();
},
onError(e) {
Assert.ok(false);
do_test_finished();
run_next_test();
},
postData: kPutDataSent,
};
httpRequest(kPutUrl, options);
});
add_test(function test_GetData() {
do_test_pending();
let options = {
onLoad(aResponse) {
Assert.equal(aResponse, "Success!");
do_test_finished();
run_next_test();
},
onError(e) {
Assert.ok(false);
do_test_finished();
run_next_test();
},
postData: null,
};
httpRequest(kGetUrl, options);
});
add_test(function test_OptionalParameters() {
let options = {
onLoad: null,
onError: null,
logger: null,
};
// Just make sure that nothing throws when doing this (i.e. httpRequest
// doesn't try to access null options).
httpRequest(kGetUrl, options);
run_next_test();
});
/**
* Makes sure that httpRequest API allows setting a custom Content-Type header
* for POST requests when data is a string.
*/
add_test(function test_CustomContentTypeOnPost() {
do_test_pending();
// Preparing the request parameters.
let options = {
onLoad(aResponse) {
Assert.equal(aResponse, "Success!");
do_test_finished();
run_next_test();
},
onError(e) {
Assert.ok(false);
do_test_finished();
run_next_test();
},
postData: kJsonPostData,
// Setting a custom Content-Type header.
headers: [["Content-Type", "application/json"]],
};
// Firing the request.
httpRequest(kJsonPostUrl, options);
});
/**
* Ensures that the httpRequest API provides a way to override the response
* MIME type.
*/
add_test(function test_OverrideMimeType() {
do_test_pending();
// Preparing the request parameters.
const kMimeType = "text/xml; charset=UTF-8";
let options = {
onLoad(aResponse, xhr) {
Assert.equal(aResponse, "Success!");
// Set the expected MIME-type.
let reportedMimeType = xhr.getResponseHeader("Content-Type");
Assert.notEqual(reportedMimeType, kMimeType);
// responseXML should not be not null if overriding mime type succeeded.
Assert.ok(xhr.responseXML != null);
do_test_finished();
run_next_test();
},
onError(e) {
Assert.ok(false);
do_test_finished();
run_next_test();
},
};
// Firing the request.
let xhr = httpRequest(kGetUrl, options);
// Override the response MIME type.
xhr.overrideMimeType(kMimeType);
});
function run_test() {
const PREF = "dom.xhr.standard_content_type_normalization";
Services.prefs.setBoolPref(PREF, true);
// Set up a mock HTTP server to serve a success page.
server = new HttpServer();
server.registerPathHandler(kSuccessPath, successResult);
server.registerPathHandler(
kPostPath,
getDataChecker("POST", kPostDataReceived, kPostMimeTypeReceived)
);
server.registerPathHandler(kPutPath, getDataChecker("PUT", kPutDataReceived));
server.registerPathHandler(kGetPath, getDataChecker("GET", ""));
server.registerPathHandler(
kJsonPostPath,
getDataChecker("POST", kJsonPostData, kJsonPostMimeType)
);
server.start(kDefaultServerPort);
run_next_test();
// Teardown.
registerCleanupFunction(function () {
Services.prefs.clearUserPref(PREF);
server.stop(function () {});
});
}
|