diff options
| author | Igor Minar | 2011-12-28 09:26:22 -0800 |
|---|---|---|
| committer | Vojta Jina | 2012-01-09 13:17:48 -0800 |
| commit | a13b5ed3bc337a493029815c595b89c39eb95af6 (patch) | |
| tree | 2ca5380d5cf5aea68218280cccda5d0221517454 /src/service/httpBackend.js | |
| parent | 63cca9afbcf7a772086eb4582d2f409c39e0ed12 (diff) | |
| download | angular.js-a13b5ed3bc337a493029815c595b89c39eb95af6.tar.bz2 | |
fix($http): fix and cleanup $http and friends
$http:
- use promises internally
- get rid of XhrFuture that was previously used internally
- get rid of $browser.defer calls for async stuff (serving from cache),
promises will take care of asynchronicity
- fix transformation bugs (when caching requested + multiple request
pending + error is returned)
- get rid of native header parsing and instead just lazily parse the
header string
$httpBackend:
- don't return raw/mock XMLHttpRequest object (we don't use it for
anything anymore)
- call the callback with response headers string
mock $httpBackend:
- unify response api for expect and when
- call the callback with response headers string
- changed the expect/when failure error message so that EXPECTED and GOT
values are aligned
Conflicts:
src/service/http.js
test/service/compilerSpec.js
test/service/httpSpec.js
Diffstat (limited to 'src/service/httpBackend.js')
| -rw-r--r-- | src/service/httpBackend.js | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/src/service/httpBackend.js b/src/service/httpBackend.js index c64519d9..c3114814 100644 --- a/src/service/httpBackend.js +++ b/src/service/httpBackend.js @@ -14,6 +14,11 @@ var XHR = window.XMLHttpRequest || function() { * @requires $document * * @description + * HTTP backend used by the {@link angular.module.ng.$http service} that delegates to + * XMLHttpRequest object. + * + * During testing this implementation is swapped with {@link angular.module.ngMock.$httpBackend mock + * $httpBackend} which can be trained with responses. */ function $HttpBackendProvider() { this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) { @@ -46,24 +51,21 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, body, locati var xhr = new XHR(); xhr.open(method, url, true); forEach(headers, function(value, key) { - if (value) xhr.setRequestHeader(key, value); + if (value) xhr.setRequestHeader(key, value); }); var status; - xhr.send(post || ''); - // IE6, IE7 bug - does sync when serving from cache - if (xhr.readyState == 4) { - $browserDefer(function() { - completeRequest(callback, status || xhr.status, xhr.responseText); - }, 0); - } else { - xhr.onreadystatechange = function() { - if (xhr.readyState == 4) { - completeRequest(callback, status || xhr.status, xhr.responseText); - } - }; - } + // In IE6 and 7, this might be called synchronously when xhr.send below is called and the + // response is in the cache + xhr.onreadystatechange = function() { + if (xhr.readyState == 4) { + completeRequest( + callback, status || xhr.status, xhr.responseText, xhr.getAllResponseHeaders()); + } + }; + + xhr.send(post || ''); if (timeout > 0) { $browserDefer(function() { @@ -71,23 +73,21 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, body, locati xhr.abort(); }, timeout); } - - return xhr; } - function completeRequest(callback, status, response) { + + function completeRequest(callback, status, response, headersString) { // URL_MATCH is defined in src/service/location.js var protocol = (url.match(URL_MATCH) || ['', locationProtocol])[1]; // fix status code for file protocol (it's always 0) - status = protocol == 'file' ? (response ? 200 : 404) : status; + status = (protocol == 'file') ? (response ? 200 : 404) : status; // normalize IE bug (http://bugs.jquery.com/ticket/1450) status = status == 1223 ? 204 : status; - callback(status, response); + callback(status, response, headersString); $browser.$$completeOutstandingRequest(noop); } }; } - |
