Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1199)

Unified Diff: third_party/pkg/angular/test/mock/http_backend_spec.dart

Issue 124053002: Adding Angular and dependent packages for testing (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/pkg/angular/test/mock/http_backend_spec.dart
diff --git a/third_party/pkg/angular/test/mock/http_backend_spec.dart b/third_party/pkg/angular/test/mock/http_backend_spec.dart
new file mode 100644
index 0000000000000000000000000000000000000000..e89e5c74c0523ada9e3ceecf3129b0c1bb2c91a0
--- /dev/null
+++ b/third_party/pkg/angular/test/mock/http_backend_spec.dart
@@ -0,0 +1,500 @@
+library angular.mock.http_backend_spec;
+
+import '../_specs.dart';
+
+class _Chain {
+ var _thenFn;
+ _Chain({then}) {
+ _thenFn = then;
+ }
+
+ then(x) => _thenFn(x);
+}
+
+main() => describe('MockHttpBackend', () {
+ TestBed _;
+ beforeEach(inject((TestBed tb) => _ = tb));
+
+ var hb, callback, realBackendSpy;
+
+ var noop = (_, __) {};
+ var undefined = null;
+
+ beforeEach(inject((HttpBackend httpBackend) {
+ callback = jasmine.createSpy('callback');
+ hb = httpBackend;
+ }));
+
+
+ it('should respond with first matched definition', () {
+ hb.when('GET', '/url1').respond(200, 'content', {});
+ hb.when('GET', '/url1').respond(201, 'another', {});
+
+ callback.andCallFake((status, response) {
+ expect(status).toBe(200);
+ expect(response).toBe('content');
+ });
+
+ hb('GET', '/url1', null, callback);
+ expect(callback).not.toHaveBeenCalled();
+ hb.flush();
+ expect(callback).toHaveBeenCalledOnce();
+ });
+
+
+ it('should throw error when unexpected request', () {
+ hb.when('GET', '/url1').respond(200, 'content');
+ expect(() {
+ hb('GET', '/xxx');
+ }).toThrow('Unexpected request: GET /xxx\nNo more requests expected');
+ });
+
+
+ it('should throw an error on an exception in then', async(() {
+ hb.expectGET('/url').respond(200, 'content');
+
+ expect(() {
+ hb.request('/url', method: 'GET').then((x) {
+ throw ["exceptiona"];
+ });
+ hb.flush();
+ microLeap();
+ }).toThrow('exceptiona');
+ }));
+
+
+ it('should match headers if specified', () {
+ try {
+ hb.when('GET', '/url', null, {'X': 'val1'}).respond(201, 'content1');
+ hb.when('GET', '/url', null, {'X': 'val2'}).respond(202, 'content2');
+ hb.when('GET', '/url').respond(203, 'content3');
+
+ hb('GET', '/url', null, (status, response) {
+ expect(status).toBe(203);
+ expect(response).toBe('content3');
+ });
+
+ hb('GET', '/url', null, (status, response) {
+ expect(status).toBe(201);
+ expect(response).toBe('content1');
+ }, {'X': 'val1'});
+
+ hb('GET', '/url', null, (status, response) {
+ expect(status).toBe(202);
+ expect(response).toBe('content2');
+ }, {'X': 'val2'});
+
+ hb.flush();
+ } catch (e,s) { print("$e $s"); }
+ });
+
+
+ it('should match data if specified', () {
+ hb.when('GET', '/a/b', '{a: true}').respond(201, 'content1');
+ hb.when('GET', '/a/b').respond(202, 'content2');
+
+ hb('GET', '/a/b', '{a: true}', (status, response) {
+ expect(status).toBe(201);
+ expect(response).toBe('content1');
+ });
+
+ hb('GET', '/a/b', '{}', (status, response) {
+ expect(status).toBe(202);
+ expect(response).toBe('content2');
+ });
+
+ hb.flush();
+ });
+
+
+ it('should match only method', () {
+ hb.when('GET').respond(202, 'c');
+ callback.andCallFake((status, response) {
+ expect(status).toBe(202);
+ expect(response).toBe('c');
+ });
+
+ hb('GET', '/some', null, callback, {});
+ hb('GET', '/another', null, callback, {'X-Fake': 'Header'});
+ hb('GET', '/third', 'some-data', callback, {});
+ hb.flush();
+
+ expect(callback).toHaveBeenCalled();
+ });
+
+
+ it('should preserve the order of requests', () {
+ hb.when('GET', '/url1').respond(200, 'first');
+ hb.when('GET', '/url2').respond(201, 'second');
+
+ hb('GET', '/url2', null, callback);
+ hb('GET', '/url1', null, callback);
+
+ hb.flush();
+
+ expect(callback.callCount).toBe(2);
+ expect(callback.argsForCall[0]).toEqual([201, 'second', '']);
+ expect(callback.argsForCall[1]).toEqual([200, 'first', '']);
+ });
+
+
+ describe('respond()', () {
+ it('should take values', () {
+ hb.expect('GET', '/url1').respond(200, 'first', {'header': 'val'});
+ hb('GET', '/url1', null, callback);
+ hb.flush();
+
+ expect(callback).toHaveBeenCalledOnceWith(200, 'first', "header: val");
+ });
+
+ it('should take function', () {
+ hb.expect('GET', '/some').respond((m, u, d, h) {
+ return [301, m + u + ';' + d + ';a=' + h['a'], {'Connection': 'keep-alive'}];
+ });
+
+ hb('GET', '/some', 'data', callback, {'a': 'b'});
+ hb.flush();
+
+ expect(callback).toHaveBeenCalledOnceWith(301, 'GET/some;data;a=b', 'Connection: keep-alive');
+ });
+
+ it('should default status code to 200', () {
+ callback.andCallFake((status, response) {
+ expect(status).toBe(200);
+ expect(response).toBe('some-data');
+ });
+
+ hb.expect('GET', '/url1').respond('some-data');
+ hb.expect('GET', '/url2').respond('some-data', {'X-Header': 'true'});
+ hb('GET', '/url1', null, callback);
+ hb('GET', '/url2', null, callback);
+ hb.flush();
+ expect(callback).toHaveBeenCalled();
+ expect(callback.callCount).toBe(2);
+ });
+
+
+ it('should default response headers to ""', () {
+ hb.expect('GET', '/url1').respond(200, 'first');
+ hb.expect('GET', '/url2').respond('second');
+
+ hb('GET', '/url1', null, callback);
+ hb('GET', '/url2', null, callback);
+
+ hb.flush();
+
+ expect(callback.callCount).toBe(2);
+ expect(callback.argsForCall[0]).toEqual([200, 'first', '']);
+ expect(callback.argsForCall[1]).toEqual([200, 'second', '']);
+ });
+ });
+
+
+ describe('expect()', () {
+ it('should require specified order', () {
+ hb.expect('GET', '/url1').respond(200, '');
+ hb.expect('GET', '/url2').respond(200, '');
+
+ expect(() {
+ hb('GET', '/url2', null, noop, {});
+ }).toThrow('Unexpected request: GET /url2\nExpected GET /url1');
+ });
+
+
+ it('should have precedence over when()', () {
+ callback.andCallFake((status, response) {
+ expect(status).toBe(299);
+ expect(response).toBe('expect');
+ });
+
+ hb.when('GET', '/url').respond(200, 'when');
+ hb.expect('GET', '/url').respond(299, 'expect');
+
+ hb('GET', '/url', null, callback, null);
+ hb.flush();
+ expect(callback).toHaveBeenCalledOnce();
+ });
+
+
+ it('should throw exception when only headers differs from expectation', () {
+ hb.when('GET', '/match').respond(200, '', {});
+ hb.expect('GET', '/match', null, {'Content-Type': 'application/json'}).respond(200, '', {});
+
+ expect(() {
+ hb('GET', '/match', null, noop, {});
+ }).toThrow('Expected GET /match with different headers\n' +
+ 'EXPECTED: {"Content-Type":"application/json"}\nGOT: {}');
+ });
+
+
+ it('should throw exception when only data differs from expectation', () {
+ hb.when('GET', '/match').respond(200, '', {});
+ hb.expect('GET', '/match', 'some-data').respond(200, '', {});
+
+ expect(() {
+ hb('GET', '/match', 'different', noop, null);
+ }).toThrow('Expected GET /match with different data\n' +
+ 'EXPECTED: some-data\nGOT: different');
+ });
+
+
+ it("should use when's respond() when no expect() respond is defined", () {
+ callback.andCallFake((status, response) {
+ expect(status).toBe(201);
+ expect(response).toBe('data');
+ });
+
+ hb.when('GET', '/some').respond(201, 'data');
+ hb.expect('GET', '/some');
+ hb('GET', '/some', null, callback);
+ hb.flush();
+
+ expect(callback).toHaveBeenCalled();
+ expect(() { hb.verifyNoOutstandingExpectation(); }).not.toThrow();
+ });
+ });
+
+
+ describe('flush()', () {
+ it('flush() should flush requests fired during callbacks', () {
+ hb.when('GET', '/some').respond(200, '');
+ hb.when('GET', '/other').respond(200, '');
+ hb('GET', '/some', null, (_, __) {
+ hb('GET', '/other', null, callback);
+ });
+
+ hb.flush();
+ expect(callback).toHaveBeenCalled();
+ });
+
+
+ it('should flush given number of pending requests', () {
+ hb.when('GET').respond(200, '');
+ hb('GET', '/some', null, callback);
+ hb('GET', '/some', null, callback);
+ hb('GET', '/some', null, callback);
+
+ hb.flush(2);
+ expect(callback).toHaveBeenCalled();
+ expect(callback.callCount).toBe(2);
+ });
+
+
+ it('should throw exception when flushing more requests than pending', () {
+ hb.when('GET').respond(200, '');
+ hb('GET', '/url', null, callback);
+
+ expect(() {hb.flush(2);}).toThrow('No more pending request to flush !');
+ expect(callback).toHaveBeenCalledOnce();
+ });
+
+
+ it('should throw exception when no request to flush', () {
+ expect(() {hb.flush();}).toThrow('No pending request to flush !');
+
+ hb.when('GET').respond(200, '');
+ hb('GET', '/some', null, callback);
+ hb.flush();
+
+ expect(() {hb.flush();}).toThrow('No pending request to flush !');
+ });
+
+
+ it('should throw exception if not all expectations satisfied', () {
+ hb.expect('GET', '/url1').respond();
+ hb.expect('GET', '/url2').respond();
+
+ hb('GET', '/url1', null, noop);
+ expect(() {hb.flush();}).toThrow('Unsatisfied requests: GET /url2');
+ });
+ });
+
+
+ it('should abort requests when timeout promise resolves', () {
+ hb.expect('GET', '/url1').respond(200);
+
+ var canceler, then = jasmine.createSpy('then').andCallFake((fn) {
+ canceler = fn;
+ });
+
+ hb('GET', '/url1', null, callback, null, new _Chain(then: then));
+ expect(canceler is Function).toBe(true);
+
+ canceler(); // simulate promise resolution
+
+ expect(callback).toHaveBeenCalledWith(-1, null, '');
+ hb.verifyNoOutstandingExpectation();
+ hb.verifyNoOutstandingRequest();
+ });
+
+
+ it('should throw an exception if no response defined', () {
+ hb.when('GET', '/test');
+ expect(() {
+ hb('GET', '/test', null, callback);
+ }).toThrow('No response defined !');
+ });
+
+
+ it('should throw an exception if no response for exception and no definition', () {
+ hb.expect('GET', '/url');
+ expect(() {
+ hb('GET', '/url', null, callback);
+ }).toThrow('No response defined !');
+ });
+
+
+ it('should respond undefined when JSONP method', () {
+ hb.when('JSONP', '/url1').respond(200);
+ hb.expect('JSONP', '/url2').respond(200);
+
+ expect(hb('JSONP', '/url1')).toBeNull();
+ expect(hb('JSONP', '/url2')).toBeNull();
+ });
+
+
+ describe('verifyExpectations', () {
+
+ it('should throw exception if not all expectations were satisfied', () {
+ hb.expect('POST', '/u1', 'ddd').respond(201, '', {});
+ hb.expect('GET', '/u2').respond(200, '', {});
+ hb.expect('POST', '/u3').respond(201, '', {});
+
+ hb('POST', '/u1', 'ddd', noop, {});
+
+ expect(() {hb.verifyNoOutstandingExpectation();}).
+ toThrow('Unsatisfied requests: GET /u2, POST /u3');
+ });
+
+
+ it('should do nothing when no expectation', () {
+ hb.when('DELETE', '/some').respond(200, '');
+
+ expect(() {hb.verifyNoOutstandingExpectation();}).not.toThrow();
+ });
+
+
+ it('should do nothing when all expectations satisfied', () {
+ hb.expect('GET', '/u2').respond(200, '', {});
+ hb.expect('POST', '/u3').respond(201, '', {});
+ hb.when('DELETE', '/some').respond(200, '');
+
+ hb('GET', '/u2');
+ hb('POST', '/u3');
+
+ expect(() {hb.verifyNoOutstandingExpectation();}).not.toThrow();
+ });
+ });
+
+ describe('verifyRequests', () {
+
+ it('should throw exception if not all requests were flushed', () {
+ hb.when('GET').respond(200);
+ hb('GET', '/some', null, noop, {});
+
+ expect(() {
+ hb.verifyNoOutstandingRequest();
+ }).toThrow('Unflushed requests: 1');
+ });
+ });
+
+
+ describe('resetExpectations', () {
+
+ it('should remove all expectations', () {
+ hb.expect('GET', '/u2').respond(200, '', {});
+ hb.expect('POST', '/u3').respond(201, '', {});
+ hb.resetExpectations();
+
+ expect(() {hb.verifyNoOutstandingExpectation();}).not.toThrow();
+ });
+
+
+ it('should remove all pending responses', () {
+ var cancelledClb = jasmine.createSpy('cancelled');
+
+ hb.expect('GET', '/url').respond(200, '');
+ hb('GET', '/url', null, cancelledClb);
+ hb.resetExpectations();
+
+ hb.expect('GET', '/url').respond(300, '');
+ hb('GET', '/url', null, callback, {});
+ hb.flush();
+
+ expect(callback).toHaveBeenCalledOnce();
+ expect(cancelledClb).not.toHaveBeenCalled();
+ });
+
+
+ it('should not remove definitions', () {
+ var cancelledClb = jasmine.createSpy('cancelled');
+
+ hb.when('GET', '/url').respond(200, 'success');
+ hb('GET', '/url', null, cancelledClb);
+ hb.resetExpectations();
+
+ hb('GET', '/url', null, callback, {});
+ hb.flush();
+
+ expect(callback).toHaveBeenCalledOnce();
+ expect(cancelledClb).not.toHaveBeenCalled();
+ });
+ });
+
+
+ describe('expect/when shortcuts', () {
+ [[(x) => hb.expectGET(x), 'GET'],
+ [(x) => hb.expectPOST(x), 'POST'],
+ [(x) => hb.expectPUT(x), 'PUT'],
+ [(x) => hb.expectPATCH(x), 'PATCH'],
+ [(x) => hb.expectDELETE(x), 'DELETE'],
+ [(x) => hb.expectJSONP(x), 'JSONP'],
+ [(x) => hb.whenGET(x), 'GET'],
+ [(x) => hb.whenPOST(x), 'POST'],
+ [(x) => hb.whenPUT(x), 'PUT'],
+ [(x) => hb.whenPATCH(x), 'PATCH'],
+ [(x) => hb.whenDELETE(x), 'DELETE'],
+ [(x) => hb.whenJSONP(x), 'JSONP']
+ ].forEach((step) {
+ var shortcut = step[0], method = step[1];
+ it('should provide $shortcut shortcut method', () {
+ shortcut('/foo').respond('bar');
+ hb(method, '/foo', undefined, callback);
+ hb.flush();
+ expect(callback).toHaveBeenCalledOnceWith(200, 'bar', '');
+ });
+ });
+ });
+
+
+ describe('MockHttpExpectation', () {
+
+ it('should accept url as regexp', () {
+ var exp = new MockHttpExpectation('GET', new RegExp('^\/x'));
+
+ expect(exp.match('GET', '/x')).toBe(true);
+ expect(exp.match('GET', '/xxx/x')).toBe(true);
+ expect(exp.match('GET', 'x')).toBe(false);
+ expect(exp.match('GET', 'a/x')).toBe(false);
+ });
+
+
+ it('should accept data as regexp', () {
+ var exp = new MockHttpExpectation('POST', '/url', new RegExp('\{.*?\}'));
+
+ expect(exp.match('POST', '/url', '{"a": "aa"}')).toBe(true);
+ expect(exp.match('POST', '/url', '{"one": "two"}')).toBe(true);
+ expect(exp.match('POST', '/url', '{"one"')).toBe(false);
+ });
+
+
+ it('should accept headers as function', () {
+ var exp = new MockHttpExpectation('GET', '/url', undefined, (h) {
+ return h['Content-Type'] == 'application/json';
+ });
+
+ expect(exp.matchHeaders({})).toBe(false);
+ expect(exp.matchHeaders({'Content-Type': 'application/json', 'X-Another': 'true'})).toBe(true);
+ });
+ });
+});

Powered by Google App Engine
This is Rietveld 408576698