diff --git a/lib/http.dart b/lib/http.dart index 20dbd15bb..f9d6801c0 100644 --- a/lib/http.dart +++ b/lib/http.dart @@ -3,7 +3,7 @@ part of angular; // NOTE(deboer): This should be a generic utility class, but lets make sure // it works in this case first! class HttpFutures { - value(x) => new async.Future.value(x); + async.Future value(x) => new async.Future.value(x); } class UrlRewriter { @@ -11,9 +11,18 @@ class UrlRewriter { } class HttpBackend { - getString(String url, {bool withCredentials, void onProgress(dom.ProgressEvent e)}) { - return dom.HttpRequest.getString(url, withCredentials: withCredentials, onProgress: onProgress); - } + async.Future request(String url, + {String method, bool withCredentials, String responseType, + String mimeType, Map requestHeaders, sendData, + void onProgress(dom.ProgressEvent e)}) => + dom.HttpRequest.request(url, + method: method, + withCredentials: withCredentials, + responseType: responseType, + mimeType: mimeType, + requestHeaders: requestHeaders, + sendData: sendData, + onProgress: onProgress); } class Http { @@ -24,7 +33,22 @@ class Http { Http(UrlRewriter this.rewriter, HttpBackend this.backend, HttpFutures this.futures); - async.Future getString(String rawUrl, {bool withCredentials, void onProgress(dom.ProgressEvent e), Cache cache}) { + async.Future getString(String url, + {bool withCredentials, void onProgress(ProgressEvent e), Cache cache}) { + return request(url, + withCredentials: withCredentials, + onProgress: onProgress, + cache: cache).then((xhr) => xhr.responseText); + } + + // TODO(deboer): The cache is keyed on the url only. It should be keyed on + // (url, method, mimeType, requestHeaders, ...) + // Better yet, we should be using a HTTP standard cache. + async.Future request(String rawUrl, + {String method, bool withCredentials, String responseType, + String mimeType, Map requestHeaders, sendData, + void onProgress(dom.ProgressEvent e), + Cache cache}) { String url = rewriter(rawUrl); // We return a pending request only if caching is enabled. @@ -35,7 +59,14 @@ class Http { if (cachedValue != null) { return futures.value(cachedValue); } - var result = backend.getString(url, withCredentials: withCredentials, onProgress: onProgress).then((value) { + var result = backend.request(url, + method: method, + withCredentials: withCredentials, + responseType: responseType, + mimeType: mimeType, + requestHeaders: requestHeaders, + sendData: sendData, + onProgress: onProgress).then((value) { if (cache != null) { cache.put(url, value); } @@ -49,3 +80,4 @@ class Http { return result; } } + diff --git a/test/_http.dart b/test/_http.dart index 3e6e82140..a6d6322df 100644 --- a/test/_http.dart +++ b/test/_http.dart @@ -2,8 +2,7 @@ library ng_mock_http; import 'dart:async'; -import 'dart:html'; -import 'package:angular/angular.dart'; +import '_specs.dart'; class MockHttp extends Http { Map gets = {}; @@ -62,6 +61,13 @@ class MockHttpFutures extends HttpFutures { } } +// NOTE(deboer): If I try to extend HttpRequest, I get an error: +// class 'MockHttpRequest' is trying to extend a native fields class, but library '_http.dart' has no native resolvers +class MockHttpRequest /*extends HttpRequest*/ { + String responseText; + MockHttpRequest(this.responseText); +} + class MockHttpBackend extends HttpBackend { Map gets = {}; List completersAndValues = []; @@ -81,14 +87,17 @@ class MockHttpBackend extends HttpBackend { } } - getString(String url, {bool withCredentials, void onProgress(ProgressEvent e)}) { + Future request(String url, + {String method, bool withCredentials, String responseType, + String mimeType, Map requestHeaders, sendData, + void onProgress(ProgressEvent e)}) { if (!gets.containsKey(url)) throw "Unexpected URL $url $gets"; var data = gets[url]; data.times--; if (data.times <= 0) { gets.remove(url); } - var expectedValue = data.value; + var expectedValue = new MockHttpRequest(data.value); var completer = new Completer.sync(); completersAndValues.add([completer, expectedValue]); return completer.future; diff --git a/test/http_spec.dart b/test/http_spec.dart index e812e4b04..9ecb5a225 100644 --- a/test/http_spec.dart +++ b/test/http_spec.dart @@ -6,7 +6,7 @@ var CACHED_VALUE = 'cached_value'; class FakeCache implements Cache { - get(x) => x == 'f' ? CACHED_VALUE : null; + get(x) => x == 'f' ? new MockHttpRequest(CACHED_VALUE) : null; put(_,__) => null; }