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

Commit 595ffdf

Browse files
committed
Fixed http service to work correctly with cache
1 parent 08b32d2 commit 595ffdf

File tree

4 files changed

+63
-24
lines changed

4 files changed

+63
-24
lines changed

lib/http.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
part of angular;
22

33
class Http {
4+
Map<String, async.Future<String>> _pendingRequests = <String, async.Future<String>>{};
5+
46
async.Future<String> getString(String url, {bool withCredentials, void onProgress(dom.ProgressEvent e), Cache cache}) {
7+
// We return a pending request only if caching is enabled.
8+
if (cache != null && _pendingRequests.containsKey(url)) {
9+
return _pendingRequests[url];
10+
}
511
var cachedValue = cache != null ? cache.get(url) : null;
612
if (cachedValue != null) {
713
return new async.Future.value(cachedValue);
814
}
9-
return dom.HttpRequest.getString(url, withCredentials: withCredentials, onProgress: onProgress).then((value) {
15+
var result = dom.HttpRequest.getString(url, withCredentials: withCredentials, onProgress: onProgress).then((value) {
1016
if (cache != null) {
1117
cache.put(url, value);
1218
}
19+
_pendingRequests.remove(url);
1320
return value;
21+
}, onError: (error) {
22+
_pendingRequests.remove(url);
23+
throw error;
1424
});
25+
_pendingRequests[url] = result;
26+
return result;
1527
}
1628
}

test/_http.dart

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,29 @@ import 'dart:html';
66
import 'package:angular/angular.dart';
77

88
class MockHttp extends Http {
9-
Map<String, String> gets = {};
9+
Map<String, MockHttpData> gets = {};
1010
List futures = [];
1111

12-
expectGET(url, content) {
13-
gets[url] = content;
12+
expectGET(String url, String content, {int times: 1}) {
13+
gets[url] = new MockHttpData(content, times);
1414
}
1515

16-
flush() => gets.length == 0 ? Future.wait(futures) :
16+
flush() => Future.wait(futures);
17+
18+
assertAllGetsCalled() {
19+
if (gets.length != 0) {
1720
throw "Expected GETs not called $gets";
21+
}
22+
}
1823

1924
Future<String> getString(String url, {bool withCredentials, void onProgress(ProgressEvent e), Cache cache}) {
20-
var cachedValue = cache != null ? cache.get(url) : null;
21-
if (cachedValue != null) {
22-
return new Future.value(cachedValue);
25+
if (!gets.containsKey(url)) throw "Unexpected URL $url $gets";
26+
var data = gets[url];
27+
data.times--;
28+
if (data.times <= 0) {
29+
gets.remove(url);
2330
}
24-
25-
if (!gets.containsKey(url)) throw "Unexpected URL $url";
26-
var expectedValue = gets.remove(url);
31+
var expectedValue = data.value;
2732
if (cache != null) {
2833
cache.put(url, expectedValue);
2934
}
@@ -33,4 +38,12 @@ class MockHttp extends Http {
3338
}
3439
}
3540

41+
class MockHttpData {
42+
String value;
43+
int times;
44+
MockHttpData(this.value, this.times);
45+
46+
toString() => value;
47+
}
48+
3649
main() {}

test/_http_spec.dart

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@ main() {
1414
expect(data).toEqual('response');
1515
}));
1616
});
17+
18+
it('should replay an http request which is expected multiple times', () {
19+
http.expectGET('request', 'response', times: 2);
20+
http.getString('request').then(expectAsync1((data) {
21+
expect(http.gets.length).toEqual(1);
22+
expect(data).toEqual('response');
23+
http.getString('request').then(expectAsync1((data) {
24+
expect(http.gets.length).toEqual(0);
25+
expect(data).toEqual('response');
26+
}));
27+
}));
28+
});
29+
30+
it('should throw an exeception on assertAllGetsCalled when not all expected GETs were called', () {
31+
http.expectGET('request', 'response', times: 2);
32+
http.getString('request').then(expectAsync1((data) {
33+
expect(() {
34+
http.assertAllGetsCalled();
35+
}).toThrow('Expected GETs not called {request: response}');
36+
}));
37+
});
1738

1839
it('should cache results', inject((CacheFactory $cacheFactory) {
1940
http.expectGET('request', 'response');
@@ -25,18 +46,6 @@ main() {
2546
}));
2647
}));
2748

28-
it('should return cached results', inject((CacheFactory $cacheFactory) {
29-
http.expectGET('request', 'response');
30-
Cache cache = $cacheFactory('test');
31-
http.getString('request', cache: cache).then(expectAsync1((data) {
32-
expect(data).toEqual('response');
33-
34-
http.getString('request', cache: cache).then(expectAsync1((data) {
35-
expect(data).toEqual('response');
36-
}));
37-
}));
38-
}));
39-
4049
it('should barf on an unseen request', () {
4150
expect(() {
4251
http.getString('unknown');
@@ -47,6 +56,7 @@ main() {
4756
http.expectGET('request', 'response');
4857
expect(() {
4958
http.flush();
59+
http.assertAllGetsCalled();
5060
}).toThrow('Expected GETs not called {request: response}');
5161
});
5262
});

test/templateurl_spec.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ main() {
4444
module.directive(OnlyCssComponent);
4545
module.directive(InlineWithCssComponent);
4646
}));
47+
48+
afterEach(inject((MockHttp $http) {
49+
$http.assertAllGetsCalled();
50+
}));
4751

4852
it('should replace element with template from url', inject((MockHttp $http, Compiler $compile, Scope $rootScope, Log log, Injector injector) {
4953
$http.expectGET('simple.html', '<div log="SIMPLE">Simple!</div>');
@@ -59,7 +63,7 @@ main() {
5963
}));
6064

6165
it('should load template from URL once', inject((MockHttp $http, Compiler $compile, Scope $rootScope, Log log, Injector injector) {
62-
$http.expectGET('simple.html', '<div log="SIMPLE">Simple!</div>');
66+
$http.expectGET('simple.html', '<div log="SIMPLE">Simple!</div>', times: 2);
6367

6468
var element = $('<div><simple-url log>ignore</simple-url><simple-url log>ignore</simple-url><div>');
6569
$compile(element)(injector, element);

0 commit comments

Comments
 (0)