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

feat(http): Expose HttpRequest.request in Http #40

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions lib/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@ part of angular;
// NOTE(deboer): This should be a generic utility class, but lets make sure
// it works in this case first!
class HttpFutures {
value(x) => new async.Future.value(x);
async.Future value(x) => new async.Future.value(x);
}

class UrlRewriter {
String call(url) => url;
}

class HttpBackend {
getString(String url, {bool withCredentials, void onProgress(dom.ProgressEvent e)}) {
return dom.HttpRequest.getString(url, withCredentials: withCredentials, onProgress: onProgress);
}
async.Future request(String url,
{String method, bool withCredentials, String responseType,
String mimeType, Map<String, String> requestHeaders, sendData,
void onProgress(dom.ProgressEvent e)}) =>
dom.HttpRequest.request(url,
method: method,
withCredentials: withCredentials,
responseType: responseType,
mimeType: mimeType,
requestHeaders: requestHeaders,
sendData: sendData,
onProgress: onProgress);
}

class Http {
Expand All @@ -24,7 +33,22 @@ class Http {

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

async.Future<String> getString(String rawUrl, {bool withCredentials, void onProgress(dom.ProgressEvent e), Cache cache}) {
async.Future<String> getString(String url,
{bool withCredentials, void onProgress(ProgressEvent e), Cache cache}) {
return request(url,
withCredentials: withCredentials,
onProgress: onProgress,
cache: cache).then((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,
{String method, bool withCredentials, String responseType,
String mimeType, Map<String, String> requestHeaders, sendData,
void onProgress(dom.ProgressEvent e),
Cache cache}) {
String url = rewriter(rawUrl);

// We return a pending request only if caching is enabled.
Expand All @@ -35,7 +59,14 @@ class Http {
if (cachedValue != null) {
return futures.value(cachedValue);
}
var result = backend.getString(url, withCredentials: withCredentials, onProgress: onProgress).then((value) {
var result = backend.request(url,
method: method,
withCredentials: withCredentials,
responseType: responseType,
mimeType: mimeType,
requestHeaders: requestHeaders,
sendData: sendData,
onProgress: onProgress).then((value) {
if (cache != null) {
cache.put(url, value);
}
Expand All @@ -49,3 +80,4 @@ class Http {
return result;
}
}

17 changes: 13 additions & 4 deletions test/_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ library ng_mock_http;


import 'dart:async';
import 'dart:html';
import 'package:angular/angular.dart';
import '_specs.dart';

class MockHttp extends Http {
Map<String, MockHttpData> gets = {};
Expand Down Expand Up @@ -62,6 +61,13 @@ 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<String, MockHttpData> gets = {};
List completersAndValues = [];
Expand All @@ -81,14 +87,17 @@ class MockHttpBackend extends HttpBackend {
}
}

getString(String url, {bool withCredentials, void onProgress(ProgressEvent e)}) {
Future<HttpRequest> request(String url,
{String method, bool withCredentials, String responseType,
String mimeType, Map<String, String> requestHeaders, sendData,
void onProgress(ProgressEvent e)}) {
if (!gets.containsKey(url)) throw "Unexpected URL $url $gets";
var data = gets[url];
data.times--;
if (data.times <= 0) {
gets.remove(url);
}
var expectedValue = data.value;
var expectedValue = new MockHttpRequest(data.value);
var completer = new Completer.sync();
completersAndValues.add([completer, expectedValue]);
return completer.future;
Expand Down
2 changes: 1 addition & 1 deletion test/http_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var CACHED_VALUE = 'cached_value';


class FakeCache implements Cache {
get(x) => x == 'f' ? CACHED_VALUE : null;
get(x) => x == 'f' ? new MockHttpRequest(CACHED_VALUE) : null;
put(_,__) => null;

}
Expand Down