-
-
Save bbraithwaite/9effa5a77638a49e045d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Http Tests</title> | |
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.css"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.js"></script> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine-html.min.js"></script> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/boot.min.js"></script> | |
<script type="text/javascript" src="https://code.angularjs.org/1.4.0-rc.2/angular.js"></script> | |
<script type="text/javascript" src="https://code.angularjs.org/1.4.0-rc.2/angular-mocks.js"></script> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
var app = angular.module('moviesApp', []); | |
app.controller('MovieController', function movieController($scope, $http) { | |
var getMovieData = function getMovieData() { | |
$http.get('http://www.omdbapi.com/', { | |
params: { s: $scope.keyword } | |
}).success(function(data, status, headers, config) { | |
$scope.movies = data.Search; | |
}).error(function(data, status, headers, config) { | |
$scope.movies = []; | |
}); | |
}; | |
/* On Load */ | |
$scope.movies = []; | |
$scope.keyword = 'terminator'; | |
getMovieData(); | |
/* User Search Function */ | |
$scope.search = function searchMovies() { | |
getMovieData(); | |
} | |
}); | |
/* Tests */ | |
describe('movie app tests', function () { | |
var $controller; | |
var $httpBackend; | |
var $scope; | |
var testData = { | |
query: { | |
terminator: [{'Title':'Terminator 2: Judgment Day'}, {'Title':'The Terminator'}], | |
starwars: [{'Title':'Star Wars: Episode IV - A New Hope'},{'Title':'Star Wars: Episode V - The Empire Strikes Back'}] | |
} | |
} | |
beforeEach(module('moviesApp')); | |
beforeEach(inject(function(_$controller_, _$httpBackend_) { | |
$controller = _$controller_; | |
$scope = {}; | |
$httpBackend = _$httpBackend_; | |
})); | |
describe('http tests (original example)', function() { | |
it('should load default movies', function () { | |
$httpBackend.when('GET', 'http://www.omdbapi.com/?s=terminator') | |
.respond({ Search: testData.query.terminator }); | |
$httpBackend.expect('GET', 'http://www.omdbapi.com/?s=terminator'); | |
$controller('MovieController', { $scope: $scope }); | |
$httpBackend.flush(); | |
expect($scope.movies).toEqual(testData.query.terminator); | |
}); | |
it('should load user defined movies on search', function () { | |
$httpBackend.when('GET', 'http://www.omdbapi.com/?s=terminator') | |
.respond(200, { Search: testData.query.terminator }); | |
$httpBackend.when('GET', 'http://www.omdbapi.com/?s=star+wars') | |
.respond(200, { Search: testData.query.starwars }); | |
$httpBackend.expect('GET', 'http://www.omdbapi.com/?s=terminator'); | |
$httpBackend.expect('GET', 'http://www.omdbapi.com/?s=star+wars'); | |
$controller('MovieController', { $scope: $scope }); | |
$scope.keyword = 'star wars'; | |
$scope.search(); | |
$httpBackend.flush(); | |
expect($scope.movies).toEqual(testData.query.starwars); | |
}); | |
}); | |
describe('http tests (when vs expect)', function () { | |
it('should demonstrate using when (200 status)', inject(function($http) { | |
var $scope = {}; | |
/* code under test */ | |
$http.get('http://localhost/foo') | |
.success(function(data, status, headers, config) { | |
$scope.valid = true; | |
$scope.response = data; | |
}).error(function(data, status, headers, config) { | |
$scope.valid = false; | |
}); | |
/* end */ | |
$httpBackend | |
.when('GET', 'http://localhost/foo') | |
.respond(200, { foo: 'bar' }); | |
$httpBackend.flush(); | |
expect($scope.valid).toBe(true); | |
expect($scope.response).toEqual({ foo: 'bar' }); | |
})); | |
it('should demonstrate using expect (200 status)', inject(function($http) { | |
var $scope = {}; | |
/* code under test */ | |
$http.get('http://localhost/foo') | |
.success(function(data, status, headers, config) { | |
$scope.valid = true; | |
$scope.response = data; | |
}).error(function(data, status, headers, config) { | |
$scope.valid = false; | |
}); | |
/* end */ | |
$httpBackend | |
.expect('GET', 'http://localhost/foo') | |
.respond(200, { foo: 'bar' }); | |
expect($httpBackend.flush).not.toThrow(); | |
})); | |
it('should demonstrate using when and expect (200 status)', inject(function($http) { | |
var $scope = {}; | |
/* code under test */ | |
$http.get('http://localhost/foo') | |
.success(function(data, status, headers, config) { | |
$scope.valid = true; | |
$scope.response = data; | |
}).error(function(data, status, headers, config) { | |
$scope.valid = false; | |
}); | |
/* end */ | |
$httpBackend | |
.when('GET', 'http://localhost/foo') | |
.respond(200, { foo: 'bar' }); | |
$httpBackend | |
.expect('GET', 'http://localhost/foo'); | |
expect($httpBackend.flush).not.toThrow(); | |
expect($scope.valid).toBe(true); | |
expect($scope.response).toEqual({ foo: 'bar' }); | |
})); | |
it('should demonstrate using when (401 status)', inject(function($http) { | |
var $scope = {}; | |
/* Code under test */ | |
$http.get('http://localhost/foo') | |
.success(function(data, status, headers, config) { | |
$scope.valid = true; | |
}).error(function(data, status, headers, config) { | |
$scope.valid = false; | |
}); | |
/* End */ | |
$httpBackend.whenGET('http://localhost/foo').respond(401); | |
$httpBackend.flush(); | |
expect($scope.valid).toBe(false); | |
})); | |
it('should demonstrate using expect', inject(function($http) { | |
$httpBackend.expectGET('http://localhost/1').respond(200); | |
$httpBackend.expectGET('http://localhost/2').respond(200); | |
$httpBackend.expectGET('http://localhost/2').respond(200); | |
$httpBackend.expectGET('http://localhost/3').respond(200); | |
/* Code under test */ | |
$http.get('http://localhost/1'); | |
$http.get('http://localhost/2'); | |
$http.get('http://localhost/2'); | |
$http.get('http://localhost/3'); | |
/* End */ | |
expect($httpBackend.flush).not.toThrow(); | |
})); | |
it('should demonstrate bad case for using when', inject(function($http) { | |
$httpBackend.whenGET('http://localhost/1').respond(200); | |
$httpBackend.whenGET('http://localhost/2').respond(200); | |
$httpBackend.whenGET('http://localhost/3').respond(200); | |
/* Code under test */ | |
$http.get('http://localhost/2'); | |
$http.get('http://localhost/1'); | |
$http.get('http://localhost/3'); | |
$http.get('http://localhost/2'); | |
/* End */ | |
expect($httpBackend.flush).not.toThrow(); | |
})); | |
it('should demonstrate using expect and resetExpectations', inject(function($http) { | |
$httpBackend.expectGET('http://localhost/1').respond(200); | |
$http.get('http://localhost/1'); | |
$httpBackend.resetExpectations(); | |
$httpBackend.expectGET('http://localhost/1').respond(201); | |
expect($httpBackend.flush).not.toThrow(); | |
})); | |
}); | |
describe('when url parameter examples', function () { | |
it('should use regex for url', inject(function($http) { | |
var $scope = {}; | |
/* code under test */ | |
$http.get('http://localhost/foo') | |
.success(function(data, status, headers, config) { | |
$scope.fooData = data; | |
}); | |
$http.get('http://localhost/bar') | |
.success(function(data, status, headers, config) { | |
$scope.barData = data; | |
}); | |
/* end */ | |
$httpBackend | |
.when('GET', /^http:\/\/localhost\/(foo|bar)/) | |
.respond(200, { data: 'value' }); | |
$httpBackend.flush(); | |
expect($scope.fooData).toEqual({ data: 'value' }); | |
expect($scope.barData).toEqual({ data: 'value' }); | |
})); | |
it('should use function for url', inject(function($http) { | |
var $scope = {}; | |
/* code under test */ | |
$http.get('http://localhost/foo') | |
.success(function(data, status, headers, config) { | |
$scope.fooData = data; | |
}); | |
$http.get('http://localhost/bar') | |
.success(function(data, status, headers, config) { | |
$scope.barData = data; | |
}); | |
/* end */ | |
$httpBackend | |
.when('GET', function(url) { | |
return url.indexOf('http://localhost/') !== -1; | |
}) | |
.respond(200, { data: 'value' }); | |
$httpBackend.flush(); | |
expect($scope.fooData).toEqual({ data: 'value' }); | |
expect($scope.barData).toEqual({ data: 'value' }); | |
})); | |
}); | |
describe('when data parameter examples', function () { | |
it('should post data (object)', inject(function($http) { | |
var $scope = {}; | |
/* Code Under Test */ | |
$http.post('http://localhost/auth', { | |
username: 'hardcoded_user', | |
password: 'hardcoded_password' | |
}) | |
.success(function(data, status, headers, config) { | |
$scope.user = data; | |
}); | |
/* End Code */ | |
$httpBackend | |
.when('POST', 'http://localhost/auth', { | |
username: 'hardcoded_user', | |
password: 'hardcoded_password' | |
}) | |
.respond({ | |
username: 'hardcoded_user' | |
}); | |
$httpBackend.flush(); | |
expect($scope.user).toEqual({ username: 'hardcoded_user' }); | |
})); | |
it('should post data (string)', inject(function($http) { | |
var $scope = {}; | |
/* Code Under Test */ | |
$http.post('http://localhost/auth', { | |
username: 'hardcoded_user', | |
password: 'hardcoded_password' | |
}) | |
.success(function(data, status, headers, config) { | |
$scope.user = data; | |
}); | |
/* End Code */ | |
$httpBackend | |
.when('POST', 'http://localhost/auth', '{"username":"hardcoded_user","password":"hardcoded_password"}') | |
.respond({ | |
username: 'hardcoded_user' | |
}); | |
$httpBackend.flush(); | |
expect($scope.user).toEqual({ username: 'hardcoded_user' }); | |
})); | |
it('should post data (regex)', inject(function($http) { | |
var $scope = {}; | |
/* Code Under Test */ | |
$http.post('http://localhost/auth', { | |
username: 'hardcoded_user', | |
password: 'hardcoded_password' | |
}) | |
.success(function(data, status, headers, config) { | |
$scope.user = data; | |
}) | |
.error(function(data, status, headers, config) { | |
$scope.status = 'login failed'; | |
}); | |
/* End Code */ | |
$httpBackend | |
.when('POST', 'http://localhost/auth', /{"username":".*","password":"hardcoded_password"}/) | |
.respond({ | |
username: 'hardcoded_user' | |
}); | |
$httpBackend.flush(); | |
expect($scope.user).toEqual({ username: 'hardcoded_user' }); | |
})); | |
it('should post data (function)', inject(function($http) { | |
var $scope = {}; | |
/* Code Under Test */ | |
$http.post('http://localhost/auth', { | |
username: 'hardcoded_user', | |
password: 'hardcoded_password' | |
}) | |
.success(function(data, status, headers, config) { | |
$scope.user = data; | |
}); | |
/* End Code */ | |
$httpBackend | |
.when('POST', 'http://localhost/auth', function(data) { | |
return angular.fromJson(data).username === 'hardcoded_user' | |
}) | |
.respond({ | |
username: 'hardcoded_user' | |
}); | |
$httpBackend.flush(); | |
expect($scope.user).toEqual({ username: 'hardcoded_user' }); | |
})); | |
}); | |
describe('when header parameter examples', function () { | |
it('should use Object', inject(function($http) { | |
var $scope = {}; | |
/* code under test */ | |
$http.get('http://localhost/foo', { | |
headers: { 'authToken': 'teddybear' } | |
}) | |
.success(function(data, status, headers, config) { | |
$scope.fooData = data; | |
}); | |
/* end */ | |
$httpBackend | |
.when('GET', 'http://localhost/foo', undefined, { | |
authToken: "teddybear", | |
Accept: "application/json, text/plain, */*" | |
}) | |
.respond(200, { data: 'value' }); | |
$httpBackend.flush(); | |
expect($scope.fooData).toEqual({ data: 'value' }); | |
})); | |
it('should use Function', inject(function($http) { | |
var $scope = {}; | |
/* code under test */ | |
$http.get('http://localhost/foo', { | |
headers: { 'authToken': 'teddybear' } | |
}) | |
.success(function(data, status, headers, config) { | |
$scope.fooData = data; | |
}); | |
/* end */ | |
$httpBackend | |
.when('GET', 'http://localhost/foo', undefined, function(headers) { | |
return headers.authToken === 'teddybear'; | |
}) | |
.respond(200, { data: 'value' }); | |
$httpBackend.flush(); | |
expect($scope.fooData).toEqual({ data: 'value' }); | |
})); | |
}); | |
describe('when response object', function() { | |
it('should use respond with params', inject(function($http) { | |
var $scope = {}; | |
/* Code Under Test */ | |
$http | |
.get('http://localhost/foo') | |
.success(function(data, status, headers, config, statusText) { | |
// demonstrates how the values set in respond are used | |
if (status === 200 && statusText !== 'OK') { | |
$scope.fooData = data; | |
$scope.tokenValue = headers('token'); | |
} | |
}); | |
/* End */ | |
$httpBackend | |
.when('GET', 'http://localhost/foo') | |
.respond(200, { data: 'value' }, { token: 'token value' }, 'OK'); | |
$httpBackend.flush(); | |
expect($scope.fooData).toEqual({ data: 'value' }); | |
expect($scope.tokenValue).toEqual('token value'); | |
})); | |
it('should use respond with function', inject(function($http) { | |
var $scope = {}; | |
/* Code Under Test */ | |
$http | |
.get('http://localhost/foo') | |
.success(function(data, status, headers, config, statusText) { | |
// console.log(statusText); | |
// demonstrates how the values set in respond are used | |
if (status === 200) { | |
$scope.fooData = data; | |
$scope.tokenValue = headers('token'); | |
} | |
}); | |
/* End */ | |
$httpBackend | |
.when('GET', /.*/) | |
.respond(function(method, url, data, headers) { | |
// example of how one might use function arguments to access request | |
if (url === 'http://localhost/foo') { | |
return [200, { data: 'value' }, { token: 'token value' }, 'OK']; | |
} | |
return [404]; | |
}); | |
$httpBackend.flush(); | |
expect($scope.fooData).toEqual({ data: 'value' }); | |
expect($scope.tokenValue).toEqual('token value'); | |
})); | |
it('should re-use respond function', inject(function($http) { | |
var $scope = {}; | |
/* Code Under Test */ | |
var foo = function() { | |
$http | |
.get('http://localhost/foo') | |
.success(function(data, status, headers, config, statusText) { | |
// demonstrates how the values set in respond are used | |
if (status === 200) { | |
$scope.fooData = data; | |
$scope.tokenValue = headers('token'); | |
} | |
}); | |
}; | |
foo(); | |
foo(); | |
/* End */ | |
var whenRespondObj = $httpBackend | |
.when('GET', 'http://localhost/foo') | |
.respond(200, { data: 'value' }, { token: 'token value' }, 'OK'); | |
$httpBackend.flush(1); | |
expect($scope.fooData).toEqual({ data: 'value' }); | |
expect($scope.tokenValue).toEqual('token value'); | |
whenRespondObj.respond(200, { data: 'second value' }, { token: 'token value' }, 'OK'); | |
$httpBackend.flush(1); | |
expect($scope.fooData).toEqual({ data: 'second value' }); | |
expect($scope.tokenValue).toEqual('token value'); | |
})); | |
}); | |
describe('using flush', function() { | |
it('should re-use respond function', inject(function($http) { | |
var $scope = {}; | |
/* Code Under Test */ | |
var foo = function() { | |
$http | |
.get('http://localhost/foo') | |
.success(function(data, status, headers, config, statusText) { | |
// demonstrates how the values set in respond are used | |
if (status === 200) { | |
$scope.fooData = data; | |
$scope.tokenValue = headers('token'); | |
} | |
}); | |
}; | |
foo(); | |
foo(); | |
/* End */ | |
var whenRespondObj = $httpBackend | |
.when('GET', 'http://localhost/foo') | |
.respond(200, { data: 'value' }, { token: 'token value' }, 'OK'); | |
$httpBackend.flush(1); | |
expect($scope.fooData).toEqual({ data: 'value' }); | |
expect($scope.tokenValue).toEqual('token value'); | |
whenRespondObj.respond(200, { data: 'second value' }, { token: 'token value' }, 'OK'); | |
$httpBackend.flush(1); | |
expect($scope.fooData).toEqual({ data: 'second value' }); | |
expect($scope.tokenValue).toEqual('token value'); | |
})); | |
}); | |
describe('using verify and reset', function() { | |
it('should demonstrate usage of verifyNoOutstandingExpectation and rest', inject(function($http) { | |
$httpBackend.expectGET('http://localhost/foo').respond(200); | |
$httpBackend.expectGET('http://localhost/bar').respond(500); | |
// without this, verifyNoOutstandingExpectation would throw an exception | |
$httpBackend.resetExpectations(); | |
expect($httpBackend.verifyNoOutstandingExpectation).not.toThrow(); | |
expect($httpBackend.verifyNoOutstandingRequest).not.toThrow(); | |
})); | |
it('should demonstrate usage of verifyNoOutstandingRequest', inject(function($http) { | |
$httpBackend | |
.whenGET('http://localhost/foo') | |
.respond(200, { foo: 'bar' }); | |
$http.get('http://localhost/foo'); | |
$http.get('http://localhost/foo'); | |
// NB we have only flushed the first http call, leaving one un-flushed | |
$httpBackend.flush(1); | |
expect($httpBackend.verifyNoOutstandingRequest).toThrow(); | |
})); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment