@@ -57,6 +57,10 @@ angular.module.ngMock.$Browser = function() {
5757 self . $$lastUrl = self . $$url ; // used by url polling fn
5858 self . pollFns = [ ] ;
5959
60+ // TODO(vojta): remove this temporary api
61+ self . $$completeOutstandingRequest = noop ;
62+ self . $$incOutstandingRequestCount = noop ;
63+
6064
6165 // register url polling fn
6266
@@ -73,165 +77,6 @@ angular.module.ngMock.$Browser = function() {
7377 return listener ;
7478 } ;
7579
76-
77- /**
78- * @ngdoc method
79- * @name angular.module.ngMock.$browser#xhr
80- * @methodOf angular.module.ngMock.$browser
81- *
82- * @description
83- * Generic method for training browser to expect a request in a test and respond to it.
84- *
85- * See also convenience methods for browser training:
86- *
87- * - {@link #xhr.expectGET}
88- * - {@link #xhr.expectPOST}
89- * - {@link #xhr.expectPUT}
90- * - {@link #xhr.expectDELETE}
91- * - {@link #xhr.expectJSON}
92- *
93- * To flush pending requests in tests use
94- * {@link #xhr.flush}.
95- *
96- * @param {string } method Expected HTTP method.
97- * @param {string } url Url path for which a request is expected.
98- * @param {(object|string)= } data Expected body of the (POST) HTTP request.
99- * @param {function(number, *) } callback Callback to call when response is flushed.
100- * @param {object } headers Key-value pairs of expected headers.
101- * @returns {object } Response configuration object. You can call its `respond()` method to
102- * configure what should the browser mock return when the response is
103- * {@link #xhr.flush flushed}.
104- */
105- self . xhr = function ( method , url , data , callback , headers ) {
106- headers = headers || { } ;
107- if ( data && angular . isObject ( data ) ) data = angular . toJson ( data ) ;
108- if ( data && angular . isString ( data ) ) url += "|" + data ;
109- var expect = expectations [ method ] || { } ;
110- var expectation = expect [ url ] ;
111- if ( ! expectation ) {
112- throw new Error ( "Unexpected request for method '" + method + "' and url '" + url + "'." ) ;
113- }
114- requests . push ( function ( ) {
115- angular . forEach ( expectation . headers , function ( value , key ) {
116- if ( headers [ key ] !== value ) {
117- throw new Error ( "Missing HTTP request header: " + key + ": " + value ) ;
118- }
119- } ) ;
120- callback ( expectation . code , expectation . response ) ;
121- } ) ;
122- // TODO(vojta): return mock request object
123- } ;
124- self . xhr . expectations = expectations ;
125- self . xhr . requests = requests ;
126- self . xhr . expect = function ( method , url , data , headers ) {
127- if ( data && angular . isObject ( data ) ) data = angular . toJson ( data ) ;
128- if ( data && angular . isString ( data ) ) url += "|" + data ;
129- var expect = expectations [ method ] || ( expectations [ method ] = { } ) ;
130- return {
131- respond : function ( code , response ) {
132- if ( ! angular . isNumber ( code ) ) {
133- response = code ;
134- code = 200 ;
135- }
136- expect [ url ] = { code :code , response :response , headers : headers || { } } ;
137- }
138- } ;
139- } ;
140-
141- /**
142- * @ngdoc method
143- * @name angular.module.ngMock.$browser#xhr.expectGET
144- * @methodOf angular.module.ngMock.$browser
145- *
146- * @description
147- * Trains browser to expect a `GET` request and respond to it.
148- *
149- * @param {string } url Url path for which a request is expected.
150- * @returns {object } Response configuration object. You can call its `respond()` method to
151- * configure what should the browser mock return when the response is
152- * {@link angular.module.ngMock.$browser#xhr.flush flushed}.
153- */
154- self . xhr . expectGET = angular . bind ( self , self . xhr . expect , 'GET' ) ;
155-
156- /**
157- * @ngdoc method
158- * @name angular.module.ngMock.$browser#xhr.expectPOST
159- * @methodOf angular.module.ngMock.$browser
160- *
161- * @description
162- * Trains browser to expect a `POST` request and respond to it.
163- *
164- * @param {string } url Url path for which a request is expected.
165- * @returns {object } Response configuration object. You can call its `respond()` method to
166- * configure what should the browser mock return when the response is
167- * {@link angular.module.ngMock.$browser#xhr.flush flushed}.
168- */
169- self . xhr . expectPOST = angular . bind ( self , self . xhr . expect , 'POST' ) ;
170-
171- /**
172- * @ngdoc method
173- * @name angular.module.ngMock.$browser#xhr.expectDELETE
174- * @methodOf angular.module.ngMock.$browser
175- *
176- * @description
177- * Trains browser to expect a `DELETE` request and respond to it.
178- *
179- * @param {string } url Url path for which a request is expected.
180- * @returns {object } Response configuration object. You can call its `respond()` method to
181- * configure what should the browser mock return when the response is
182- * {@link angular.module.ngMock.$browser#xhr.flush flushed}.
183- */
184- self . xhr . expectDELETE = angular . bind ( self , self . xhr . expect , 'DELETE' ) ;
185-
186- /**
187- * @ngdoc method
188- * @name angular.module.ngMock.$browser#xhr.expectPUT
189- * @methodOf angular.module.ngMock.$browser
190- *
191- * @description
192- * Trains browser to expect a `PUT` request and respond to it.
193- *
194- * @param {string } url Url path for which a request is expected.
195- * @returns {object } Response configuration object. You can call its `respond()` method to
196- * configure what should the browser mock return when the response is
197- * {@link angular.module.ngMock.$browser#xhr.flush flushed}.
198- */
199- self . xhr . expectPUT = angular . bind ( self , self . xhr . expect , 'PUT' ) ;
200-
201- /**
202- * @ngdoc method
203- * @name angular.module.ngMock.$browser#xhr.expectJSON
204- * @methodOf angular.module.ngMock.$browser
205- *
206- * @description
207- * Trains browser to expect a `JSON` request and respond to it.
208- *
209- * @param {string } url Url path for which a request is expected.
210- * @returns {object } Response configuration object. You can call its `respond()` method to
211- * configure what should the browser mock return when the response is
212- * {@link angular.module.ngMock.$browser#xhr.flush flushed}.
213- */
214- self . xhr . expectJSON = angular . bind ( self , self . xhr . expect , 'JSON' ) ;
215-
216- /**
217- * @ngdoc method
218- * @name angular.module.ngMock.$browser#xhr.flush
219- * @methodOf angular.module.ngMock.$browser
220- *
221- * @description
222- * Flushes all pending requests and executes xhr callbacks with the trained response as the
223- * argument.
224- */
225- self . xhr . flush = function ( ) {
226- if ( requests . length == 0 ) {
227- throw new Error ( "No xhr requests to be flushed!" ) ;
228- }
229-
230- while ( requests . length ) {
231- requests . pop ( ) ( ) ;
232- }
233- } ;
234-
23580 self . cookieHash = { } ;
23681 self . lastCookieHash = { } ;
23782 self . deferredFns = [ ] ;
@@ -871,9 +716,24 @@ function MockHttpExpectation(method, url, data, headers) {
871716
872717function MockXhr ( ) {
873718
874- // hack for testing $http
719+ // hack for testing $http, $httpBackend
875720 MockXhr . $$lastInstance = this ;
876721
722+ this . open = function ( method , url , async ) {
723+ this . $$method = method ;
724+ this . $$url = url ;
725+ this . $$async = async ;
726+ this . $$headers = { } ;
727+ } ;
728+
729+ this . send = function ( data ) {
730+ this . $$data = data ;
731+ } ;
732+
733+ this . setRequestHeader = function ( key , value ) {
734+ this . $$headers [ key ] = value ;
735+ } ;
736+
877737 this . getResponseHeader = function ( name ) {
878738 return this . $$headers [ name ] ;
879739 } ;
0 commit comments