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