Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. myApp.factory('UserApi', ['$http', function($http) {
  2. var checkCredentialsUrl = '....';
  3. return {
  4. checkCredentials: function(user, pass) {
  5. return $http({
  6. method: 'POST',
  7. data: {
  8. jUser: user,
  9. jPass: pass
  10. },
  11. 'headers': {
  12. 'Content-Type': 'application/x-www-form-urlencoded'
  13. },
  14. transformRequest: function(obj) {
  15. var str = [];
  16. for(var p in obj)
  17. str.push(encodeURIComponent(p)+"="+encodeURIComponent(obj[p]));
  18. return str.join("&");
  19. },
  20. url: checkCredentialsUrl
  21. });
  22. }
  23. }
  24. }]);
  25.  
  26. describe('UserApi', function () {
  27.  
  28. var UserApi, $httpBackend, user, pass, checkCredentialsUrl, succeeded = false;
  29.  
  30. beforeEach(module('myApp'));
  31.  
  32. beforeEach(inject(function (_UserApi_, _$httpBackend_){
  33. UserApi = _UserApi_;
  34. $httpBackend = _$httpBackend_;
  35.  
  36. checkCredentialsUrl = '...';
  37. user = 'qwerty';
  38. pass = 'qwerty';
  39. }));
  40.  
  41. afterEach(function() {
  42. $httpBackend.verifyNoOutstandingExpectation();
  43. $httpBackend.verifyNoOutstandingRequest();
  44. });
  45.  
  46. describe('Auth', function () {
  47.  
  48. it('CheckCredentials', function () {
  49.  
  50. $httpBackend.expectPOST( checkCredentialsUrl, {
  51. jUser: user,
  52. jPass: pass
  53. }, function(headers) {
  54. return headers['Content-Type'] === 'application/x-www-form-urlencoded';
  55. }).respond(200);
  56.  
  57. UserApi.checkCredentials(user, pass).then(function(response){
  58. succeeded = true;
  59. });
  60.  
  61. $httpBackend.flush();
  62.  
  63. expect(succeeded).toBe(true);
  64. });
  65. });
  66.  
  67. });
  68.  
  69. SyntaxError: Unexpected token j in JSON at position 0
  70. at Object.parse (native)
  71. at Object.fromJson (node_modules/angular/angular.js:1333:14)
  72. at MockHttpExpectation.matchData (node_modules/angular-mocks/angular-mocks.js:1928:77)
  73. at $httpBackend (node_modules/angular-mocks/angular-mocks.js:1384:24)
  74. at sendReq (node_modules/angular/angular.js:11776:9)
  75. at serverRequest (node_modules/angular/angular.js:11571:16)
  76. at processQueue (node_modules/angular/angular.js:16383:28)
  77. at node_modules/angular/angular.js:16399:27
  78. at Scope.$eval (node_modules/angular/angular.js:17682:28)
  79. at Scope.$digest (node_modules/angular/angular.js:17495:31)
  80. Error: [$rootScope:inprog] $digest already in progress
  81. http://errors.angularjs.org/1.5.8/$rootScope/inprog?p0=%24digest
  82. at node_modules/angular/angular.js:68:12
  83. at beginPhase (node_modules/angular/angular.js:18042:15)
  84. at Scope.$digest (node_modules/angular/angular.js:17472:9)
  85. at Function.$httpBackend.verifyNoOutstandingExpectation (node_modules/angular-mocks/angular-mocks.js:1830:38)
  86. at Object.<anonymous> (tests/controllers/auth.test.js:19:22)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement