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

Commit af7151e

Browse files
committed
feat(http): Expose HttpRequest.request in Http
1 parent 71f8bb5 commit af7151e

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

lib/http.dart

+38-6
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@ part of angular;
33
// NOTE(deboer): This should be a generic utility class, but lets make sure
44
// it works in this case first!
55
class HttpFutures {
6-
value(x) => new async.Future.value(x);
6+
async.Future value(x) => new async.Future.value(x);
77
}
88

99
class UrlRewriter {
1010
String call(url) => url;
1111
}
1212

1313
class HttpBackend {
14-
getString(String url, {bool withCredentials, void onProgress(dom.ProgressEvent e)}) {
15-
return dom.HttpRequest.getString(url, withCredentials: withCredentials, onProgress: onProgress);
16-
}
14+
async.Future request(String url,
15+
{String method, bool withCredentials, String responseType,
16+
String mimeType, Map<String, String> requestHeaders, sendData,
17+
void onProgress(dom.ProgressEvent e)}) =>
18+
dom.HttpRequest.request(url,
19+
method: method,
20+
withCredentials: withCredentials,
21+
responseType: responseType,
22+
mimeType: mimeType,
23+
requestHeaders: requestHeaders,
24+
sendData: sendData,
25+
onProgress: onProgress);
1726
}
1827

1928
class Http {
@@ -24,7 +33,22 @@ class Http {
2433

2534
Http(UrlRewriter this.rewriter, HttpBackend this.backend, HttpFutures this.futures);
2635

27-
async.Future<String> getString(String rawUrl, {bool withCredentials, void onProgress(dom.ProgressEvent e), Cache cache}) {
36+
async.Future<String> getString(String url,
37+
{bool withCredentials, void onProgress(ProgressEvent e), Cache cache}) {
38+
return request(url,
39+
withCredentials: withCredentials,
40+
onProgress: onProgress,
41+
cache: cache).then((xhr) => xhr.responseText);
42+
}
43+
44+
// TODO(deboer): The cache is keyed on the url only. It should be keyed on
45+
// (url, method, mimeType, requestHeaders, ...)
46+
// Better yet, we should be using a HTTP standard cache.
47+
async.Future request(String rawUrl,
48+
{String method, bool withCredentials, String responseType,
49+
String mimeType, Map<String, String> requestHeaders, sendData,
50+
void onProgress(dom.ProgressEvent e),
51+
Cache cache}) {
2852
String url = rewriter(rawUrl);
2953

3054
// We return a pending request only if caching is enabled.
@@ -35,7 +59,14 @@ class Http {
3559
if (cachedValue != null) {
3660
return futures.value(cachedValue);
3761
}
38-
var result = backend.getString(url, withCredentials: withCredentials, onProgress: onProgress).then((value) {
62+
var result = backend.request(url,
63+
method: method,
64+
withCredentials: withCredentials,
65+
responseType: responseType,
66+
mimeType: mimeType,
67+
requestHeaders: requestHeaders,
68+
sendData: sendData,
69+
onProgress: onProgress).then((value) {
3970
if (cache != null) {
4071
cache.put(url, value);
4172
}
@@ -49,3 +80,4 @@ class Http {
4980
return result;
5081
}
5182
}
83+

test/_http.dart

+13-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ library ng_mock_http;
22

33

44
import 'dart:async';
5-
import 'dart:html';
6-
import 'package:angular/angular.dart';
5+
import '_specs.dart';
76

87
class MockHttp extends Http {
98
Map<String, MockHttpData> gets = {};
@@ -62,6 +61,13 @@ class MockHttpFutures extends HttpFutures {
6261
}
6362
}
6463

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+
6571
class MockHttpBackend extends HttpBackend {
6672
Map<String, MockHttpData> gets = {};
6773
List completersAndValues = [];
@@ -81,14 +87,17 @@ class MockHttpBackend extends HttpBackend {
8187
}
8288
}
8389

84-
getString(String url, {bool withCredentials, void onProgress(ProgressEvent e)}) {
90+
Future<HttpRequest> request(String url,
91+
{String method, bool withCredentials, String responseType,
92+
String mimeType, Map<String, String> requestHeaders, sendData,
93+
void onProgress(ProgressEvent e)}) {
8594
if (!gets.containsKey(url)) throw "Unexpected URL $url $gets";
8695
var data = gets[url];
8796
data.times--;
8897
if (data.times <= 0) {
8998
gets.remove(url);
9099
}
91-
var expectedValue = data.value;
100+
var expectedValue = new MockHttpRequest(data.value);
92101
var completer = new Completer.sync();
93102
completersAndValues.add([completer, expectedValue]);
94103
return completer.future;

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' ? CACHED_VALUE : null;
9+
get(x) => x == 'f' ? new MockHttpRequest(CACHED_VALUE) : null;
1010
put(_,__) => null;
1111

1212
}

0 commit comments

Comments
 (0)