Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(658)

Side by Side Diff: third_party/pkg/angular/lib/mock/http_backend.dart

Issue 180843004: Revert revision 33053 (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 part of angular.mock; 1 part of angular.mock;
2 2
3 class _MockXhr { 3 class _MockXhr {
4 var $$method, $$url, $$async, $$reqHeaders, $$respHeaders; 4 var $$method, $$url, $$async, $$reqHeaders, $$respHeaders;
5 5
6 void open(method, url, async) { 6 open(method, url, async) {
7 $$method = method; 7 $$method = method;
8 $$url = url; 8 $$url = url;
9 $$async = async; 9 $$async = async;
10 $$reqHeaders = {}; 10 $$reqHeaders = {};
11 $$respHeaders = {}; 11 $$respHeaders = {};
12 } 12 }
13 13
14 var $$data; 14 var $$data;
15 15
16 void send(data) { 16 send(data) {
17 $$data = data; 17 $$data = data;
18 } 18 }
19 19
20 void setRequestHeader(key, value) { 20 setRequestHeader(key, value) {
21 $$reqHeaders[key] = value; 21 $$reqHeaders[key] = value;
22 } 22 }
23 23
24 String getResponseHeader(name) { 24 getResponseHeader(name) {
25 // the lookup must be case insensitive, that's why we try two quick 25 // the lookup must be case insensitive, that's why we try two quick lookups and full scan at last
26 // lookups and full scan at last 26 if ($$respHeaders.containsKey(name)) {
27 if ($$respHeaders.containsKey(name)) return $$respHeaders[name]; 27 return $$respHeaders[name];
28 }
28 29
29 name = name.toLowerCase(); 30 name = name.toLowerCase();
30 if ($$respHeaders.containsKey(name)) return $$respHeaders[name]; 31 if ($$respHeaders.containsKey(name)) {
32 return $$respHeaders[name];
33 }
31 34
32 String header = null; 35 String header = null;
33 $$respHeaders.forEach((headerName, headerVal) { 36 $$respHeaders.forEach((headerName, headerVal) {
34 if (header != null) return; 37 if (header != null) return;
35 if (headerName.toLowerCase()) header = headerVal; 38 if (headerName.toLowerCase()) header = headerVal;
36 }); 39 });
37 return header; 40 return header;
38 } 41 }
39 42
40 getAllResponseHeaders() { 43 getAllResponseHeaders() {
41 if ($$respHeaders == null) return ''; 44 if ($$respHeaders == null) return '';
42 45
43 var lines = []; 46 var lines = [];
44 47
45 $$respHeaders.forEach((key, value) { 48 $$respHeaders.forEach((key, value) {
46 lines.add("$key: $value"); 49 lines.add("$key: $value");
47 }); 50 });
48 return lines.join('\n'); 51 return lines.join('\n');
49 } 52 }
50 53
51 // noop 54 // noop
52 abort() {} 55 abort() {}
53 } 56 }
54 57
55 /** 58 /**
56 * An internal class used by [MockHttpBackend]. 59 * An internal class used by [MockHttpBackend].
57 */ 60 */
58 class MockHttpExpectation { 61 class MockHttpExpectation {
59 final method; 62
60 final url; 63 var method, url, data, headers;
61 final data;
62 final headers;
63 64
64 var response; 65 var response;
65 66
66 MockHttpExpectation(this.method, this.url, [this.data, this.headers]); 67 MockHttpExpectation(this.method, this.url, [this.data, this.headers]);
67 68
68 bool match(method, url, [data, headers]) { 69 match(m, u, [d, h]) {
69 if (method != method) return false; 70 if (method != m) return false;
70 if (!matchUrl(url)) return false; 71 if (!matchUrl(u)) return false;
71 if (data != null && !matchData(data)) return false; 72 if (d != null && !matchData(d)) return false;
72 if (headers != null && !matchHeaders(headers)) return false; 73 if (h != null && !matchHeaders(h)) return false;
73 return true; 74 return true;
74 } 75 }
75 76
76 bool matchUrl(u) { 77 matchUrl(u) {
77 if (url == null) return true; 78 if (url == null) return true;
78 if (url is RegExp) return url.hasMatch(u); 79 if (url is RegExp) return url.hasMatch(u);
79 return url == u; 80 return url == u;
80 } 81 }
81 82
82 bool matchHeaders(h) { 83 matchHeaders(h) {
83 if (headers == null) return true; 84 if (headers == null) return true;
84 if (headers is Function) return headers(h); 85 if (headers is Function) return headers(h);
85 return "$headers" == "$h"; 86 return "$headers" == "$h";
86 } 87 }
87 88
88 bool matchData(d) { 89 matchData(d) {
89 if (data == null) return true; 90 if (data == null) return true;
90 if (d == null) return false; 91 if (d == null) return false; // data is not null, but d is.
91 if (data is File) return data == d; 92 if (data is File) return data == d;
92 assert(d is String); 93 assert(d is String);
93 if (data is RegExp) return data.hasMatch(d); 94 if (data is RegExp) return data.hasMatch(d);
94 return JSON.encode(data) == JSON.encode(d); 95 return JSON.encode(data) == JSON.encode(d);
95 } 96 }
96 97
97 String toString() => "$method $url"; 98 toString() {
99 return "$method $url";
100 }
98 } 101 }
99 102
100 103
101 class _Chain { 104 class _Chain {
102 final Function _respondFn; 105 var _respondFn;
103 _Chain({respond}): _respondFn = respond; 106 _Chain({respond}) {
107 _respondFn = respond;
108 }
104 respond([x,y,z]) => _respondFn(x,y,z); 109 respond([x,y,z]) => _respondFn(x,y,z);
105 } 110 }
106 111
107 /** 112 /**
108 * A mock implementation of [HttpBackend], used in tests. 113 * A mock implementation of [HttpBackend], used in tests.
109 */ 114 */
110 class MockHttpBackend implements HttpBackend { 115 class MockHttpBackend implements HttpBackend {
111 var definitions = [], 116 var definitions = [],
112 expectations = [], 117 expectations = [],
113 responses = []; 118 responses = [];
114 119
115 /** 120 /**
116 * This function is called from [Http] and designed to mimic the Dart APIs. 121 * This function is called from [Http] and designed to mimic the Dart APIs.
117 */ 122 */
118 dart_async.Future request(String url, 123 dart_async.Future request(String url,
119 {String method, bool withCredentials, String responseType, 124 {String method, bool withCredentials, String responseType,
120 String mimeType, Map<String, String> requestHeaders, sendData, 125 String mimeType, Map<String, String> requestHeaders, sendData,
121 void onProgress(ProgressEvent e)}) { 126 void onProgress(ProgressEvent e)}) {
122 dart_async.Completer c = new dart_async.Completer(); 127 dart_async.Completer c = new dart_async.Completer();
123 var callback = (status, data, headers) { 128 var callback = (status, data, headers) {
124 if (status >= 200 && status < 300) { 129 if (status >= 200 && status < 300) {
125 c.complete(new MockHttpRequest(status, data, headers)); 130 c.complete(new MockHttpRequest(status, data, headers));
126 } else { 131 } else {
127 c.completeError(new MockProgressEvent( 132 c.completeError(
128 new MockHttpRequest(status, data, headers))); 133 new MockProgressEvent(
134 new MockHttpRequest(status, data, headers)));
129 } 135 }
130 }; 136 };
131 call(method == null ? 'GET' : method, url, sendData, callback, 137 call(method == null ? 'GET' : method, url, sendData, callback, requestHeader s);
132 requestHeaders);
133 return c.future; 138 return c.future;
134 } 139 }
135 140
136 _createResponse(statusOrDataOrFunction, dataOrHeaders, headersOrNone) { 141 _createResponse(statusOrDataOrFunction, dataOrHeaders, headersOrNone) {
137 if (statusOrDataOrFunction is Function) return statusOrDataOrFunction; 142 if (statusOrDataOrFunction is Function) return statusOrDataOrFunction;
138 var status, data, headers; 143 var status, data, headers;
139 if (statusOrDataOrFunction is num) { 144 if (statusOrDataOrFunction is num) {
140 status = statusOrDataOrFunction; 145 status = statusOrDataOrFunction;
141 data = dataOrHeaders; 146 data = dataOrHeaders;
142 headers = headersOrNone; 147 headers = headersOrNone;
143 } else { 148 } else {
144 status = 200; 149 status = 200;
145 data = statusOrDataOrFunction; 150 data = statusOrDataOrFunction;
146 headers = dataOrHeaders; 151 headers = dataOrHeaders;
147 } 152 }
148 if (data is Map || data is List) data = JSON.encode(data); 153 if (data is Map || data is List) {
154 data = JSON.encode(data);
155 }
149 156
150 return ([a,b,c,d,e]) => [status, data, headers]; 157 return ([a,b,c,d,e]) => [status, data, headers];
151 } 158 }
152 159
153 160
154 /** 161 /**
155 * A callback oriented API. This function takes a callback with 162 * A callback oriented API. This function takes a callback with
156 * will be called with (status, data, headers) 163 * will be called with (status, data, headers)
157 */ 164 */
158 void call(method, [url, data, callback, headers, timeout]) { 165 call(method, [url, data, callback, headers, timeout]) {
159 var xhr = new _MockXhr(), 166 var xhr = new _MockXhr(),
160 expectation = expectations.isEmpty ? null : expectations[0], 167 expectation = expectations.isEmpty ? null : expectations[0],
161 wasExpected = false; 168 wasExpected = false;
162 169
163 var prettyPrint = (data) { 170 var prettyPrint = (data) {
164 return (data is String || data is Function || data is RegExp) 171 return (data is String || data is Function || data is RegExp)
165 ? data 172 ? data
166 : JSON.encode(data); 173 : JSON.encode(data);
167 }; 174 };
168 175
169 var wrapResponse = (wrapped) { 176 var wrapResponse = (wrapped) {
170 var handleResponse = () { 177 var handleResponse = () {
171 var response = wrapped.response(method, url, data, headers); 178 var response = wrapped.response(method, url, data, headers);
172 xhr.$$respHeaders = response[2]; 179 xhr.$$respHeaders = response[2];
173 utils.relaxFnApply(callback, [response[0], response[1], 180 utils.relaxFnApply(callback, [response[0], response[1], xhr.getAllRespon seHeaders()]);
174 xhr.getAllResponseHeaders()]);
175 }; 181 };
176 182
177 var handleTimeout = () { 183 var handleTimeout = () {
178 for (var i = 0; i < responses.length; i++) { 184 for (var i = 0, ii = responses.length; i < ii; i++) {
179 if (identical(responses[i], handleResponse)) { 185 if (identical(responses[i], handleResponse)) {
180 responses.removeAt(i); 186 responses.removeAt(i);
181 callback(-1, null, ''); 187 callback(-1, null, '');
182 break; 188 break;
183 } 189 }
184 } 190 }
185 }; 191 };
186 192
187 if (timeout != null) timeout.then(handleTimeout); 193 if (timeout != null) timeout.then(handleTimeout);
188 return handleResponse; 194 return handleResponse;
189 }; 195 };
190 196
191 if (expectation != null && expectation.match(method, url)) { 197 if (expectation != null && expectation.match(method, url)) {
192 if (!expectation.matchData(data)) 198 if (!expectation.matchData(data))
193 throw ['Expected $expectation with different data\n' + 199 throw ['Expected $expectation with different data\n' +
194 'EXPECTED: ${prettyPrint(expectation.data)}\nGOT: $data']; 200 'EXPECTED: ${prettyPrint(expectation.data)}\nGOT: $data'];
195 201
196 if (!expectation.matchHeaders(headers)) 202 if (!expectation.matchHeaders(headers))
197 throw ['Expected $expectation with different headers\n' 203 throw ['Expected $expectation with different headers\n' +
198 'EXPECTED: ${prettyPrint(expectation.headers)}\n' 204 'EXPECTED: ${prettyPrint(expectation.headers)}\nGOT: ${prettyPr int(headers)}'];
199 'GOT: ${prettyPrint(headers)}'];
200 205
201 expectations.removeAt(0); 206 expectations.removeAt(0);
202 207
203 if (expectation.response != null) { 208 if (expectation.response != null) {
204 responses.add(wrapResponse(expectation)); 209 responses.add(wrapResponse(expectation));
205 return; 210 return;
206 } 211 }
207 wasExpected = true; 212 wasExpected = true;
208 } 213 }
209 214
210 for (var definition in definitions) { 215 for (var definition in definitions) {
211 if (definition.match(method, url, data, headers != null ? headers : {})) { 216 if (definition.match(method, url, data, headers != null ? headers : {})) {
212 if (definition.response != null) { 217 if (definition.response != null) {
213 // if $browser specified, we do auto flush all requests 218 // if $browser specified, we do auto flush all requests
214 responses.add(wrapResponse(definition)); 219 responses.add(wrapResponse(definition));
215 } else throw ['No response defined !']; 220 } else throw ['No response defined !'];
216 return; 221 return;
217 } 222 }
218 } 223 }
219 throw wasExpected ? 224 throw wasExpected ?
220 ['No response defined !'] : 225 ['No response defined !'] :
221 ['Unexpected request: $method $url\n' + (expectation != null ? 226 ['Unexpected request: $method $url\n' +
222 'Expected $expectation' : 227 (expectation != null ? 'Expected $expectation' : 'No more requests e xpected')];
223 'No more requests expected')];
224 } 228 }
225 229
226 /** 230 /**
227 * Creates a new backend definition. 231 * Creates a new backend definition.
228 * 232 *
229 * @param {string} method HTTP method. 233 * @param {string} method HTTP method.
230 * @param {string|RegExp} url HTTP url. 234 * @param {string|RegExp} url HTTP url.
231 * @param {(string|RegExp)=} data HTTP request body. 235 * @param {(string|RegExp)=} data HTTP request body.
232 * @param {(Object|function(Object))=} headers HTTP headers or function that 236 * @param {(Object|function(Object))=} headers HTTP headers or function that r eceives http header
233 * receives http header object and returns true if the headers match the 237 * object and returns true if the headers match the current definition.
234 * current definition. 238 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
235 * @returns {requestHandler} Returns an object with `respond` method that 239 * request is handled.
236 * control how a matched request is handled.
237 * 240 *
238 * - respond – `{function([status,] data[, headers])|function(function(method , url, data, headers)}` 241 * - respond – `{function([status,] data[, headers])|function(function(method , url, data, headers)}`
239 * – The respond method takes a set of static data to be returned or a func tion that can return 242 * – The respond method takes a set of static data to be returned or a func tion that can return
240 * an array containing response status (number), response data (string) and response headers 243 * an array containing response status (number), response data (string) and response headers
241 * (Object). 244 * (Object).
242 */ 245 */
243 _Chain when(method, [url, data, headers]) { 246 when(method, [url, data, headers]) {
244 var definition = new MockHttpExpectation(method, url, data, headers), 247 var definition = new MockHttpExpectation(method, url, data, headers),
245 chain = new _Chain(respond: (status, data, headers) { 248 chain = new _Chain(respond: (status, data, headers) {
246 definition.response = _createResponse(status, data, headers); 249 definition.response = _createResponse(status, data, headers);
247 }); 250 });
248 251
249 definitions.add(definition); 252 definitions.add(definition);
250 return chain; 253 return chain;
251 } 254 }
252 255
253 /** 256 /**
254 * @ngdoc method 257 * @ngdoc method
255 * @name ngMock.$httpBackend#whenGET 258 * @name ngMock.$httpBackend#whenGET
256 * @methodOf ngMock.$httpBackend 259 * @methodOf ngMock.$httpBackend
257 * @description 260 * @description
258 * Creates a new backend definition for GET requests. For more info see `when( )`. 261 * Creates a new backend definition for GET requests. For more info see `when( )`.
259 * 262 *
260 * @param {string|RegExp} url HTTP url. 263 * @param {string|RegExp} url HTTP url.
261 * @param {(Object|function(Object))=} headers HTTP headers. 264 * @param {(Object|function(Object))=} headers HTTP headers.
262 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched 265 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
263 * request is handled. 266 * request is handled.
264 */ 267 */
265 _Chain whenGET(url, [headers]) => when('GET', url, null, headers); 268
269
270 whenGET(url, [headers]) =>
271 when('GET', url, null, headers);
272 whenDELETE(url, [headers]) =>
273 when('DELETE', url, null, headers);
274 whenJSONP(url, [headers]) =>
275 when('JSONP', url, null, headers);
276
277 whenPUT(url, [data, headers]) =>
278 when('PUT', url, data, headers);
279 whenPOST(url, [data, headers]) =>
280 when('POST', url, data, headers);
281 whenPATCH(url, [data, headers]) =>
282 when('PATCH', url, data, headers);
283
284 /**
285 * @ngdoc method
286 * @name ngMock.$httpBackend#whenHEAD
287 * @methodOf ngMock.$httpBackend
288 * @description
289 * Creates a new backend definition for HEAD requests. For more info see `when ()`.
290 *
291 * @param {string|RegExp} url HTTP url.
292 * @param {(Object|function(Object))=} headers HTTP headers.
293 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
294 * request is handled.
295 */
266 296
267 /** 297 /**
268 * @ngdoc method 298 * @ngdoc method
269 * @name ngMock.$httpBackend#whenDELETE 299 * @name ngMock.$httpBackend#whenDELETE
270 * @methodOf ngMock.$httpBackend 300 * @methodOf ngMock.$httpBackend
271 * @description 301 * @description
272 * Creates a new backend definition for DELETE requests. For more info see `wh en()`. 302 * Creates a new backend definition for DELETE requests. For more info see `wh en()`.
273 * 303 *
274 * @param {string|RegExp} url HTTP url. 304 * @param {string|RegExp} url HTTP url.
275 * @param {(Object|function(Object))=} headers HTTP headers. 305 * @param {(Object|function(Object))=} headers HTTP headers.
276 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched 306 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
277 * request is handled. 307 * request is handled.
278 */ 308 */
279 _Chain whenDELETE(url, [headers]) => when('DELETE', url, null, headers);
280 309
281 /** 310 /**
282 * @ngdoc method 311 * @ngdoc method
283 * @name ngMock.$httpBackend#whenJSONP 312 * @name ngMock.$httpBackend#whenPOST
284 * @methodOf ngMock.$httpBackend 313 * @methodOf ngMock.$httpBackend
285 * @description 314 * @description
286 * Creates a new backend definition for JSONP requests. For more info see `whe n()`. 315 * Creates a new backend definition for POST requests. For more info see `when ()`.
287 *
288 * @param {string|RegExp} url HTTP url.
289 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
290 * request is handled.
291 */
292 _Chain whenJSONP(url, [headers]) => when('JSONP', url, null, headers);
293
294 /**
295 * @ngdoc method
296 * @name ngMock.$httpBackend#whenPUT
297 * @methodOf ngMock.$httpBackend
298 * @description
299 * Creates a new backend definition for PUT requests. For more info see `when ()`.
300 * 316 *
301 * @param {string|RegExp} url HTTP url. 317 * @param {string|RegExp} url HTTP url.
302 * @param {(string|RegExp)=} data HTTP request body. 318 * @param {(string|RegExp)=} data HTTP request body.
303 * @param {(Object|function(Object))=} headers HTTP headers. 319 * @param {(Object|function(Object))=} headers HTTP headers.
304 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched 320 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
305 * request is handled. 321 * request is handled.
306 */ 322 */
307 _Chain whenPUT(url, [data, headers]) => when('PUT', url, data, headers);
308 323
309 /** 324 /**
310 * @ngdoc method 325 * @ngdoc method
311 * @name ngMock.$httpBackend#whenPOST 326 * @name ngMock.$httpBackend#whenPUT
312 * @methodOf ngMock.$httpBackend 327 * @methodOf ngMock.$httpBackend
313 * @description 328 * @description
314 * Creates a new backend definition for POST requests. For more info see `when ()`. 329 * Creates a new backend definition for PUT requests. For more info see `when ()`.
315 * 330 *
316 * @param {string|RegExp} url HTTP url. 331 * @param {string|RegExp} url HTTP url.
317 * @param {(string|RegExp)=} data HTTP request body. 332 * @param {(string|RegExp)=} data HTTP request body.
318 * @param {(Object|function(Object))=} headers HTTP headers. 333 * @param {(Object|function(Object))=} headers HTTP headers.
319 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched 334 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
320 * request is handled. 335 * request is handled.
321 */ 336 */
322 _Chain whenPOST(url, [data, headers]) => when('POST', url, data, headers);
323
324 _Chain whenPATCH(url, [data, headers]) => when('PATCH', url, data, headers);
325
326 /**
327 * @ngdoc method
328 * @name ngMock.$httpBackend#whenHEAD
329 * @methodOf ngMock.$httpBackend
330 * @description
331 * Creates a new backend definition for HEAD requests. For more info see `when ()`.
332 *
333 * @param {string|RegExp} url HTTP url.
334 * @param {(Object|function(Object))=} headers HTTP headers.
335 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
336 * request is handled.
337 */
338 337
339 /** 338 /**
340 * @ngdoc method 339 * @ngdoc method
340 * @name ngMock.$httpBackend#whenJSONP
341 * @methodOf ngMock.$httpBackend
342 * @description
343 * Creates a new backend definition for JSONP requests. For more info see `whe n()`.
344 *
345 * @param {string|RegExp} url HTTP url.
346 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
347 * request is handled.
348 */
349 //createShortMethods('when');
350
351
352 /**
353 * @ngdoc method
341 * @name ngMock.$httpBackend#expect 354 * @name ngMock.$httpBackend#expect
342 * @methodOf ngMock.$httpBackend 355 * @methodOf ngMock.$httpBackend
343 * @description 356 * @description
344 * Creates a new request expectation. 357 * Creates a new request expectation.
345 * 358 *
346 * @param {string} method HTTP method. 359 * @param {string} method HTTP method.
347 * @param {string|RegExp} url HTTP url. 360 * @param {string|RegExp} url HTTP url.
348 * @param {(string|RegExp)=} data HTTP request body. 361 * @param {(string|RegExp)=} data HTTP request body.
349 * @param {(Object|function(Object))=} headers HTTP headers or function that r eceives http header 362 * @param {(Object|function(Object))=} headers HTTP headers or function that r eceives http header
350 * object and returns true if the headers match the current expectation. 363 * object and returns true if the headers match the current expectation.
351 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched 364 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
352 * request is handled. 365 * request is handled.
353 * 366 *
354 * - respond – `{function([status,] data[, headers])|function(function(method , url, data, headers)}` 367 * - respond – `{function([status,] data[, headers])|function(function(method , url, data, headers)}`
355 * – The respond method takes a set of static data to be returned or a func tion that can return 368 * – The respond method takes a set of static data to be returned or a func tion that can return
356 * an array containing response status (number), response data (string) and response headers 369 * an array containing response status (number), response data (string) and response headers
357 * (Object). 370 * (Object).
358 */ 371 */
359 _Chain expect(method, [url, data, headers]) { 372 expect(method, [url, data, headers]) {
360 var expectation = new MockHttpExpectation(method, url, data, headers); 373 var expectation = new MockHttpExpectation(method, url, data, headers);
361 expectations.add(expectation); 374 expectations.add(expectation);
362 return new _Chain(respond: (status, data, headers) { 375 return new _Chain(respond: (status, data, headers) {
363 expectation.response = _createResponse(status, data, headers); 376 expectation.response = _createResponse(status, data, headers);
364 }); 377 });
365 } 378 }
366 379
367 380
368 /** 381 /**
369 * @ngdoc method 382 * @ngdoc method
370 * @name ngMock.$httpBackend#expectGET 383 * @name ngMock.$httpBackend#expectGET
371 * @methodOf ngMock.$httpBackend 384 * @methodOf ngMock.$httpBackend
372 * @description 385 * @description
373 * Creates a new request expectation for GET requests. For more info see `expe ct()`. 386 * Creates a new request expectation for GET requests. For more info see `expe ct()`.
374 * 387 *
375 * @param {string|RegExp} url HTTP url. 388 * @param {string|RegExp} url HTTP url.
376 * @param {Object=} headers HTTP headers. 389 * @param {Object=} headers HTTP headers.
377 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched 390 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
378 * request is handled. See #expect for more info. 391 * request is handled. See #expect for more info.
379 */ 392 */
380 _Chain expectGET(url, [headers]) => expect('GET', url, null, headers); 393 expectGET(url, [headers]) =>
394 expect('GET', url, null, headers);
395 expectDELETE(url, [headers]) =>
396 expect('DELETE', url, null, headers);
397 expectJSONP(url, [headers]) =>
398 expect('JSONP', url, null, headers);
399
400 expectPUT(url, [data, headers]) =>
401 expect('PUT', url, data, headers);
402 expectPOST(url, [data, headers]) =>
403 expect('POST', url, data, headers);
404 expectPATCH(url, [data, headers]) =>
405 expect('PATCH', url, data, headers);
406
407 /**
408 * @ngdoc method
409 * @name ngMock.$httpBackend#expectHEAD
410 * @methodOf ngMock.$httpBackend
411 * @description
412 * Creates a new request expectation for HEAD requests. For more info see `exp ect()`.
413 *
414 * @param {string|RegExp} url HTTP url.
415 * @param {Object=} headers HTTP headers.
416 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
417 * request is handled.
418 */
381 419
382 /** 420 /**
383 * @ngdoc method 421 * @ngdoc method
384 * @name ngMock.$httpBackend#expectDELETE 422 * @name ngMock.$httpBackend#expectDELETE
385 * @methodOf ngMock.$httpBackend 423 * @methodOf ngMock.$httpBackend
386 * @description 424 * @description
387 * Creates a new request expectation for DELETE requests. For more info see `e xpect()`. 425 * Creates a new request expectation for DELETE requests. For more info see `e xpect()`.
388 * 426 *
389 * @param {string|RegExp} url HTTP url. 427 * @param {string|RegExp} url HTTP url.
390 * @param {Object=} headers HTTP headers. 428 * @param {Object=} headers HTTP headers.
391 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched 429 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
392 * request is handled. 430 * request is handled.
393 */ 431 */
394 _Chain expectDELETE(url, [headers]) => expect('DELETE', url, null, headers);
395 432
396 /** 433 /**
397 * @ngdoc method 434 * @ngdoc method
398 * @name ngMock.$httpBackend#expectJSONP 435 * @name ngMock.$httpBackend#expectPOST
399 * @methodOf ngMock.$httpBackend 436 * @methodOf ngMock.$httpBackend
400 * @description 437 * @description
401 * Creates a new request expectation for JSONP requests. For more info see `ex pect()`. 438 * Creates a new request expectation for POST requests. For more info see `exp ect()`.
402 *
403 * @param {string|RegExp} url HTTP url.
404 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
405 * request is handled.
406 */
407 _Chain expectJSONP(url, [headers]) => expect('JSONP', url, null, headers);
408
409 /**
410 * @ngdoc method
411 * @name ngMock.$httpBackend#expectPUT
412 * @methodOf ngMock.$httpBackend
413 * @description
414 * Creates a new request expectation for PUT requests. For more info see `expe ct()`.
415 * 439 *
416 * @param {string|RegExp} url HTTP url. 440 * @param {string|RegExp} url HTTP url.
417 * @param {(string|RegExp)=} data HTTP request body. 441 * @param {(string|RegExp)=} data HTTP request body.
418 * @param {Object=} headers HTTP headers. 442 * @param {Object=} headers HTTP headers.
419 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched 443 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
420 * request is handled. 444 * request is handled.
421 */ 445 */
422 _Chain expectPUT(url, [data, headers]) => expect('PUT', url, data, headers);
423 446
424 /** 447 /**
425 * @ngdoc method 448 * @ngdoc method
426 * @name ngMock.$httpBackend#expectPOST 449 * @name ngMock.$httpBackend#expectPUT
427 * @methodOf ngMock.$httpBackend 450 * @methodOf ngMock.$httpBackend
428 * @description 451 * @description
429 * Creates a new request expectation for POST requests. For more info see `exp ect()`. 452 * Creates a new request expectation for PUT requests. For more info see `expe ct()`.
430 * 453 *
431 * @param {string|RegExp} url HTTP url. 454 * @param {string|RegExp} url HTTP url.
432 * @param {(string|RegExp)=} data HTTP request body. 455 * @param {(string|RegExp)=} data HTTP request body.
433 * @param {Object=} headers HTTP headers. 456 * @param {Object=} headers HTTP headers.
434 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched 457 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
435 * request is handled. 458 * request is handled.
436 */ 459 */
437 _Chain expectPOST(url, [data, headers]) => expect('POST', url, data, headers);
438 460
439 /** 461 /**
440 * @ngdoc method 462 * @ngdoc method
441 * @name ngMock.$httpBackend#expectPATCH 463 * @name ngMock.$httpBackend#expectPATCH
442 * @methodOf ngMock.$httpBackend 464 * @methodOf ngMock.$httpBackend
443 * @description 465 * @description
444 * Creates a new request expectation for PATCH requests. For more info see `ex pect()`. 466 * Creates a new request expectation for PATCH requests. For more info see `ex pect()`.
445 * 467 *
446 * @param {string|RegExp} url HTTP url. 468 * @param {string|RegExp} url HTTP url.
447 * @param {(string|RegExp)=} data HTTP request body. 469 * @param {(string|RegExp)=} data HTTP request body.
448 * @param {Object=} headers HTTP headers. 470 * @param {Object=} headers HTTP headers.
449 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched 471 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
450 * request is handled. 472 * request is handled.
451 */ 473 */
452 _Chain expectPATCH(url, [data, headers]) => expect('PATCH', url, data, headers );
453
454 /**
455 * @ngdoc method
456 * @name ngMock.$httpBackend#expectHEAD
457 * @methodOf ngMock.$httpBackend
458 * @description
459 * Creates a new request expectation for HEAD requests. For more info see `exp ect()`.
460 *
461 * @param {string|RegExp} url HTTP url.
462 * @param {Object=} headers HTTP headers.
463 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
464 * request is handled.
465 */
466 474
467 /** 475 /**
468 * @ngdoc method 476 * @ngdoc method
477 * @name ngMock.$httpBackend#expectJSONP
478 * @methodOf ngMock.$httpBackend
479 * @description
480 * Creates a new request expectation for JSONP requests. For more info see `ex pect()`.
481 *
482 * @param {string|RegExp} url HTTP url.
483 * @returns {requestHandler} Returns an object with `respond` method that cont rol how a matched
484 * request is handled.
485 */
486 //createShortMethods('expect');
487
488
489 /**
490 * @ngdoc method
469 * @name ngMock.$httpBackend#flush 491 * @name ngMock.$httpBackend#flush
470 * @methodOf ngMock.$httpBackend 492 * @methodOf ngMock.$httpBackend
471 * @description 493 * @description
472 * Flushes all pending requests using the trained responses. 494 * Flushes all pending requests using the trained responses.
473 * 495 *
474 * @param {number=} count Number of responses to flush (in the order they arri ved). If undefined, 496 * @param {number=} count Number of responses to flush (in the order they arri ved). If undefined,
475 * all pending requests will be flushed. If there are no pending requests wh en the flush method 497 * all pending requests will be flushed. If there are no pending requests wh en the flush method
476 * is called an exception is thrown (as this typically a sign of programming error). 498 * is called an exception is thrown (as this typically a sign of programming error).
477 */ 499 */
478 void flush([count]) { 500 flush([count]) {
479 if (responses.isEmpty) throw ['No pending request to flush !']; 501 if (responses.isEmpty) throw ['No pending request to flush !'];
480 502
481 if (count != null) { 503 if (count != null) {
482 while (count-- > 0) { 504 while (count-- > 0) {
483 if (responses.isEmpty) throw ['No more pending request to flush !']; 505 if (responses.isEmpty) throw ['No more pending request to flush !'];
484 responses.removeAt(0)(); 506 responses.removeAt(0)();
485 } 507 }
486 } else { 508 } else {
487 while (!responses.isEmpty) { 509 while (!responses.isEmpty) {
488 responses.removeAt(0)(); 510 responses.removeAt(0)();
(...skipping 11 matching lines...) Expand all
500 * Verifies that all of the requests defined via the `expect` api were made. I f any of the 522 * Verifies that all of the requests defined via the `expect` api were made. I f any of the
501 * requests were not made, verifyNoOutstandingExpectation throws an exception. 523 * requests were not made, verifyNoOutstandingExpectation throws an exception.
502 * 524 *
503 * Typically, you would call this method following each test case that asserts requests using an 525 * Typically, you would call this method following each test case that asserts requests using an
504 * "afterEach" clause. 526 * "afterEach" clause.
505 * 527 *
506 * <pre> 528 * <pre>
507 * afterEach($httpBackend.verifyNoOutstandingExpectation); 529 * afterEach($httpBackend.verifyNoOutstandingExpectation);
508 * </pre> 530 * </pre>
509 */ 531 */
510 void verifyNoOutstandingExpectation() { 532 verifyNoOutstandingExpectation() {
511 if (!expectations.isEmpty) { 533 if (!expectations.isEmpty) {
512 throw ['Unsatisfied requests: ${expectations.join(', ')}']; 534 throw ['Unsatisfied requests: ${expectations.join(', ')}'];
513 } 535 }
514 } 536 }
515 537
516 538
517 /** 539 /**
518 * @ngdoc method 540 * @ngdoc method
519 * @name ngMock.$httpBackend#verifyNoOutstandingRequest 541 * @name ngMock.$httpBackend#verifyNoOutstandingRequest
520 * @methodOf ngMock.$httpBackend 542 * @methodOf ngMock.$httpBackend
521 * @description 543 * @description
522 * Verifies that there are no outstanding requests that need to be flushed. 544 * Verifies that there are no outstanding requests that need to be flushed.
523 * 545 *
524 * Typically, you would call this method following each test case that asserts requests using an 546 * Typically, you would call this method following each test case that asserts requests using an
525 * "afterEach" clause. 547 * "afterEach" clause.
526 * 548 *
527 * <pre> 549 * <pre>
528 * afterEach($httpBackend.verifyNoOutstandingRequest); 550 * afterEach($httpBackend.verifyNoOutstandingRequest);
529 * </pre> 551 * </pre>
530 */ 552 */
531 void verifyNoOutstandingRequest() { 553 verifyNoOutstandingRequest() {
532 if (!responses.isEmpty) throw ['Unflushed requests: ${responses.length}']; 554 if (!responses.isEmpty) {
555 throw ['Unflushed requests: ${responses.length}'];
556 }
533 } 557 }
534 558
535 559
536 /** 560 /**
537 * @ngdoc method 561 * @ngdoc method
538 * @name ngMock.$httpBackend#resetExpectations 562 * @name ngMock.$httpBackend#resetExpectations
539 * @methodOf ngMock.$httpBackend 563 * @methodOf ngMock.$httpBackend
540 * @description 564 * @description
541 * Resets all request expectations, but preserves all backend definitions. Typ ically, you would 565 * Resets all request expectations, but preserves all backend definitions. Typ ically, you would
542 * call resetExpectations during a multiple-phase test when you want to reuse the same instance of 566 * call resetExpectations during a multiple-phase test when you want to reuse the same instance of
543 * $httpBackend mock. 567 * $httpBackend mock.
544 */ 568 */
545 void resetExpectations() { 569 resetExpectations() {
546 expectations.length = 0; 570 expectations.length = 0;
547 responses.length = 0; 571 responses.length = 0;
548 } 572 }
549 } 573 }
550 574
551 /** 575 /**
552 * Mock implementation of the [HttpRequest] object returned from the HttpBackend . 576 * Mock implementation of the [HttpRequest] object returned from the HttpBackend .
553 */ 577 */
554 class MockHttpRequest implements HttpRequest { 578 class MockHttpRequest implements HttpRequest {
555 final bool supportsCrossOrigin = false; 579 final bool supportsCrossOrigin = false;
(...skipping 24 matching lines...) Expand all
580 bool withCredentials; 604 bool withCredentials;
581 605
582 final int status; 606 final int status;
583 final response; 607 final response;
584 final String headers; 608 final String headers;
585 609
586 MockHttpRequest(this.status, this.response, [this.headers]); 610 MockHttpRequest(this.status, this.response, [this.headers]);
587 611
588 void abort() {} 612 void abort() {}
589 bool dispatchEvent(Event event) => false; 613 bool dispatchEvent(Event event) => false;
590 String getAllResponseHeaders() => headers; 614 String getAllResponseHeaders() {
615 if (headers == null) return null;
616 return headers;
617 }
591 String getResponseHeader(String header) => null; 618 String getResponseHeader(String header) => null;
592 619
593 void open(String method, String url, {bool async, String user, String password }) {} 620 void open(String method, String url, {bool async, String user, String password }) {}
594 void overrideMimeType(String override) {} 621 void overrideMimeType(String override) {}
595 void send([data]) {} 622 void send([data]) {}
596 void setRequestHeader(String header, String value) {} 623 void setRequestHeader(String header, String value) {}
597 void addEventListener(String type, EventListener listener, [bool useCapture]) {} 624 void addEventListener(String type, EventListener listener, [bool useCapture]) {}
598 void removeEventListener(String type, EventListener listener, [bool useCapture ]) {} 625 void removeEventListener(String type, EventListener listener, [bool useCapture ]) {}
599 } 626 }
600 627
(...skipping 17 matching lines...) Expand all
618 final String type = null; 645 final String type = null;
619 646
620 bool cancelBubble = false; 647 bool cancelBubble = false;
621 648
622 MockProgressEvent(MockHttpRequest this.currentTarget); 649 MockProgressEvent(MockHttpRequest this.currentTarget);
623 650
624 void preventDefault() {} 651 void preventDefault() {}
625 void stopImmediatePropagation() {} 652 void stopImmediatePropagation() {}
626 void stopPropagation() {} 653 void stopPropagation() {}
627 } 654 }
OLDNEW
« no previous file with comments | « third_party/pkg/angular/lib/mock/exception_handler.dart ('k') | third_party/pkg/angular/lib/mock/log.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698