Ryan's question on conditional HTTP calls

Ryan's question on conditional HTTP calls

by eitanp461

HTML

<script src="http://pivotal.github.io/jasmine/lib/jasmine-1.3.1/jasmine.js"></script>
<script src="http://pivotal.github.io/jasmine/lib/jasmine-1.3.1/jasmine-html.js"></script>
<link rel="stylesheet" href="http://pivotal.github.io/jasmine/lib/jasmine-1.3.1/jasmine.css">
<script src="http://code.angularjs.org/1.0.5/angular.js"></script>
<script src="http://code.angularjs.org/1.0.5/angular-mocks.js"></script>

JavaScript

//--- CODE --------------------------
var myApp = angular.module('myApp', []);

// Create a service with dependencies
myApp.provider('MyService', function () {
    // Implement $get as this is a service provider
    this.$get = ['$http', '$q', 

    function ($http, $q) {
        // If false it will return a cached object if true it goes to server
        function getWorkflow(forceGetFromServer) {

            if (!forceGetFromServer) {
                return 'cached';
            }
            
            // Get server url from configuration service dependency
            var url = '/path/to/resource';

            var defer = $q.defer();

            // Perform the actual HTTP call with query parameters
            // e.g. GET <server url>/someUrl?key=value1
            $http({
                method: 'GET',
                url: url,
                headers: {
                    'Accept-Language': 'en'
                },
                params: {
                    key1: 'value1'
                }
            }).
            success(function (data, status, headers, config) {
                // this callback will be called asynchronously
                // when the response is available
                defer.resolve();
            }).
            error(function (data, status, headers, config) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
                defer.reject();
            });

            return defer.promise;
        }
        return {
            getWorkflow: function (forceGetFromServer) {
                return getWorkflow(forceGetFromServer);
            }
        };
    }];
});

// --- SPECS -------------------------

describe('myApp', function () {
    var myService,
    $httpBackend,
    ConfigurationServiceMock,
    url = '/path/to/resource',
        paramUrl = '/path/to/resource?key1=value1';

    beforeEach(function () {
        // Initialize myApp...