Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit b68964f

Browse files
committed
feat(http): Added a HttpResponse object
1 parent cdcb35f commit b68964f

8 files changed

+32
-40
lines changed

lib/block.dart

+3-2
Original file line numberDiff line numberDiff line change
@@ -380,15 +380,16 @@ class BlockCache {
380380
/**
381381
* A convinience wrapper for "templates" cache.
382382
*/
383-
class TemplateCache implements Cache {
383+
class TemplateCache implements Cache<HttpResponse> {
384384
Cache _cache;
385385

386386
TemplateCache(CacheFactory $cacheFactory) {
387387
_cache = $cacheFactory('templates');
388388
}
389389

390390
Object get(key) => _cache.get(key);
391-
Object put(key, Object value) => _cache.put(key, value);
391+
put(key, HttpResponse value) => _cache.put(key, value);
392+
putString(key, String value) => _cache.put(key, new HttpResponse(200, value));
392393
void remove(key) => _cache.remove(key);
393394
void removeAll() => _cache.removeAll();
394395
CacheInfo info() => _cache.info();

lib/cache.dart

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ part of angular;
44
* A simple map-backed cache.
55
* TODO(pavelgj): add LRU support.
66
*/
7-
class Cache {
7+
class Cache<T> {
88
final String _id;
99
Map<String, Object> _data = <String, Object>{};
1010
CacheFactory _factory;
1111

1212
Cache._newCache(String this._id, CacheFactory this._factory);
1313

14-
Object get(key) {
14+
T get(String key) {
1515
_checkIfDestroyed();
1616
key = _stringifyKey(key);
1717
return _data[key];
1818
}
1919

20-
Object put(key, Object value) {
20+
T put(String key, T value) {
2121
_checkIfDestroyed();
2222
if (value == null) {
2323
return null;
@@ -26,7 +26,7 @@ class Cache {
2626
return _data[key] = value;
2727
}
2828

29-
void remove(key) {
29+
void remove(String key) {
3030
_checkIfDestroyed();
3131
key = _stringifyKey(key);
3232
_data.remove(key);
@@ -59,19 +59,19 @@ class Cache {
5959
}
6060
}
6161

62-
class CacheFactory {
62+
class CacheFactory<T> {
6363
Map<String, Cache> _cacheMap = <String, Cache>{};
6464

65-
Cache call(String cacheId) {
65+
Cache<T> call(String cacheId) {
6666
var cache = _cacheMap[cacheId];
6767
if (cache != null) {
6868
throw "[\$cacheFactory:iid] CacheId '$cacheId' is already taken!";
6969
}
70-
_cacheMap[cacheId] = cache = new Cache._newCache(cacheId, this);
70+
_cacheMap[cacheId] = cache = new Cache<T>._newCache(cacheId, this);
7171
return cache;
7272
}
7373

74-
Cache get(String cacheId) {
74+
Cache<T> get(String cacheId) {
7575
return _cacheMap[cacheId];
7676
}
7777

@@ -95,4 +95,4 @@ class CacheInfo {
9595
}
9696

9797
String toString() => '{size: $size, id: $id}';
98-
}
98+
}

lib/http.dart

+15-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ class HttpBackend {
2525
onProgress: onProgress);
2626
}
2727

28+
class HttpResponse {
29+
int status;
30+
String responseText;
31+
Map<String, String> headers;
32+
HttpResponse([this.status, this.responseText, this.headers]);
33+
}
34+
2835
class Http {
2936
Map<String, async.Future<String>> _pendingRequests = <String, async.Future<String>>{};
3037
UrlRewriter rewriter;
@@ -38,17 +45,17 @@ class Http {
3845
return request(url,
3946
withCredentials: withCredentials,
4047
onProgress: onProgress,
41-
cache: cache).then((xhr) => xhr.responseText);
48+
cache: cache).then((HttpResponse xhr) => xhr.responseText);
4249
}
4350

4451
// TODO(deboer): The cache is keyed on the url only. It should be keyed on
4552
// (url, method, mimeType, requestHeaders, ...)
4653
// Better yet, we should be using a HTTP standard cache.
47-
async.Future request(String rawUrl,
54+
async.Future<HttpResponse> request(String rawUrl,
4855
{String method, bool withCredentials, String responseType,
4956
String mimeType, Map<String, String> requestHeaders, sendData,
5057
void onProgress(dom.ProgressEvent e),
51-
Cache cache}) {
58+
Cache<HttpResponse> cache}) {
5259
String url = rewriter(rawUrl);
5360

5461
// We return a pending request only if caching is enabled.
@@ -67,11 +74,14 @@ class Http {
6774
requestHeaders: requestHeaders,
6875
sendData: sendData,
6976
onProgress: onProgress).then((value) {
77+
// NOTE(deboer): Missing headers. Ask the Dart team for a sane API.
78+
var response = new HttpResponse(value.status, value.responseText);
79+
7080
if (cache != null) {
71-
cache.put(url, value);
81+
cache.put(url, response);
7282
}
7383
_pendingRequests.remove(url);
74-
return value;
84+
return response;
7585
}, onError: (error) {
7686
_pendingRequests.remove(url);
7787
throw error;

test/_http.dart

+2-9
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MockHttp extends Http {
3131
}
3232
var expectedValue = data.value;
3333
if (cache != null) {
34-
cache.put(url, expectedValue);
34+
cache.put(url, new HttpResponse(200, expectedValue));
3535
}
3636
var future = new Future.value(expectedValue);
3737
futures.add(future);
@@ -61,13 +61,6 @@ class MockHttpFutures extends HttpFutures {
6161
}
6262
}
6363

64-
// NOTE(deboer): If I try to extend HttpRequest, I get an error:
65-
// class 'MockHttpRequest' is trying to extend a native fields class, but library '_http.dart' has no native resolvers
66-
class MockHttpRequest /*extends HttpRequest*/ {
67-
String responseText;
68-
MockHttpRequest(this.responseText);
69-
}
70-
7164
class MockHttpBackend extends HttpBackend {
7265
Map<String, MockHttpData> gets = {};
7366
List completersAndValues = [];
@@ -97,7 +90,7 @@ class MockHttpBackend extends HttpBackend {
9790
if (data.times <= 0) {
9891
gets.remove(url);
9992
}
100-
var expectedValue = new MockHttpRequest(data.value);
93+
var expectedValue = new HttpResponse(200, data.value);
10194
var completer = new Completer.sync();
10295
completersAndValues.add([completer, expectedValue]);
10396
return completer.future;

test/_http_spec.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ main() {
4242
http.getString('request', cache: cache).then(expectAsync1((data) {
4343
expect(data).toEqual('response');
4444
expect(cache.info().size).toEqual(1);
45-
expect(cache.get('request')).toEqual('response');
45+
expect(cache.get('request').responseText).toEqual('response');
4646
}));
4747
}));
4848

test/cache_spec.dart

-13
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,6 @@ main() => describe('CacheFactory', () {
9393
expect(cache.remove('non-existent')).toBeNull();
9494
}));
9595

96-
97-
it('should stringify keys', inject((CacheFactory $cacheFactory) {
98-
cache.put('123', 'foo');
99-
cache.put(123, 'bar');
100-
101-
expect(cache.get('123')).toBe('bar');
102-
expect(cache.info().size).toBe(1);
103-
104-
cache.remove(123);
105-
expect(cache.info().size).toBe(0);
106-
}));
107-
108-
10996
it("should return value from put", inject((CacheFactory $cacheFactory) {
11097
var obj = {};
11198
expect(cache.put('k1', obj)).toBe(obj);

test/http_spec.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var CACHED_VALUE = 'cached_value';
66

77

88
class FakeCache implements Cache {
9-
get(x) => x == 'f' ? new MockHttpRequest(CACHED_VALUE) : null;
9+
get(x) => x == 'f' ? new HttpResponse(200, CACHED_VALUE) : null;
1010
put(_,__) => null;
1111

1212
}

test/templateurl_spec.dart

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ main() {
5555
$compile(element)(injector, element);
5656

5757
backend.flush();
58+
backend.assertAllGetsCalled();
5859

5960
expect(renderedText(element)).toEqual('@import "PREFIX:simple.css"Simple!');
6061
expect(element[0].nodes[0].shadowRoot.innerHtml).toEqual(

0 commit comments

Comments
 (0)