Skip to content

Commit efed0f7

Browse files
jbdeboerDiana Salsbury
authored and
Diana Salsbury
committed
test(MockHttpBackend): Add named parameters to MockHttpBackend.call
1 parent 33d1ca2 commit efed0f7

File tree

2 files changed

+56
-55
lines changed

2 files changed

+56
-55
lines changed

lib/mock/http_backend.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ class MockHttpBackend implements HttpBackend {
136136
new MockHttpRequest(status, data, headers)));
137137
}
138138
};
139-
call(method == null ? 'GET' : method, url, sendData, callback,
140-
requestHeaders);
139+
call(method == null ? 'GET' : method, url, callback,
140+
data: sendData, headers: requestHeaders);
141141
return c.future;
142142
}
143143

@@ -163,7 +163,7 @@ class MockHttpBackend implements HttpBackend {
163163
* A callback oriented API. This function takes a callback with
164164
* will be called with (status, data, headers)
165165
*/
166-
void call(method, [url, data, callback, headers, timeout]) {
166+
void call(method, url, callback, {data, headers, timeout}) {
167167
var xhr = new _MockXhr(),
168168
expectation = expectations.isEmpty ? null : expectations[0],
169169
wasExpected = false;

test/mock/http_backend_spec.dart

+53-52
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ void main() {
1616
TestBed _;
1717
beforeEach((TestBed tb) => _ = tb);
1818

19-
var hb, callback, realBackendSpy;
19+
MockHttpBackend hb;
20+
var callback, realBackendSpy;
2021

2122
var noop = (_, __) {};
2223
var undefined = null;
@@ -36,7 +37,7 @@ void main() {
3637
expect(response).toBe('content');
3738
});
3839

39-
hb('GET', '/url1', null, callback);
40+
hb('GET', '/url1', callback);
4041
expect(callback).not.toHaveBeenCalled();
4142
hb.flush();
4243
expect(callback).toHaveBeenCalledOnce();
@@ -53,8 +54,8 @@ void main() {
5354
logger(response);
5455
});
5556

56-
hb('GET', '/url1', null, callback);
57-
hb('GET', '/url2', null, callback);
57+
hb('GET', '/url1', callback);
58+
hb('GET', '/url2', callback);
5859
hb.flush();
5960
expect(logger).toEqual(['["abc"]', '{"key":"value"}']);
6061
});
@@ -63,7 +64,7 @@ void main() {
6364
it('should throw error when unexpected request', () {
6465
hb.when('GET', '/url1').respond(200, 'content');
6566
expect(() {
66-
hb('GET', '/xxx');
67+
hb('GET', '/xxx', noop);
6768
}).toThrow('Unexpected request: GET /xxx\nNo more requests expected');
6869
});
6970

@@ -87,20 +88,20 @@ void main() {
8788
hb.when('GET', '/url', null, {'X': 'val2'}).respond(202, 'content2');
8889
hb.when('GET', '/url').respond(203, 'content3');
8990

90-
hb('GET', '/url', null, (status, response, _) {
91+
hb('GET', '/url', (status, response, _) {
9192
expect(status).toBe(203);
9293
expect(response).toBe('content3');
9394
});
9495

95-
hb('GET', '/url', null, (status, response, _) {
96+
hb('GET', '/url', (status, response, _) {
9697
expect(status).toBe(201);
9798
expect(response).toBe('content1');
98-
}, {'X': 'val1'});
99+
}, headers: {'X': 'val1'});
99100

100-
hb('GET', '/url', null, (status, response, _) {
101+
hb('GET', '/url', (status, response, _) {
101102
expect(status).toBe(202);
102103
expect(response).toBe('content2');
103-
}, {'X': 'val2'});
104+
}, headers: {'X': 'val2'});
104105

105106
hb.flush();
106107
} catch (e,s) { print("$e $s"); }
@@ -111,15 +112,15 @@ void main() {
111112
hb.when('GET', '/a/b', '{a: true}').respond(201, 'content1');
112113
hb.when('GET', '/a/b').respond(202, 'content2');
113114

114-
hb('GET', '/a/b', '{a: true}', (status, response) {
115+
hb('GET', '/a/b', (status, response) {
115116
expect(status).toBe(201);
116117
expect(response).toBe('content1');
117-
});
118+
}, data: '{a: true}');
118119

119-
hb('GET', '/a/b', '{}', (status, response) {
120+
hb('GET', '/a/b', (status, response) {
120121
expect(status).toBe(202);
121122
expect(response).toBe('content2');
122-
});
123+
}, data: '{}');
123124

124125
hb.flush();
125126
});
@@ -132,9 +133,9 @@ void main() {
132133
expect(response).toBe('c');
133134
});
134135

135-
hb('GET', '/some', null, callback, {});
136-
hb('GET', '/another', null, callback, {'X-Fake': 'Header'});
137-
hb('GET', '/third', 'some-data', callback, {});
136+
hb('GET', '/some', callback, headers: {});
137+
hb('GET', '/another', callback, headers: {'X-Fake': 'Header'});
138+
hb('GET', '/third', callback, data: 'some-data', headers: {});
138139
hb.flush();
139140

140141
expect(callback).toHaveBeenCalled();
@@ -145,8 +146,8 @@ void main() {
145146
hb.when('GET', '/url1').respond(200, 'first');
146147
hb.when('GET', '/url2').respond(201, 'second');
147148

148-
hb('GET', '/url2', null, callback);
149-
hb('GET', '/url1', null, callback);
149+
hb('GET', '/url2', callback);
150+
hb('GET', '/url1', callback);
150151

151152
hb.flush();
152153

@@ -159,7 +160,7 @@ void main() {
159160
describe('respond()', () {
160161
it('should take values', () {
161162
hb.expect('GET', '/url1').respond(200, 'first', {'header': 'val'});
162-
hb('GET', '/url1', null, callback);
163+
hb('GET', '/url1', callback);
163164
hb.flush();
164165

165166
expect(callback).toHaveBeenCalledOnceWith(200, 'first', "header: val");
@@ -170,7 +171,7 @@ void main() {
170171
return [301, m + u + ';' + d + ';a=' + h['a'], {'Connection': 'keep-alive'}];
171172
});
172173

173-
hb('GET', '/some', 'data', callback, {'a': 'b'});
174+
hb('GET', '/some', callback, data: 'data', headers: {'a': 'b'});
174175
hb.flush();
175176

176177
expect(callback).toHaveBeenCalledOnceWith(301, 'GET/some;data;a=b', 'Connection: keep-alive');
@@ -184,8 +185,8 @@ void main() {
184185

185186
hb.expect('GET', '/url1').respond('some-data');
186187
hb.expect('GET', '/url2').respond('some-data', {'X-Header': 'true'});
187-
hb('GET', '/url1', null, callback);
188-
hb('GET', '/url2', null, callback);
188+
hb('GET', '/url1', callback);
189+
hb('GET', '/url2', callback);
189190
hb.flush();
190191
expect(callback).toHaveBeenCalled();
191192
expect(callback.callCount).toBe(2);
@@ -196,8 +197,8 @@ void main() {
196197
hb.expect('GET', '/url1').respond(200, 'first');
197198
hb.expect('GET', '/url2').respond('second');
198199

199-
hb('GET', '/url1', null, callback);
200-
hb('GET', '/url2', null, callback);
200+
hb('GET', '/url1', callback);
201+
hb('GET', '/url2', callback);
201202

202203
hb.flush();
203204

@@ -214,7 +215,7 @@ void main() {
214215
hb.expect('GET', '/url2').respond(200, '');
215216

216217
expect(() {
217-
hb('GET', '/url2', null, noop, {});
218+
hb('GET', '/url2', noop, headers: {});
218219
}).toThrow('Unexpected request: GET /url2\nExpected GET /url1');
219220
});
220221

@@ -228,7 +229,7 @@ void main() {
228229
hb.when('GET', '/url').respond(200, 'when');
229230
hb.expect('GET', '/url').respond(299, 'expect');
230231

231-
hb('GET', '/url', null, callback, null);
232+
hb('GET', '/url', callback);
232233
hb.flush();
233234
expect(callback).toHaveBeenCalledOnce();
234235
});
@@ -239,7 +240,7 @@ void main() {
239240
hb.expect('GET', '/match', null, {'Content-Type': 'application/json'}).respond(200, '', {});
240241

241242
expect(() {
242-
hb('GET', '/match', null, noop, {});
243+
hb('GET', '/match', noop, headers: {});
243244
}).toThrow('Expected GET /match with different headers\n' +
244245
'EXPECTED: {"Content-Type":"application/json"}\nGOT: {}');
245246
});
@@ -250,7 +251,7 @@ void main() {
250251
hb.expect('GET', '/match', 'some-data').respond(200, '', {});
251252

252253
expect(() {
253-
hb('GET', '/match', 'different', noop, null);
254+
hb('GET', '/match', noop, data: 'different');
254255
}).toThrow('Expected GET /match with different data\n' +
255256
'EXPECTED: some-data\nGOT: different');
256257
});
@@ -264,7 +265,7 @@ void main() {
264265

265266
hb.when('GET', '/some').respond(201, 'data');
266267
hb.expect('GET', '/some');
267-
hb('GET', '/some', null, callback);
268+
hb('GET', '/some', callback);
268269
hb.flush();
269270

270271
expect(callback).toHaveBeenCalled();
@@ -277,8 +278,8 @@ void main() {
277278
it('flush() should flush requests fired during callbacks', () {
278279
hb.when('GET', '/some').respond(200, '');
279280
hb.when('GET', '/other').respond(200, '');
280-
hb('GET', '/some', null, (_, __) {
281-
hb('GET', '/other', null, callback);
281+
hb('GET', '/some', (_, __) {
282+
hb('GET', '/other', callback);
282283
});
283284

284285
hb.flush();
@@ -288,9 +289,9 @@ void main() {
288289

289290
it('should flush given number of pending requests', () {
290291
hb.when('GET').respond(200, '');
291-
hb('GET', '/some', null, callback);
292-
hb('GET', '/some', null, callback);
293-
hb('GET', '/some', null, callback);
292+
hb('GET', '/some', callback);
293+
hb('GET', '/some', callback);
294+
hb('GET', '/some', callback);
294295

295296
hb.flush(2);
296297
expect(callback).toHaveBeenCalled();
@@ -300,7 +301,7 @@ void main() {
300301

301302
it('should throw exception when flushing more requests than pending', () {
302303
hb.when('GET').respond(200, '');
303-
hb('GET', '/url', null, callback);
304+
hb('GET', '/url', callback);
304305

305306
expect(() {hb.flush(2);}).toThrow('No more pending request to flush !');
306307
expect(callback).toHaveBeenCalledOnce();
@@ -311,7 +312,7 @@ void main() {
311312
expect(() {hb.flush();}).toThrow('No pending request to flush !');
312313

313314
hb.when('GET').respond(200, '');
314-
hb('GET', '/some', null, callback);
315+
hb('GET', '/some', callback);
315316
hb.flush();
316317

317318
expect(() {hb.flush();}).toThrow('No pending request to flush !');
@@ -322,7 +323,7 @@ void main() {
322323
hb.expect('GET', '/url1').respond();
323324
hb.expect('GET', '/url2').respond();
324325

325-
hb('GET', '/url1', null, noop);
326+
hb('GET', '/url1', noop);
326327
expect(() {hb.flush();}).toThrow('Unsatisfied requests: GET /url2');
327328
});
328329
});
@@ -335,7 +336,7 @@ void main() {
335336
canceler = fn;
336337
});
337338

338-
hb('GET', '/url1', null, callback, null, new _Chain(then: then));
339+
hb('GET', '/url1', callback, timeout: new _Chain(then: then));
339340
expect(canceler is Function).toBe(true);
340341

341342
canceler(); // simulate promise resolution
@@ -349,15 +350,15 @@ void main() {
349350
it('should throw an exception if no response defined', () {
350351
hb.when('GET', '/test');
351352
expect(() {
352-
hb('GET', '/test', null, callback);
353+
hb('GET', '/test', callback);
353354
}).toThrow('No response defined !');
354355
});
355356

356357

357358
it('should throw an exception if no response for exception and no definition', () {
358359
hb.expect('GET', '/url');
359360
expect(() {
360-
hb('GET', '/url', null, callback);
361+
hb('GET', '/url', callback);
361362
}).toThrow('No response defined !');
362363
});
363364

@@ -366,8 +367,8 @@ void main() {
366367
hb.when('JSONP', '/url1').respond(200);
367368
hb.expect('JSONP', '/url2').respond(200);
368369

369-
expect(hb('JSONP', '/url1')).toBeNull();
370-
expect(hb('JSONP', '/url2')).toBeNull();
370+
expect(hb('JSONP', '/url1', noop)).toBeNull();
371+
expect(hb('JSONP', '/url2', noop)).toBeNull();
371372
});
372373

373374

@@ -378,7 +379,7 @@ void main() {
378379
hb.expect('GET', '/u2').respond(200, '', {});
379380
hb.expect('POST', '/u3').respond(201, '', {});
380381

381-
hb('POST', '/u1', 'ddd', noop, {});
382+
hb('POST', '/u1', noop, data: 'ddd', headers: {});
382383

383384
expect(() {hb.verifyNoOutstandingExpectation();}).
384385
toThrow('Unsatisfied requests: GET /u2, POST /u3');
@@ -397,8 +398,8 @@ void main() {
397398
hb.expect('POST', '/u3').respond(201, '', {});
398399
hb.when('DELETE', '/some').respond(200, '');
399400

400-
hb('GET', '/u2');
401-
hb('POST', '/u3');
401+
hb('GET', '/u2', noop);
402+
hb('POST', '/u3', noop);
402403

403404
expect(() {hb.verifyNoOutstandingExpectation();}).not.toThrow();
404405
});
@@ -408,7 +409,7 @@ void main() {
408409

409410
it('should throw exception if not all requests were flushed', () {
410411
hb.when('GET').respond(200);
411-
hb('GET', '/some', null, noop, {});
412+
hb('GET', '/some', noop, headers: {});
412413

413414
expect(() {
414415
hb.verifyNoOutstandingRequest();
@@ -432,11 +433,11 @@ void main() {
432433
var cancelledClb = guinness.createSpy('cancelled');
433434

434435
hb.expect('GET', '/url').respond(200, '');
435-
hb('GET', '/url', null, cancelledClb);
436+
hb('GET', '/url', cancelledClb);
436437
hb.resetExpectations();
437438

438439
hb.expect('GET', '/url').respond(300, '');
439-
hb('GET', '/url', null, callback, {});
440+
hb('GET', '/url', callback, headers: {});
440441
hb.flush();
441442

442443
expect(callback).toHaveBeenCalledOnce();
@@ -448,10 +449,10 @@ void main() {
448449
var cancelledClb = guinness.createSpy('cancelled');
449450

450451
hb.when('GET', '/url').respond(200, 'success');
451-
hb('GET', '/url', null, cancelledClb);
452+
hb('GET', '/url', cancelledClb);
452453
hb.resetExpectations();
453454

454-
hb('GET', '/url', null, callback, {});
455+
hb('GET', '/url', callback, headers: {});
455456
hb.flush();
456457

457458
expect(callback).toHaveBeenCalledOnce();
@@ -477,7 +478,7 @@ void main() {
477478
var shortcut = step[0], method = step[1];
478479
it('should provide $shortcut shortcut method', () {
479480
shortcut('/foo').respond('bar');
480-
hb(method, '/foo', undefined, callback);
481+
hb(method, '/foo', callback);
481482
hb.flush();
482483
expect(callback).toHaveBeenCalledOnceWith(200, 'bar', '');
483484
});

0 commit comments

Comments
 (0)