diff --git a/lib/block.dart b/lib/block.dart index 008e78519..a161f720d 100644 --- a/lib/block.dart +++ b/lib/block.dart @@ -380,7 +380,7 @@ class BlockCache { /** * A convinience wrapper for "templates" cache. */ -class TemplateCache implements Cache { +class TemplateCache implements Cache { Cache _cache; TemplateCache(CacheFactory $cacheFactory) { @@ -388,7 +388,8 @@ class TemplateCache implements Cache { } Object get(key) => _cache.get(key); - Object put(key, Object value) => _cache.put(key, value); + put(key, HttpResponse value) => _cache.put(key, value); + putString(key, String value) => _cache.put(key, new HttpResponse(200, value)); void remove(key) => _cache.remove(key); void removeAll() => _cache.removeAll(); CacheInfo info() => _cache.info(); diff --git a/lib/cache.dart b/lib/cache.dart index d360e3be2..f77444cfd 100644 --- a/lib/cache.dart +++ b/lib/cache.dart @@ -4,20 +4,20 @@ part of angular; * A simple map-backed cache. * TODO(pavelgj): add LRU support. */ -class Cache { +class Cache { final String _id; Map _data = {}; CacheFactory _factory; Cache._newCache(String this._id, CacheFactory this._factory); - Object get(key) { + T get(String key) { _checkIfDestroyed(); key = _stringifyKey(key); return _data[key]; } - Object put(key, Object value) { + T put(String key, T value) { _checkIfDestroyed(); if (value == null) { return null; @@ -26,7 +26,7 @@ class Cache { return _data[key] = value; } - void remove(key) { + void remove(String key) { _checkIfDestroyed(); key = _stringifyKey(key); _data.remove(key); @@ -59,19 +59,19 @@ class Cache { } } -class CacheFactory { +class CacheFactory { Map _cacheMap = {}; - Cache call(String cacheId) { + Cache call(String cacheId) { var cache = _cacheMap[cacheId]; if (cache != null) { throw "[\$cacheFactory:iid] CacheId '$cacheId' is already taken!"; } - _cacheMap[cacheId] = cache = new Cache._newCache(cacheId, this); + _cacheMap[cacheId] = cache = new Cache._newCache(cacheId, this); return cache; } - Cache get(String cacheId) { + Cache get(String cacheId) { return _cacheMap[cacheId]; } @@ -95,4 +95,4 @@ class CacheInfo { } String toString() => '{size: $size, id: $id}'; -} \ No newline at end of file +} diff --git a/lib/http.dart b/lib/http.dart index f9d6801c0..c3d814a2a 100644 --- a/lib/http.dart +++ b/lib/http.dart @@ -25,6 +25,13 @@ class HttpBackend { onProgress: onProgress); } +class HttpResponse { + int status; + String responseText; + Map headers; + HttpResponse([this.status, this.responseText, this.headers]); +} + class Http { Map> _pendingRequests = >{}; UrlRewriter rewriter; @@ -38,17 +45,17 @@ class Http { return request(url, withCredentials: withCredentials, onProgress: onProgress, - cache: cache).then((xhr) => xhr.responseText); + cache: cache).then((HttpResponse 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, + async.Future request(String rawUrl, {String method, bool withCredentials, String responseType, String mimeType, Map requestHeaders, sendData, void onProgress(dom.ProgressEvent e), - Cache cache}) { + Cache cache}) { String url = rewriter(rawUrl); // We return a pending request only if caching is enabled. @@ -67,11 +74,14 @@ class Http { requestHeaders: requestHeaders, sendData: sendData, onProgress: onProgress).then((value) { + // NOTE(deboer): Missing headers. Ask the Dart team for a sane API. + var response = new HttpResponse(value.status, value.responseText); + if (cache != null) { - cache.put(url, value); + cache.put(url, response); } _pendingRequests.remove(url); - return value; + return response; }, onError: (error) { _pendingRequests.remove(url); throw error; diff --git a/test/_http.dart b/test/_http.dart index a6d6322df..bfafded04 100644 --- a/test/_http.dart +++ b/test/_http.dart @@ -31,7 +31,7 @@ class MockHttp extends Http { } var expectedValue = data.value; if (cache != null) { - cache.put(url, expectedValue); + cache.put(url, new HttpResponse(200, expectedValue)); } var future = new Future.value(expectedValue); futures.add(future); @@ -61,13 +61,6 @@ 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 = []; @@ -97,7 +90,7 @@ class MockHttpBackend extends HttpBackend { if (data.times <= 0) { gets.remove(url); } - var expectedValue = new MockHttpRequest(data.value); + var expectedValue = new HttpResponse(200, 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 301402790..3bb164cb5 100644 --- a/test/_http_spec.dart +++ b/test/_http_spec.dart @@ -42,7 +42,7 @@ main() { http.getString('request', cache: cache).then(expectAsync1((data) { expect(data).toEqual('response'); expect(cache.info().size).toEqual(1); - expect(cache.get('request')).toEqual('response'); + expect(cache.get('request').responseText).toEqual('response'); })); })); diff --git a/test/cache_spec.dart b/test/cache_spec.dart index 3cead14b5..95fd8e261 100644 --- a/test/cache_spec.dart +++ b/test/cache_spec.dart @@ -93,19 +93,6 @@ main() => describe('CacheFactory', () { expect(cache.remove('non-existent')).toBeNull(); })); - - it('should stringify keys', inject((CacheFactory $cacheFactory) { - cache.put('123', 'foo'); - cache.put(123, 'bar'); - - expect(cache.get('123')).toBe('bar'); - expect(cache.info().size).toBe(1); - - cache.remove(123); - expect(cache.info().size).toBe(0); - })); - - it("should return value from put", inject((CacheFactory $cacheFactory) { var obj = {}; expect(cache.put('k1', obj)).toBe(obj); diff --git a/test/http_spec.dart b/test/http_spec.dart index 9ecb5a225..288a29cf7 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' ? new MockHttpRequest(CACHED_VALUE) : null; + get(x) => x == 'f' ? new HttpResponse(200, CACHED_VALUE) : null; put(_,__) => null; } diff --git a/test/templateurl_spec.dart b/test/templateurl_spec.dart index 4d8101e3a..2dd748e1f 100644 --- a/test/templateurl_spec.dart +++ b/test/templateurl_spec.dart @@ -55,6 +55,7 @@ main() { $compile(element)(injector, element); backend.flush(); + backend.assertAllGetsCalled(); expect(renderedText(element)).toEqual('@import "PREFIX:simple.css"Simple!'); expect(element[0].nodes[0].shadowRoot.innerHtml).toEqual(