| OLD | NEW |
| (Empty) |
| 1 library angular.mock.http_backend_spec; | |
| 2 | |
| 3 import '../_specs.dart'; | |
| 4 | |
| 5 class _Chain { | |
| 6 var _thenFn; | |
| 7 _Chain({then}) { | |
| 8 _thenFn = then; | |
| 9 } | |
| 10 | |
| 11 then(x) => _thenFn(x); | |
| 12 } | |
| 13 | |
| 14 main() => describe('MockHttpBackend', () { | |
| 15 TestBed _; | |
| 16 beforeEach(inject((TestBed tb) => _ = tb)); | |
| 17 | |
| 18 var hb, callback, realBackendSpy; | |
| 19 | |
| 20 var noop = (_, __) {}; | |
| 21 var undefined = null; | |
| 22 | |
| 23 beforeEach(inject((HttpBackend httpBackend) { | |
| 24 callback = jasmine.createSpy('callback'); | |
| 25 hb = httpBackend; | |
| 26 })); | |
| 27 | |
| 28 | |
| 29 it('should respond with first matched definition', () { | |
| 30 hb.when('GET', '/url1').respond(200, 'content', {}); | |
| 31 hb.when('GET', '/url1').respond(201, 'another', {}); | |
| 32 | |
| 33 callback.andCallFake((status, response) { | |
| 34 expect(status).toBe(200); | |
| 35 expect(response).toBe('content'); | |
| 36 }); | |
| 37 | |
| 38 hb('GET', '/url1', null, callback); | |
| 39 expect(callback).not.toHaveBeenCalled(); | |
| 40 hb.flush(); | |
| 41 expect(callback).toHaveBeenCalledOnce(); | |
| 42 }); | |
| 43 | |
| 44 | |
| 45 it('should respond with JSON', inject((Logger logger) { | |
| 46 hb.when('GET', '/url1').respond(200, ['abc'], {}); | |
| 47 hb.when('GET', '/url2').respond(200, {'key': 'value'}, {}); | |
| 48 | |
| 49 | |
| 50 callback.andCallFake((status, response) { | |
| 51 expect(status).toBe(200); | |
| 52 logger(response); | |
| 53 }); | |
| 54 | |
| 55 hb('GET', '/url1', null, callback); | |
| 56 hb('GET', '/url2', null, callback); | |
| 57 hb.flush(); | |
| 58 expect(logger).toEqual(['["abc"]', '{"key":"value"}']); | |
| 59 })); | |
| 60 | |
| 61 | |
| 62 it('should throw error when unexpected request', () { | |
| 63 hb.when('GET', '/url1').respond(200, 'content'); | |
| 64 expect(() { | |
| 65 hb('GET', '/xxx'); | |
| 66 }).toThrow('Unexpected request: GET /xxx\nNo more requests expected'); | |
| 67 }); | |
| 68 | |
| 69 | |
| 70 it('should throw an error on an exception in then', async(() { | |
| 71 hb.expectGET('/url').respond(200, 'content'); | |
| 72 | |
| 73 expect(() { | |
| 74 hb.request('/url', method: 'GET').then((x) { | |
| 75 throw ["exceptiona"]; | |
| 76 }); | |
| 77 hb.flush(); | |
| 78 microLeap(); | |
| 79 }).toThrow('exceptiona'); | |
| 80 })); | |
| 81 | |
| 82 | |
| 83 it('should match headers if specified', () { | |
| 84 try { | |
| 85 hb.when('GET', '/url', null, {'X': 'val1'}).respond(201, 'content1'); | |
| 86 hb.when('GET', '/url', null, {'X': 'val2'}).respond(202, 'content2'); | |
| 87 hb.when('GET', '/url').respond(203, 'content3'); | |
| 88 | |
| 89 hb('GET', '/url', null, (status, response) { | |
| 90 expect(status).toBe(203); | |
| 91 expect(response).toBe('content3'); | |
| 92 }); | |
| 93 | |
| 94 hb('GET', '/url', null, (status, response) { | |
| 95 expect(status).toBe(201); | |
| 96 expect(response).toBe('content1'); | |
| 97 }, {'X': 'val1'}); | |
| 98 | |
| 99 hb('GET', '/url', null, (status, response) { | |
| 100 expect(status).toBe(202); | |
| 101 expect(response).toBe('content2'); | |
| 102 }, {'X': 'val2'}); | |
| 103 | |
| 104 hb.flush(); | |
| 105 } catch (e,s) { print("$e $s"); } | |
| 106 }); | |
| 107 | |
| 108 | |
| 109 it('should match data if specified', () { | |
| 110 hb.when('GET', '/a/b', '{a: true}').respond(201, 'content1'); | |
| 111 hb.when('GET', '/a/b').respond(202, 'content2'); | |
| 112 | |
| 113 hb('GET', '/a/b', '{a: true}', (status, response) { | |
| 114 expect(status).toBe(201); | |
| 115 expect(response).toBe('content1'); | |
| 116 }); | |
| 117 | |
| 118 hb('GET', '/a/b', '{}', (status, response) { | |
| 119 expect(status).toBe(202); | |
| 120 expect(response).toBe('content2'); | |
| 121 }); | |
| 122 | |
| 123 hb.flush(); | |
| 124 }); | |
| 125 | |
| 126 | |
| 127 it('should match only method', () { | |
| 128 hb.when('GET').respond(202, 'c'); | |
| 129 callback.andCallFake((status, response) { | |
| 130 expect(status).toBe(202); | |
| 131 expect(response).toBe('c'); | |
| 132 }); | |
| 133 | |
| 134 hb('GET', '/some', null, callback, {}); | |
| 135 hb('GET', '/another', null, callback, {'X-Fake': 'Header'}); | |
| 136 hb('GET', '/third', 'some-data', callback, {}); | |
| 137 hb.flush(); | |
| 138 | |
| 139 expect(callback).toHaveBeenCalled(); | |
| 140 }); | |
| 141 | |
| 142 | |
| 143 it('should preserve the order of requests', () { | |
| 144 hb.when('GET', '/url1').respond(200, 'first'); | |
| 145 hb.when('GET', '/url2').respond(201, 'second'); | |
| 146 | |
| 147 hb('GET', '/url2', null, callback); | |
| 148 hb('GET', '/url1', null, callback); | |
| 149 | |
| 150 hb.flush(); | |
| 151 | |
| 152 expect(callback.callCount).toBe(2); | |
| 153 expect(callback.argsForCall[0]).toEqual([201, 'second', '']); | |
| 154 expect(callback.argsForCall[1]).toEqual([200, 'first', '']); | |
| 155 }); | |
| 156 | |
| 157 | |
| 158 describe('respond()', () { | |
| 159 it('should take values', () { | |
| 160 hb.expect('GET', '/url1').respond(200, 'first', {'header': 'val'}); | |
| 161 hb('GET', '/url1', null, callback); | |
| 162 hb.flush(); | |
| 163 | |
| 164 expect(callback).toHaveBeenCalledOnceWith(200, 'first', "header: val"); | |
| 165 }); | |
| 166 | |
| 167 it('should take function', () { | |
| 168 hb.expect('GET', '/some').respond((m, u, d, h) { | |
| 169 return [301, m + u + ';' + d + ';a=' + h['a'], {'Connection': 'keep-aliv
e'}]; | |
| 170 }); | |
| 171 | |
| 172 hb('GET', '/some', 'data', callback, {'a': 'b'}); | |
| 173 hb.flush(); | |
| 174 | |
| 175 expect(callback).toHaveBeenCalledOnceWith(301, 'GET/some;data;a=b', 'Conne
ction: keep-alive'); | |
| 176 }); | |
| 177 | |
| 178 it('should default status code to 200', () { | |
| 179 callback.andCallFake((status, response) { | |
| 180 expect(status).toBe(200); | |
| 181 expect(response).toBe('some-data'); | |
| 182 }); | |
| 183 | |
| 184 hb.expect('GET', '/url1').respond('some-data'); | |
| 185 hb.expect('GET', '/url2').respond('some-data', {'X-Header': 'true'}); | |
| 186 hb('GET', '/url1', null, callback); | |
| 187 hb('GET', '/url2', null, callback); | |
| 188 hb.flush(); | |
| 189 expect(callback).toHaveBeenCalled(); | |
| 190 expect(callback.callCount).toBe(2); | |
| 191 }); | |
| 192 | |
| 193 | |
| 194 it('should default response headers to ""', () { | |
| 195 hb.expect('GET', '/url1').respond(200, 'first'); | |
| 196 hb.expect('GET', '/url2').respond('second'); | |
| 197 | |
| 198 hb('GET', '/url1', null, callback); | |
| 199 hb('GET', '/url2', null, callback); | |
| 200 | |
| 201 hb.flush(); | |
| 202 | |
| 203 expect(callback.callCount).toBe(2); | |
| 204 expect(callback.argsForCall[0]).toEqual([200, 'first', '']); | |
| 205 expect(callback.argsForCall[1]).toEqual([200, 'second', '']); | |
| 206 }); | |
| 207 }); | |
| 208 | |
| 209 | |
| 210 describe('expect()', () { | |
| 211 it('should require specified order', () { | |
| 212 hb.expect('GET', '/url1').respond(200, ''); | |
| 213 hb.expect('GET', '/url2').respond(200, ''); | |
| 214 | |
| 215 expect(() { | |
| 216 hb('GET', '/url2', null, noop, {}); | |
| 217 }).toThrow('Unexpected request: GET /url2\nExpected GET /url1'); | |
| 218 }); | |
| 219 | |
| 220 | |
| 221 it('should have precedence over when()', () { | |
| 222 callback.andCallFake((status, response) { | |
| 223 expect(status).toBe(299); | |
| 224 expect(response).toBe('expect'); | |
| 225 }); | |
| 226 | |
| 227 hb.when('GET', '/url').respond(200, 'when'); | |
| 228 hb.expect('GET', '/url').respond(299, 'expect'); | |
| 229 | |
| 230 hb('GET', '/url', null, callback, null); | |
| 231 hb.flush(); | |
| 232 expect(callback).toHaveBeenCalledOnce(); | |
| 233 }); | |
| 234 | |
| 235 | |
| 236 it('should throw exception when only headers differs from expectation', () { | |
| 237 hb.when('GET', '/match').respond(200, '', {}); | |
| 238 hb.expect('GET', '/match', null, {'Content-Type': 'application/json'}).res
pond(200, '', {}); | |
| 239 | |
| 240 expect(() { | |
| 241 hb('GET', '/match', null, noop, {}); | |
| 242 }).toThrow('Expected GET /match with different headers\n' + | |
| 243 'EXPECTED: {"Content-Type":"application/json"}\nGOT: {}'); | |
| 244 }); | |
| 245 | |
| 246 | |
| 247 it('should throw exception when only data differs from expectation', () { | |
| 248 hb.when('GET', '/match').respond(200, '', {}); | |
| 249 hb.expect('GET', '/match', 'some-data').respond(200, '', {}); | |
| 250 | |
| 251 expect(() { | |
| 252 hb('GET', '/match', 'different', noop, null); | |
| 253 }).toThrow('Expected GET /match with different data\n' + | |
| 254 'EXPECTED: some-data\nGOT: different'); | |
| 255 }); | |
| 256 | |
| 257 | |
| 258 it("should use when's respond() when no expect() respond is defined", () { | |
| 259 callback.andCallFake((status, response) { | |
| 260 expect(status).toBe(201); | |
| 261 expect(response).toBe('data'); | |
| 262 }); | |
| 263 | |
| 264 hb.when('GET', '/some').respond(201, 'data'); | |
| 265 hb.expect('GET', '/some'); | |
| 266 hb('GET', '/some', null, callback); | |
| 267 hb.flush(); | |
| 268 | |
| 269 expect(callback).toHaveBeenCalled(); | |
| 270 expect(() { hb.verifyNoOutstandingExpectation(); }).not.toThrow(); | |
| 271 }); | |
| 272 }); | |
| 273 | |
| 274 | |
| 275 describe('flush()', () { | |
| 276 it('flush() should flush requests fired during callbacks', () { | |
| 277 hb.when('GET', '/some').respond(200, ''); | |
| 278 hb.when('GET', '/other').respond(200, ''); | |
| 279 hb('GET', '/some', null, (_, __) { | |
| 280 hb('GET', '/other', null, callback); | |
| 281 }); | |
| 282 | |
| 283 hb.flush(); | |
| 284 expect(callback).toHaveBeenCalled(); | |
| 285 }); | |
| 286 | |
| 287 | |
| 288 it('should flush given number of pending requests', () { | |
| 289 hb.when('GET').respond(200, ''); | |
| 290 hb('GET', '/some', null, callback); | |
| 291 hb('GET', '/some', null, callback); | |
| 292 hb('GET', '/some', null, callback); | |
| 293 | |
| 294 hb.flush(2); | |
| 295 expect(callback).toHaveBeenCalled(); | |
| 296 expect(callback.callCount).toBe(2); | |
| 297 }); | |
| 298 | |
| 299 | |
| 300 it('should throw exception when flushing more requests than pending', () { | |
| 301 hb.when('GET').respond(200, ''); | |
| 302 hb('GET', '/url', null, callback); | |
| 303 | |
| 304 expect(() {hb.flush(2);}).toThrow('No more pending request to flush !'); | |
| 305 expect(callback).toHaveBeenCalledOnce(); | |
| 306 }); | |
| 307 | |
| 308 | |
| 309 it('should throw exception when no request to flush', () { | |
| 310 expect(() {hb.flush();}).toThrow('No pending request to flush !'); | |
| 311 | |
| 312 hb.when('GET').respond(200, ''); | |
| 313 hb('GET', '/some', null, callback); | |
| 314 hb.flush(); | |
| 315 | |
| 316 expect(() {hb.flush();}).toThrow('No pending request to flush !'); | |
| 317 }); | |
| 318 | |
| 319 | |
| 320 it('should throw exception if not all expectations satisfied', () { | |
| 321 hb.expect('GET', '/url1').respond(); | |
| 322 hb.expect('GET', '/url2').respond(); | |
| 323 | |
| 324 hb('GET', '/url1', null, noop); | |
| 325 expect(() {hb.flush();}).toThrow('Unsatisfied requests: GET /url2'); | |
| 326 }); | |
| 327 }); | |
| 328 | |
| 329 | |
| 330 it('should abort requests when timeout promise resolves', () { | |
| 331 hb.expect('GET', '/url1').respond(200); | |
| 332 | |
| 333 var canceler, then = jasmine.createSpy('then').andCallFake((fn) { | |
| 334 canceler = fn; | |
| 335 }); | |
| 336 | |
| 337 hb('GET', '/url1', null, callback, null, new _Chain(then: then)); | |
| 338 expect(canceler is Function).toBe(true); | |
| 339 | |
| 340 canceler(); // simulate promise resolution | |
| 341 | |
| 342 expect(callback).toHaveBeenCalledWith(-1, null, ''); | |
| 343 hb.verifyNoOutstandingExpectation(); | |
| 344 hb.verifyNoOutstandingRequest(); | |
| 345 }); | |
| 346 | |
| 347 | |
| 348 it('should throw an exception if no response defined', () { | |
| 349 hb.when('GET', '/test'); | |
| 350 expect(() { | |
| 351 hb('GET', '/test', null, callback); | |
| 352 }).toThrow('No response defined !'); | |
| 353 }); | |
| 354 | |
| 355 | |
| 356 it('should throw an exception if no response for exception and no definition',
() { | |
| 357 hb.expect('GET', '/url'); | |
| 358 expect(() { | |
| 359 hb('GET', '/url', null, callback); | |
| 360 }).toThrow('No response defined !'); | |
| 361 }); | |
| 362 | |
| 363 | |
| 364 it('should respond undefined when JSONP method', () { | |
| 365 hb.when('JSONP', '/url1').respond(200); | |
| 366 hb.expect('JSONP', '/url2').respond(200); | |
| 367 | |
| 368 expect(hb('JSONP', '/url1')).toBeNull(); | |
| 369 expect(hb('JSONP', '/url2')).toBeNull(); | |
| 370 }); | |
| 371 | |
| 372 | |
| 373 describe('verifyExpectations', () { | |
| 374 | |
| 375 it('should throw exception if not all expectations were satisfied', () { | |
| 376 hb.expect('POST', '/u1', 'ddd').respond(201, '', {}); | |
| 377 hb.expect('GET', '/u2').respond(200, '', {}); | |
| 378 hb.expect('POST', '/u3').respond(201, '', {}); | |
| 379 | |
| 380 hb('POST', '/u1', 'ddd', noop, {}); | |
| 381 | |
| 382 expect(() {hb.verifyNoOutstandingExpectation();}). | |
| 383 toThrow('Unsatisfied requests: GET /u2, POST /u3'); | |
| 384 }); | |
| 385 | |
| 386 | |
| 387 it('should do nothing when no expectation', () { | |
| 388 hb.when('DELETE', '/some').respond(200, ''); | |
| 389 | |
| 390 expect(() {hb.verifyNoOutstandingExpectation();}).not.toThrow(); | |
| 391 }); | |
| 392 | |
| 393 | |
| 394 it('should do nothing when all expectations satisfied', () { | |
| 395 hb.expect('GET', '/u2').respond(200, '', {}); | |
| 396 hb.expect('POST', '/u3').respond(201, '', {}); | |
| 397 hb.when('DELETE', '/some').respond(200, ''); | |
| 398 | |
| 399 hb('GET', '/u2'); | |
| 400 hb('POST', '/u3'); | |
| 401 | |
| 402 expect(() {hb.verifyNoOutstandingExpectation();}).not.toThrow(); | |
| 403 }); | |
| 404 }); | |
| 405 | |
| 406 describe('verifyRequests', () { | |
| 407 | |
| 408 it('should throw exception if not all requests were flushed', () { | |
| 409 hb.when('GET').respond(200); | |
| 410 hb('GET', '/some', null, noop, {}); | |
| 411 | |
| 412 expect(() { | |
| 413 hb.verifyNoOutstandingRequest(); | |
| 414 }).toThrow('Unflushed requests: 1'); | |
| 415 }); | |
| 416 }); | |
| 417 | |
| 418 | |
| 419 describe('resetExpectations', () { | |
| 420 | |
| 421 it('should remove all expectations', () { | |
| 422 hb.expect('GET', '/u2').respond(200, '', {}); | |
| 423 hb.expect('POST', '/u3').respond(201, '', {}); | |
| 424 hb.resetExpectations(); | |
| 425 | |
| 426 expect(() {hb.verifyNoOutstandingExpectation();}).not.toThrow(); | |
| 427 }); | |
| 428 | |
| 429 | |
| 430 it('should remove all pending responses', () { | |
| 431 var cancelledClb = jasmine.createSpy('cancelled'); | |
| 432 | |
| 433 hb.expect('GET', '/url').respond(200, ''); | |
| 434 hb('GET', '/url', null, cancelledClb); | |
| 435 hb.resetExpectations(); | |
| 436 | |
| 437 hb.expect('GET', '/url').respond(300, ''); | |
| 438 hb('GET', '/url', null, callback, {}); | |
| 439 hb.flush(); | |
| 440 | |
| 441 expect(callback).toHaveBeenCalledOnce(); | |
| 442 expect(cancelledClb).not.toHaveBeenCalled(); | |
| 443 }); | |
| 444 | |
| 445 | |
| 446 it('should not remove definitions', () { | |
| 447 var cancelledClb = jasmine.createSpy('cancelled'); | |
| 448 | |
| 449 hb.when('GET', '/url').respond(200, 'success'); | |
| 450 hb('GET', '/url', null, cancelledClb); | |
| 451 hb.resetExpectations(); | |
| 452 | |
| 453 hb('GET', '/url', null, callback, {}); | |
| 454 hb.flush(); | |
| 455 | |
| 456 expect(callback).toHaveBeenCalledOnce(); | |
| 457 expect(cancelledClb).not.toHaveBeenCalled(); | |
| 458 }); | |
| 459 }); | |
| 460 | |
| 461 | |
| 462 describe('expect/when shortcuts', () { | |
| 463 [[(x) => hb.expectGET(x), 'GET'], | |
| 464 [(x) => hb.expectPOST(x), 'POST'], | |
| 465 [(x) => hb.expectPUT(x), 'PUT'], | |
| 466 [(x) => hb.expectPATCH(x), 'PATCH'], | |
| 467 [(x) => hb.expectDELETE(x), 'DELETE'], | |
| 468 [(x) => hb.expectJSONP(x), 'JSONP'], | |
| 469 [(x) => hb.whenGET(x), 'GET'], | |
| 470 [(x) => hb.whenPOST(x), 'POST'], | |
| 471 [(x) => hb.whenPUT(x), 'PUT'], | |
| 472 [(x) => hb.whenPATCH(x), 'PATCH'], | |
| 473 [(x) => hb.whenDELETE(x), 'DELETE'], | |
| 474 [(x) => hb.whenJSONP(x), 'JSONP'] | |
| 475 ].forEach((step) { | |
| 476 var shortcut = step[0], method = step[1]; | |
| 477 it('should provide $shortcut shortcut method', () { | |
| 478 shortcut('/foo').respond('bar'); | |
| 479 hb(method, '/foo', undefined, callback); | |
| 480 hb.flush(); | |
| 481 expect(callback).toHaveBeenCalledOnceWith(200, 'bar', ''); | |
| 482 }); | |
| 483 }); | |
| 484 }); | |
| 485 | |
| 486 | |
| 487 describe('MockHttpExpectation', () { | |
| 488 | |
| 489 it('should accept url as regexp', () { | |
| 490 var exp = new MockHttpExpectation('GET', new RegExp('^\/x')); | |
| 491 | |
| 492 expect(exp.match('GET', '/x')).toBe(true); | |
| 493 expect(exp.match('GET', '/xxx/x')).toBe(true); | |
| 494 expect(exp.match('GET', 'x')).toBe(false); | |
| 495 expect(exp.match('GET', 'a/x')).toBe(false); | |
| 496 }); | |
| 497 | |
| 498 | |
| 499 it('should accept data as regexp', () { | |
| 500 var exp = new MockHttpExpectation('POST', '/url', new RegExp('\{.*?\}')); | |
| 501 | |
| 502 expect(exp.match('POST', '/url', '{"a": "aa"}')).toBe(true); | |
| 503 expect(exp.match('POST', '/url', '{"one": "two"}')).toBe(true); | |
| 504 expect(exp.match('POST', '/url', '{"one"')).toBe(false); | |
| 505 }); | |
| 506 | |
| 507 | |
| 508 it('should accept headers as function', () { | |
| 509 var exp = new MockHttpExpectation('GET', '/url', undefined, (h) { | |
| 510 return h['Content-Type'] == 'application/json'; | |
| 511 }); | |
| 512 | |
| 513 expect(exp.matchHeaders({})).toBe(false); | |
| 514 expect(exp.matchHeaders({'Content-Type': 'application/json', 'X-Another':
'true'})).toBe(true); | |
| 515 }); | |
| 516 }); | |
| 517 }); | |
| OLD | NEW |