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