diff --git a/lib/core_dom/http.dart b/lib/core_dom/http.dart index 74126e594..fdae907e8 100644 --- a/lib/core_dom/http.dart +++ b/lib/core_dom/http.dart @@ -387,16 +387,6 @@ class Http { Http(this._cookies, this._location, this._rewriter, this._backend, this.defaults, this._interceptors); - /** - * DEPRECATED - */ - async.Future getString(String url, {bool withCredentials, - void onProgress(dom.ProgressEvent e), Cache cache}) => - request(url, - withCredentials: withCredentials, - onProgress: onProgress, - cache: cache).then((HttpResponse xhr) => xhr.responseText); - /** * Parse a [requestUrl] and determine whether this is a same-origin request as * the application document. @@ -442,6 +432,7 @@ class Http { throw ['timeout not implemented']; } + url = _rewriter(url); method = method.toUpperCase(); if (headers == null) headers = {}; @@ -461,8 +452,7 @@ class Http { }); var serverRequest = (HttpResponseConfig config) { - assert(config.data == null || config.data is String || - config.data is dom.File); + assert(config.data == null || config.data is String || config.data is dom.File); // Strip content-type if data is undefined if (config.data == null) { @@ -471,12 +461,45 @@ class Http { .forEach((h) => headers.remove(h)); } - return request(null, - config: config, - method: method, - sendData: config.data, - requestHeaders: config.headers, - cache: cache); + url = _buildUrl(config.url, config.params); + + if (cache == false) { + cache = null; + } else if (cache == null) { + cache = defaults.cache; + } + + // We return a pending request only if caching is enabled. + if (cache != null && _pendingRequests.containsKey(url)) { + return _pendingRequests[url]; + } + var cachedResponse = (cache != null && method == 'GET') ? cache.get(url) : null; + if (cachedResponse != null) { + return new async.Future.value(new HttpResponse.copy(cachedResponse)); + } + + var result = _backend.request(url, + method: method, + requestHeaders: config.headers, + sendData: config.data).then((dom.HttpRequest value) { + // TODO: Uncomment after apps migrate off of this class. + // assert(value.status >= 200 && value.status < 300); + + var response = new HttpResponse(value.status, value.responseText, + parseHeaders(value), config); + + if (cache != null) cache.put(url, response); + _pendingRequests.remove(url); + return response; + }, onError: (error) { + if (error is! dom.ProgressEvent) throw error; + dom.ProgressEvent event = error; + _pendingRequests.remove(url); + dom.HttpRequest request = event.currentTarget; + return new async.Future.error( + new HttpResponse(request.status, request.response, parseHeaders(request), config)); + }); + return _pendingRequests[url] = result; }; var chain = [[serverRequest, null]]; @@ -632,7 +655,6 @@ class Http { }); return parsed; } - /** * Returns an [Iterable] of [Future] [HttpResponse]s for the requests * that the [Http] service is currently waiting for. @@ -640,73 +662,6 @@ class Http { Iterable > get pendingRequests => _pendingRequests.values; - /** - * DEPRECATED - */ - async.Future request(String rawUrl, - { HttpResponseConfig config, - String method: 'GET', - bool withCredentials: false, - String responseType, - String mimeType, - Map requestHeaders, - sendData, - void onProgress(dom.ProgressEvent e), - /*Cache or false*/ cache }) { - String url; - - if (config == null) { - url = _rewriter(rawUrl); - config = new HttpResponseConfig(url: url); - } else { - url = _buildUrl(config.url, config.params); - } - - if (cache == false) { - cache = null; - } else if (cache == null) { - cache = defaults.cache; - } - // We return a pending request only if caching is enabled. - if (cache != null && _pendingRequests.containsKey(url)) { - return _pendingRequests[url]; - } - var cachedResponse = (cache != null && method == 'GET') - ? cache.get(url) - : null; - if (cachedResponse != null) { - return new async.Future.value(new HttpResponse.copy(cachedResponse)); - } - - var result = _backend.request(url, - method: method, - withCredentials: withCredentials, - responseType: responseType, - mimeType: mimeType, - requestHeaders: requestHeaders, - sendData: sendData, - onProgress: onProgress).then((dom.HttpRequest value) { - // TODO: Uncomment after apps migrate off of this class. - // assert(value.status >= 200 && value.status < 300); - - var response = new HttpResponse(value.status, value.responseText, - parseHeaders(value), config); - - if (cache != null) cache.put(url, response); - _pendingRequests.remove(url); - return response; - }, onError: (error) { - if (error is! dom.ProgressEvent) throw error; - dom.ProgressEvent event = error; - _pendingRequests.remove(url); - dom.HttpRequest request = event.currentTarget; - return new async.Future.error( - new HttpResponse(request.status, request.response, - parseHeaders(request), config)); - }); - return _pendingRequests[url] = result; - } - _buildUrl(String url, Map params) { if (params == null) return url; var parts = []; diff --git a/lib/core_dom/view_factory.dart b/lib/core_dom/view_factory.dart index 574975c47..4a8d2ed94 100644 --- a/lib/core_dom/view_factory.dart +++ b/lib/core_dom/view_factory.dart @@ -138,8 +138,8 @@ class ViewCache { } async.Future fromUrl(String url, DirectiveMap directives) { - return http.getString(url, cache: templateCache).then( - (html) => fromHtml(html, directives)); + return http.get(url, cache: templateCache).then( + (resp) => fromHtml(resp.responseText, directives)); } } @@ -182,8 +182,8 @@ class _ComponentFactory implements Function { var cssUrls = []..addAll(_baseCss.urls)..addAll(component.cssUrls); if (cssUrls.isNotEmpty) { cssUrls.forEach((css) => cssFutures.add(http - .getString(css, cache: templateCache) - .catchError((e) => '/*\n$e\n*/\n') + .get(css, cache: templateCache).then((resp) => resp.responseText, + onError: (e) => '/*\n$e\n*/\n') )); } else { cssFutures.add(new async.Future.value(null)); diff --git a/test/core/templateurl_spec.dart b/test/core/templateurl_spec.dart index e573f7c66..cf3933c26 100644 --- a/test/core/templateurl_spec.dart +++ b/test/core/templateurl_spec.dart @@ -90,11 +90,12 @@ void main() { it('should replace element with template from url', async(inject( (Http http, Compiler compile, Scope rootScope, Logger log, Injector injector, MockHttpBackend backend, DirectiveMap directives) { - backend.expectGET('simple.html').respond('
Simple!
'); + backend.expectGET('simple.html').respond(200, '
Simple!
'); var element = es('
ignore
'); compile(element, directives)(injector, element); + microLeap(); backend.flush(); microLeap(); @@ -107,7 +108,7 @@ void main() { it('should load template from URL once', async(inject( (Http http, Compiler compile, Scope rootScope, Logger log, Injector injector, MockHttpBackend backend, DirectiveMap directives) { - backend.whenGET('simple.html').respond('
Simple!
'); + backend.whenGET('simple.html').respond(200, '
Simple!
'); var element = es( '
' @@ -116,6 +117,7 @@ void main() { '
'); compile(element, directives)(injector, element); + microLeap(); backend.flush(); microLeap(); @@ -130,12 +132,13 @@ void main() { (Http http, Compiler compile, Scope rootScope, Logger log, Injector injector, MockHttpBackend backend, DirectiveMap directives) { backend - ..expectGET('simple.css').respond('.hello{}') - ..expectGET('simple.html').respond('
Simple!
'); + ..expectGET('simple.css').respond(200, '.hello{}') + ..expectGET('simple.html').respond(200, '
Simple!
'); var element = e('
ignore
'); compile([element], directives)(injector, [element]); + microLeap(); backend.flush(); microLeap(); @@ -152,9 +155,10 @@ void main() { (Http http, Compiler compile, Scope rootScope, Injector injector, MockHttpBackend backend, DirectiveMap directives) { var element = es('
ignore
'); - backend.expectGET('simple.css').respond('.hello{}'); + backend.expectGET('simple.css').respond(200, '.hello{}'); compile(element, directives)(injector, element); + microLeap(); backend.flush(); microLeap(); expect(element[0]).toHaveText('.hello{}inline!'); @@ -167,6 +171,7 @@ void main() { backend.expectGET('simple.css').respond(500, 'some error'); compile(element, directives)(injector, element); + microLeap(); backend.flush(); microLeap(); expect(element.first).toHaveText( @@ -180,9 +185,10 @@ void main() { (Http http, Compiler compile, Scope rootScope, Injector injector, MockHttpBackend backend, DirectiveMap directives) { var element = es('
ignore
'); - backend.expectGET('simple.css').respond('.hello{}'); + backend.expectGET('simple.css').respond(200, '.hello{}'); compile(element, directives)(injector, element); + microLeap(); backend.flush(); microLeap(); expect(element[0]).toHaveText('.hello{}'); @@ -192,12 +198,13 @@ void main() { (Http http, Compiler compile, Scope rootScope, Injector injector, MockHttpBackend backend, DirectiveMap directives) { backend - ..expectGET('simple.css').respond('.hello{}') - ..expectGET('simple.html').respond('
Simple!
'); + ..expectGET('simple.css').respond(200, '.hello{}') + ..expectGET('simple.html').respond(200, '
Simple!
'); var element = es('ignore'); compile(element, directives)(injector, element); + microLeap(); backend.flush(); microLeap(); expect(element.first).toHaveText('.hello{}Simple!'); @@ -215,13 +222,14 @@ void main() { (Http http, Compiler compile, Scope rootScope, Logger log, Injector injector, MockHttpBackend backend, DirectiveMap directives) { backend - ..expectGET('simple.css').respond('.hello{}') - ..expectGET('another.css').respond('.world{}') - ..expectGET('simple.html').respond('
Simple!
'); + ..expectGET('simple.css').respond(200, '.hello{}') + ..expectGET('another.css').respond(200, '.world{}') + ..expectGET('simple.html').respond(200, '
Simple!
'); var element = e('
ignore
'); compile([element], directives)(injector, [element]); + microLeap(); backend.flush(); microLeap(); diff --git a/test/core_dom/compiler_spec.dart b/test/core_dom/compiler_spec.dart index d4874c3c5..346b4ab22 100644 --- a/test/core_dom/compiler_spec.dart +++ b/test/core_dom/compiler_spec.dart @@ -480,7 +480,7 @@ void main() { }); it('should fire onTemplate method', async((Compiler compile, Logger logger, MockHttpBackend backend) { - backend.whenGET('some/template.url').respond('
WORKED
'); + backend.whenGET('some/template.url').respond(200, '
WORKED
'); var scope = _.rootScope.createChild({}); scope.context['isReady'] = 'ready'; scope.context['logger'] = logger; @@ -503,6 +503,7 @@ void main() { expect(logger).toEqual(expected); logger.clear(); + microLeap(); backend.flush(); microLeap(); expect(logger).toEqual(['templateLoaded', _.rootScope.context['shadowRoot']]); diff --git a/test/core_dom/http_spec.dart b/test/core_dom/http_spec.dart index 7db14adcf..333076559 100644 --- a/test/core_dom/http_spec.dart +++ b/test/core_dom/http_spec.dart @@ -940,8 +940,8 @@ void main() { var called = 0; zone.run(() { - http.getString('a[not sent to backed]').then((v) { - expect(v).toEqual(VALUE); + http.get('a[not sent to backed]').then((v) { + expect(v.responseText).toEqual(VALUE); called += 1; }); }); @@ -959,12 +959,12 @@ void main() { var called = 0; zone.run(() { - http.getString('a[some string]', cache: cache).then((v) { - expect(v).toEqual(VALUE); + http.get('a[some string]', cache: cache).then((v) { + expect(v.responseText).toEqual(VALUE); called += 1; }); - http.getString('a[different string]', cache: cache).then((v) { - expect(v).toEqual(VALUE); + http.get('a[different string]', cache: cache).then((v) { + expect(v.responseText).toEqual(VALUE); called += 10; }); }); @@ -979,8 +979,8 @@ void main() { it('should support caching', async((Http http, VmTurnZone zone) { var called = 0; zone.run(() { - http.getString('fromCache', cache: cache).then((v) { - expect(v).toEqual(CACHED_VALUE); + http.get('fromCache', cache: cache).then((v) { + expect(v.responseText).toEqual(CACHED_VALUE); called += 1; }); }); @@ -995,12 +995,12 @@ void main() { var called = 0; zone.run(() { - http.getString('a').then((v) { - expect(v).toEqual(VALUE); + http.get('a').then((v) { + expect(v.responseText).toEqual(VALUE); called += 1; }); - http.getString('a').then((v) { - expect(v).toEqual(VALUE); + http.get('a').then((v) { + expect(v.responseText).toEqual(VALUE); called += 10; }); }); @@ -1018,12 +1018,12 @@ void main() { var called = 0; zone.run(() { - http.getString('a', cache: cache).then((v) { - expect(v).toEqual(VALUE); + http.get('a', cache: cache).then((v) { + expect(v.responseText).toEqual(VALUE); called += 1; }); - http.getString('a', cache: cache).then((v) { - expect(v).toEqual(VALUE); + http.get('a', cache: cache).then((v) { + expect(v.responseText).toEqual(VALUE); called += 10; }); }); @@ -1040,8 +1040,8 @@ void main() { var called = 0; zone.run(() { - http.getString('a', cache: cache).then((v) { - expect(v).toEqual(VALUE); + http.get('a', cache: cache).then((v) { + expect(v.responseText).toEqual(VALUE); called += 1; }); }); @@ -1050,8 +1050,8 @@ void main() { flush(); zone.run(() { - http.getString('a', cache: cache).then((v) { - expect(v).toEqual(VALUE); + http.get('a', cache: cache).then((v) { + expect(v.responseText).toEqual(VALUE); called += 10; }); }); @@ -1067,8 +1067,8 @@ void main() { var called = 0; // The URL string 'f' is primed in the FakeCache zone.run(() { - http.getString('f', cache: cache).then((v) { - expect(v).toEqual(CACHED_VALUE); + http.get('f', cache: cache).then((v) { + expect(v.responseText).toEqual(CACHED_VALUE); called += 1; }); expect(called).toEqual(0); @@ -1085,7 +1085,7 @@ void main() { var response = null; zone.run(() { - http.getString('404.html').then( + http.get('404.html').then( (v) => response = 'FAILED', onError:(v) { assert(v != null); return response = v; }); }); diff --git a/test/directive/ng_base_css_spec.dart b/test/directive/ng_base_css_spec.dart index d6d3862b9..4944b8760 100644 --- a/test/directive/ng_base_css_spec.dart +++ b/test/directive/ng_base_css_spec.dart @@ -16,13 +16,14 @@ main() => describe('NgBaseCss', () { it('should load css urls from ng-base-css', async((TestBed _, MockHttpBackend backend) { backend - ..expectGET('base.css').respond('.base{}') - ..expectGET('simple.css').respond('.simple{}') - ..expectGET('simple.html').respond('
Simple!
'); + ..expectGET('base.css').respond(200, '.base{}') + ..expectGET('simple.css').respond(200, '.simple{}') + ..expectGET('simple.html').respond(200, '
Simple!
'); var element = e('
ignore
'); _.compile(element); + microLeap(); backend.flush(); microLeap(); @@ -33,13 +34,14 @@ main() => describe('NgBaseCss', () { it('ng-base-css should overwrite parent ng-base-csses', async((TestBed _, MockHttpBackend backend) { backend - ..expectGET('base.css').respond('.base{}') - ..expectGET('simple.css').respond('.simple{}') - ..expectGET('simple.html').respond('
Simple!
'); + ..expectGET('base.css').respond(200, '.base{}') + ..expectGET('simple.css').respond(200, '.simple{}') + ..expectGET('simple.html').respond(200, '
Simple!
'); var element = e('
ignore
'); _.compile(element); + microLeap(); backend.flush(); microLeap(); @@ -55,13 +57,14 @@ main() => describe('NgBaseCss', () { it('ng-base-css should be available from the injector', async((TestBed _, MockHttpBackend backend) { backend - ..expectGET('injected.css').respond('.injected{}') - ..expectGET('simple.css').respond('.simple{}') - ..expectGET('simple.html').respond('
Simple!
'); + ..expectGET('injected.css').respond(200, '.injected{}') + ..expectGET('simple.css').respond(200, '.simple{}') + ..expectGET('simple.html').respond(200, '
Simple!
'); var element = e('
ignore
'); _.compile(element); + microLeap(); backend.flush(); microLeap();