(method, url, data, callback, headers, timeout, withCredentials)
| 1111 | |
| 1112 | // TODO(vojta): change params to: method, url, data, headers, callback |
| 1113 | function $httpBackend(method, url, data, callback, headers, timeout, withCredentials) { |
| 1114 | var xhr = new MockXhr(), |
| 1115 | expectation = expectations[0], |
| 1116 | wasExpected = false; |
| 1117 | |
| 1118 | function prettyPrint(data) { |
| 1119 | return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp) |
| 1120 | ? data |
| 1121 | : angular.toJson(data); |
| 1122 | } |
| 1123 | |
| 1124 | function wrapResponse(wrapped) { |
| 1125 | if (!$browser && timeout && timeout.then) timeout.then(handleTimeout); |
| 1126 | |
| 1127 | return handleResponse; |
| 1128 | |
| 1129 | function handleResponse() { |
| 1130 | var response = wrapped.response(method, url, data, headers); |
| 1131 | xhr.$$respHeaders = response[2]; |
| 1132 | callback(copy(response[0]), copy(response[1]), xhr.getAllResponseHeaders(), |
| 1133 | copy(response[3] || '')); |
| 1134 | } |
| 1135 | |
| 1136 | function handleTimeout() { |
| 1137 | for (var i = 0, ii = responses.length; i < ii; i++) { |
| 1138 | if (responses[i] === handleResponse) { |
| 1139 | responses.splice(i, 1); |
| 1140 | callback(-1, undefined, ''); |
| 1141 | break; |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | if (expectation && expectation.match(method, url)) { |
| 1148 | if (!expectation.matchData(data)) |
| 1149 | throw new Error('Expected ' + expectation + ' with different data\n' + |
| 1150 | 'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT: ' + data); |
| 1151 | |
| 1152 | if (!expectation.matchHeaders(headers)) |
| 1153 | throw new Error('Expected ' + expectation + ' with different headers\n' + |
| 1154 | 'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT: ' + |
| 1155 | prettyPrint(headers)); |
| 1156 | |
| 1157 | expectations.shift(); |
| 1158 | |
| 1159 | if (expectation.response) { |
| 1160 | responses.push(wrapResponse(expectation)); |
| 1161 | return; |
| 1162 | } |
| 1163 | wasExpected = true; |
| 1164 | } |
| 1165 | |
| 1166 | var i = -1, definition; |
| 1167 | while ((definition = definitions[++i])) { |
| 1168 | if (definition.match(method, url, data, headers || {})) { |
| 1169 | if (definition.response) { |
| 1170 | // if $browser specified, we do auto flush all requests |
no test coverage detected