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

Commit 4aec5f3

Browse files
committed
feat(http): Added a testable wrapper around dart:html's HttpRequest
1 parent 4454500 commit 4aec5f3

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

lib/angular.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
library angular;
22

33
import "dart:mirrors";
4+
import "dart:async" as async;
45
import "dart:json" as json;
56
import 'dart:html' as dom;
67
import 'package:di/di.dart';
@@ -16,6 +17,7 @@ part 'directives/ng_mustache.dart';
1617
part 'directives/ng_repeat.dart';
1718
part 'dom_utilities.dart';
1819
part 'exception_handler.dart';
20+
part 'http.dart';
1921
part 'interpolate.dart';
2022
part 'mirrors.dart';
2123
part 'node_cursor.dart';

lib/http.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
part of angular;
2+
3+
class Http {
4+
async.Future<String> getString(String url, {bool withCredentials, void onProgress(dom.ProgressEvent e)}) =>
5+
dom.HttpRequest.getString(url, withCredentials, onProgress);
6+
}

test/_http.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
library ng_mock_http;
2+
3+
4+
import 'dart:async';
5+
import 'dart:html';
6+
import 'package:angular/angular.dart';
7+
8+
class MockHttp extends Http {
9+
Map<String, String> gets = {};
10+
expectGET(url, content) {
11+
gets[url] = content;
12+
}
13+
14+
Future<String> getString(String url, {bool withCredentials, void onProgress(ProgressEvent e)}) {
15+
if (!gets.containsKey(url)) throw "Unexpected URL $url";
16+
return new Future.value(gets.remove(url));
17+
}
18+
}
19+
20+
main() {}

test/_http_spec.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import "_specs.dart";
2+
import "_http.dart";
3+
4+
main() {
5+
describe("MockHttp", () {
6+
MockHttp http;
7+
beforeEach(() {
8+
http = new MockHttp();
9+
});
10+
11+
it('should replay an http request', () {
12+
http.expectGET('request', 'response');
13+
http.getString('request').then(expectAsync1((data) {
14+
expect(data).toEqual('response');
15+
}));
16+
});
17+
18+
it('should barf on an unseen request', () {
19+
expect(() {
20+
http.getString('unknown');
21+
}).toThrow('Unexpected URL unknown');
22+
});
23+
});
24+
}

0 commit comments

Comments
 (0)