@@ -1001,13 +1001,14 @@ angular.mock.dump = function(object) {
1001
1001
```js
1002
1002
// testing controller
1003
1003
describe('MyController', function() {
1004
- var $httpBackend, $rootScope, createController;
1004
+ var $httpBackend, $rootScope, createController, authRequestHandler ;
1005
1005
1006
1006
beforeEach(inject(function($injector) {
1007
1007
// Set up the mock http service responses
1008
1008
$httpBackend = $injector.get('$httpBackend');
1009
1009
// backend definition common for all tests
1010
- $httpBackend.when('GET', '/auth.py').respond({userId: 'userX'}, {'A-Token': 'xxx'});
1010
+ authRequestHandler = $httpBackend.when('GET', '/auth.py')
1011
+ .respond({userId: 'userX'}, {'A-Token': 'xxx'});
1011
1012
1012
1013
// Get hold of a scope (i.e. the root scope)
1013
1014
$rootScope = $injector.get('$rootScope');
@@ -1033,6 +1034,18 @@ angular.mock.dump = function(object) {
1033
1034
});
1034
1035
1035
1036
1037
+ it('should fail authentication', function() {
1038
+
1039
+ // Notice how you can change the response even after it was set
1040
+ authRequestHandler.respond(401, '');
1041
+
1042
+ $httpBackend.expectGET('/auth.py');
1043
+ var controller = createController();
1044
+ $httpBackend.flush();
1045
+ expect($rootScope.status).toBe('Failed...');
1046
+ });
1047
+
1048
+
1036
1049
it('should send msg to server', function() {
1037
1050
var controller = createController();
1038
1051
$httpBackend.flush();
@@ -1187,26 +1200,32 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
1187
1200
* @param {(Object|function(Object))= } headers HTTP headers or function that receives http header
1188
1201
* object and returns true if the headers match the current definition.
1189
1202
* @returns {requestHandler } Returns an object with `respond` method that controls how a matched
1190
- * request is handled.
1203
+ * request is handled. You can save this object for later use and invoke `respond` again in
1204
+ * order to change how a matched request is handled.
1191
1205
*
1192
1206
* - respond –
1193
1207
* `{function([status,] data[, headers, statusText])
1194
1208
* | function(function(method, url, data, headers)}`
1195
1209
* – The respond method takes a set of static data to be returned or a function that can
1196
1210
* return an array containing response status (number), response data (string), response
1197
- * headers (Object), and the text for the status (string).
1211
+ * headers (Object), and the text for the status (string). The respond method returns the
1212
+ * `requestHandler` object for possible overrides.
1198
1213
*/
1199
1214
$httpBackend . when = function ( method , url , data , headers ) {
1200
1215
var definition = new MockHttpExpectation ( method , url , data , headers ) ,
1201
1216
chain = {
1202
1217
respond : function ( status , data , headers , statusText ) {
1218
+ definition . passThrough = undefined ;
1203
1219
definition . response = createResponse ( status , data , headers , statusText ) ;
1220
+ return chain ;
1204
1221
}
1205
1222
} ;
1206
1223
1207
1224
if ( $browser ) {
1208
1225
chain . passThrough = function ( ) {
1226
+ definition . response = undefined ;
1209
1227
definition . passThrough = true ;
1228
+ return chain ;
1210
1229
} ;
1211
1230
}
1212
1231
@@ -1224,7 +1243,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
1224
1243
* and returns true if the url match the current definition.
1225
1244
* @param {(Object|function(Object))= } headers HTTP headers.
1226
1245
* @returns {requestHandler } Returns an object with `respond` method that control how a matched
1227
- * request is handled.
1246
+ * request is handled. You can save this object for later use and invoke `respond` again in
1247
+ * order to change how a matched request is handled.
1228
1248
*/
1229
1249
1230
1250
/**
@@ -1237,7 +1257,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
1237
1257
* and returns true if the url match the current definition.
1238
1258
* @param {(Object|function(Object))= } headers HTTP headers.
1239
1259
* @returns {requestHandler } Returns an object with `respond` method that control how a matched
1240
- * request is handled.
1260
+ * request is handled. You can save this object for later use and invoke `respond` again in
1261
+ * order to change how a matched request is handled.
1241
1262
*/
1242
1263
1243
1264
/**
@@ -1250,7 +1271,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
1250
1271
* and returns true if the url match the current definition.
1251
1272
* @param {(Object|function(Object))= } headers HTTP headers.
1252
1273
* @returns {requestHandler } Returns an object with `respond` method that control how a matched
1253
- * request is handled.
1274
+ * request is handled. You can save this object for later use and invoke `respond` again in
1275
+ * order to change how a matched request is handled.
1254
1276
*/
1255
1277
1256
1278
/**
@@ -1265,7 +1287,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
1265
1287
* data string and returns true if the data is as expected.
1266
1288
* @param {(Object|function(Object))= } headers HTTP headers.
1267
1289
* @returns {requestHandler } Returns an object with `respond` method that control how a matched
1268
- * request is handled.
1290
+ * request is handled. You can save this object for later use and invoke `respond` again in
1291
+ * order to change how a matched request is handled.
1269
1292
*/
1270
1293
1271
1294
/**
@@ -1280,7 +1303,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
1280
1303
* data string and returns true if the data is as expected.
1281
1304
* @param {(Object|function(Object))= } headers HTTP headers.
1282
1305
* @returns {requestHandler } Returns an object with `respond` method that control how a matched
1283
- * request is handled.
1306
+ * request is handled. You can save this object for later use and invoke `respond` again in
1307
+ * order to change how a matched request is handled.
1284
1308
*/
1285
1309
1286
1310
/**
@@ -1292,7 +1316,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
1292
1316
* @param {string|RegExp|function(string) } url HTTP url or function that receives the url
1293
1317
* and returns true if the url match the current definition.
1294
1318
* @returns {requestHandler } Returns an object with `respond` method that control how a matched
1295
- * request is handled.
1319
+ * request is handled. You can save this object for later use and invoke `respond` again in
1320
+ * order to change how a matched request is handled.
1296
1321
*/
1297
1322
createShortMethods ( 'when' ) ;
1298
1323
@@ -1312,23 +1337,28 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
1312
1337
* @param {(Object|function(Object))= } headers HTTP headers or function that receives http header
1313
1338
* object and returns true if the headers match the current expectation.
1314
1339
* @returns {requestHandler } Returns an object with `respond` method that control how a matched
1315
- * request is handled.
1340
+ * request is handled. You can save this object for later use and invoke `respond` again in
1341
+ * order to change how a matched request is handled.
1316
1342
*
1317
1343
* - respond –
1318
1344
* `{function([status,] data[, headers, statusText])
1319
1345
* | function(function(method, url, data, headers)}`
1320
1346
* – The respond method takes a set of static data to be returned or a function that can
1321
1347
* return an array containing response status (number), response data (string), response
1322
- * headers (Object), and the text for the status (string).
1348
+ * headers (Object), and the text for the status (string). The respond method returns the
1349
+ * `requestHandler` object for possible overrides.
1323
1350
*/
1324
1351
$httpBackend . expect = function ( method , url , data , headers ) {
1325
- var expectation = new MockHttpExpectation ( method , url , data , headers ) ;
1352
+ var expectation = new MockHttpExpectation ( method , url , data , headers ) ,
1353
+ chain = {
1354
+ respond : function ( status , data , headers , statusText ) {
1355
+ expectation . response = createResponse ( status , data , headers , statusText ) ;
1356
+ return chain ;
1357
+ }
1358
+ } ;
1359
+
1326
1360
expectations . push ( expectation ) ;
1327
- return {
1328
- respond : function ( status , data , headers , statusText ) {
1329
- expectation . response = createResponse ( status , data , headers , statusText ) ;
1330
- }
1331
- } ;
1361
+ return chain ;
1332
1362
} ;
1333
1363
1334
1364
@@ -1342,7 +1372,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
1342
1372
* and returns true if the url match the current definition.
1343
1373
* @param {Object= } headers HTTP headers.
1344
1374
* @returns {requestHandler } Returns an object with `respond` method that control how a matched
1345
- * request is handled. See #expect for more info.
1375
+ * request is handled. You can save this object for later use and invoke `respond` again in
1376
+ * order to change how a matched request is handled. See #expect for more info.
1346
1377
*/
1347
1378
1348
1379
/**
@@ -1355,7 +1386,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
1355
1386
* and returns true if the url match the current definition.
1356
1387
* @param {Object= } headers HTTP headers.
1357
1388
* @returns {requestHandler } Returns an object with `respond` method that control how a matched
1358
- * request is handled.
1389
+ * request is handled. You can save this object for later use and invoke `respond` again in
1390
+ * order to change how a matched request is handled.
1359
1391
*/
1360
1392
1361
1393
/**
@@ -1368,7 +1400,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
1368
1400
* and returns true if the url match the current definition.
1369
1401
* @param {Object= } headers HTTP headers.
1370
1402
* @returns {requestHandler } Returns an object with `respond` method that control how a matched
1371
- * request is handled.
1403
+ * request is handled. You can save this object for later use and invoke `respond` again in
1404
+ * order to change how a matched request is handled.
1372
1405
*/
1373
1406
1374
1407
/**
@@ -1384,7 +1417,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
1384
1417
* is in JSON format.
1385
1418
* @param {Object= } headers HTTP headers.
1386
1419
* @returns {requestHandler } Returns an object with `respond` method that control how a matched
1387
- * request is handled.
1420
+ * request is handled. You can save this object for later use and invoke `respond` again in
1421
+ * order to change how a matched request is handled.
1388
1422
*/
1389
1423
1390
1424
/**
@@ -1400,7 +1434,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
1400
1434
* is in JSON format.
1401
1435
* @param {Object= } headers HTTP headers.
1402
1436
* @returns {requestHandler } Returns an object with `respond` method that control how a matched
1403
- * request is handled.
1437
+ * request is handled. You can save this object for later use and invoke `respond` again in
1438
+ * order to change how a matched request is handled.
1404
1439
*/
1405
1440
1406
1441
/**
@@ -1416,7 +1451,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
1416
1451
* is in JSON format.
1417
1452
* @param {Object= } headers HTTP headers.
1418
1453
* @returns {requestHandler } Returns an object with `respond` method that control how a matched
1419
- * request is handled.
1454
+ * request is handled. You can save this object for later use and invoke `respond` again in
1455
+ * order to change how a matched request is handled.
1420
1456
*/
1421
1457
1422
1458
/**
@@ -1428,7 +1464,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
1428
1464
* @param {string|RegExp|function(string) } url HTTP url or function that receives the url
1429
1465
* and returns true if the url match the current definition.
1430
1466
* @returns {requestHandler } Returns an object with `respond` method that control how a matched
1431
- * request is handled.
1467
+ * request is handled. You can save this object for later use and invoke `respond` again in
1468
+ * order to change how a matched request is handled.
1432
1469
*/
1433
1470
createShortMethods ( 'expect' ) ;
1434
1471
@@ -1838,7 +1875,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
1838
1875
* @param {(Object|function(Object))= } headers HTTP headers or function that receives http header
1839
1876
* object and returns true if the headers match the current definition.
1840
1877
* @returns {requestHandler } Returns an object with `respond` and `passThrough` methods that
1841
- * control how a matched request is handled.
1878
+ * control how a matched request is handled. You can save this object for later use and invoke
1879
+ * `respond` or `passThrough` again in order to change how a matched request is handled.
1842
1880
*
1843
1881
* - respond –
1844
1882
* `{function([status,] data[, headers, statusText])
@@ -1849,6 +1887,7 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
1849
1887
* - passThrough – `{function()}` – Any request matching a backend definition with
1850
1888
* `passThrough` handler will be passed through to the real backend (an XHR request will be made
1851
1889
* to the server.)
1890
+ * - Both methods return the `requestHandler` object for possible overrides.
1852
1891
*/
1853
1892
1854
1893
/**
@@ -1862,7 +1901,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
1862
1901
* and returns true if the url match the current definition.
1863
1902
* @param {(Object|function(Object))= } headers HTTP headers.
1864
1903
* @returns {requestHandler } Returns an object with `respond` and `passThrough` methods that
1865
- * control how a matched request is handled.
1904
+ * control how a matched request is handled. You can save this object for later use and invoke
1905
+ * `respond` or `passThrough` again in order to change how a matched request is handled.
1866
1906
*/
1867
1907
1868
1908
/**
@@ -1876,7 +1916,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
1876
1916
* and returns true if the url match the current definition.
1877
1917
* @param {(Object|function(Object))= } headers HTTP headers.
1878
1918
* @returns {requestHandler } Returns an object with `respond` and `passThrough` methods that
1879
- * control how a matched request is handled.
1919
+ * control how a matched request is handled. You can save this object for later use and invoke
1920
+ * `respond` or `passThrough` again in order to change how a matched request is handled.
1880
1921
*/
1881
1922
1882
1923
/**
@@ -1890,7 +1931,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
1890
1931
* and returns true if the url match the current definition.
1891
1932
* @param {(Object|function(Object))= } headers HTTP headers.
1892
1933
* @returns {requestHandler } Returns an object with `respond` and `passThrough` methods that
1893
- * control how a matched request is handled.
1934
+ * control how a matched request is handled. You can save this object for later use and invoke
1935
+ * `respond` or `passThrough` again in order to change how a matched request is handled.
1894
1936
*/
1895
1937
1896
1938
/**
@@ -1905,7 +1947,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
1905
1947
* @param {(string|RegExp)= } data HTTP request body.
1906
1948
* @param {(Object|function(Object))= } headers HTTP headers.
1907
1949
* @returns {requestHandler } Returns an object with `respond` and `passThrough` methods that
1908
- * control how a matched request is handled.
1950
+ * control how a matched request is handled. You can save this object for later use and invoke
1951
+ * `respond` or `passThrough` again in order to change how a matched request is handled.
1909
1952
*/
1910
1953
1911
1954
/**
@@ -1920,7 +1963,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
1920
1963
* @param {(string|RegExp)= } data HTTP request body.
1921
1964
* @param {(Object|function(Object))= } headers HTTP headers.
1922
1965
* @returns {requestHandler } Returns an object with `respond` and `passThrough` methods that
1923
- * control how a matched request is handled.
1966
+ * control how a matched request is handled. You can save this object for later use and invoke
1967
+ * `respond` or `passThrough` again in order to change how a matched request is handled.
1924
1968
*/
1925
1969
1926
1970
/**
@@ -1935,7 +1979,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
1935
1979
* @param {(string|RegExp)= } data HTTP request body.
1936
1980
* @param {(Object|function(Object))= } headers HTTP headers.
1937
1981
* @returns {requestHandler } Returns an object with `respond` and `passThrough` methods that
1938
- * control how a matched request is handled.
1982
+ * control how a matched request is handled. You can save this object for later use and invoke
1983
+ * `respond` or `passThrough` again in order to change how a matched request is handled.
1939
1984
*/
1940
1985
1941
1986
/**
@@ -1948,7 +1993,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
1948
1993
* @param {string|RegExp|function(string) } url HTTP url or function that receives the url
1949
1994
* and returns true if the url match the current definition.
1950
1995
* @returns {requestHandler } Returns an object with `respond` and `passThrough` methods that
1951
- * control how a matched request is handled.
1996
+ * control how a matched request is handled. You can save this object for later use and invoke
1997
+ * `respond` or `passThrough` again in order to change how a matched request is handled.
1952
1998
*/
1953
1999
angular . mock . e2e = { } ;
1954
2000
angular . mock . e2e . $httpBackendDecorator =
0 commit comments