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 2 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
3 changes: 2 additions & 1 deletion src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,8 @@ function MockHttpExpectation(method, url, data, headers) {
};

this.matchUrl = function(u) {
if (!url) return true;
if (url === undefined) return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go further and put an assertion in the MockHttpExpectation function that throws an explicit error if the url is undefined.

Then you don't need this line here at all because by the time you get here you are guaranteed that the URL is not undefined.

And also the user gets a clear error message if they failed to provide a URL.

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
35 changes: 28 additions & 7 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,14 @@ describe('ngMock', function() {
});


it('should not allow undefined URL', function(){
hb.when('GET').respond(200, '');
expect(function() {
hb('GET', '/xxx');
}).toThrow('Unexpected request: GET /xxx\nNo more request expected');
});


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 +1084,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,6 +1192,19 @@ 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.expect('GET', '/match', undefined, {'Content-Type': 'application/json'});
Expand All @@ -1207,7 +1228,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 Down Expand Up @@ -1251,7 +1272,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 +1283,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 +1295,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 +1306,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 +1408,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