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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/pkg/angular/lib/mock/http_backend.dart
===================================================================
--- third_party/pkg/angular/lib/mock/http_backend.dart (revision 33054)
+++ third_party/pkg/angular/lib/mock/http_backend.dart (working copy)
@@ -3,7 +3,7 @@
class _MockXhr {
var $$method, $$url, $$async, $$reqHeaders, $$respHeaders;
- void open(method, url, async) {
+ open(method, url, async) {
$$method = method;
$$url = url;
$$async = async;
@@ -13,21 +13,24 @@
var $$data;
- void send(data) {
+ send(data) {
$$data = data;
}
- void setRequestHeader(key, value) {
+ setRequestHeader(key, value) {
$$reqHeaders[key] = value;
}
- String getResponseHeader(name) {
- // the lookup must be case insensitive, that's why we try two quick
- // lookups and full scan at last
- if ($$respHeaders.containsKey(name)) return $$respHeaders[name];
+ getResponseHeader(name) {
+ // the lookup must be case insensitive, that's why we try two quick lookups and full scan at last
+ if ($$respHeaders.containsKey(name)) {
+ return $$respHeaders[name];
+ }
name = name.toLowerCase();
- if ($$respHeaders.containsKey(name)) return $$respHeaders[name];
+ if ($$respHeaders.containsKey(name)) {
+ return $$respHeaders[name];
+ }
String header = null;
$$respHeaders.forEach((headerName, headerVal) {
@@ -56,51 +59,53 @@
* An internal class used by [MockHttpBackend].
*/
class MockHttpExpectation {
- final method;
- final url;
- final data;
- final headers;
+ var method, url, data, headers;
+
var response;
MockHttpExpectation(this.method, this.url, [this.data, this.headers]);
- bool match(method, url, [data, headers]) {
- if (method != method) return false;
- if (!matchUrl(url)) return false;
- if (data != null && !matchData(data)) return false;
- if (headers != null && !matchHeaders(headers)) return false;
+ match(m, u, [d, h]) {
+ if (method != m) return false;
+ if (!matchUrl(u)) return false;
+ if (d != null && !matchData(d)) return false;
+ if (h != null && !matchHeaders(h)) return false;
return true;
}
- bool matchUrl(u) {
+ matchUrl(u) {
if (url == null) return true;
if (url is RegExp) return url.hasMatch(u);
return url == u;
}
- bool matchHeaders(h) {
+ matchHeaders(h) {
if (headers == null) return true;
if (headers is Function) return headers(h);
return "$headers" == "$h";
}
- bool matchData(d) {
+ matchData(d) {
if (data == null) return true;
- if (d == null) return false;
+ if (d == null) return false; // data is not null, but d is.
if (data is File) return data == d;
assert(d is String);
if (data is RegExp) return data.hasMatch(d);
return JSON.encode(data) == JSON.encode(d);
}
- String toString() => "$method $url";
+ toString() {
+ return "$method $url";
+ }
}
class _Chain {
- final Function _respondFn;
- _Chain({respond}): _respondFn = respond;
+ var _respondFn;
+ _Chain({respond}) {
+ _respondFn = respond;
+ }
respond([x,y,z]) => _respondFn(x,y,z);
}
@@ -124,12 +129,12 @@
if (status >= 200 && status < 300) {
c.complete(new MockHttpRequest(status, data, headers));
} else {
- c.completeError(new MockProgressEvent(
- new MockHttpRequest(status, data, headers)));
+ c.completeError(
+ new MockProgressEvent(
+ new MockHttpRequest(status, data, headers)));
}
};
- call(method == null ? 'GET' : method, url, sendData, callback,
- requestHeaders);
+ call(method == null ? 'GET' : method, url, sendData, callback, requestHeaders);
return c.future;
}
@@ -145,7 +150,9 @@
data = statusOrDataOrFunction;
headers = dataOrHeaders;
}
- if (data is Map || data is List) data = JSON.encode(data);
+ if (data is Map || data is List) {
+ data = JSON.encode(data);
+ }
return ([a,b,c,d,e]) => [status, data, headers];
}
@@ -155,7 +162,7 @@
* A callback oriented API. This function takes a callback with
* will be called with (status, data, headers)
*/
- void call(method, [url, data, callback, headers, timeout]) {
+ call(method, [url, data, callback, headers, timeout]) {
var xhr = new _MockXhr(),
expectation = expectations.isEmpty ? null : expectations[0],
wasExpected = false;
@@ -170,12 +177,11 @@
var handleResponse = () {
var response = wrapped.response(method, url, data, headers);
xhr.$$respHeaders = response[2];
- utils.relaxFnApply(callback, [response[0], response[1],
- xhr.getAllResponseHeaders()]);
+ utils.relaxFnApply(callback, [response[0], response[1], xhr.getAllResponseHeaders()]);
};
var handleTimeout = () {
- for (var i = 0; i < responses.length; i++) {
+ for (var i = 0, ii = responses.length; i < ii; i++) {
if (identical(responses[i], handleResponse)) {
responses.removeAt(i);
callback(-1, null, '');
@@ -194,9 +200,8 @@
'EXPECTED: ${prettyPrint(expectation.data)}\nGOT: $data'];
if (!expectation.matchHeaders(headers))
- throw ['Expected $expectation with different headers\n'
- 'EXPECTED: ${prettyPrint(expectation.headers)}\n'
- 'GOT: ${prettyPrint(headers)}'];
+ throw ['Expected $expectation with different headers\n' +
+ 'EXPECTED: ${prettyPrint(expectation.headers)}\nGOT: ${prettyPrint(headers)}'];
expectations.removeAt(0);
@@ -218,9 +223,8 @@
}
throw wasExpected ?
['No response defined !'] :
- ['Unexpected request: $method $url\n' + (expectation != null ?
- 'Expected $expectation' :
- 'No more requests expected')];
+ ['Unexpected request: $method $url\n' +
+ (expectation != null ? 'Expected $expectation' : 'No more requests expected')];
}
/**
@@ -229,22 +233,21 @@
* @param {string} method HTTP method.
* @param {string|RegExp} url HTTP url.
* @param {(string|RegExp)=} data HTTP request body.
- * @param {(Object|function(Object))=} headers HTTP headers or function that
- * receives http header object and returns true if the headers match the
- * current definition.
- * @returns {requestHandler} Returns an object with `respond` method that
- * control how a matched request is handled.
+ * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
+ * object and returns true if the headers match the current definition.
+ * @returns {requestHandler} Returns an object with `respond` method that control how a matched
+ * request is handled.
*
* - respond – `{function([status,] data[, headers])|function(function(method, url, data, headers)}`
* – The respond method takes a set of static data to be returned or a function that can return
* an array containing response status (number), response data (string) and response headers
* (Object).
*/
- _Chain when(method, [url, data, headers]) {
+ when(method, [url, data, headers]) {
var definition = new MockHttpExpectation(method, url, data, headers),
chain = new _Chain(respond: (status, data, headers) {
- definition.response = _createResponse(status, data, headers);
- });
+ definition.response = _createResponse(status, data, headers);
+ });
definitions.add(definition);
return chain;
@@ -262,41 +265,54 @@
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled.
*/
- _Chain whenGET(url, [headers]) => when('GET', url, null, headers);
+
+ whenGET(url, [headers]) =>
+ when('GET', url, null, headers);
+ whenDELETE(url, [headers]) =>
+ when('DELETE', url, null, headers);
+ whenJSONP(url, [headers]) =>
+ when('JSONP', url, null, headers);
+
+ whenPUT(url, [data, headers]) =>
+ when('PUT', url, data, headers);
+ whenPOST(url, [data, headers]) =>
+ when('POST', url, data, headers);
+ whenPATCH(url, [data, headers]) =>
+ when('PATCH', url, data, headers);
+
/**
* @ngdoc method
- * @name ngMock.$httpBackend#whenDELETE
+ * @name ngMock.$httpBackend#whenHEAD
* @methodOf ngMock.$httpBackend
* @description
- * Creates a new backend definition for DELETE requests. For more info see `when()`.
+ * Creates a new backend definition for HEAD requests. For more info see `when()`.
*
* @param {string|RegExp} url HTTP url.
* @param {(Object|function(Object))=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled.
*/
- _Chain whenDELETE(url, [headers]) => when('DELETE', url, null, headers);
/**
* @ngdoc method
- * @name ngMock.$httpBackend#whenJSONP
+ * @name ngMock.$httpBackend#whenDELETE
* @methodOf ngMock.$httpBackend
* @description
- * Creates a new backend definition for JSONP requests. For more info see `when()`.
+ * Creates a new backend definition for DELETE requests. For more info see `when()`.
*
* @param {string|RegExp} url HTTP url.
+ * @param {(Object|function(Object))=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled.
*/
- _Chain whenJSONP(url, [headers]) => when('JSONP', url, null, headers);
/**
* @ngdoc method
- * @name ngMock.$httpBackend#whenPUT
+ * @name ngMock.$httpBackend#whenPOST
* @methodOf ngMock.$httpBackend
* @description
- * Creates a new backend definition for PUT requests. For more info see `when()`.
+ * Creates a new backend definition for POST requests. For more info see `when()`.
*
* @param {string|RegExp} url HTTP url.
* @param {(string|RegExp)=} data HTTP request body.
@@ -304,14 +320,13 @@
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled.
*/
- _Chain whenPUT(url, [data, headers]) => when('PUT', url, data, headers);
/**
* @ngdoc method
- * @name ngMock.$httpBackend#whenPOST
+ * @name ngMock.$httpBackend#whenPUT
* @methodOf ngMock.$httpBackend
* @description
- * Creates a new backend definition for POST requests. For more info see `when()`.
+ * Creates a new backend definition for PUT requests. For more info see `when()`.
*
* @param {string|RegExp} url HTTP url.
* @param {(string|RegExp)=} data HTTP request body.
@@ -319,23 +334,21 @@
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled.
*/
- _Chain whenPOST(url, [data, headers]) => when('POST', url, data, headers);
- _Chain whenPATCH(url, [data, headers]) => when('PATCH', url, data, headers);
-
/**
* @ngdoc method
- * @name ngMock.$httpBackend#whenHEAD
+ * @name ngMock.$httpBackend#whenJSONP
* @methodOf ngMock.$httpBackend
* @description
- * Creates a new backend definition for HEAD requests. For more info see `when()`.
+ * Creates a new backend definition for JSONP requests. For more info see `when()`.
*
* @param {string|RegExp} url HTTP url.
- * @param {(Object|function(Object))=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled.
*/
+ //createShortMethods('when');
+
/**
* @ngdoc method
* @name ngMock.$httpBackend#expect
@@ -356,12 +369,12 @@
* an array containing response status (number), response data (string) and response headers
* (Object).
*/
- _Chain expect(method, [url, data, headers]) {
+ expect(method, [url, data, headers]) {
var expectation = new MockHttpExpectation(method, url, data, headers);
expectations.add(expectation);
return new _Chain(respond: (status, data, headers) {
- expectation.response = _createResponse(status, data, headers);
- });
+ expectation.response = _createResponse(status, data, headers);
+ });
}
@@ -377,41 +390,52 @@
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled. See #expect for more info.
*/
- _Chain expectGET(url, [headers]) => expect('GET', url, null, headers);
+ expectGET(url, [headers]) =>
+ expect('GET', url, null, headers);
+ expectDELETE(url, [headers]) =>
+ expect('DELETE', url, null, headers);
+ expectJSONP(url, [headers]) =>
+ expect('JSONP', url, null, headers);
+ expectPUT(url, [data, headers]) =>
+ expect('PUT', url, data, headers);
+ expectPOST(url, [data, headers]) =>
+ expect('POST', url, data, headers);
+ expectPATCH(url, [data, headers]) =>
+ expect('PATCH', url, data, headers);
+
/**
* @ngdoc method
- * @name ngMock.$httpBackend#expectDELETE
+ * @name ngMock.$httpBackend#expectHEAD
* @methodOf ngMock.$httpBackend
* @description
- * Creates a new request expectation for DELETE requests. For more info see `expect()`.
+ * Creates a new request expectation for HEAD requests. For more info see `expect()`.
*
* @param {string|RegExp} url HTTP url.
* @param {Object=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled.
*/
- _Chain expectDELETE(url, [headers]) => expect('DELETE', url, null, headers);
/**
* @ngdoc method
- * @name ngMock.$httpBackend#expectJSONP
+ * @name ngMock.$httpBackend#expectDELETE
* @methodOf ngMock.$httpBackend
* @description
- * Creates a new request expectation for JSONP requests. For more info see `expect()`.
+ * Creates a new request expectation for DELETE requests. For more info see `expect()`.
*
* @param {string|RegExp} url HTTP url.
+ * @param {Object=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled.
*/
- _Chain expectJSONP(url, [headers]) => expect('JSONP', url, null, headers);
/**
* @ngdoc method
- * @name ngMock.$httpBackend#expectPUT
+ * @name ngMock.$httpBackend#expectPOST
* @methodOf ngMock.$httpBackend
* @description
- * Creates a new request expectation for PUT requests. For more info see `expect()`.
+ * Creates a new request expectation for POST requests. For more info see `expect()`.
*
* @param {string|RegExp} url HTTP url.
* @param {(string|RegExp)=} data HTTP request body.
@@ -419,14 +443,13 @@
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled.
*/
- _Chain expectPUT(url, [data, headers]) => expect('PUT', url, data, headers);
/**
* @ngdoc method
- * @name ngMock.$httpBackend#expectPOST
+ * @name ngMock.$httpBackend#expectPUT
* @methodOf ngMock.$httpBackend
* @description
- * Creates a new request expectation for POST requests. For more info see `expect()`.
+ * Creates a new request expectation for PUT requests. For more info see `expect()`.
*
* @param {string|RegExp} url HTTP url.
* @param {(string|RegExp)=} data HTTP request body.
@@ -434,7 +457,6 @@
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled.
*/
- _Chain expectPOST(url, [data, headers]) => expect('POST', url, data, headers);
/**
* @ngdoc method
@@ -449,21 +471,21 @@
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled.
*/
- _Chain expectPATCH(url, [data, headers]) => expect('PATCH', url, data, headers);
/**
* @ngdoc method
- * @name ngMock.$httpBackend#expectHEAD
+ * @name ngMock.$httpBackend#expectJSONP
* @methodOf ngMock.$httpBackend
* @description
- * Creates a new request expectation for HEAD requests. For more info see `expect()`.
+ * Creates a new request expectation for JSONP requests. For more info see `expect()`.
*
* @param {string|RegExp} url HTTP url.
- * @param {Object=} headers HTTP headers.
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
* request is handled.
*/
+ //createShortMethods('expect');
+
/**
* @ngdoc method
* @name ngMock.$httpBackend#flush
@@ -475,7 +497,7 @@
* all pending requests will be flushed. If there are no pending requests when the flush method
* is called an exception is thrown (as this typically a sign of programming error).
*/
- void flush([count]) {
+ flush([count]) {
if (responses.isEmpty) throw ['No pending request to flush !'];
if (count != null) {
@@ -507,7 +529,7 @@
* afterEach($httpBackend.verifyNoOutstandingExpectation);
* </pre>
*/
- void verifyNoOutstandingExpectation() {
+ verifyNoOutstandingExpectation() {
if (!expectations.isEmpty) {
throw ['Unsatisfied requests: ${expectations.join(', ')}'];
}
@@ -528,8 +550,10 @@
* afterEach($httpBackend.verifyNoOutstandingRequest);
* </pre>
*/
- void verifyNoOutstandingRequest() {
- if (!responses.isEmpty) throw ['Unflushed requests: ${responses.length}'];
+ verifyNoOutstandingRequest() {
+ if (!responses.isEmpty) {
+ throw ['Unflushed requests: ${responses.length}'];
+ }
}
@@ -542,7 +566,7 @@
* call resetExpectations during a multiple-phase test when you want to reuse the same instance of
* $httpBackend mock.
*/
- void resetExpectations() {
+ resetExpectations() {
expectations.length = 0;
responses.length = 0;
}
@@ -587,7 +611,10 @@
void abort() {}
bool dispatchEvent(Event event) => false;
- String getAllResponseHeaders() => headers;
+ String getAllResponseHeaders() {
+ if (headers == null) return null;
+ return headers;
+ }
String getResponseHeader(String header) => null;
void open(String method, String url, {bool async, String user, String password}) {}
« 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