{"size": 4024, "ext": "js", "max_stars_repo_path": "apps/community-app/test/spec/services/HttpServiceProviderSpec.js", "max_stars_repo_name": "justmurenga/fineract", "max_stars_repo_stars_event_min_datetime": "2019-03-11T16:17:33.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-22T21:57:51.000Z", "max_issues_repo_path": "apps/community-app/test/spec/services/HttpServiceProviderSpec.js", "max_issues_repo_name": "justmurenga/fineract", "max_issues_repo_issues_event_min_datetime": "2019-11-05T20:22:16.000Z", "max_issues_repo_issues_event_max_datetime": "2019-12-11T17:09:04.000Z", "max_forks_repo_path": "apps/community-app/test/spec/services/HttpServiceProviderSpec.js", "max_forks_repo_name": "justmurenga/fineract", "max_forks_repo_forks_event_min_datetime": "2020-11-19T16:00:36.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-19T09:36:13.000Z"}
{"max_stars_count": 7, "max_issues_count": 3, "max_forks_count": 2, "avg_line_length": 41.9166666667, "max_line_length": 132, "alphanum_fraction": 0.5586481113}
describe("HttpServiceProvider", function () {
    var http, httpService;
    beforeEach(function () {
        this.provider = new mifosX.services.HttpServiceProvider();
    });

    describe("Http service", function () {
        describe("HTTP methods", function () {
            beforeEach(function () {
                http = jasmine.createSpy("$http").andReturn('httpPromise');
                httpService = this.provider.$get[1](http);
            });

            _.each(['get', 'head', 'delete'], function (method) {
                it("should delegate to the correspoding http " + method, function () {
                    var promise = httpService[method]("test_url");

                    expect(http).toHaveBeenCalledWith({ method: method.toUpperCase(), url: "test_url"});
                    expect(promise).toEqual('httpPromise');
                });
            });

            _.each(['post', 'put'], function (method) {
                it("should delegate to the correspoding http " + method, function () {
                    var promise = httpService[method]("test_url", {test: 'some_data'});

                    expect(http).toHaveBeenCalledWith({ method: method.toUpperCase(), url: "test_url", data: {test: 'some_data'} });
                    expect(promise).toEqual('httpPromise');
                });
            });
        });

        describe("Authorization", function () {
            beforeEach(function () {
                http = { defaults: { headers: { common: {} } } };
                httpService = this.provider.$get[1](http);
            });
            it("should set the Authorization header", function () {
                httpService.setAuthorization("test_key");

                expect(http.defaults.headers.common.Authorization).toEqual("Basic test_key");
            });

            it("should remove the Authorization header", function () {
                http.defaults.headers.common.Authorization = "test";
                httpService.cancelAuthorization();

                expect(http.defaults.headers.common.Authorization).toBeUndefined();
            });
        });
    });

    describe("Request interceptors", function () {
        beforeEach(function () {
            http = jasmine.createSpy("$http").andReturn('httpPromise');
            httpService = this.provider.$get[1](http);
        });

        it("should add a new interceptor", function () {
            this.provider.addRequestInterceptor("test_interceptor", function (config) {
                return _.extend(config, {url: "test_another_url"});
            });

            httpService.get("test_url");

            expect(http).toHaveBeenCalledWith({ method: 'GET', url: "test_another_url" });
        });

        it("should add a chain of interceptors", function () {
            this.provider.addRequestInterceptor("test_interceptor1", function (config) {
                return _.extend(config, {url: "test_another_url"});
            });
            this.provider.addRequestInterceptor("test_interceptor2", function (config) {
                return _.extend(config, {headers: {AnotherHeader: "test_header"}})
            });

            httpService.get("test_url");

            expect(http).toHaveBeenCalledWith({ method: 'GET', url: "test_another_url", headers: {AnotherHeader: "test_header"} });
        });

        it("should remove an interceptor", function () {
            this.provider.addRequestInterceptor("test_interceptor1", function (config) {
                return _.extend(config, {url: "test_another_url"});
            });
            this.provider.addRequestInterceptor("test_interceptor2", function (config) {
                return _.extend(config, {headers: {AnotherHeader: "test_header"}})
            });
            this.provider.removeRequestInterceptor("test_interceptor1");

            httpService.get("test_url");

            expect(http).toHaveBeenCalledWith({ method: 'GET', url: "test_url", headers: {AnotherHeader: "test_header"} });
        });
    });
});