diff options
Diffstat (limited to 'src/angular-mocks.js')
| -rw-r--r-- | src/angular-mocks.js | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/src/angular-mocks.js b/src/angular-mocks.js index 762148fd..558e71e3 100644 --- a/src/angular-mocks.js +++ b/src/angular-mocks.js @@ -101,28 +101,27 @@ function MockBrowser() { }; - self.xhr = function(method, url, data, callback) { - if (angular.isFunction(data)) { - callback = data; - data = null; - } + self.xhr = function(method, url, data, callback, headers) { + headers = headers || {}; if (data && angular.isObject(data)) data = angular.toJson(data); if (data && angular.isString(data)) url += "|" + data; var expect = expectations[method] || {}; - var response = expect[url]; - if (!response) { - throw { - message: "Unexpected request for method '" + method + "' and url '" + url + "'.", - name: "Unexpected Request" - }; + var expectation = expect[url]; + if (!expectation) { + throw new Error("Unexpected request for method '" + method + "' and url '" + url + "'."); } requests.push(function(){ - callback(response.code, response.response); + forEach(expectation.headers, function(value, key){ + if (headers[key] !== value) { + throw new Error("Missing HTTP request header: " + key + ": " + value); + } + }); + callback(expectation.code, expectation.response); }); }; self.xhr.expectations = expectations; self.xhr.requests = requests; - self.xhr.expect = function(method, url, data) { + self.xhr.expect = function(method, url, data, headers) { if (data && angular.isObject(data)) data = angular.toJson(data); if (data && angular.isString(data)) url += "|" + data; var expect = expectations[method] || (expectations[method] = {}); @@ -132,7 +131,7 @@ function MockBrowser() { response = code; code = 200; } - expect[url] = {code:code, response:response}; + expect[url] = {code:code, response:response, headers: headers || {}}; } }; }; |
