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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
Cu.import("resource://gre/modules/Log.jsm");
Cu.import("resource://services-sync/constants.js");
Cu.import("resource://services-sync/rest.js");
Cu.import("resource://services-sync/service.js");
Cu.import("resource://services-sync/util.js");
Cu.import("resource://testing-common/services/sync/utils.js");
var httpProtocolHandler = Cc["@mozilla.org/network/protocol;1?name=http"]
.getService(Ci.nsIHttpProtocolHandler);
function run_test() {
Log.repository.getLogger("Sync.RESTRequest").level = Log.Level.Trace;
initTestLogging();
ensureLegacyIdentityManager();
run_next_test();
}
add_test(function test_user_agent_desktop() {
let handler = httpd_handler(200, "OK");
let server = httpd_setup({"/resource": handler});
let expectedUA = Services.appinfo.name + "/" + Services.appinfo.version +
" (" + httpProtocolHandler.oscpu + ")" +
" FxSync/" + WEAVE_VERSION + "." +
Services.appinfo.appBuildID + ".desktop";
let request = new SyncStorageRequest(server.baseURI + "/resource");
request.onComplete = function onComplete(error) {
do_check_eq(error, null);
do_check_eq(this.response.status, 200);
do_check_eq(handler.request.getHeader("User-Agent"), expectedUA);
server.stop(run_next_test);
};
do_check_eq(request.get(), request);
});
add_test(function test_user_agent_mobile() {
let handler = httpd_handler(200, "OK");
let server = httpd_setup({"/resource": handler});
Svc.Prefs.set("client.type", "mobile");
let expectedUA = Services.appinfo.name + "/" + Services.appinfo.version +
" (" + httpProtocolHandler.oscpu + ")" +
" FxSync/" + WEAVE_VERSION + "." +
Services.appinfo.appBuildID + ".mobile";
let request = new SyncStorageRequest(server.baseURI + "/resource");
request.get(function (error) {
do_check_eq(error, null);
do_check_eq(this.response.status, 200);
do_check_eq(handler.request.getHeader("User-Agent"), expectedUA);
Svc.Prefs.resetBranch("");
server.stop(run_next_test);
});
});
add_test(function test_auth() {
let handler = httpd_handler(200, "OK");
let server = httpd_setup({"/resource": handler});
setBasicCredentials("johndoe", "ilovejane", "XXXXXXXXX");
let request = Service.getStorageRequest(server.baseURI + "/resource");
request.get(function (error) {
do_check_eq(error, null);
do_check_eq(this.response.status, 200);
do_check_true(basic_auth_matches(handler.request, "johndoe", "ilovejane"));
Svc.Prefs.reset("");
server.stop(run_next_test);
});
});
/**
* The X-Weave-Timestamp header updates SyncStorageRequest.serverTime.
*/
add_test(function test_weave_timestamp() {
const TIMESTAMP = 1274380461;
function handler(request, response) {
response.setHeader("X-Weave-Timestamp", "" + TIMESTAMP, false);
response.setStatusLine(request.httpVersion, 200, "OK");
}
let server = httpd_setup({"/resource": handler});
do_check_eq(SyncStorageRequest.serverTime, undefined);
let request = new SyncStorageRequest(server.baseURI + "/resource");
request.get(function (error) {
do_check_eq(error, null);
do_check_eq(this.response.status, 200);
do_check_eq(SyncStorageRequest.serverTime, TIMESTAMP);
delete SyncStorageRequest.serverTime;
server.stop(run_next_test);
});
});
/**
* The X-Weave-Backoff header notifies an observer.
*/
add_test(function test_weave_backoff() {
function handler(request, response) {
response.setHeader("X-Weave-Backoff", '600', false);
response.setStatusLine(request.httpVersion, 200, "OK");
}
let server = httpd_setup({"/resource": handler});
let backoffInterval;
Svc.Obs.add("weave:service:backoff:interval", function onBackoff(subject) {
Svc.Obs.remove("weave:service:backoff:interval", onBackoff);
backoffInterval = subject;
});
let request = new SyncStorageRequest(server.baseURI + "/resource");
request.get(function (error) {
do_check_eq(error, null);
do_check_eq(this.response.status, 200);
do_check_eq(backoffInterval, 600);
server.stop(run_next_test);
});
});
/**
* X-Weave-Quota-Remaining header notifies observer on successful requests.
*/
add_test(function test_weave_quota_notice() {
function handler(request, response) {
response.setHeader("X-Weave-Quota-Remaining", '1048576', false);
response.setStatusLine(request.httpVersion, 200, "OK");
}
let server = httpd_setup({"/resource": handler});
let quotaValue;
Svc.Obs.add("weave:service:quota:remaining", function onQuota(subject) {
Svc.Obs.remove("weave:service:quota:remaining", onQuota);
quotaValue = subject;
});
let request = new SyncStorageRequest(server.baseURI + "/resource");
request.get(function (error) {
do_check_eq(error, null);
do_check_eq(this.response.status, 200);
do_check_eq(quotaValue, 1048576);
server.stop(run_next_test);
});
});
/**
* X-Weave-Quota-Remaining header doesn't notify observer on failed requests.
*/
add_test(function test_weave_quota_error() {
function handler(request, response) {
response.setHeader("X-Weave-Quota-Remaining", '1048576', false);
response.setStatusLine(request.httpVersion, 400, "Bad Request");
}
let server = httpd_setup({"/resource": handler});
let quotaValue;
function onQuota(subject) {
quotaValue = subject;
}
Svc.Obs.add("weave:service:quota:remaining", onQuota);
let request = new SyncStorageRequest(server.baseURI + "/resource");
request.get(function (error) {
do_check_eq(error, null);
do_check_eq(this.response.status, 400);
do_check_eq(quotaValue, undefined);
Svc.Obs.remove("weave:service:quota:remaining", onQuota);
server.stop(run_next_test);
});
});
add_test(function test_abort() {
function handler(request, response) {
response.setHeader("X-Weave-Timestamp", "" + TIMESTAMP, false);
response.setHeader("X-Weave-Quota-Remaining", '1048576', false);
response.setHeader("X-Weave-Backoff", '600', false);
response.setStatusLine(request.httpVersion, 200, "OK");
}
let server = httpd_setup({"/resource": handler});
let request = new SyncStorageRequest(server.baseURI + "/resource");
// Aborting a request that hasn't been sent yet is pointless and will throw.
do_check_throws(function () {
request.abort();
});
function throwy() {
do_throw("Shouldn't have gotten here!");
}
Svc.Obs.add("weave:service:backoff:interval", throwy);
Svc.Obs.add("weave:service:quota:remaining", throwy);
request.onProgress = request.onComplete = throwy;
request.get();
request.abort();
do_check_eq(request.status, request.ABORTED);
// Aborting an already aborted request is pointless and will throw.
do_check_throws(function () {
request.abort();
});
Utils.nextTick(function () {
// Verify that we didn't try to process any of the values.
do_check_eq(SyncStorageRequest.serverTime, undefined);
Svc.Obs.remove("weave:service:backoff:interval", throwy);
Svc.Obs.remove("weave:service:quota:remaining", throwy);
server.stop(run_next_test);
});
});
|