| 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 main() => describe('MockHttpBackend', () { | 14 void main() { |
| 15 TestBed _; | 15 describe('MockHttpBackend', () { |
| 16 beforeEach(inject((TestBed tb) => _ = tb)); | 16 TestBed _; |
| 17 | 17 beforeEach((TestBed tb) => _ = tb); |
| 18 var hb, callback, realBackendSpy; | 18 |
| 19 | 19 var hb, callback, realBackendSpy; |
| 20 var noop = (_, __) {}; | 20 |
| 21 var undefined = null; | 21 var noop = (_, __) {}; |
| 22 | 22 var undefined = null; |
| 23 beforeEach(inject((HttpBackend httpBackend) { | 23 |
| 24 callback = jasmine.createSpy('callback'); | 24 beforeEach((HttpBackend httpBackend) { |
| 25 hb = httpBackend; | 25 callback = jasmine.createSpy('callback'); |
| 26 })); | 26 hb = httpBackend; |
| 27 | 27 }); |
| 28 | 28 |
| 29 it('should respond with first matched definition', () { | 29 |
| 30 hb.when('GET', '/url1').respond(200, 'content', {}); | 30 it('should respond with first matched definition', () { |
| 31 hb.when('GET', '/url1').respond(201, 'another', {}); | 31 hb.when('GET', '/url1').respond(200, 'content', {}); |
| 32 | 32 hb.when('GET', '/url1').respond(201, 'another', {}); |
| 33 callback.andCallFake((status, response) { | 33 |
| 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) { | 34 callback.andCallFake((status, response) { |
| 180 expect(status).toBe(200); | 35 expect(status).toBe(200); |
| 181 expect(response).toBe('some-data'); | 36 expect(response).toBe('content'); |
| 182 }); | 37 }); |
| 183 | 38 |
| 184 hb.expect('GET', '/url1').respond('some-data'); | 39 hb('GET', '/url1', null, callback); |
| 185 hb.expect('GET', '/url2').respond('some-data', {'X-Header': 'true'}); | 40 expect(callback).not.toHaveBeenCalled(); |
| 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 |
| 186 hb('GET', '/url1', null, callback); | 56 hb('GET', '/url1', null, callback); |
| 187 hb('GET', '/url2', null, callback); | 57 hb('GET', '/url2', null, callback); |
| 188 hb.flush(); | 58 hb.flush(); |
| 59 expect(logger).toEqual(['["abc"]', '{"key":"value"}']); |
| 60 }); |
| 61 |
| 62 |
| 63 it('should throw error when unexpected request', () { |
| 64 hb.when('GET', '/url1').respond(200, 'content'); |
| 65 expect(() { |
| 66 hb('GET', '/xxx'); |
| 67 }).toThrow('Unexpected request: GET /xxx\nNo more requests expected'); |
| 68 }); |
| 69 |
| 70 |
| 71 it('should throw an error on an exception in then', async(() { |
| 72 hb.expectGET('/url').respond(200, 'content'); |
| 73 |
| 74 expect(() { |
| 75 hb.request('/url', method: 'GET').then((x) { |
| 76 throw ["exceptiona"]; |
| 77 }); |
| 78 hb.flush(); |
| 79 microLeap(); |
| 80 }).toThrow('exceptiona'); |
| 81 })); |
| 82 |
| 83 |
| 84 it('should match headers if specified', () { |
| 85 try { |
| 86 hb.when('GET', '/url', null, {'X': 'val1'}).respond(201, 'content1'); |
| 87 hb.when('GET', '/url', null, {'X': 'val2'}).respond(202, 'content2'); |
| 88 hb.when('GET', '/url').respond(203, 'content3'); |
| 89 |
| 90 hb('GET', '/url', null, (status, response) { |
| 91 expect(status).toBe(203); |
| 92 expect(response).toBe('content3'); |
| 93 }); |
| 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); |
| 116 expect(response).toBe('content1'); |
| 117 }); |
| 118 |
| 119 hb('GET', '/a/b', '{}', (status, response) { |
| 120 expect(status).toBe(202); |
| 121 expect(response).toBe('content2'); |
| 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(); |
| 139 |
| 189 expect(callback).toHaveBeenCalled(); | 140 expect(callback).toHaveBeenCalled(); |
| 141 }); |
| 142 |
| 143 |
| 144 it('should preserve the order of requests', () { |
| 145 hb.when('GET', '/url1').respond(200, 'first'); |
| 146 hb.when('GET', '/url2').respond(201, 'second'); |
| 147 |
| 148 hb('GET', '/url2', null, callback); |
| 149 hb('GET', '/url1', null, callback); |
| 150 |
| 151 hb.flush(); |
| 152 |
| 190 expect(callback.callCount).toBe(2); | 153 expect(callback.callCount).toBe(2); |
| 191 }); | 154 expect(callback.argsForCall[0]).toEqual([201, 'second', '']); |
| 192 | 155 expect(callback.argsForCall[1]).toEqual([200, 'first', '']); |
| 193 | 156 }); |
| 194 it('should default response headers to ""', () { | 157 |
| 195 hb.expect('GET', '/url1').respond(200, 'first'); | 158 |
| 196 hb.expect('GET', '/url2').respond('second'); | 159 describe('respond()', () { |
| 197 | 160 it('should take values', () { |
| 198 hb('GET', '/url1', null, callback); | 161 hb.expect('GET', '/url1').respond(200, 'first', {'header': 'val'}); |
| 199 hb('GET', '/url2', null, callback); | 162 hb('GET', '/url1', null, callback); |
| 200 | 163 hb.flush(); |
| 201 hb.flush(); | 164 |
| 202 | 165 expect(callback).toHaveBeenCalledOnceWith(200, 'first', "header: val"); |
| 203 expect(callback.callCount).toBe(2); | 166 }); |
| 204 expect(callback.argsForCall[0]).toEqual([200, 'first', '']); | 167 |
| 205 expect(callback.argsForCall[1]).toEqual([200, 'second', '']); | 168 it('should take function', () { |
| 206 }); | 169 hb.expect('GET', '/some').respond((m, u, d, h) { |
| 207 }); | 170 return [301, m + u + ';' + d + ';a=' + h['a'], {'Connection': 'keep-al
ive'}]; |
| 208 | 171 }); |
| 209 | 172 |
| 210 describe('expect()', () { | 173 hb('GET', '/some', 'data', callback, {'a': 'b'}); |
| 211 it('should require specified order', () { | 174 hb.flush(); |
| 212 hb.expect('GET', '/url1').respond(200, ''); | 175 |
| 213 hb.expect('GET', '/url2').respond(200, ''); | 176 expect(callback).toHaveBeenCalledOnceWith(301, 'GET/some;data;a=b', 'Con
nection: keep-alive'); |
| 214 | 177 }); |
| 178 |
| 179 it('should default status code to 200', () { |
| 180 callback.andCallFake((status, response) { |
| 181 expect(status).toBe(200); |
| 182 expect(response).toBe('some-data'); |
| 183 }); |
| 184 |
| 185 hb.expect('GET', '/url1').respond('some-data'); |
| 186 hb.expect('GET', '/url2').respond('some-data', {'X-Header': 'true'}); |
| 187 hb('GET', '/url1', null, callback); |
| 188 hb('GET', '/url2', null, callback); |
| 189 hb.flush(); |
| 190 expect(callback).toHaveBeenCalled(); |
| 191 expect(callback.callCount).toBe(2); |
| 192 }); |
| 193 |
| 194 |
| 195 it('should default response headers to ""', () { |
| 196 hb.expect('GET', '/url1').respond(200, 'first'); |
| 197 hb.expect('GET', '/url2').respond('second'); |
| 198 |
| 199 hb('GET', '/url1', null, callback); |
| 200 hb('GET', '/url2', null, callback); |
| 201 |
| 202 hb.flush(); |
| 203 |
| 204 expect(callback.callCount).toBe(2); |
| 205 expect(callback.argsForCall[0]).toEqual([200, 'first', '']); |
| 206 expect(callback.argsForCall[1]).toEqual([200, 'second', '']); |
| 207 }); |
| 208 }); |
| 209 |
| 210 |
| 211 describe('expect()', () { |
| 212 it('should require specified order', () { |
| 213 hb.expect('GET', '/url1').respond(200, ''); |
| 214 hb.expect('GET', '/url2').respond(200, ''); |
| 215 |
| 216 expect(() { |
| 217 hb('GET', '/url2', null, noop, {}); |
| 218 }).toThrow('Unexpected request: GET /url2\nExpected GET /url1'); |
| 219 }); |
| 220 |
| 221 |
| 222 it('should have precedence over when()', () { |
| 223 callback.andCallFake((status, response) { |
| 224 expect(status).toBe(299); |
| 225 expect(response).toBe('expect'); |
| 226 }); |
| 227 |
| 228 hb.when('GET', '/url').respond(200, 'when'); |
| 229 hb.expect('GET', '/url').respond(299, 'expect'); |
| 230 |
| 231 hb('GET', '/url', null, callback, null); |
| 232 hb.flush(); |
| 233 expect(callback).toHaveBeenCalledOnce(); |
| 234 }); |
| 235 |
| 236 |
| 237 it('should throw exception when only headers differs from expectation', ()
{ |
| 238 hb.when('GET', '/match').respond(200, '', {}); |
| 239 hb.expect('GET', '/match', null, {'Content-Type': 'application/json'}).r
espond(200, '', {}); |
| 240 |
| 241 expect(() { |
| 242 hb('GET', '/match', null, noop, {}); |
| 243 }).toThrow('Expected GET /match with different headers\n' + |
| 244 'EXPECTED: {"Content-Type":"application/json"}\nGOT: {}'); |
| 245 }); |
| 246 |
| 247 |
| 248 it('should throw exception when only data differs from expectation', () { |
| 249 hb.when('GET', '/match').respond(200, '', {}); |
| 250 hb.expect('GET', '/match', 'some-data').respond(200, '', {}); |
| 251 |
| 252 expect(() { |
| 253 hb('GET', '/match', 'different', noop, null); |
| 254 }).toThrow('Expected GET /match with different data\n' + |
| 255 'EXPECTED: some-data\nGOT: different'); |
| 256 }); |
| 257 |
| 258 |
| 259 it("should use when's respond() when no expect() respond is defined", () { |
| 260 callback.andCallFake((status, response) { |
| 261 expect(status).toBe(201); |
| 262 expect(response).toBe('data'); |
| 263 }); |
| 264 |
| 265 hb.when('GET', '/some').respond(201, 'data'); |
| 266 hb.expect('GET', '/some'); |
| 267 hb('GET', '/some', null, callback); |
| 268 hb.flush(); |
| 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'); |
| 215 expect(() { | 351 expect(() { |
| 216 hb('GET', '/url2', null, noop, {}); | 352 hb('GET', '/test', null, callback); |
| 217 }).toThrow('Unexpected request: GET /url2\nExpected GET /url1'); | 353 }).toThrow('No response defined !'); |
| 218 }); | 354 }); |
| 219 | 355 |
| 220 | 356 |
| 221 it('should have precedence over when()', () { | 357 it('should throw an exception if no response for exception and no definition
', () { |
| 222 callback.andCallFake((status, response) { | 358 hb.expect('GET', '/url'); |
| 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(() { | 359 expect(() { |
| 241 hb('GET', '/match', null, noop, {}); | 360 hb('GET', '/url', null, callback); |
| 242 }).toThrow('Expected GET /match with different headers\n' + | 361 }).toThrow('No response defined !'); |
| 243 'EXPECTED: {"Content-Type":"application/json"}\nGOT: {}'); | 362 }); |
| 244 }); | 363 |
| 245 | 364 |
| 246 | 365 it('should respond undefined when JSONP method', () { |
| 247 it('should throw exception when only data differs from expectation', () { | 366 hb.when('JSONP', '/url1').respond(200); |
| 248 hb.when('GET', '/match').respond(200, '', {}); | 367 hb.expect('JSONP', '/url2').respond(200); |
| 249 hb.expect('GET', '/match', 'some-data').respond(200, '', {}); | 368 |
| 250 | 369 expect(hb('JSONP', '/url1')).toBeNull(); |
| 251 expect(() { | 370 expect(hb('JSONP', '/url2')).toBeNull(); |
| 252 hb('GET', '/match', 'different', noop, null); | 371 }); |
| 253 }).toThrow('Expected GET /match with different data\n' + | 372 |
| 254 'EXPECTED: some-data\nGOT: different'); | 373 |
| 255 }); | 374 describe('verifyExpectations', () { |
| 256 | 375 |
| 257 | 376 it('should throw exception if not all expectations were satisfied', () { |
| 258 it("should use when's respond() when no expect() respond is defined", () { | 377 hb.expect('POST', '/u1', 'ddd').respond(201, '', {}); |
| 259 callback.andCallFake((status, response) { | 378 hb.expect('GET', '/u2').respond(200, '', {}); |
| 260 expect(status).toBe(201); | 379 hb.expect('POST', '/u3').respond(201, '', {}); |
| 261 expect(response).toBe('data'); | 380 |
| 262 }); | 381 hb('POST', '/u1', 'ddd', noop, {}); |
| 263 | 382 |
| 264 hb.when('GET', '/some').respond(201, 'data'); | 383 expect(() {hb.verifyNoOutstandingExpectation();}). |
| 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 toThrow('Unsatisfied requests: GET /u2, POST /u3'); |
| 384 }); | 385 }); |
| 385 | 386 |
| 386 | 387 |
| 387 it('should do nothing when no expectation', () { | 388 it('should do nothing when no expectation', () { |
| 388 hb.when('DELETE', '/some').respond(200, ''); | 389 hb.when('DELETE', '/some').respond(200, ''); |
| 389 | 390 |
| 390 expect(() {hb.verifyNoOutstandingExpectation();}).not.toThrow(); | 391 expect(() {hb.verifyNoOutstandingExpectation();}).not.toThrow(); |
| 391 }); | 392 }); |
| 392 | 393 |
| 393 | 394 |
| 394 it('should do nothing when all expectations satisfied', () { | 395 it('should do nothing when all expectations satisfied', () { |
| 395 hb.expect('GET', '/u2').respond(200, '', {}); | 396 hb.expect('GET', '/u2').respond(200, '', {}); |
| 396 hb.expect('POST', '/u3').respond(201, '', {}); | 397 hb.expect('POST', '/u3').respond(201, '', {}); |
| 397 hb.when('DELETE', '/some').respond(200, ''); | 398 hb.when('DELETE', '/some').respond(200, ''); |
| 398 | 399 |
| 399 hb('GET', '/u2'); | 400 hb('GET', '/u2'); |
| 400 hb('POST', '/u3'); | 401 hb('POST', '/u3'); |
| 401 | 402 |
| 402 expect(() {hb.verifyNoOutstandingExpectation();}).not.toThrow(); | 403 expect(() {hb.verifyNoOutstandingExpectation();}).not.toThrow(); |
| 403 }); | 404 }); |
| 404 }); | 405 }); |
| 405 | 406 |
| 406 describe('verifyRequests', () { | 407 describe('verifyRequests', () { |
| 407 | 408 |
| 408 it('should throw exception if not all requests were flushed', () { | 409 it('should throw exception if not all requests were flushed', () { |
| 409 hb.when('GET').respond(200); | 410 hb.when('GET').respond(200); |
| 410 hb('GET', '/some', null, noop, {}); | 411 hb('GET', '/some', null, noop, {}); |
| 411 | 412 |
| 412 expect(() { | 413 expect(() { |
| 413 hb.verifyNoOutstandingRequest(); | 414 hb.verifyNoOutstandingRequest(); |
| 414 }).toThrow('Unflushed requests: 1'); | 415 }).toThrow('Unflushed requests: 1'); |
| 415 }); | 416 }); |
| 416 }); | 417 }); |
| 417 | 418 |
| 418 | 419 |
| 419 describe('resetExpectations', () { | 420 describe('resetExpectations', () { |
| 420 | 421 |
| 421 it('should remove all expectations', () { | 422 it('should remove all expectations', () { |
| 422 hb.expect('GET', '/u2').respond(200, '', {}); | 423 hb.expect('GET', '/u2').respond(200, '', {}); |
| 423 hb.expect('POST', '/u3').respond(201, '', {}); | 424 hb.expect('POST', '/u3').respond(201, '', {}); |
| 424 hb.resetExpectations(); | 425 hb.resetExpectations(); |
| 425 | 426 |
| 426 expect(() {hb.verifyNoOutstandingExpectation();}).not.toThrow(); | 427 expect(() {hb.verifyNoOutstandingExpectation();}).not.toThrow(); |
| 427 }); | 428 }); |
| 428 | 429 |
| 429 | 430 |
| 430 it('should remove all pending responses', () { | 431 it('should remove all pending responses', () { |
| 431 var cancelledClb = jasmine.createSpy('cancelled'); | 432 var cancelledClb = jasmine.createSpy('cancelled'); |
| 432 | 433 |
| 433 hb.expect('GET', '/url').respond(200, ''); | 434 hb.expect('GET', '/url').respond(200, ''); |
| 434 hb('GET', '/url', null, cancelledClb); | 435 hb('GET', '/url', null, cancelledClb); |
| 435 hb.resetExpectations(); | 436 hb.resetExpectations(); |
| 436 | 437 |
| 437 hb.expect('GET', '/url').respond(300, ''); | 438 hb.expect('GET', '/url').respond(300, ''); |
| 438 hb('GET', '/url', null, callback, {}); | 439 hb('GET', '/url', null, callback, {}); |
| 439 hb.flush(); | 440 hb.flush(); |
| 440 | 441 |
| 441 expect(callback).toHaveBeenCalledOnce(); | 442 expect(callback).toHaveBeenCalledOnce(); |
| 442 expect(cancelledClb).not.toHaveBeenCalled(); | 443 expect(cancelledClb).not.toHaveBeenCalled(); |
| 443 }); | 444 }); |
| 444 | 445 |
| 445 | 446 |
| 446 it('should not remove definitions', () { | 447 it('should not remove definitions', () { |
| 447 var cancelledClb = jasmine.createSpy('cancelled'); | 448 var cancelledClb = jasmine.createSpy('cancelled'); |
| 448 | 449 |
| 449 hb.when('GET', '/url').respond(200, 'success'); | 450 hb.when('GET', '/url').respond(200, 'success'); |
| 450 hb('GET', '/url', null, cancelledClb); | 451 hb('GET', '/url', null, cancelledClb); |
| 451 hb.resetExpectations(); | 452 hb.resetExpectations(); |
| 452 | 453 |
| 453 hb('GET', '/url', null, callback, {}); | 454 hb('GET', '/url', null, callback, {}); |
| 454 hb.flush(); | 455 hb.flush(); |
| 455 | 456 |
| 456 expect(callback).toHaveBeenCalledOnce(); | 457 expect(callback).toHaveBeenCalledOnce(); |
| 457 expect(cancelledClb).not.toHaveBeenCalled(); | 458 expect(cancelledClb).not.toHaveBeenCalled(); |
| 458 }); | 459 }); |
| 459 }); | 460 }); |
| 460 | 461 |
| 461 | 462 |
| 462 describe('expect/when shortcuts', () { | 463 describe('expect/when shortcuts', () { |
| 463 [[(x) => hb.expectGET(x), 'GET'], | 464 [[(x) => hb.expectGET(x), 'GET'], |
| 464 [(x) => hb.expectPOST(x), 'POST'], | 465 [(x) => hb.expectPOST(x), 'POST'], |
| 465 [(x) => hb.expectPUT(x), 'PUT'], | 466 [(x) => hb.expectPUT(x), 'PUT'], |
| 466 [(x) => hb.expectPATCH(x), 'PATCH'], | 467 [(x) => hb.expectPATCH(x), 'PATCH'], |
| 467 [(x) => hb.expectDELETE(x), 'DELETE'], | 468 [(x) => hb.expectDELETE(x), 'DELETE'], |
| 468 [(x) => hb.expectJSONP(x), 'JSONP'], | 469 [(x) => hb.expectJSONP(x), 'JSONP'], |
| 469 [(x) => hb.whenGET(x), 'GET'], | 470 [(x) => hb.whenGET(x), 'GET'], |
| 470 [(x) => hb.whenPOST(x), 'POST'], | 471 [(x) => hb.whenPOST(x), 'POST'], |
| 471 [(x) => hb.whenPUT(x), 'PUT'], | 472 [(x) => hb.whenPUT(x), 'PUT'], |
| 472 [(x) => hb.whenPATCH(x), 'PATCH'], | 473 [(x) => hb.whenPATCH(x), 'PATCH'], |
| 473 [(x) => hb.whenDELETE(x), 'DELETE'], | 474 [(x) => hb.whenDELETE(x), 'DELETE'], |
| 474 [(x) => hb.whenJSONP(x), 'JSONP'] | 475 [(x) => hb.whenJSONP(x), 'JSONP'] |
| 475 ].forEach((step) { | 476 ].forEach((step) { |
| 476 var shortcut = step[0], method = step[1]; | 477 var shortcut = step[0], method = step[1]; |
| 477 it('should provide $shortcut shortcut method', () { | 478 it('should provide $shortcut shortcut method', () { |
| 478 shortcut('/foo').respond('bar'); | 479 shortcut('/foo').respond('bar'); |
| 479 hb(method, '/foo', undefined, callback); | 480 hb(method, '/foo', undefined, callback); |
| 480 hb.flush(); | 481 hb.flush(); |
| 481 expect(callback).toHaveBeenCalledOnceWith(200, 'bar', ''); | 482 expect(callback).toHaveBeenCalledOnceWith(200, 'bar', ''); |
| 482 }); | 483 }); |
| 483 }); | 484 }); |
| 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 }); | 485 }); |
| 497 | 486 |
| 498 | 487 |
| 499 it('should accept data as regexp', () { | 488 describe('MockHttpExpectation', () { |
| 500 var exp = new MockHttpExpectation('POST', '/url', new RegExp('\{.*?\}')); | |
| 501 | 489 |
| 502 expect(exp.match('POST', '/url', '{"a": "aa"}')).toBe(true); | 490 it('should accept url as regexp', () { |
| 503 expect(exp.match('POST', '/url', '{"one": "two"}')).toBe(true); | 491 var exp = new MockHttpExpectation('GET', new RegExp('^\/x')); |
| 504 expect(exp.match('POST', '/url', '{"one"')).toBe(false); | 492 |
| 505 }); | 493 expect(exp.match('GET', '/x')).toBe(true); |
| 494 expect(exp.match('GET', '/xxx/x')).toBe(true); |
| 495 expect(exp.match('GET', 'x')).toBe(false); |
| 496 expect(exp.match('GET', 'a/x')).toBe(false); |
| 497 }); |
| 506 | 498 |
| 507 | 499 |
| 508 it('should accept headers as function', () { | 500 it('should accept data as regexp', () { |
| 509 var exp = new MockHttpExpectation('GET', '/url', undefined, (h) { | 501 var exp = new MockHttpExpectation('POST', '/url', new RegExp('\{.*?\}'))
; |
| 510 return h['Content-Type'] == 'application/json'; | 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); |
| 511 }); | 506 }); |
| 512 | 507 |
| 513 expect(exp.matchHeaders({})).toBe(false); | 508 |
| 514 expect(exp.matchHeaders({'Content-Type': 'application/json', 'X-Another':
'true'})).toBe(true); | 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 }); |
| 515 }); | 517 }); |
| 516 }); | 518 }); |
| 517 }); | 519 } |
| OLD | NEW |