AngularJS + Jasmine + JWT Token in $http requests

一笑奈何 提交于 2019-12-12 13:51:16

问题


I'm trying to build some test with an API that works with JWT authentication tokens, but the factories that I'm trying to test use $resouce, which needs to be configured with JWT Token. In the app I configure it in the .config, but in Jasmine I have no idea how it should be, because Jasmine runs before the app.

I'm trying to find the way to configure Jasmine's requests or maybe run the tests after the app is configured.

Here I'm trying to configure the Jasmine's requests.

describe("Sync.Remote", function () {
    var Remote = angular.injector(['sync.remote']).get('Remote')('Test', 'test')
      , testingObject = { name: "Mario", lastname: "López" }

    beforeEach(inject(function (_$httpProvider_, _jwtInterceptorProvider_, _$resourceProvider_) {
        $httpProvider = _$httpProvider_
        jwtInterceptorProvider = _jwtInterceptorProvider_
        $resourceProvider = _$resourceProvider_

        jwtInterceptorProvider.authPrefix = "JWT "
        jwtInterceptorProvider.tokenGetter = function(config) {
            if(config.url.indexOf("http://some.website.com") === 0) {
                return "XXXXXXX-JWT-TOKEN"
            }
        }

        $httpProvider.interceptors.push('jwtInterceptor')
        $httpProvider.defaults.withCredentials = true
        $resourceProvider.defaults.stripTrailingSlashes = false
    }))

    it("#_create es capaz de crear un objeto en la nube", function (done) {
        Remote._create(testingObject)
        .then(function (item) {
            expect(item.id).not.toBe(undefined)
        }, function (err) {
            expect(0).toEqual(1)
        })
        .finally(done)
    })
})

But it doesn't work because the $resouce service is inside the Remote factory, it is a dependecy actually. Any ideas?

来源:https://stackoverflow.com/questions/30947701/angularjs-jasmine-jwt-token-in-http-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!