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

lastCookieString.split error with httpBackend.flush() #11373

Closed
jcdalton2201 opened this issue Mar 19, 2015 · 19 comments
Closed

lastCookieString.split error with httpBackend.flush() #11373

jcdalton2201 opened this issue Mar 19, 2015 · 19 comments

Comments

@jcdalton2201
Copy link

Using v1.4.0-build.3918+sha.4588e62 my unit test are now having the following error.

    TypeError: 'undefined' is not an object (evaluating 'lastCookieString.split'
)
        at C:/TR Forks/fef/bower_components/angular/angular.js:17354
        at sendReq (C:/TR Forks/fef/bower_components/angular/angular.js:9824)
        at C:/TR Forks/fef/bower_components/angular/angular.js:9542
        at processQueue (C:/TR Forks/fef/bower_components/angular/angular.js:140
60)
        at C:/TR Forks/fef/bower_components/angular/angular.js:14076
        at C:/TR Forks/fef/bower_components/angular/angular.js:15283
        at C:/TR Forks/fef/bower_components/angular/angular.js:15098
        at C:/TR Forks/fef/bower_components/angular-mocks/angular-mocks.js:1508
        at C:/TR Forks/fef/test/spec/services/FefLoagger.js:263

The unit test is this

it('test mult call is correct format.', function() {
        var success = true;
        httpBackend.expectPOST('/demo/logs')
            .respond(
                function(method, url, data, headers) {
                    var validData = {
                        message: ['test'],
                        level: 'log',
                        name: 'fef:log1'
                    };
                    data = JSON.parse(data);
                    if (data.message[0] === validData.message[0] &&
                        data.level === validData.level &&
                        data.name === validData.name) {
                        return [200, {
                            token: '12345'
                        }];
                    } else {
                        success = false;
                        return [400, {
                            failure: 'bad'
                        }];
                    }
                });
        var log1 = FefLogger.getLogger('log1');
        log1.log('test');
        httpBackend.flush();
        expect(success).toBe(true);

    });

the line 263 coralates with the httpBackend.flush();
this unit test did work with 1.4.0-beta.0 and 1.3.15

@shahata
Copy link
Contributor

shahata commented Mar 21, 2015

Yup, that's my fault. Fix is coming :)

@shahata
Copy link
Contributor

shahata commented Mar 22, 2015

@jcdalton2201 can you confirm that you are indeed mocking out $documentin your tests as I suggested in the fix? If not, can you try to create a minimal reproduce of the issue so I can look into it?

@jcdalton2201
Copy link
Author

We do not have the $document mocked out. Our test was simply trying to test a $resource call.

@shahata
Copy link
Contributor

shahata commented Mar 22, 2015

@jcdalton2201 can you post a minimal reproduce on plunker or something similar?

@jcdalton2201
Copy link
Author

@mfrye-intuit
Copy link

I don't know if it's related or not, but the latest release has been breaking our tests as well and points to the cookies. Our version is 1.4.0-build.3869+sha.3b8163b.

The errors:

PhantomJS 1.9.8 (Mac OS X) Appointments Controller:  OLB Reschedule Modal Controller should call for appointment types FAILED
    TypeError: 'undefined' is not a function (evaluating '$browser.cookies()')
        at sendReq (/Users/mfrye2/code/onboarding/bower_components/angular/angular.js:8420)
        at /Users/mfrye2/code/onboarding/bower_components/angular/angular.js:8146
        at /Users/mfrye2/code/onboarding/bower_components/angular/angular.js:11682
        at /Users/mfrye2/code/onboarding/bower_components/angular/angular.js:11768
        at /Users/mfrye2/code/onboarding/bower_components/angular/angular.js:12811
        at /Users/mfrye2/code/onboarding/bower_components/angular/angular.js:12623
        at /Users/mfrye2/code/onboarding/node_modules/angular-mocks/angular-mocks.js:1508
        at /Users/mfrye2/code/onboarding/test/unit/controllers/appointments.js:430
PhantomJS 1.9.8 (Mac OS X) Appointments Controller:  OLB Reschedule Modal Controller should build the mixpanel payload correctly FAILED
    TypeError: 'undefined' is not a function (evaluating '$browser.cookies()')
        at sendReq (/Users/mfrye2/code/onboarding/bower_components/angular/angular.js:8420)
        at /Users/mfrye2/code/onboarding/bower_components/angular/angular.js:8146
        at /Users/mfrye2/code/onboarding/bower_components/angular/angular.js:11682
        at /Users/mfrye2/code/onboarding/bower_components/angular/angular.js:11768
        at /Users/mfrye2/code/onboarding/bower_components/angular/angular.js:12811
        at /Users/mfrye2/code/onboarding/bower_components/angular/angular.js:12623
        at /Users/mfrye2/code/onboarding/node_modules/angular-mocks/angular-mocks.js:1508
        at /Users/mfrye2/code/onboarding/test/unit/controllers/appointments.js:435
PhantomJS 1.9.8 (Mac OS X) Controllers: Onboarding Controller "before each" hook FAILED
    TypeError: 'undefined' is not a function (evaluating '$browser.addPollFn')
        at /Users/mfrye2/code/onboarding/bower_components/angular-cookies/angular-cookies.js:68
        at invoke (/Users/mfrye2/code/onboarding/bower_components/angular/angular.js:3965)
        at /Users/mfrye2/code/onboarding/bower_components/angular/angular.js:3807
        at getService (/Users/mfrye2/code/onboarding/bower_components/angular/angular.js:3929)
        at invoke (/Users/mfrye2/code/onboarding/bower_components/angular/angular.js:3956)
        at instantiate (/Users/mfrye2/code/onboarding/bower_components/angular/angular.js:3976)
        at /Users/mfrye2/code/onboarding/bower_components/angular/angular.js:7315
        at /Users/mfrye2/code/onboarding/node_modules/angular-mocks/angular-mocks.js:1861
        at /Users/mfrye2/code/onboarding/test/unit/controllers/controllersSpec.js:12
        at invoke (/Users/mfrye2/code/onboarding/bower_components/angular/angular.js:3965)
        at workFn (/Users/mfrye2/code/onboarding/node_modules/angular-mocks/angular-mocks.js:2418)

Then I noticed that this section is missing from the latest release, which may be causing the problems:

lines 69, 70:

self.cookieHash = {};
self.lastCookieHash = {};

lines 150-153:

addPollFn: function(pollFn) {
    this.pollFns.push(pollFn);
    return pollFn;
  },

lines 173-190:

 cookies:  function(name, value) {
    if (name) {
      if (angular.isUndefined(value)) {
        delete this.cookieHash[name];
      } else {
        if (angular.isString(value) &&       //strings only
            value.length <= 4096) {          //strict cookie storage limits
          this.cookieHash[name] = value;
        }
      }
    } else {
      if (!angular.equals(this.cookieHash, this.lastCookieHash)) {
        this.lastCookieHash = angular.copy(this.cookieHash);
        this.cookieHash = angular.copy(this.cookieHash);
      }
      return this.cookieHash;
    }
  },

@petebacondarwin
Copy link
Contributor

@jcdalton2201 - I think the problem is that you have mocked out the $window service and the new version of $cookies expects to get the $document from the $window and it expects the $document to contain a property called cookies that is a string. A simple workaround is on lines 15-17 of your Plunker's app.js, change it to:

    var windowMock = {
        navigator: {},
        document: { cookie: '' }
    };

See http://plnkr.co/edit/TQl5Qc?p=preview

But I think that a sensible thing to do inside $cookieReader is to convert undefined to '' for this property on $document.

@shahata shahata closed this as completed in a057e08 Apr 3, 2015
@juristr
Copy link

juristr commented May 29, 2015

hmm..what if I get the same issue with v1.3.15 ??? 😒

@Pyo25
Copy link

Pyo25 commented Jun 2, 2015

I'm facing the same problem with Angular v1.3.15. The problem appeared when I deleted/re-installed all my bower dependencies (it worked with Angular v1.3.15 before).

To fix the problem I used this small hack that mocks the $browser service.

    beforeEach(function() {
        inject(function(_$browser_) {
          _$browser_['cookies'] = function() { return {}; } 
        });
    });

@csreddy
Copy link

csreddy commented Jun 2, 2015

@Pyo25 - I'm facing exact problem like yours. Thanks for sharing the workaround.

@petebacondarwin
Copy link
Contributor

@Pyo25 and @csreddy - can you please provide an example of this failing in 1.3.15 - this will make it easier to provide an update to 1.3.x

@juristr
Copy link

juristr commented Jun 3, 2015

@Pyo25 @csreddy Happened to me as well. When I deleted my bower dependencies and reinstalled them, it did no more work (while it did before). My 1st thought was an implicit Angular upgrade as well, but I also tried to downgrade to various other v1.3.x versions, which didn't solve the issue.
Given that (although that seems strange), it looks like the problem lies in one of the other bower packages...

@shahata
Copy link
Contributor

shahata commented Jun 3, 2015

@Pyo25 @csreddy @juristr can you please share the stack trace?

@juristr
Copy link

juristr commented Jun 3, 2015

@shahata It looks like this:

 TypeError: 'undefined' is not a function (evaluating '$browser.cookies()')
            at sendReq (c:/projects/sdi/mapsconsole_bkup/bower_components/angular/angular.js:9661)
            at c:/projects/sdi/mapsconsole_bkup/bower_components/angular/angular.js:9383
            at processQueue (c:/projects/sdi/mapsconsole_bkup/bower_components/angular/angular.js:13248)
            at c:/projects/sdi/mapsconsole_bkup/bower_components/angular/angular.js:13264
            at c:/projects/sdi/mapsconsole_bkup/bower_components/angular/angular.js:14466
            at c:/projects/sdi/mapsconsole_bkup/bower_components/angular/angular.js:14282
            at c:/projects/sdi/mapsconsole_bkup/bower_components/angular-mocks/angular-mocks.js:1510
            at c:/projects/sdi/mapsconsole_bkup/test/spec/authentication/user.service.spec.js:151

I never use Angular cookies explicitly. What I do in user.service.spec.js is to use the $httpBackend. As already mentioned by others, it seems like the $httpBackend.flush() call causes the error. For instance, user.service.spec.js:151 in the stacktrace is where I invoke ...flush(). Here's an excerpt:

...
beforeEach(function(){
    var dummyUserObj = {
        id: 1,
        username: 'Juri',
        roles: [
            {
                role: 'admin',
                functions: [ 'person.read', 'person.write']
            }
        ]
    };

    $httpBackend
        .when('GET', '/api/v1/users/current')
        .respond(dummyUserObj);

    service.getCurrent();

    $httpBackend.flush();
});
...

@csreddy
Copy link

csreddy commented Jun 3, 2015

Here's my example, stack trace is similar to @juristr

beforeEach(inject(function(_Bug_, $httpBackend) {
        Bug = _Bug_;
        httpBackend = $httpBackend;
        currentUser = {
            name: 'admin',
            username: 'admin',
            email: 'admin@email.com'
        };
    }));

    // make sure no expectations were missed in your tests.
    afterEach(function() {
        httpBackend.verifyNoOutstandingExpectation();
        httpBackend.verifyNoOutstandingRequest();
    });
    it('should get bug count', function() {
        httpBackend.whenGET('/api/bugs/count').respond({
            count: 100
        });
        Bug.count().then(function(response) {
            expect(response.data.count).toBe(100);
        });
        httpBackend.flush();
    });
TypeError: 'undefined' is not a function (evaluating '$browser.cookies()')
        at sendReq (/Users/sreddy/space/angularjs/ML-BugTrack/client/bower_components/angular/angular.js:9661)
        at /Users/sreddy/space/angularjs/ML-BugTrack/client/bower_components/angular/angular.js:9383
        at processQueue (/Users/sreddy/space/angularjs/ML-BugTrack/client/bower_components/angular/angular.js:13248)
        at /Users/sreddy/space/angularjs/ML-BugTrack/client/bower_components/angular/angular.js:13264
        at /Users/sreddy/space/angularjs/ML-BugTrack/client/bower_components/angular/angular.js:14466
        at /Users/sreddy/space/angularjs/ML-BugTrack/client/bower_components/angular/angular.js:14282
        at /Users/sreddy/space/angularjs/ML-BugTrack/client/bower_components/angular-mocks/angular-mocks.js:1510
        at /Users/sreddy/space/angularjs/ML-BugTrack/client/app/services/bug/bug.service.spec.js:40

@shahata
Copy link
Contributor

shahata commented Jun 3, 2015

@csreddy @juristr - Which version of angular-mocks are you using? I'm pretty certain you are using angular-mocks 1.4.0 with angular 1.3.15. Please make sure you are using the same version for angular and all of its modules.

@juristr
Copy link

juristr commented Jun 3, 2015

@shahata Ya, that was it...damn, could have come to this conclusion by myself 😊

Thx @shahata for the hint 😄

@csreddy
Copy link

csreddy commented Jun 3, 2015

Yes that was it 👍 (face palm moment). Thank you!

@Pyo25
Copy link

Pyo25 commented Jun 5, 2015

Same for me, thanks @shahata !

netman92 pushed a commit to netman92/angular.js that referenced this issue Aug 8, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.