| OLD | NEW |
| 1 part of angular.mock; | 1 part of angular.mock; |
| 2 | 2 |
| 3 class _MockXhr { | 3 class _MockXhr { |
| 4 var $$method, $$url, $$async, $$reqHeaders, $$respHeaders; | 4 var $$method, $$url, $$async, $$reqHeaders, $$respHeaders; |
| 5 | 5 |
| 6 open(method, url, async) { | 6 void open(method, url, async) { |
| 7 $$method = method; | 7 $$method = method; |
| 8 $$url = url; | 8 $$url = url; |
| 9 $$async = async; | 9 $$async = async; |
| 10 $$reqHeaders = {}; | 10 $$reqHeaders = {}; |
| 11 $$respHeaders = {}; | 11 $$respHeaders = {}; |
| 12 } | 12 } |
| 13 | 13 |
| 14 var $$data; | 14 var $$data; |
| 15 | 15 |
| 16 send(data) { | 16 void send(data) { |
| 17 $$data = data; | 17 $$data = data; |
| 18 } | 18 } |
| 19 | 19 |
| 20 setRequestHeader(key, value) { | 20 void setRequestHeader(key, value) { |
| 21 $$reqHeaders[key] = value; | 21 $$reqHeaders[key] = value; |
| 22 } | 22 } |
| 23 | 23 |
| 24 getResponseHeader(name) { | 24 String getResponseHeader(name) { |
| 25 // the lookup must be case insensitive, that's why we try two quick lookups
and full scan at last | 25 // the lookup must be case insensitive, that's why we try two quick |
| 26 if ($$respHeaders.containsKey(name)) { | 26 // lookups and full scan at last |
| 27 return $$respHeaders[name]; | 27 if ($$respHeaders.containsKey(name)) return $$respHeaders[name]; |
| 28 } | |
| 29 | 28 |
| 30 name = name.toLowerCase(); | 29 name = name.toLowerCase(); |
| 31 if ($$respHeaders.containsKey(name)) { | 30 if ($$respHeaders.containsKey(name)) return $$respHeaders[name]; |
| 32 return $$respHeaders[name]; | |
| 33 } | |
| 34 | 31 |
| 35 String header = null; | 32 String header = null; |
| 36 $$respHeaders.forEach((headerName, headerVal) { | 33 $$respHeaders.forEach((headerName, headerVal) { |
| 37 if (header != null) return; | 34 if (header != null) return; |
| 38 if (headerName.toLowerCase()) header = headerVal; | 35 if (headerName.toLowerCase()) header = headerVal; |
| 39 }); | 36 }); |
| 40 return header; | 37 return header; |
| 41 } | 38 } |
| 42 | 39 |
| 43 getAllResponseHeaders() { | 40 getAllResponseHeaders() { |
| 44 if ($$respHeaders == null) return ''; | 41 if ($$respHeaders == null) return ''; |
| 45 | 42 |
| 46 var lines = []; | 43 var lines = []; |
| 47 | 44 |
| 48 $$respHeaders.forEach((key, value) { | 45 $$respHeaders.forEach((key, value) { |
| 49 lines.add("$key: $value"); | 46 lines.add("$key: $value"); |
| 50 }); | 47 }); |
| 51 return lines.join('\n'); | 48 return lines.join('\n'); |
| 52 } | 49 } |
| 53 | 50 |
| 54 // noop | 51 // noop |
| 55 abort() {} | 52 abort() {} |
| 56 } | 53 } |
| 57 | 54 |
| 58 /** | 55 /** |
| 59 * An internal class used by [MockHttpBackend]. | 56 * An internal class used by [MockHttpBackend]. |
| 60 */ | 57 */ |
| 61 class MockHttpExpectation { | 58 class MockHttpExpectation { |
| 62 | 59 final method; |
| 63 var method, url, data, headers; | 60 final url; |
| 61 final data; |
| 62 final headers; |
| 64 | 63 |
| 65 var response; | 64 var response; |
| 66 | 65 |
| 67 MockHttpExpectation(this.method, this.url, [this.data, this.headers]); | 66 MockHttpExpectation(this.method, this.url, [this.data, this.headers]); |
| 68 | 67 |
| 69 match(m, u, [d, h]) { | 68 bool match(method, url, [data, headers]) { |
| 70 if (method != m) return false; | 69 if (method != method) return false; |
| 71 if (!matchUrl(u)) return false; | 70 if (!matchUrl(url)) return false; |
| 72 if (d != null && !matchData(d)) return false; | 71 if (data != null && !matchData(data)) return false; |
| 73 if (h != null && !matchHeaders(h)) return false; | 72 if (headers != null && !matchHeaders(headers)) return false; |
| 74 return true; | 73 return true; |
| 75 } | 74 } |
| 76 | 75 |
| 77 matchUrl(u) { | 76 bool matchUrl(u) { |
| 78 if (url == null) return true; | 77 if (url == null) return true; |
| 79 if (url is RegExp) return url.hasMatch(u); | 78 if (url is RegExp) return url.hasMatch(u); |
| 80 return url == u; | 79 return url == u; |
| 81 } | 80 } |
| 82 | 81 |
| 83 matchHeaders(h) { | 82 bool matchHeaders(h) { |
| 84 if (headers == null) return true; | 83 if (headers == null) return true; |
| 85 if (headers is Function) return headers(h); | 84 if (headers is Function) return headers(h); |
| 86 return "$headers" == "$h"; | 85 return "$headers" == "$h"; |
| 87 } | 86 } |
| 88 | 87 |
| 89 matchData(d) { | 88 bool matchData(d) { |
| 90 if (data == null) return true; | 89 if (data == null) return true; |
| 91 if (d == null) return false; // data is not null, but d is. | 90 if (d == null) return false; |
| 92 if (data is File) return data == d; | 91 if (data is File) return data == d; |
| 93 assert(d is String); | 92 assert(d is String); |
| 94 if (data is RegExp) return data.hasMatch(d); | 93 if (data is RegExp) return data.hasMatch(d); |
| 95 return JSON.encode(data) == JSON.encode(d); | 94 return JSON.encode(data) == JSON.encode(d); |
| 96 } | 95 } |
| 97 | 96 |
| 98 toString() { | 97 String toString() => "$method $url"; |
| 99 return "$method $url"; | |
| 100 } | |
| 101 } | 98 } |
| 102 | 99 |
| 103 | 100 |
| 104 class _Chain { | 101 class _Chain { |
| 105 var _respondFn; | 102 final Function _respondFn; |
| 106 _Chain({respond}) { | 103 _Chain({respond}): _respondFn = respond; |
| 107 _respondFn = respond; | |
| 108 } | |
| 109 respond([x,y,z]) => _respondFn(x,y,z); | 104 respond([x,y,z]) => _respondFn(x,y,z); |
| 110 } | 105 } |
| 111 | 106 |
| 112 /** | 107 /** |
| 113 * A mock implementation of [HttpBackend], used in tests. | 108 * A mock implementation of [HttpBackend], used in tests. |
| 114 */ | 109 */ |
| 115 class MockHttpBackend implements HttpBackend { | 110 class MockHttpBackend implements HttpBackend { |
| 116 var definitions = [], | 111 var definitions = [], |
| 117 expectations = [], | 112 expectations = [], |
| 118 responses = []; | 113 responses = []; |
| 119 | 114 |
| 120 /** | 115 /** |
| 121 * This function is called from [Http] and designed to mimic the Dart APIs. | 116 * This function is called from [Http] and designed to mimic the Dart APIs. |
| 122 */ | 117 */ |
| 123 dart_async.Future request(String url, | 118 dart_async.Future request(String url, |
| 124 {String method, bool withCredentials, String responseType, | 119 {String method, bool withCredentials, String responseType, |
| 125 String mimeType, Map<String, String> requestHeaders, sendData, | 120 String mimeType, Map<String, String> requestHeaders, sendData, |
| 126 void onProgress(ProgressEvent e)}) { | 121 void onProgress(ProgressEvent e)}) { |
| 127 dart_async.Completer c = new dart_async.Completer(); | 122 dart_async.Completer c = new dart_async.Completer(); |
| 128 var callback = (status, data, headers) { | 123 var callback = (status, data, headers) { |
| 129 if (status >= 200 && status < 300) { | 124 if (status >= 200 && status < 300) { |
| 130 c.complete(new MockHttpRequest(status, data, headers)); | 125 c.complete(new MockHttpRequest(status, data, headers)); |
| 131 } else { | 126 } else { |
| 132 c.completeError( | 127 c.completeError(new MockProgressEvent( |
| 133 new MockProgressEvent( | 128 new MockHttpRequest(status, data, headers))); |
| 134 new MockHttpRequest(status, data, headers))); | |
| 135 } | 129 } |
| 136 }; | 130 }; |
| 137 call(method == null ? 'GET' : method, url, sendData, callback, requestHeader
s); | 131 call(method == null ? 'GET' : method, url, sendData, callback, |
| 132 requestHeaders); |
| 138 return c.future; | 133 return c.future; |
| 139 } | 134 } |
| 140 | 135 |
| 141 _createResponse(statusOrDataOrFunction, dataOrHeaders, headersOrNone) { | 136 _createResponse(statusOrDataOrFunction, dataOrHeaders, headersOrNone) { |
| 142 if (statusOrDataOrFunction is Function) return statusOrDataOrFunction; | 137 if (statusOrDataOrFunction is Function) return statusOrDataOrFunction; |
| 143 var status, data, headers; | 138 var status, data, headers; |
| 144 if (statusOrDataOrFunction is num) { | 139 if (statusOrDataOrFunction is num) { |
| 145 status = statusOrDataOrFunction; | 140 status = statusOrDataOrFunction; |
| 146 data = dataOrHeaders; | 141 data = dataOrHeaders; |
| 147 headers = headersOrNone; | 142 headers = headersOrNone; |
| 148 } else { | 143 } else { |
| 149 status = 200; | 144 status = 200; |
| 150 data = statusOrDataOrFunction; | 145 data = statusOrDataOrFunction; |
| 151 headers = dataOrHeaders; | 146 headers = dataOrHeaders; |
| 152 } | 147 } |
| 153 if (data is Map || data is List) { | 148 if (data is Map || data is List) data = JSON.encode(data); |
| 154 data = JSON.encode(data); | |
| 155 } | |
| 156 | 149 |
| 157 return ([a,b,c,d,e]) => [status, data, headers]; | 150 return ([a,b,c,d,e]) => [status, data, headers]; |
| 158 } | 151 } |
| 159 | 152 |
| 160 | 153 |
| 161 /** | 154 /** |
| 162 * A callback oriented API. This function takes a callback with | 155 * A callback oriented API. This function takes a callback with |
| 163 * will be called with (status, data, headers) | 156 * will be called with (status, data, headers) |
| 164 */ | 157 */ |
| 165 call(method, [url, data, callback, headers, timeout]) { | 158 void call(method, [url, data, callback, headers, timeout]) { |
| 166 var xhr = new _MockXhr(), | 159 var xhr = new _MockXhr(), |
| 167 expectation = expectations.isEmpty ? null : expectations[0], | 160 expectation = expectations.isEmpty ? null : expectations[0], |
| 168 wasExpected = false; | 161 wasExpected = false; |
| 169 | 162 |
| 170 var prettyPrint = (data) { | 163 var prettyPrint = (data) { |
| 171 return (data is String || data is Function || data is RegExp) | 164 return (data is String || data is Function || data is RegExp) |
| 172 ? data | 165 ? data |
| 173 : JSON.encode(data); | 166 : JSON.encode(data); |
| 174 }; | 167 }; |
| 175 | 168 |
| 176 var wrapResponse = (wrapped) { | 169 var wrapResponse = (wrapped) { |
| 177 var handleResponse = () { | 170 var handleResponse = () { |
| 178 var response = wrapped.response(method, url, data, headers); | 171 var response = wrapped.response(method, url, data, headers); |
| 179 xhr.$$respHeaders = response[2]; | 172 xhr.$$respHeaders = response[2]; |
| 180 utils.relaxFnApply(callback, [response[0], response[1], xhr.getAllRespon
seHeaders()]); | 173 utils.relaxFnApply(callback, [response[0], response[1], |
| 174 xhr.getAllResponseHeaders()]); |
| 181 }; | 175 }; |
| 182 | 176 |
| 183 var handleTimeout = () { | 177 var handleTimeout = () { |
| 184 for (var i = 0, ii = responses.length; i < ii; i++) { | 178 for (var i = 0; i < responses.length; i++) { |
| 185 if (identical(responses[i], handleResponse)) { | 179 if (identical(responses[i], handleResponse)) { |
| 186 responses.removeAt(i); | 180 responses.removeAt(i); |
| 187 callback(-1, null, ''); | 181 callback(-1, null, ''); |
| 188 break; | 182 break; |
| 189 } | 183 } |
| 190 } | 184 } |
| 191 }; | 185 }; |
| 192 | 186 |
| 193 if (timeout != null) timeout.then(handleTimeout); | 187 if (timeout != null) timeout.then(handleTimeout); |
| 194 return handleResponse; | 188 return handleResponse; |
| 195 }; | 189 }; |
| 196 | 190 |
| 197 if (expectation != null && expectation.match(method, url)) { | 191 if (expectation != null && expectation.match(method, url)) { |
| 198 if (!expectation.matchData(data)) | 192 if (!expectation.matchData(data)) |
| 199 throw ['Expected $expectation with different data\n' + | 193 throw ['Expected $expectation with different data\n' + |
| 200 'EXPECTED: ${prettyPrint(expectation.data)}\nGOT: $data']; | 194 'EXPECTED: ${prettyPrint(expectation.data)}\nGOT: $data']; |
| 201 | 195 |
| 202 if (!expectation.matchHeaders(headers)) | 196 if (!expectation.matchHeaders(headers)) |
| 203 throw ['Expected $expectation with different headers\n' + | 197 throw ['Expected $expectation with different headers\n' |
| 204 'EXPECTED: ${prettyPrint(expectation.headers)}\nGOT: ${prettyPr
int(headers)}']; | 198 'EXPECTED: ${prettyPrint(expectation.headers)}\n' |
| 199 'GOT: ${prettyPrint(headers)}']; |
| 205 | 200 |
| 206 expectations.removeAt(0); | 201 expectations.removeAt(0); |
| 207 | 202 |
| 208 if (expectation.response != null) { | 203 if (expectation.response != null) { |
| 209 responses.add(wrapResponse(expectation)); | 204 responses.add(wrapResponse(expectation)); |
| 210 return; | 205 return; |
| 211 } | 206 } |
| 212 wasExpected = true; | 207 wasExpected = true; |
| 213 } | 208 } |
| 214 | 209 |
| 215 for (var definition in definitions) { | 210 for (var definition in definitions) { |
| 216 if (definition.match(method, url, data, headers != null ? headers : {})) { | 211 if (definition.match(method, url, data, headers != null ? headers : {})) { |
| 217 if (definition.response != null) { | 212 if (definition.response != null) { |
| 218 // if $browser specified, we do auto flush all requests | 213 // if $browser specified, we do auto flush all requests |
| 219 responses.add(wrapResponse(definition)); | 214 responses.add(wrapResponse(definition)); |
| 220 } else throw ['No response defined !']; | 215 } else throw ['No response defined !']; |
| 221 return; | 216 return; |
| 222 } | 217 } |
| 223 } | 218 } |
| 224 throw wasExpected ? | 219 throw wasExpected ? |
| 225 ['No response defined !'] : | 220 ['No response defined !'] : |
| 226 ['Unexpected request: $method $url\n' + | 221 ['Unexpected request: $method $url\n' + (expectation != null ? |
| 227 (expectation != null ? 'Expected $expectation' : 'No more requests e
xpected')]; | 222 'Expected $expectation' : |
| 223 'No more requests expected')]; |
| 228 } | 224 } |
| 229 | 225 |
| 230 /** | 226 /** |
| 231 * Creates a new backend definition. | 227 * Creates a new backend definition. |
| 232 * | 228 * |
| 233 * @param {string} method HTTP method. | 229 * @param {string} method HTTP method. |
| 234 * @param {string|RegExp} url HTTP url. | 230 * @param {string|RegExp} url HTTP url. |
| 235 * @param {(string|RegExp)=} data HTTP request body. | 231 * @param {(string|RegExp)=} data HTTP request body. |
| 236 * @param {(Object|function(Object))=} headers HTTP headers or function that r
eceives http header | 232 * @param {(Object|function(Object))=} headers HTTP headers or function that |
| 237 * object and returns true if the headers match the current definition. | 233 * receives http header object and returns true if the headers match the |
| 238 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 234 * current definition. |
| 239 * request is handled. | 235 * @returns {requestHandler} Returns an object with `respond` method that |
| 236 * control how a matched request is handled. |
| 240 * | 237 * |
| 241 * - respond – `{function([status,] data[, headers])|function(function(method
, url, data, headers)}` | 238 * - respond – `{function([status,] data[, headers])|function(function(method
, url, data, headers)}` |
| 242 * – The respond method takes a set of static data to be returned or a func
tion that can return | 239 * – The respond method takes a set of static data to be returned or a func
tion that can return |
| 243 * an array containing response status (number), response data (string) and
response headers | 240 * an array containing response status (number), response data (string) and
response headers |
| 244 * (Object). | 241 * (Object). |
| 245 */ | 242 */ |
| 246 when(method, [url, data, headers]) { | 243 _Chain when(method, [url, data, headers]) { |
| 247 var definition = new MockHttpExpectation(method, url, data, headers), | 244 var definition = new MockHttpExpectation(method, url, data, headers), |
| 248 chain = new _Chain(respond: (status, data, headers) { | 245 chain = new _Chain(respond: (status, data, headers) { |
| 249 definition.response = _createResponse(status, data, headers); | 246 definition.response = _createResponse(status, data, headers); |
| 250 }); | 247 }); |
| 251 | 248 |
| 252 definitions.add(definition); | 249 definitions.add(definition); |
| 253 return chain; | 250 return chain; |
| 254 } | 251 } |
| 255 | 252 |
| 256 /** | 253 /** |
| 257 * @ngdoc method | 254 * @ngdoc method |
| 258 * @name ngMock.$httpBackend#whenGET | 255 * @name ngMock.$httpBackend#whenGET |
| 259 * @methodOf ngMock.$httpBackend | 256 * @methodOf ngMock.$httpBackend |
| 260 * @description | 257 * @description |
| 261 * Creates a new backend definition for GET requests. For more info see `when(
)`. | 258 * Creates a new backend definition for GET requests. For more info see `when(
)`. |
| 262 * | 259 * |
| 263 * @param {string|RegExp} url HTTP url. | 260 * @param {string|RegExp} url HTTP url. |
| 264 * @param {(Object|function(Object))=} headers HTTP headers. | 261 * @param {(Object|function(Object))=} headers HTTP headers. |
| 265 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 262 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
| 266 * request is handled. | 263 * request is handled. |
| 267 */ | 264 */ |
| 268 | 265 _Chain whenGET(url, [headers]) => when('GET', url, null, headers); |
| 269 | |
| 270 whenGET(url, [headers]) => | |
| 271 when('GET', url, null, headers); | |
| 272 whenDELETE(url, [headers]) => | |
| 273 when('DELETE', url, null, headers); | |
| 274 whenJSONP(url, [headers]) => | |
| 275 when('JSONP', url, null, headers); | |
| 276 | |
| 277 whenPUT(url, [data, headers]) => | |
| 278 when('PUT', url, data, headers); | |
| 279 whenPOST(url, [data, headers]) => | |
| 280 when('POST', url, data, headers); | |
| 281 whenPATCH(url, [data, headers]) => | |
| 282 when('PATCH', url, data, headers); | |
| 283 | |
| 284 /** | |
| 285 * @ngdoc method | |
| 286 * @name ngMock.$httpBackend#whenHEAD | |
| 287 * @methodOf ngMock.$httpBackend | |
| 288 * @description | |
| 289 * Creates a new backend definition for HEAD requests. For more info see `when
()`. | |
| 290 * | |
| 291 * @param {string|RegExp} url HTTP url. | |
| 292 * @param {(Object|function(Object))=} headers HTTP headers. | |
| 293 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | |
| 294 * request is handled. | |
| 295 */ | |
| 296 | 266 |
| 297 /** | 267 /** |
| 298 * @ngdoc method | 268 * @ngdoc method |
| 299 * @name ngMock.$httpBackend#whenDELETE | 269 * @name ngMock.$httpBackend#whenDELETE |
| 300 * @methodOf ngMock.$httpBackend | 270 * @methodOf ngMock.$httpBackend |
| 301 * @description | 271 * @description |
| 302 * Creates a new backend definition for DELETE requests. For more info see `wh
en()`. | 272 * Creates a new backend definition for DELETE requests. For more info see `wh
en()`. |
| 303 * | 273 * |
| 304 * @param {string|RegExp} url HTTP url. | 274 * @param {string|RegExp} url HTTP url. |
| 305 * @param {(Object|function(Object))=} headers HTTP headers. | 275 * @param {(Object|function(Object))=} headers HTTP headers. |
| 306 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 276 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
| 307 * request is handled. | 277 * request is handled. |
| 308 */ | 278 */ |
| 279 _Chain whenDELETE(url, [headers]) => when('DELETE', url, null, headers); |
| 309 | 280 |
| 310 /** | 281 /** |
| 311 * @ngdoc method | 282 * @ngdoc method |
| 312 * @name ngMock.$httpBackend#whenPOST | 283 * @name ngMock.$httpBackend#whenJSONP |
| 313 * @methodOf ngMock.$httpBackend | 284 * @methodOf ngMock.$httpBackend |
| 314 * @description | 285 * @description |
| 315 * Creates a new backend definition for POST requests. For more info see `when
()`. | 286 * Creates a new backend definition for JSONP requests. For more info see `whe
n()`. |
| 287 * |
| 288 * @param {string|RegExp} url HTTP url. |
| 289 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
| 290 * request is handled. |
| 291 */ |
| 292 _Chain whenJSONP(url, [headers]) => when('JSONP', url, null, headers); |
| 293 |
| 294 /** |
| 295 * @ngdoc method |
| 296 * @name ngMock.$httpBackend#whenPUT |
| 297 * @methodOf ngMock.$httpBackend |
| 298 * @description |
| 299 * Creates a new backend definition for PUT requests. For more info see `when
()`. |
| 316 * | 300 * |
| 317 * @param {string|RegExp} url HTTP url. | 301 * @param {string|RegExp} url HTTP url. |
| 318 * @param {(string|RegExp)=} data HTTP request body. | 302 * @param {(string|RegExp)=} data HTTP request body. |
| 319 * @param {(Object|function(Object))=} headers HTTP headers. | 303 * @param {(Object|function(Object))=} headers HTTP headers. |
| 320 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 304 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
| 321 * request is handled. | 305 * request is handled. |
| 322 */ | 306 */ |
| 307 _Chain whenPUT(url, [data, headers]) => when('PUT', url, data, headers); |
| 323 | 308 |
| 324 /** | 309 /** |
| 325 * @ngdoc method | 310 * @ngdoc method |
| 326 * @name ngMock.$httpBackend#whenPUT | 311 * @name ngMock.$httpBackend#whenPOST |
| 327 * @methodOf ngMock.$httpBackend | 312 * @methodOf ngMock.$httpBackend |
| 328 * @description | 313 * @description |
| 329 * Creates a new backend definition for PUT requests. For more info see `when
()`. | 314 * Creates a new backend definition for POST requests. For more info see `when
()`. |
| 330 * | 315 * |
| 331 * @param {string|RegExp} url HTTP url. | 316 * @param {string|RegExp} url HTTP url. |
| 332 * @param {(string|RegExp)=} data HTTP request body. | 317 * @param {(string|RegExp)=} data HTTP request body. |
| 333 * @param {(Object|function(Object))=} headers HTTP headers. | 318 * @param {(Object|function(Object))=} headers HTTP headers. |
| 334 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 319 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
| 335 * request is handled. | 320 * request is handled. |
| 336 */ | 321 */ |
| 322 _Chain whenPOST(url, [data, headers]) => when('POST', url, data, headers); |
| 323 |
| 324 _Chain whenPATCH(url, [data, headers]) => when('PATCH', url, data, headers); |
| 325 |
| 326 /** |
| 327 * @ngdoc method |
| 328 * @name ngMock.$httpBackend#whenHEAD |
| 329 * @methodOf ngMock.$httpBackend |
| 330 * @description |
| 331 * Creates a new backend definition for HEAD requests. For more info see `when
()`. |
| 332 * |
| 333 * @param {string|RegExp} url HTTP url. |
| 334 * @param {(Object|function(Object))=} headers HTTP headers. |
| 335 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
| 336 * request is handled. |
| 337 */ |
| 337 | 338 |
| 338 /** | 339 /** |
| 339 * @ngdoc method | 340 * @ngdoc method |
| 340 * @name ngMock.$httpBackend#whenJSONP | |
| 341 * @methodOf ngMock.$httpBackend | |
| 342 * @description | |
| 343 * Creates a new backend definition for JSONP requests. For more info see `whe
n()`. | |
| 344 * | |
| 345 * @param {string|RegExp} url HTTP url. | |
| 346 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | |
| 347 * request is handled. | |
| 348 */ | |
| 349 //createShortMethods('when'); | |
| 350 | |
| 351 | |
| 352 /** | |
| 353 * @ngdoc method | |
| 354 * @name ngMock.$httpBackend#expect | 341 * @name ngMock.$httpBackend#expect |
| 355 * @methodOf ngMock.$httpBackend | 342 * @methodOf ngMock.$httpBackend |
| 356 * @description | 343 * @description |
| 357 * Creates a new request expectation. | 344 * Creates a new request expectation. |
| 358 * | 345 * |
| 359 * @param {string} method HTTP method. | 346 * @param {string} method HTTP method. |
| 360 * @param {string|RegExp} url HTTP url. | 347 * @param {string|RegExp} url HTTP url. |
| 361 * @param {(string|RegExp)=} data HTTP request body. | 348 * @param {(string|RegExp)=} data HTTP request body. |
| 362 * @param {(Object|function(Object))=} headers HTTP headers or function that r
eceives http header | 349 * @param {(Object|function(Object))=} headers HTTP headers or function that r
eceives http header |
| 363 * object and returns true if the headers match the current expectation. | 350 * object and returns true if the headers match the current expectation. |
| 364 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 351 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
| 365 * request is handled. | 352 * request is handled. |
| 366 * | 353 * |
| 367 * - respond – `{function([status,] data[, headers])|function(function(method
, url, data, headers)}` | 354 * - respond – `{function([status,] data[, headers])|function(function(method
, url, data, headers)}` |
| 368 * – The respond method takes a set of static data to be returned or a func
tion that can return | 355 * – The respond method takes a set of static data to be returned or a func
tion that can return |
| 369 * an array containing response status (number), response data (string) and
response headers | 356 * an array containing response status (number), response data (string) and
response headers |
| 370 * (Object). | 357 * (Object). |
| 371 */ | 358 */ |
| 372 expect(method, [url, data, headers]) { | 359 _Chain expect(method, [url, data, headers]) { |
| 373 var expectation = new MockHttpExpectation(method, url, data, headers); | 360 var expectation = new MockHttpExpectation(method, url, data, headers); |
| 374 expectations.add(expectation); | 361 expectations.add(expectation); |
| 375 return new _Chain(respond: (status, data, headers) { | 362 return new _Chain(respond: (status, data, headers) { |
| 376 expectation.response = _createResponse(status, data, headers); | 363 expectation.response = _createResponse(status, data, headers); |
| 377 }); | 364 }); |
| 378 } | 365 } |
| 379 | 366 |
| 380 | 367 |
| 381 /** | 368 /** |
| 382 * @ngdoc method | 369 * @ngdoc method |
| 383 * @name ngMock.$httpBackend#expectGET | 370 * @name ngMock.$httpBackend#expectGET |
| 384 * @methodOf ngMock.$httpBackend | 371 * @methodOf ngMock.$httpBackend |
| 385 * @description | 372 * @description |
| 386 * Creates a new request expectation for GET requests. For more info see `expe
ct()`. | 373 * Creates a new request expectation for GET requests. For more info see `expe
ct()`. |
| 387 * | 374 * |
| 388 * @param {string|RegExp} url HTTP url. | 375 * @param {string|RegExp} url HTTP url. |
| 389 * @param {Object=} headers HTTP headers. | 376 * @param {Object=} headers HTTP headers. |
| 390 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 377 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
| 391 * request is handled. See #expect for more info. | 378 * request is handled. See #expect for more info. |
| 392 */ | 379 */ |
| 393 expectGET(url, [headers]) => | 380 _Chain expectGET(url, [headers]) => expect('GET', url, null, headers); |
| 394 expect('GET', url, null, headers); | |
| 395 expectDELETE(url, [headers]) => | |
| 396 expect('DELETE', url, null, headers); | |
| 397 expectJSONP(url, [headers]) => | |
| 398 expect('JSONP', url, null, headers); | |
| 399 | |
| 400 expectPUT(url, [data, headers]) => | |
| 401 expect('PUT', url, data, headers); | |
| 402 expectPOST(url, [data, headers]) => | |
| 403 expect('POST', url, data, headers); | |
| 404 expectPATCH(url, [data, headers]) => | |
| 405 expect('PATCH', url, data, headers); | |
| 406 | |
| 407 /** | |
| 408 * @ngdoc method | |
| 409 * @name ngMock.$httpBackend#expectHEAD | |
| 410 * @methodOf ngMock.$httpBackend | |
| 411 * @description | |
| 412 * Creates a new request expectation for HEAD requests. For more info see `exp
ect()`. | |
| 413 * | |
| 414 * @param {string|RegExp} url HTTP url. | |
| 415 * @param {Object=} headers HTTP headers. | |
| 416 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | |
| 417 * request is handled. | |
| 418 */ | |
| 419 | 381 |
| 420 /** | 382 /** |
| 421 * @ngdoc method | 383 * @ngdoc method |
| 422 * @name ngMock.$httpBackend#expectDELETE | 384 * @name ngMock.$httpBackend#expectDELETE |
| 423 * @methodOf ngMock.$httpBackend | 385 * @methodOf ngMock.$httpBackend |
| 424 * @description | 386 * @description |
| 425 * Creates a new request expectation for DELETE requests. For more info see `e
xpect()`. | 387 * Creates a new request expectation for DELETE requests. For more info see `e
xpect()`. |
| 426 * | 388 * |
| 427 * @param {string|RegExp} url HTTP url. | 389 * @param {string|RegExp} url HTTP url. |
| 428 * @param {Object=} headers HTTP headers. | 390 * @param {Object=} headers HTTP headers. |
| 429 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 391 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
| 430 * request is handled. | 392 * request is handled. |
| 431 */ | 393 */ |
| 394 _Chain expectDELETE(url, [headers]) => expect('DELETE', url, null, headers); |
| 432 | 395 |
| 433 /** | 396 /** |
| 434 * @ngdoc method | 397 * @ngdoc method |
| 435 * @name ngMock.$httpBackend#expectPOST | 398 * @name ngMock.$httpBackend#expectJSONP |
| 436 * @methodOf ngMock.$httpBackend | 399 * @methodOf ngMock.$httpBackend |
| 437 * @description | 400 * @description |
| 438 * Creates a new request expectation for POST requests. For more info see `exp
ect()`. | 401 * Creates a new request expectation for JSONP requests. For more info see `ex
pect()`. |
| 402 * |
| 403 * @param {string|RegExp} url HTTP url. |
| 404 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
| 405 * request is handled. |
| 406 */ |
| 407 _Chain expectJSONP(url, [headers]) => expect('JSONP', url, null, headers); |
| 408 |
| 409 /** |
| 410 * @ngdoc method |
| 411 * @name ngMock.$httpBackend#expectPUT |
| 412 * @methodOf ngMock.$httpBackend |
| 413 * @description |
| 414 * Creates a new request expectation for PUT requests. For more info see `expe
ct()`. |
| 439 * | 415 * |
| 440 * @param {string|RegExp} url HTTP url. | 416 * @param {string|RegExp} url HTTP url. |
| 441 * @param {(string|RegExp)=} data HTTP request body. | 417 * @param {(string|RegExp)=} data HTTP request body. |
| 442 * @param {Object=} headers HTTP headers. | 418 * @param {Object=} headers HTTP headers. |
| 443 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 419 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
| 444 * request is handled. | 420 * request is handled. |
| 445 */ | 421 */ |
| 422 _Chain expectPUT(url, [data, headers]) => expect('PUT', url, data, headers); |
| 446 | 423 |
| 447 /** | 424 /** |
| 448 * @ngdoc method | 425 * @ngdoc method |
| 449 * @name ngMock.$httpBackend#expectPUT | 426 * @name ngMock.$httpBackend#expectPOST |
| 450 * @methodOf ngMock.$httpBackend | 427 * @methodOf ngMock.$httpBackend |
| 451 * @description | 428 * @description |
| 452 * Creates a new request expectation for PUT requests. For more info see `expe
ct()`. | 429 * Creates a new request expectation for POST requests. For more info see `exp
ect()`. |
| 453 * | 430 * |
| 454 * @param {string|RegExp} url HTTP url. | 431 * @param {string|RegExp} url HTTP url. |
| 455 * @param {(string|RegExp)=} data HTTP request body. | 432 * @param {(string|RegExp)=} data HTTP request body. |
| 456 * @param {Object=} headers HTTP headers. | 433 * @param {Object=} headers HTTP headers. |
| 457 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 434 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
| 458 * request is handled. | 435 * request is handled. |
| 459 */ | 436 */ |
| 437 _Chain expectPOST(url, [data, headers]) => expect('POST', url, data, headers); |
| 460 | 438 |
| 461 /** | 439 /** |
| 462 * @ngdoc method | 440 * @ngdoc method |
| 463 * @name ngMock.$httpBackend#expectPATCH | 441 * @name ngMock.$httpBackend#expectPATCH |
| 464 * @methodOf ngMock.$httpBackend | 442 * @methodOf ngMock.$httpBackend |
| 465 * @description | 443 * @description |
| 466 * Creates a new request expectation for PATCH requests. For more info see `ex
pect()`. | 444 * Creates a new request expectation for PATCH requests. For more info see `ex
pect()`. |
| 467 * | 445 * |
| 468 * @param {string|RegExp} url HTTP url. | 446 * @param {string|RegExp} url HTTP url. |
| 469 * @param {(string|RegExp)=} data HTTP request body. | 447 * @param {(string|RegExp)=} data HTTP request body. |
| 470 * @param {Object=} headers HTTP headers. | 448 * @param {Object=} headers HTTP headers. |
| 471 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 449 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
| 472 * request is handled. | 450 * request is handled. |
| 473 */ | 451 */ |
| 452 _Chain expectPATCH(url, [data, headers]) => expect('PATCH', url, data, headers
); |
| 453 |
| 454 /** |
| 455 * @ngdoc method |
| 456 * @name ngMock.$httpBackend#expectHEAD |
| 457 * @methodOf ngMock.$httpBackend |
| 458 * @description |
| 459 * Creates a new request expectation for HEAD requests. For more info see `exp
ect()`. |
| 460 * |
| 461 * @param {string|RegExp} url HTTP url. |
| 462 * @param {Object=} headers HTTP headers. |
| 463 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
| 464 * request is handled. |
| 465 */ |
| 474 | 466 |
| 475 /** | 467 /** |
| 476 * @ngdoc method | 468 * @ngdoc method |
| 477 * @name ngMock.$httpBackend#expectJSONP | |
| 478 * @methodOf ngMock.$httpBackend | |
| 479 * @description | |
| 480 * Creates a new request expectation for JSONP requests. For more info see `ex
pect()`. | |
| 481 * | |
| 482 * @param {string|RegExp} url HTTP url. | |
| 483 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | |
| 484 * request is handled. | |
| 485 */ | |
| 486 //createShortMethods('expect'); | |
| 487 | |
| 488 | |
| 489 /** | |
| 490 * @ngdoc method | |
| 491 * @name ngMock.$httpBackend#flush | 469 * @name ngMock.$httpBackend#flush |
| 492 * @methodOf ngMock.$httpBackend | 470 * @methodOf ngMock.$httpBackend |
| 493 * @description | 471 * @description |
| 494 * Flushes all pending requests using the trained responses. | 472 * Flushes all pending requests using the trained responses. |
| 495 * | 473 * |
| 496 * @param {number=} count Number of responses to flush (in the order they arri
ved). If undefined, | 474 * @param {number=} count Number of responses to flush (in the order they arri
ved). If undefined, |
| 497 * all pending requests will be flushed. If there are no pending requests wh
en the flush method | 475 * all pending requests will be flushed. If there are no pending requests wh
en the flush method |
| 498 * is called an exception is thrown (as this typically a sign of programming
error). | 476 * is called an exception is thrown (as this typically a sign of programming
error). |
| 499 */ | 477 */ |
| 500 flush([count]) { | 478 void flush([count]) { |
| 501 if (responses.isEmpty) throw ['No pending request to flush !']; | 479 if (responses.isEmpty) throw ['No pending request to flush !']; |
| 502 | 480 |
| 503 if (count != null) { | 481 if (count != null) { |
| 504 while (count-- > 0) { | 482 while (count-- > 0) { |
| 505 if (responses.isEmpty) throw ['No more pending request to flush !']; | 483 if (responses.isEmpty) throw ['No more pending request to flush !']; |
| 506 responses.removeAt(0)(); | 484 responses.removeAt(0)(); |
| 507 } | 485 } |
| 508 } else { | 486 } else { |
| 509 while (!responses.isEmpty) { | 487 while (!responses.isEmpty) { |
| 510 responses.removeAt(0)(); | 488 responses.removeAt(0)(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 522 * Verifies that all of the requests defined via the `expect` api were made. I
f any of the | 500 * Verifies that all of the requests defined via the `expect` api were made. I
f any of the |
| 523 * requests were not made, verifyNoOutstandingExpectation throws an exception. | 501 * requests were not made, verifyNoOutstandingExpectation throws an exception. |
| 524 * | 502 * |
| 525 * Typically, you would call this method following each test case that asserts
requests using an | 503 * Typically, you would call this method following each test case that asserts
requests using an |
| 526 * "afterEach" clause. | 504 * "afterEach" clause. |
| 527 * | 505 * |
| 528 * <pre> | 506 * <pre> |
| 529 * afterEach($httpBackend.verifyNoOutstandingExpectation); | 507 * afterEach($httpBackend.verifyNoOutstandingExpectation); |
| 530 * </pre> | 508 * </pre> |
| 531 */ | 509 */ |
| 532 verifyNoOutstandingExpectation() { | 510 void verifyNoOutstandingExpectation() { |
| 533 if (!expectations.isEmpty) { | 511 if (!expectations.isEmpty) { |
| 534 throw ['Unsatisfied requests: ${expectations.join(', ')}']; | 512 throw ['Unsatisfied requests: ${expectations.join(', ')}']; |
| 535 } | 513 } |
| 536 } | 514 } |
| 537 | 515 |
| 538 | 516 |
| 539 /** | 517 /** |
| 540 * @ngdoc method | 518 * @ngdoc method |
| 541 * @name ngMock.$httpBackend#verifyNoOutstandingRequest | 519 * @name ngMock.$httpBackend#verifyNoOutstandingRequest |
| 542 * @methodOf ngMock.$httpBackend | 520 * @methodOf ngMock.$httpBackend |
| 543 * @description | 521 * @description |
| 544 * Verifies that there are no outstanding requests that need to be flushed. | 522 * Verifies that there are no outstanding requests that need to be flushed. |
| 545 * | 523 * |
| 546 * Typically, you would call this method following each test case that asserts
requests using an | 524 * Typically, you would call this method following each test case that asserts
requests using an |
| 547 * "afterEach" clause. | 525 * "afterEach" clause. |
| 548 * | 526 * |
| 549 * <pre> | 527 * <pre> |
| 550 * afterEach($httpBackend.verifyNoOutstandingRequest); | 528 * afterEach($httpBackend.verifyNoOutstandingRequest); |
| 551 * </pre> | 529 * </pre> |
| 552 */ | 530 */ |
| 553 verifyNoOutstandingRequest() { | 531 void verifyNoOutstandingRequest() { |
| 554 if (!responses.isEmpty) { | 532 if (!responses.isEmpty) throw ['Unflushed requests: ${responses.length}']; |
| 555 throw ['Unflushed requests: ${responses.length}']; | |
| 556 } | |
| 557 } | 533 } |
| 558 | 534 |
| 559 | 535 |
| 560 /** | 536 /** |
| 561 * @ngdoc method | 537 * @ngdoc method |
| 562 * @name ngMock.$httpBackend#resetExpectations | 538 * @name ngMock.$httpBackend#resetExpectations |
| 563 * @methodOf ngMock.$httpBackend | 539 * @methodOf ngMock.$httpBackend |
| 564 * @description | 540 * @description |
| 565 * Resets all request expectations, but preserves all backend definitions. Typ
ically, you would | 541 * Resets all request expectations, but preserves all backend definitions. Typ
ically, you would |
| 566 * call resetExpectations during a multiple-phase test when you want to reuse
the same instance of | 542 * call resetExpectations during a multiple-phase test when you want to reuse
the same instance of |
| 567 * $httpBackend mock. | 543 * $httpBackend mock. |
| 568 */ | 544 */ |
| 569 resetExpectations() { | 545 void resetExpectations() { |
| 570 expectations.length = 0; | 546 expectations.length = 0; |
| 571 responses.length = 0; | 547 responses.length = 0; |
| 572 } | 548 } |
| 573 } | 549 } |
| 574 | 550 |
| 575 /** | 551 /** |
| 576 * Mock implementation of the [HttpRequest] object returned from the HttpBackend
. | 552 * Mock implementation of the [HttpRequest] object returned from the HttpBackend
. |
| 577 */ | 553 */ |
| 578 class MockHttpRequest implements HttpRequest { | 554 class MockHttpRequest implements HttpRequest { |
| 579 final bool supportsCrossOrigin = false; | 555 final bool supportsCrossOrigin = false; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 604 bool withCredentials; | 580 bool withCredentials; |
| 605 | 581 |
| 606 final int status; | 582 final int status; |
| 607 final response; | 583 final response; |
| 608 final String headers; | 584 final String headers; |
| 609 | 585 |
| 610 MockHttpRequest(this.status, this.response, [this.headers]); | 586 MockHttpRequest(this.status, this.response, [this.headers]); |
| 611 | 587 |
| 612 void abort() {} | 588 void abort() {} |
| 613 bool dispatchEvent(Event event) => false; | 589 bool dispatchEvent(Event event) => false; |
| 614 String getAllResponseHeaders() { | 590 String getAllResponseHeaders() => headers; |
| 615 if (headers == null) return null; | |
| 616 return headers; | |
| 617 } | |
| 618 String getResponseHeader(String header) => null; | 591 String getResponseHeader(String header) => null; |
| 619 | 592 |
| 620 void open(String method, String url, {bool async, String user, String password
}) {} | 593 void open(String method, String url, {bool async, String user, String password
}) {} |
| 621 void overrideMimeType(String override) {} | 594 void overrideMimeType(String override) {} |
| 622 void send([data]) {} | 595 void send([data]) {} |
| 623 void setRequestHeader(String header, String value) {} | 596 void setRequestHeader(String header, String value) {} |
| 624 void addEventListener(String type, EventListener listener, [bool useCapture])
{} | 597 void addEventListener(String type, EventListener listener, [bool useCapture])
{} |
| 625 void removeEventListener(String type, EventListener listener, [bool useCapture
]) {} | 598 void removeEventListener(String type, EventListener listener, [bool useCapture
]) {} |
| 626 } | 599 } |
| 627 | 600 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 645 final String type = null; | 618 final String type = null; |
| 646 | 619 |
| 647 bool cancelBubble = false; | 620 bool cancelBubble = false; |
| 648 | 621 |
| 649 MockProgressEvent(MockHttpRequest this.currentTarget); | 622 MockProgressEvent(MockHttpRequest this.currentTarget); |
| 650 | 623 |
| 651 void preventDefault() {} | 624 void preventDefault() {} |
| 652 void stopImmediatePropagation() {} | 625 void stopImmediatePropagation() {} |
| 653 void stopPropagation() {} | 626 void stopPropagation() {} |
| 654 } | 627 } |
| OLD | NEW |