Skip to content

Instantly share code, notes, and snippets.

@callmehiphop
Last active August 29, 2015 14:04
Show Gist options
  • Save callmehiphop/45f0884b20e4402e0603 to your computer and use it in GitHub Desktop.
Save callmehiphop/45f0884b20e4402e0603 to your computer and use it in GitHub Desktop.
Convenience wrapper for testing HTTP headers
function expectRequest (method, url, data) {
var $httpBackend;
inject(function ($injector) {
$httpBackend = $injector.get('$httpBackend');
});
function withHeaders (expectedHeaders) {
return $httpBackend.expect(method, url, data, function (actualHeaders) {
for (var header in expectedHeaders) {
if (actualHeaders[header] !== expectedHeaders[header]) {
return false;
}
}
return true;
});
}
return {
withHeaders: withHeaders
};
}
it('should do a thing', function () {
expectRequest('GET', 'http://place.com/thing')
.withHeaders({
Authorization: 'FuzzyMonkeys'
})
.respond({
message: 'You are the fuzziest monkey.'
});
$http.get('http://place.com/thing', {
headers: {
Authorization: 'BaldMonkeys'
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment