Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

refactor(mocks) : changed the implementations of expect any URL #8462

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,8 @@ function MockHttpExpectation(method, url, data, headers) {
this.data = data;
this.headers = headers;

if (url === undefined) throw new Error('No URL defined!');

this.match = function(m, u, d, h) {
if (method != m) return false;
if (!this.matchUrl(u)) return false;
Expand All @@ -1548,7 +1550,7 @@ function MockHttpExpectation(method, url, data, headers) {
};

this.matchUrl = function(u) {
if (!url) return true;
if (url === null) return true;
if (angular.isFunction(url.test)) return url.test(u);
if (angular.isFunction(url)) return url(u);
return url == u;
Expand Down
12 changes: 6 additions & 6 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -890,23 +890,23 @@ describe('$http', function() {
describe('scope.$apply', function() {

it('should $apply after success callback', function() {
$httpBackend.when('GET').respond(200);
$httpBackend.when('GET', null).respond(200);
$http({method: 'GET', url: '/some'});
$httpBackend.flush();
expect($rootScope.$apply).toHaveBeenCalledOnce();
});


it('should $apply after error callback', function() {
$httpBackend.when('GET').respond(404);
$httpBackend.when('GET', null).respond(404);
$http({method: 'GET', url: '/some'});
$httpBackend.flush();
expect($rootScope.$apply).toHaveBeenCalledOnce();
});


it('should $apply even if exception thrown during callback', inject(function($exceptionHandler){
$httpBackend.when('GET').respond(200);
$httpBackend.when('GET', null).respond(200);
callback.andThrow('error in callback');

$http({method: 'GET', url: '/some'}).then(callback);
Expand Down Expand Up @@ -1402,7 +1402,7 @@ describe('$http', function() {
describe('pendingRequests', function() {

it('should be an array of pending requests', function() {
$httpBackend.when('GET').respond(200);
$httpBackend.when('GET', null).respond(200);
expect($http.pendingRequests.length).toBe(0);

$http({method: 'get', url: '/some'});
Expand All @@ -1415,7 +1415,7 @@ describe('$http', function() {


it('should update pending requests even when served from cache', inject(function($rootScope) {
$httpBackend.when('GET').respond(200);
$httpBackend.when('GET', null).respond(200);

$http({method: 'get', url: '/cached', cache: true});
$http({method: 'get', url: '/cached', cache: true});
Expand All @@ -1436,7 +1436,7 @@ describe('$http', function() {


it('should remove the request before firing callbacks', function() {
$httpBackend.when('GET').respond(200);
$httpBackend.when('GET', null).respond(200);
$http({method: 'get', url: '/url'}).success(function() {
expect($http.pendingRequests.length).toBe(0);
});
Expand Down
40 changes: 30 additions & 10 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,13 @@ describe('ngMock', function() {
});


it('should not allow undefined URL', function(){
expect(function() {
hb.when('GET').respond(200, '');
}).toThrow('No URL defined!');
});


it('should match headers if specified', function() {
hb.when('GET', '/url', null, {'X': 'val1'}).respond(201, 'content1');
hb.when('GET', '/url', null, {'X': 'val2'}).respond(202, 'content2');
Expand Down Expand Up @@ -1076,7 +1083,7 @@ describe('ngMock', function() {


it('should match only method', function() {
hb.when('GET').respond(202, 'c');
hb.when('GET', null).respond(202, 'c');
callback.andCallFake(function(status, response) {
expect(status).toBe(202);
expect(response).toBe('c');
Expand Down Expand Up @@ -1184,8 +1191,21 @@ describe('ngMock', function() {
});


it('should allow null as any url', function(){
hb.expect('GET', null).respond(200, '');
expect(function() {
hb('GET', '/url');
}).not.toThrow();

hb.expect('GET', '/url1').respond(200, '');
expect(function() {
hb('GET', '/url2');
}).toThrow('Unexpected request: GET /url2\nExpected GET /url1');
});


it ('should throw exception when only headers differs from expectation', function() {
hb.when('GET').respond(200, '', {});
hb.when('GET', null).respond(200, '', {});
hb.expect('GET', '/match', undefined, {'Content-Type': 'application/json'});

expect(function() {
Expand All @@ -1196,7 +1216,7 @@ describe('ngMock', function() {


it ('should throw exception when only data differs from expectation', function() {
hb.when('GET').respond(200, '', {});
hb.when('GET', null).respond(200, '', {});
hb.expect('GET', '/match', 'some-data');

expect(function() {
Expand All @@ -1207,7 +1227,7 @@ describe('ngMock', function() {


it ('should not throw an exception when parsed body is equal to expected body object', function() {
hb.when('GET').respond(200, '', {});
hb.when('GET', null).respond(200, '', {});

hb.expect('GET', '/match', {a: 1, b: 2});
expect(function() {
Expand All @@ -1222,7 +1242,7 @@ describe('ngMock', function() {


it ('should throw exception when only parsed body differs from expected body object', function() {
hb.when('GET').respond(200, '', {});
hb.when('GET', null).respond(200, '', {});
hb.expect('GET', '/match', {a: 1, b: 2});

expect(function() {
Expand Down Expand Up @@ -1251,7 +1271,7 @@ describe('ngMock', function() {

describe('flush()', function() {
it('flush() should flush requests fired during callbacks', function() {
hb.when('GET').respond(200, '');
hb.when('GET', null).respond(200, '');
hb('GET', '/some', null, function() {
hb('GET', '/other', null, callback);
});
Expand All @@ -1262,7 +1282,7 @@ describe('ngMock', function() {


it('should flush given number of pending requests', function() {
hb.when('GET').respond(200, '');
hb.when('GET', null).respond(200, '');
hb('GET', '/some', null, callback);
hb('GET', '/some', null, callback);
hb('GET', '/some', null, callback);
Expand All @@ -1274,7 +1294,7 @@ describe('ngMock', function() {


it('should throw exception when flushing more requests than pending', function() {
hb.when('GET').respond(200, '');
hb.when('GET', null).respond(200, '');
hb('GET', '/url', null, callback);

expect(function() {hb.flush(2);}).toThrow('No more pending request to flush !');
Expand All @@ -1285,7 +1305,7 @@ describe('ngMock', function() {
it('should throw exception when no request to flush', function() {
expect(function() {hb.flush();}).toThrow('No pending request to flush !');

hb.when('GET').respond(200, '');
hb.when('GET', null).respond(200, '');
hb('GET', '/some', null, callback);
hb.flush();

Expand Down Expand Up @@ -1387,7 +1407,7 @@ describe('ngMock', function() {
describe('verifyRequests', function() {

it('should throw exception if not all requests were flushed', function() {
hb.when('GET').respond(200);
hb.when('GET', null).respond(200);
hb('GET', '/some', null, noop, {});

expect(function() {
Expand Down
2 changes: 1 addition & 1 deletion test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ describe("resource", function() {
it('should handle multiple params with same name', function() {
var R = $resource('/:id/:id');

$httpBackend.when('GET').respond('{}');
$httpBackend.when('GET', null).respond('{}');
$httpBackend.expect('GET', '/1/1');

R.get({id:1});
Expand Down