(method, url, data, callback, headers, timeout, withCredentials, responseType)
| 1249 | |
| 1250 | // TODO(vojta): change params to: method, url, data, headers, callback |
| 1251 | function $httpBackend(method, url, data, callback, headers, timeout, withCredentials, responseType) { |
| 1252 | |
| 1253 | var xhr = new MockXhr(), |
| 1254 | expectation = expectations[0], |
| 1255 | wasExpected = false; |
| 1256 | |
| 1257 | function prettyPrint(data) { |
| 1258 | return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp) |
| 1259 | ? data |
| 1260 | : angular.toJson(data); |
| 1261 | } |
| 1262 | |
| 1263 | function wrapResponse(wrapped) { |
| 1264 | if (!$browser && timeout) { |
| 1265 | timeout.then ? timeout.then(handleTimeout) : $timeout(handleTimeout, timeout); |
| 1266 | } |
| 1267 | |
| 1268 | return handleResponse; |
| 1269 | |
| 1270 | function handleResponse() { |
| 1271 | var response = wrapped.response(method, url, data, headers); |
| 1272 | xhr.$$respHeaders = response[2]; |
| 1273 | callback(copy(response[0]), copy(response[1]), xhr.getAllResponseHeaders(), |
| 1274 | copy(response[3] || '')); |
| 1275 | } |
| 1276 | |
| 1277 | function handleTimeout() { |
| 1278 | for (var i = 0, ii = responses.length; i < ii; i++) { |
| 1279 | if (responses[i] === handleResponse) { |
| 1280 | responses.splice(i, 1); |
| 1281 | callback(-1, undefined, ''); |
| 1282 | break; |
| 1283 | } |
| 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | if (expectation && expectation.match(method, url)) { |
| 1289 | if (!expectation.matchData(data)) { |
| 1290 | throw new Error('Expected ' + expectation + ' with different data\n' + |
| 1291 | 'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT: ' + data); |
| 1292 | } |
| 1293 | |
| 1294 | if (!expectation.matchHeaders(headers)) { |
| 1295 | throw new Error('Expected ' + expectation + ' with different headers\n' + |
| 1296 | 'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT: ' + |
| 1297 | prettyPrint(headers)); |
| 1298 | } |
| 1299 | |
| 1300 | expectations.shift(); |
| 1301 | |
| 1302 | if (expectation.response) { |
| 1303 | responses.push(wrapResponse(expectation)); |
| 1304 | return; |
| 1305 | } |
| 1306 | wasExpected = true; |
| 1307 | } |
| 1308 |
no test coverage detected