├── .editorconfig
├── .eslintrc.js
├── .gitignore
├── .npmignore
├── .travis.yml
├── README.md
├── bower.json
├── demo
├── css
│ └── main.css
├── data
│ └── flags.json
├── images
│ └── icon.png
├── index.html
├── scripts
│ └── directives.js
└── vendor
│ ├── angular.min.js
│ └── angular.min.js.map
├── dist
├── featureFlags.js
└── featureFlags.min.js
├── gulpfile.js
├── index.js
├── package.json
├── src
├── featureFlag.app.js
├── featureFlag.directive.js
├── featureFlagOverrides.directive.js
├── featureFlagOverrides.service.js
└── featureFlags.provider.js
└── test
├── featureFlag.directive.spec.js
├── featureFlagOverrides.directive.spec.js
├── featureFlagOverrides.service.spec.js
├── featureFlags.provider.spec.js
└── vendor
└── angular-mocks.js
/.editorconfig:
--------------------------------------------------------------------------------
1 | # editorconfig.org
2 | root = true
3 |
4 | [*]
5 | indent_style = space
6 | indent_size = 2
7 | end_of_line = lf
8 | charset = utf-8
9 | trim_trailing_whitespace = true
10 | insert_final_newline = true
11 |
12 | [*.md]
13 | trim_trailing_whitespace = false
14 |
--------------------------------------------------------------------------------
/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | "env": {
3 | "browser": true,
4 | "jasmine": true
5 | },
6 | "globals": {
7 | "angular": true
8 | },
9 | "rules": {
10 | // Possible Errors
11 | "comma-dangle": 2,
12 | "no-cond-assign": 2,
13 | "no-console": 0,
14 | "no-constant-condition": 2,
15 | "no-control-regex": 2,
16 | "no-debugger": 2,
17 | "no-dupe-args": 2,
18 | "no-dupe-keys": 2,
19 | "no-duplicate-case": 2,
20 | "no-empty-character-class": 2,
21 | "no-empty": 2,
22 | "no-ex-assign": 2,
23 | "no-extra-boolean-cast": 2,
24 | "no-extra-parens": 0,
25 | "no-extra-semi": 2,
26 | "no-func-assign": 2,
27 | "no-inner-declarations": 2,
28 | "no-invalid-regexp": 2,
29 | "no-irregular-whitespace": 2,
30 | "no-negated-in-lhs": 2,
31 | "no-obj-calls": 2,
32 | "no-regex-spaces": 2,
33 | "no-sparse-arrays": 2,
34 | "no-unexpected-multiline": 2,
35 | "no-unreachable": 2,
36 | "use-isnan": 2,
37 | "valid-jsdoc": 0,
38 | "valid-typeof": 2,
39 |
40 | // Best Practices
41 | "accessor-pairs": 0,
42 | "block-scoped-var": 2,
43 | "complexity": 0,
44 | "consistent-return": 2,
45 | "curly": [2, "all"],
46 | "default-case": 2,
47 | "dot-notation": 2,
48 | "dot-location": [2, "property"],
49 | "eqeqeq": 2,
50 | "guard-for-in": 2,
51 | "no-alert": 2,
52 | "no-caller": 2,
53 | "no-div-regex": 2,
54 | "no-else-return": 2,
55 | "no-empty-label": 2,
56 | "no-eq-null": 2,
57 | "no-eval": 2,
58 | "no-extend-native": 2,
59 | "no-extra-bind": 2,
60 | "no-fallthrough": 2,
61 | "no-floating-decimal": 2,
62 | "no-implicit-coercion": 2,
63 | "no-implied-eval": 2,
64 | "no-invalid-this": 2,
65 | "no-iterator": 2,
66 | "no-labels": 2,
67 | "no-lone-blocks": 2,
68 | "no-loop-func": 2,
69 | "no-multi-spaces": 2,
70 | "no-multi-str": 2,
71 | "no-native-reassign": 2,
72 | "no-new-func": 2,
73 | "no-new-wrappers": 2,
74 | "no-new": 2,
75 | "no-octal-escape": 2,
76 | "no-octal": 2,
77 | "no-param-reassign": 2,
78 | "no-process-env": 0,
79 | "no-proto": 2,
80 | "no-redeclare": 2,
81 | "no-return-assign": 2,
82 | "no-script-url": 2,
83 | "no-self-compare": 2,
84 | "no-sequences": 2,
85 | "no-throw-literal": 2,
86 | "no-unused-expressions": 2,
87 | "no-useless-call": 2,
88 | "no-void": 2,
89 | "no-warning-comments": 2,
90 | "no-with": 2,
91 | "radix": 2,
92 | "vars-on-top": 2,
93 | "wrap-iife": 2,
94 | "yoda": 2,
95 |
96 | // Strict Mode
97 | "strict": 0,
98 |
99 | // Variables
100 | "init-declarations": 0,
101 | "no-catch-shadow": 2,
102 | "no-delete-var": 2,
103 | "no-label-var": 2,
104 | "no-shadow-restricted-names": 2,
105 | "no-shadow": 2,
106 | "no-undef-init": 2,
107 | "no-undef": 2,
108 | "no-undefined": 2,
109 | "no-unused-vars": 2,
110 | "no-use-before-define": 2,
111 |
112 | // Node.js
113 | "callback-return": 0,
114 | "handle-callback-err": 2,
115 | "no-mixed-requires": 0,
116 | "no-new-require": 2,
117 | "no-path-concat": 2,
118 | "no-process-exit": 2,
119 | "no-restricted-modules": 2,
120 | "no-sync": 2,
121 |
122 | // Stylistic Issues
123 | "block-spacing": [2, "always"],
124 | "brace-style": [2, "1tbs"],
125 | "comma-spacing": [2, { "before": false, "after": true }],
126 | "comma-style": [2, "last"],
127 | "eol-last": 2,
128 | "func-style": 0,
129 | "indent": [2, 2, { "SwitchCase": 1 }],
130 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }],
131 | "linebreak-style": [2, "unix"],
132 | "new-cap": 2,
133 | "new-parens": 2,
134 | "no-lonely-if": 2,
135 | "no-mixed-spaces-and-tabs": 2,
136 | "no-multiple-empty-lines": [2, { "max": 1 }],
137 | "no-nested-ternary": 2,
138 | "no-spaced-func": 2,
139 | "no-trailing-spaces": 2,
140 | "no-unneeded-ternary": 2,
141 | "object-curly-spacing": [2, "always"],
142 | "operator-linebreak": [2, "after"],
143 | "padded-blocks": [2, "never"],
144 | "quotes": [2, "single"],
145 | "semi-spacing": [2, { "before": false, "after": true }],
146 | "semi": [2, "always"],
147 | "space-after-keywords": [2, "always"],
148 | "space-before-blocks": [2, "always"],
149 | "space-before-function-paren": [2, "never"],
150 | "space-in-parens": [2, "never"],
151 | "space-infix-ops": 2,
152 | "space-return-throw-case": 2,
153 | "spaced-comment": [2, "always"]
154 | }
155 | }
156 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | test/coverage
3 | demo/scripts/featureFlags.min.js
4 | .idea
5 | bower_components
6 | coverage
7 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | src
2 | coverage
3 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 |
3 | node_js:
4 | - 0.10
5 |
6 | after_script:
7 | - npm run coveralls
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## PSA: This repo is no longer maintained. Feel free to fork it to suit your use case.
2 |
3 | [](https://travis-ci.org/michaeltaranto/angular-feature-flags)
4 | [](https://coveralls.io/github/michaeltaranto/angular-feature-flags?branch=master)
5 | [](https://www.npmjs.com/package/angular-feature-flags)
6 |
7 | ## angular-feature-flags
8 |
9 | An AngularJS module that allows you to control when you release new features in your app by putting them behind feature flags/switches. **This module only supports Angular v1.2 and up.**
10 |
11 |
12 | ### The idea
13 |
14 | Abstracting your application functionality into small chunks and implementing them as loosely coupled directives. This allows you to completely remove sections of your application by simply toggling a single dom element.
15 |
16 |
17 | ### How it works
18 |
19 | The basic premise is you write your feature and wrap it up in a directive, then where you implement that directive in your markup you add the **feature-flag** directive to the same element. You can then pass the **key** of the flag to this directive to resolve whether of not this feature should be enabled.
20 |
21 | The module pulls a json file down which defines the feature flags and which ones are active. If enabled angular will process the directive as normal, if disabled angular will remove the element from the dom and not compile or execute any other directives is has.
22 |
23 | You can then add the **override** panel to your app and turn individual features on override the server values, saving the override in local storage which is useful in development.
24 |
25 |
26 | ### Flag data
27 |
28 | The flag data that drives the feature flag service is a json format. Below is an example:
29 | ```json
30 | [
31 | { "key": "...", "active": "...", "name": "...", "description": "..." },
32 | ...
33 | ]
34 | ```
35 |
36 |
37 |
key
38 |
Unique key that is used from the markup to resolve whether a flag is active or not.
39 |
40 |
41 |
active
42 |
Boolean value for enabling/disabling the feature
43 |
44 |
45 |
name
46 |
A short name of the flag (only visible in the list of flags)
47 |
48 |
49 |
description
50 |
A long description of the flag to further explain the feature being toggled (only visible in the list of flags)
51 |
52 |
53 |
54 |
55 | ### Setting flag data
56 |
57 | Flag data can be set via the `featureFlags` service using the `set` method. This currently accepts either a [HttpPromise](https://docs.angularjs.org/api/ng/service/$http) or a regular [Promise](https://docs.angularjs.org/api/ng/service/$q). The promise must resolve to a valid collection of [flag data](#flag-data).
58 |
59 | For example, if you were loading your flag data from a remote JSON file:
60 |
61 | ```js
62 | var myApp = angular.module('app', ['feature-flags']);
63 |
64 | myApp.run(function(featureFlags, $http) {
65 | featureFlags.set($http.get('/data/flags.json'));
66 | });
67 | ```
68 |
69 | ### Setting flag data on config phase (≥ v1.1.0)
70 |
71 | From version v1.1.0 you can also initialize the feature flags in the config phase of your application:
72 |
73 | ```js
74 | var myApp = angular.module('app', ['feature-flags']);
75 |
76 | myApp.config(function(featureFlagsProvider) {
77 | featureFlagsProvider.setInitialFlags([
78 | { "key": "...", "active": "...", "name": "...", "description": "..." },
79 | ]);
80 | });
81 | ```
82 |
83 | ### Toggling elements
84 |
85 | The `feature-flag` directive allows simple toggling of elements based on feature flags, e.g:
86 |
87 | ```html
88 |
89 | I will be visible if 'myFlag' is enabled
90 |
91 | ```
92 |
93 | If you need to *hide* elements when a flag is enabled, add the `feature-flag-hide` attribute, e.g:
94 |
95 | ```html
96 |
97 | I will *NOT* be visible if 'myFlag' is enabled
98 |
374 | * $log.log('Some Error');
375 | * var first = $log.error.logs.unshift();
376 | *
377 | */
378 | $log.error.logs = [];
379 | };
380 |
381 | /**
382 | * @ngdoc method
383 | * @name ngMock.$log#assertEmpty
384 | * @methodOf ngMock.$log
385 | *
386 | * @description
387 | * Assert that the all of the logging methods have no logged messages. If messages present, an exception is thrown.
388 | */
389 | $log.assertEmpty = function() {
390 | var errors = [];
391 | angular.forEach(['error', 'warn', 'info', 'log'], function(logLevel) {
392 | angular.forEach($log[logLevel].logs, function(log) {
393 | angular.forEach(log, function (logItem) {
394 | errors.push('MOCK $log (' + logLevel + '): ' + String(logItem) + '\n' + (logItem.stack || ''));
395 | });
396 | });
397 | });
398 | if (errors.length) {
399 | errors.unshift("Expected $log to be empty! Either a message was logged unexpectedly, or an expected " +
400 | "log message was not checked and removed:");
401 | errors.push('');
402 | throw new Error(errors.join('\n---------\n'));
403 | }
404 | };
405 |
406 | $log.reset();
407 | return $log;
408 | };
409 | };
410 |
411 |
412 | (function() {
413 | var R_ISO8061_STR = /^(\d{4})-?(\d\d)-?(\d\d)(?:T(\d\d)(?:\:?(\d\d)(?:\:?(\d\d)(?:\.(\d{3}))?)?)?(Z|([+-])(\d\d):?(\d\d)))?$/;
414 |
415 | function jsonStringToDate(string){
416 | var match;
417 | if (match = string.match(R_ISO8061_STR)) {
418 | var date = new Date(0),
419 | tzHour = 0,
420 | tzMin = 0;
421 | if (match[9]) {
422 | tzHour = int(match[9] + match[10]);
423 | tzMin = int(match[9] + match[11]);
424 | }
425 | date.setUTCFullYear(int(match[1]), int(match[2]) - 1, int(match[3]));
426 | date.setUTCHours(int(match[4]||0) - tzHour, int(match[5]||0) - tzMin, int(match[6]||0), int(match[7]||0));
427 | return date;
428 | }
429 | return string;
430 | }
431 |
432 | function int(str) {
433 | return parseInt(str, 10);
434 | }
435 |
436 | function padNumber(num, digits, trim) {
437 | var neg = '';
438 | if (num < 0) {
439 | neg = '-';
440 | num = -num;
441 | }
442 | num = '' + num;
443 | while(num.length < digits) num = '0' + num;
444 | if (trim)
445 | num = num.substr(num.length - digits);
446 | return neg + num;
447 | }
448 |
449 |
450 | /**
451 | * @ngdoc object
452 | * @name angular.mock.TzDate
453 | * @description
454 | *
455 | * *NOTE*: this is not an injectable instance, just a globally available mock class of `Date`.
456 | *
457 | * Mock of the Date type which has its timezone specified via constructor arg.
458 | *
459 | * The main purpose is to create Date-like instances with timezone fixed to the specified timezone
460 | * offset, so that we can test code that depends on local timezone settings without dependency on
461 | * the time zone settings of the machine where the code is running.
462 | *
463 | * @param {number} offset Offset of the *desired* timezone in hours (fractions will be honored)
464 | * @param {(number|string)} timestamp Timestamp representing the desired time in *UTC*
465 | *
466 | * @example
467 | * !!!! WARNING !!!!!
468 | * This is not a complete Date object so only methods that were implemented can be called safely.
469 | * To make matters worse, TzDate instances inherit stuff from Date via a prototype.
470 | *
471 | * We do our best to intercept calls to "unimplemented" methods, but since the list of methods is
472 | * incomplete we might be missing some non-standard methods. This can result in errors like:
473 | * "Date.prototype.foo called on incompatible Object".
474 | *
475 | *
637 | *
638 | */
639 | angular.mock.createMockWindow = function() {
640 | var mockWindow = {};
641 | var setTimeoutQueue = [];
642 |
643 | mockWindow.document = window.document;
644 | mockWindow.getComputedStyle = angular.bind(window, window.getComputedStyle);
645 | mockWindow.scrollTo = angular.bind(window, window.scrollTo);
646 | mockWindow.navigator = window.navigator;
647 | mockWindow.setTimeout = function(fn, delay) {
648 | setTimeoutQueue.push({fn: fn, delay: delay});
649 | };
650 | mockWindow.setTimeout.queue = setTimeoutQueue;
651 | mockWindow.setTimeout.expect = function(delay) {
652 | if (setTimeoutQueue.length > 0) {
653 | return {
654 | process: function() {
655 | var tick = setTimeoutQueue.shift();
656 | expect(tick.delay).toEqual(delay);
657 | tick.fn();
658 | }
659 | };
660 | } else {
661 | expect('SetTimoutQueue empty. Expecting delay of ').toEqual(delay);
662 | }
663 | };
664 |
665 | return mockWindow;
666 | };
667 |
668 | /**
669 | * @ngdoc function
670 | * @name angular.mock.dump
671 | * @description
672 | *
673 | * *NOTE*: this is not an injectable instance, just a globally available function.
674 | *
675 | * Method for serializing common angular objects (scope, elements, etc..) into strings, useful for debugging.
676 | *
677 | * This method is also available on window, where it can be used to display objects on debug console.
678 | *
679 | * @param {*} object - any object to turn into string.
680 | * @return {string} a serialized string of the argument
681 | */
682 | angular.mock.dump = function(object) {
683 | return serialize(object);
684 |
685 | function serialize(object) {
686 | var out;
687 |
688 | if (angular.isElement(object)) {
689 | object = angular.element(object);
690 | out = angular.element('');
691 | angular.forEach(object, function(element) {
692 | out.append(angular.element(element).clone());
693 | });
694 | out = out.html();
695 | } else if (angular.isArray(object)) {
696 | out = [];
697 | angular.forEach(object, function(o) {
698 | out.push(serialize(o));
699 | });
700 | out = '[ ' + out.join(', ') + ' ]';
701 | } else if (angular.isObject(object)) {
702 | if (angular.isFunction(object.$eval) && angular.isFunction(object.$apply)) {
703 | out = serializeScope(object);
704 | } else if (object instanceof Error) {
705 | out = object.stack || ('' + object.name + ': ' + object.message);
706 | } else {
707 | out = angular.toJson(object, true);
708 | }
709 | } else {
710 | out = String(object);
711 | }
712 |
713 | return out;
714 | }
715 |
716 | function serializeScope(scope, offset) {
717 | offset = offset || ' ';
718 | var log = [offset + 'Scope(' + scope.$id + '): {'];
719 | for ( var key in scope ) {
720 | if (scope.hasOwnProperty(key) && !key.match(/^(\$|this)/)) {
721 | log.push(' ' + key + ': ' + angular.toJson(scope[key]));
722 | }
723 | }
724 | var child = scope.$$childHead;
725 | while(child) {
726 | log.push(serializeScope(child, offset + ' '));
727 | child = child.$$nextSibling;
728 | }
729 | log.push('}');
730 | return log.join('\n' + offset);
731 | }
732 | };
733 |
734 | /**
735 | * @ngdoc object
736 | * @name ngMock.$httpBackend
737 | * @description
738 | * Fake HTTP backend implementation suitable for unit testing applications that use the
739 | * {@link ng.$http $http service}.
740 | *
741 | * *Note*: For fake HTTP backend implementation suitable for end-to-end testing or backend-less
742 | * development please see {@link ngMockE2E.$httpBackend e2e $httpBackend mock}.
743 | *
744 | * During unit testing, we want our unit tests to run quickly and have no external dependencies so
745 | * we don’t want to send {@link https://developer.mozilla.org/en/xmlhttprequest XHR} or
746 | * {@link http://en.wikipedia.org/wiki/JSONP JSONP} requests to a real server. All we really need is
747 | * to verify whether a certain request has been sent or not, or alternatively just let the
748 | * application make requests, respond with pre-trained responses and assert that the end result is
749 | * what we expect it to be.
750 | *
751 | * This mock implementation can be used to respond with static or dynamic responses via the
752 | * `expect` and `when` apis and their shortcuts (`expectGET`, `whenPOST`, etc).
753 | *
754 | * When an Angular application needs some data from a server, it calls the $http service, which
755 | * sends the request to a real server using $httpBackend service. With dependency injection, it is
756 | * easy to inject $httpBackend mock (which has the same API as $httpBackend) and use it to verify
757 | * the requests and respond with some testing data without sending a request to real server.
758 | *
759 | * There are two ways to specify what test data should be returned as http responses by the mock
760 | * backend when the code under test makes http requests:
761 | *
762 | * - `$httpBackend.expect` - specifies a request expectation
763 | * - `$httpBackend.when` - specifies a backend definition
764 | *
765 | *
766 | * # Request Expectations vs Backend Definitions
767 | *
768 | * Request expectations provide a way to make assertions about requests made by the application and
769 | * to define responses for those requests. The test will fail if the expected requests are not made
770 | * or they are made in the wrong order.
771 | *
772 | * Backend definitions allow you to define a fake backend for your application which doesn't assert
773 | * if a particular request was made or not, it just returns a trained response if a request is made.
774 | * The test will pass whether or not the request gets made during testing.
775 | *
776 | *
777 | *
778 | *
Request expectations
Backend definitions
779 | *
780 | *
Syntax
781 | *
.expect(...).respond(...)
782 | *
.when(...).respond(...)
783 | *
784 | *
785 | *
Typical usage
786 | *
strict unit tests
787 | *
loose (black-box) unit testing
788 | *
789 | *
790 | *
Fulfills multiple requests
791 | *
NO
792 | *
YES
793 | *
794 | *
795 | *
Order of requests matters
796 | *
YES
797 | *
NO
798 | *
799 | *
800 | *
Request required
801 | *
YES
802 | *
NO
803 | *
804 | *
805 | *
Response required
806 | *
optional (see below)
807 | *
YES
808 | *
809 | *
810 | *
811 | * In cases where both backend definitions and request expectations are specified during unit
812 | * testing, the request expectations are evaluated first.
813 | *
814 | * If a request expectation has no response specified, the algorithm will search your backend
815 | * definitions for an appropriate response.
816 | *
817 | * If a request didn't match any expectation or if the expectation doesn't have the response
818 | * defined, the backend definitions are evaluated in sequential order to see if any of them match
819 | * the request. The response from the first matched definition is returned.
820 | *
821 | *
822 | * # Flushing HTTP requests
823 | *
824 | * The $httpBackend used in production, always responds to requests with responses asynchronously.
825 | * If we preserved this behavior in unit testing, we'd have to create async unit tests, which are
826 | * hard to write, follow and maintain. At the same time the testing mock, can't respond
827 | * synchronously because that would change the execution of the code under test. For this reason the
828 | * mock $httpBackend has a `flush()` method, which allows the test to explicitly flush pending
829 | * requests and thus preserving the async api of the backend, while allowing the test to execute
830 | * synchronously.
831 | *
832 | *
833 | * # Unit testing with mock $httpBackend
834 | *
835 | *
836 | // controller
837 | function MyController($scope, $http) {
838 | $http.get('/auth.py').success(function(data) {
839 | $scope.user = data;
840 | });
841 |
842 | this.saveMessage = function(message) {
843 | $scope.status = 'Saving...';
844 | $http.post('/add-msg.py', message).success(function(response) {
845 | $scope.status = '';
846 | }).error(function() {
847 | $scope.status = 'ERROR!';
848 | });
849 | };
850 | }
851 |
852 | // testing controller
853 | var $httpBackend;
854 |
855 | beforeEach(inject(function($injector) {
856 | $httpBackend = $injector.get('$httpBackend');
857 |
858 | // backend definition common for all tests
859 | $httpBackend.when('GET', '/auth.py').respond({userId: 'userX'}, {'A-Token': 'xxx'});
860 | }));
861 |
862 |
863 | afterEach(function() {
864 | $httpBackend.verifyNoOutstandingExpectation();
865 | $httpBackend.verifyNoOutstandingRequest();
866 | });
867 |
868 |
869 | it('should fetch authentication token', function() {
870 | $httpBackend.expectGET('/auth.py');
871 | var controller = scope.$new(MyController);
872 | $httpBackend.flush();
873 | });
874 |
875 |
876 | it('should send msg to server', function() {
877 | // now you don’t care about the authentication, but
878 | // the controller will still send the request and
879 | // $httpBackend will respond without you having to
880 | // specify the expectation and response for this request
881 | $httpBackend.expectPOST('/add-msg.py', 'message content').respond(201, '');
882 |
883 | var controller = scope.$new(MyController);
884 | $httpBackend.flush();
885 | controller.saveMessage('message content');
886 | expect(controller.status).toBe('Saving...');
887 | $httpBackend.flush();
888 | expect(controller.status).toBe('');
889 | });
890 |
891 |
892 | it('should send auth header', function() {
893 | $httpBackend.expectPOST('/add-msg.py', undefined, function(headers) {
894 | // check if the header was send, if it wasn't the expectation won't
895 | // match the request and the test will fail
896 | return headers['Authorization'] == 'xxx';
897 | }).respond(201, '');
898 |
899 | var controller = scope.$new(MyController);
900 | controller.saveMessage('whatever');
901 | $httpBackend.flush();
902 | });
903 |
904 | */
905 | angular.mock.$HttpBackendProvider = function() {
906 | this.$get = ['$rootScope', createHttpBackendMock];
907 | };
908 |
909 | /**
910 | * General factory function for $httpBackend mock.
911 | * Returns instance for unit testing (when no arguments specified):
912 | * - passing through is disabled
913 | * - auto flushing is disabled
914 | *
915 | * Returns instance for e2e testing (when `$delegate` and `$browser` specified):
916 | * - passing through (delegating request to real backend) is enabled
917 | * - auto flushing is enabled
918 | *
919 | * @param {Object=} $delegate Real $httpBackend instance (allow passing through if specified)
920 | * @param {Object=} $browser Auto-flushing enabled if specified
921 | * @return {Object} Instance of $httpBackend mock
922 | */
923 | function createHttpBackendMock($rootScope, $delegate, $browser) {
924 | var definitions = [],
925 | expectations = [],
926 | responses = [],
927 | responsesPush = angular.bind(responses, responses.push);
928 |
929 | function createResponse(status, data, headers) {
930 | if (angular.isFunction(status)) return status;
931 |
932 | return function() {
933 | return angular.isNumber(status)
934 | ? [status, data, headers]
935 | : [200, status, data];
936 | };
937 | }
938 |
939 | // TODO(vojta): change params to: method, url, data, headers, callback
940 | function $httpBackend(method, url, data, callback, headers, timeout) {
941 | var xhr = new MockXhr(),
942 | expectation = expectations[0],
943 | wasExpected = false;
944 |
945 | function prettyPrint(data) {
946 | return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp)
947 | ? data
948 | : angular.toJson(data);
949 | }
950 |
951 | function wrapResponse(wrapped) {
952 | if (!$browser && timeout && timeout.then) timeout.then(handleTimeout);
953 |
954 | return handleResponse;
955 |
956 | function handleResponse() {
957 | var response = wrapped.response(method, url, data, headers);
958 | xhr.$$respHeaders = response[2];
959 | callback(response[0], response[1], xhr.getAllResponseHeaders());
960 | }
961 |
962 | function handleTimeout() {
963 | for (var i = 0, ii = responses.length; i < ii; i++) {
964 | if (responses[i] === handleResponse) {
965 | responses.splice(i, 1);
966 | callback(-1, undefined, '');
967 | break;
968 | }
969 | }
970 | }
971 | }
972 |
973 | if (expectation && expectation.match(method, url)) {
974 | if (!expectation.matchData(data))
975 | throw Error('Expected ' + expectation + ' with different data\n' +
976 | 'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT: ' + data);
977 |
978 | if (!expectation.matchHeaders(headers))
979 | throw Error('Expected ' + expectation + ' with different headers\n' +
980 | 'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT: ' +
981 | prettyPrint(headers));
982 |
983 | expectations.shift();
984 |
985 | if (expectation.response) {
986 | responses.push(wrapResponse(expectation));
987 | return;
988 | }
989 | wasExpected = true;
990 | }
991 |
992 | var i = -1, definition;
993 | while ((definition = definitions[++i])) {
994 | if (definition.match(method, url, data, headers || {})) {
995 | if (definition.response) {
996 | // if $browser specified, we do auto flush all requests
997 | ($browser ? $browser.defer : responsesPush)(wrapResponse(definition));
998 | } else if (definition.passThrough) {
999 | $delegate(method, url, data, callback, headers, timeout);
1000 | } else throw Error('No response defined !');
1001 | return;
1002 | }
1003 | }
1004 | throw wasExpected ?
1005 | Error('No response defined !') :
1006 | Error('Unexpected request: ' + method + ' ' + url + '\n' +
1007 | (expectation ? 'Expected ' + expectation : 'No more request expected'));
1008 | }
1009 |
1010 | /**
1011 | * @ngdoc method
1012 | * @name ngMock.$httpBackend#when
1013 | * @methodOf ngMock.$httpBackend
1014 | * @description
1015 | * Creates a new backend definition.
1016 | *
1017 | * @param {string} method HTTP method.
1018 | * @param {string|RegExp} url HTTP url.
1019 | * @param {(string|RegExp)=} data HTTP request body.
1020 | * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
1021 | * object and returns true if the headers match the current definition.
1022 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1023 | * request is handled.
1024 | *
1025 | * - respond – `{function([status,] data[, headers])|function(function(method, url, data, headers)}`
1026 | * – The respond method takes a set of static data to be returned or a function that can return
1027 | * an array containing response status (number), response data (string) and response headers
1028 | * (Object).
1029 | */
1030 | $httpBackend.when = function(method, url, data, headers) {
1031 | var definition = new MockHttpExpectation(method, url, data, headers),
1032 | chain = {
1033 | respond: function(status, data, headers) {
1034 | definition.response = createResponse(status, data, headers);
1035 | }
1036 | };
1037 |
1038 | if ($browser) {
1039 | chain.passThrough = function() {
1040 | definition.passThrough = true;
1041 | };
1042 | }
1043 |
1044 | definitions.push(definition);
1045 | return chain;
1046 | };
1047 |
1048 | /**
1049 | * @ngdoc method
1050 | * @name ngMock.$httpBackend#whenGET
1051 | * @methodOf ngMock.$httpBackend
1052 | * @description
1053 | * Creates a new backend definition for GET requests. For more info see `when()`.
1054 | *
1055 | * @param {string|RegExp} url HTTP url.
1056 | * @param {(Object|function(Object))=} headers HTTP headers.
1057 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1058 | * request is handled.
1059 | */
1060 |
1061 | /**
1062 | * @ngdoc method
1063 | * @name ngMock.$httpBackend#whenHEAD
1064 | * @methodOf ngMock.$httpBackend
1065 | * @description
1066 | * Creates a new backend definition for HEAD requests. For more info see `when()`.
1067 | *
1068 | * @param {string|RegExp} url HTTP url.
1069 | * @param {(Object|function(Object))=} headers HTTP headers.
1070 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1071 | * request is handled.
1072 | */
1073 |
1074 | /**
1075 | * @ngdoc method
1076 | * @name ngMock.$httpBackend#whenDELETE
1077 | * @methodOf ngMock.$httpBackend
1078 | * @description
1079 | * Creates a new backend definition for DELETE requests. For more info see `when()`.
1080 | *
1081 | * @param {string|RegExp} url HTTP url.
1082 | * @param {(Object|function(Object))=} headers HTTP headers.
1083 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1084 | * request is handled.
1085 | */
1086 |
1087 | /**
1088 | * @ngdoc method
1089 | * @name ngMock.$httpBackend#whenPOST
1090 | * @methodOf ngMock.$httpBackend
1091 | * @description
1092 | * Creates a new backend definition for POST requests. For more info see `when()`.
1093 | *
1094 | * @param {string|RegExp} url HTTP url.
1095 | * @param {(string|RegExp)=} data HTTP request body.
1096 | * @param {(Object|function(Object))=} headers HTTP headers.
1097 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1098 | * request is handled.
1099 | */
1100 |
1101 | /**
1102 | * @ngdoc method
1103 | * @name ngMock.$httpBackend#whenPUT
1104 | * @methodOf ngMock.$httpBackend
1105 | * @description
1106 | * Creates a new backend definition for PUT requests. For more info see `when()`.
1107 | *
1108 | * @param {string|RegExp} url HTTP url.
1109 | * @param {(string|RegExp)=} data HTTP request body.
1110 | * @param {(Object|function(Object))=} headers HTTP headers.
1111 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1112 | * request is handled.
1113 | */
1114 |
1115 | /**
1116 | * @ngdoc method
1117 | * @name ngMock.$httpBackend#whenJSONP
1118 | * @methodOf ngMock.$httpBackend
1119 | * @description
1120 | * Creates a new backend definition for JSONP requests. For more info see `when()`.
1121 | *
1122 | * @param {string|RegExp} url HTTP url.
1123 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1124 | * request is handled.
1125 | */
1126 | createShortMethods('when');
1127 |
1128 |
1129 | /**
1130 | * @ngdoc method
1131 | * @name ngMock.$httpBackend#expect
1132 | * @methodOf ngMock.$httpBackend
1133 | * @description
1134 | * Creates a new request expectation.
1135 | *
1136 | * @param {string} method HTTP method.
1137 | * @param {string|RegExp} url HTTP url.
1138 | * @param {(string|RegExp)=} data HTTP request body.
1139 | * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
1140 | * object and returns true if the headers match the current expectation.
1141 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1142 | * request is handled.
1143 | *
1144 | * - respond – `{function([status,] data[, headers])|function(function(method, url, data, headers)}`
1145 | * – The respond method takes a set of static data to be returned or a function that can return
1146 | * an array containing response status (number), response data (string) and response headers
1147 | * (Object).
1148 | */
1149 | $httpBackend.expect = function(method, url, data, headers) {
1150 | var expectation = new MockHttpExpectation(method, url, data, headers);
1151 | expectations.push(expectation);
1152 | return {
1153 | respond: function(status, data, headers) {
1154 | expectation.response = createResponse(status, data, headers);
1155 | }
1156 | };
1157 | };
1158 |
1159 |
1160 | /**
1161 | * @ngdoc method
1162 | * @name ngMock.$httpBackend#expectGET
1163 | * @methodOf ngMock.$httpBackend
1164 | * @description
1165 | * Creates a new request expectation for GET requests. For more info see `expect()`.
1166 | *
1167 | * @param {string|RegExp} url HTTP url.
1168 | * @param {Object=} headers HTTP headers.
1169 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1170 | * request is handled. See #expect for more info.
1171 | */
1172 |
1173 | /**
1174 | * @ngdoc method
1175 | * @name ngMock.$httpBackend#expectHEAD
1176 | * @methodOf ngMock.$httpBackend
1177 | * @description
1178 | * Creates a new request expectation for HEAD requests. For more info see `expect()`.
1179 | *
1180 | * @param {string|RegExp} url HTTP url.
1181 | * @param {Object=} headers HTTP headers.
1182 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1183 | * request is handled.
1184 | */
1185 |
1186 | /**
1187 | * @ngdoc method
1188 | * @name ngMock.$httpBackend#expectDELETE
1189 | * @methodOf ngMock.$httpBackend
1190 | * @description
1191 | * Creates a new request expectation for DELETE requests. For more info see `expect()`.
1192 | *
1193 | * @param {string|RegExp} url HTTP url.
1194 | * @param {Object=} headers HTTP headers.
1195 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1196 | * request is handled.
1197 | */
1198 |
1199 | /**
1200 | * @ngdoc method
1201 | * @name ngMock.$httpBackend#expectPOST
1202 | * @methodOf ngMock.$httpBackend
1203 | * @description
1204 | * Creates a new request expectation for POST requests. For more info see `expect()`.
1205 | *
1206 | * @param {string|RegExp} url HTTP url.
1207 | * @param {(string|RegExp)=} data HTTP request body.
1208 | * @param {Object=} headers HTTP headers.
1209 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1210 | * request is handled.
1211 | */
1212 |
1213 | /**
1214 | * @ngdoc method
1215 | * @name ngMock.$httpBackend#expectPUT
1216 | * @methodOf ngMock.$httpBackend
1217 | * @description
1218 | * Creates a new request expectation for PUT requests. For more info see `expect()`.
1219 | *
1220 | * @param {string|RegExp} url HTTP url.
1221 | * @param {(string|RegExp)=} data HTTP request body.
1222 | * @param {Object=} headers HTTP headers.
1223 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1224 | * request is handled.
1225 | */
1226 |
1227 | /**
1228 | * @ngdoc method
1229 | * @name ngMock.$httpBackend#expectPATCH
1230 | * @methodOf ngMock.$httpBackend
1231 | * @description
1232 | * Creates a new request expectation for PATCH requests. For more info see `expect()`.
1233 | *
1234 | * @param {string|RegExp} url HTTP url.
1235 | * @param {(string|RegExp)=} data HTTP request body.
1236 | * @param {Object=} headers HTTP headers.
1237 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1238 | * request is handled.
1239 | */
1240 |
1241 | /**
1242 | * @ngdoc method
1243 | * @name ngMock.$httpBackend#expectJSONP
1244 | * @methodOf ngMock.$httpBackend
1245 | * @description
1246 | * Creates a new request expectation for JSONP requests. For more info see `expect()`.
1247 | *
1248 | * @param {string|RegExp} url HTTP url.
1249 | * @returns {requestHandler} Returns an object with `respond` method that control how a matched
1250 | * request is handled.
1251 | */
1252 | createShortMethods('expect');
1253 |
1254 |
1255 | /**
1256 | * @ngdoc method
1257 | * @name ngMock.$httpBackend#flush
1258 | * @methodOf ngMock.$httpBackend
1259 | * @description
1260 | * Flushes all pending requests using the trained responses.
1261 | *
1262 | * @param {number=} count Number of responses to flush (in the order they arrived). If undefined,
1263 | * all pending requests will be flushed. If there are no pending requests when the flush method
1264 | * is called an exception is thrown (as this typically a sign of programming error).
1265 | */
1266 | $httpBackend.flush = function(count) {
1267 | $rootScope.$digest();
1268 | if (!responses.length) throw Error('No pending request to flush !');
1269 |
1270 | if (angular.isDefined(count)) {
1271 | while (count--) {
1272 | if (!responses.length) throw Error('No more pending request to flush !');
1273 | responses.shift()();
1274 | }
1275 | } else {
1276 | while (responses.length) {
1277 | responses.shift()();
1278 | }
1279 | }
1280 | $httpBackend.verifyNoOutstandingExpectation();
1281 | };
1282 |
1283 |
1284 | /**
1285 | * @ngdoc method
1286 | * @name ngMock.$httpBackend#verifyNoOutstandingExpectation
1287 | * @methodOf ngMock.$httpBackend
1288 | * @description
1289 | * Verifies that all of the requests defined via the `expect` api were made. If any of the
1290 | * requests were not made, verifyNoOutstandingExpectation throws an exception.
1291 | *
1292 | * Typically, you would call this method following each test case that asserts requests using an
1293 | * "afterEach" clause.
1294 | *
1295 | *