<commit_before>var HttpRequest  = require('./httprequest.js'),
    HttpResponse = require('./httpresponse.js'),
    JavascriptBuilder = require('./javascriptbuilder.js'),
    _            = require('lodash');

/**
* HttpBackend
*/
var HttpBackend = function(browser, options) {
    this.browser = browser;
    this.browserGet = this.browser.get;
    this.options = options;
    this.flow = this.browser.driver.controlFlow;
    if (!this.options || this.options == 'undefined') {
        this.options = {};
    }

    if (!_.isBoolean(this.options.autoSync)) {
        this.options.autoSync = true;
    }

    this.reset();
};

HttpBackend.prototype.reset = function() {
    this.isInitialized = false;
    this.clear();
    this._init();
};

HttpBackend.prototype.clear = function() {
    this.responses = [];
    this.expectations = [];
    this.unSyncResponses = [];
    this.browser.get = this.browserGet;
    if (this.isInitialized) {
        this.browser.removeMockModule('httpBackendMock');
    }
};



HttpBackend.prototype.when = function(method, url) {
    return this._when(method, url);
};

HttpBackend.prototype.expect = function(method, url) {
    return this._expect(method, url);
};

var generateWhenHelpers = function(methods) {
    methods.forEach(function(method) {
        HttpBackend.prototype['when' + method] = function(url) {
            return this._when(method, url);
        };    
    })
};

var generateExpectHelpers = function(methods) {
    methods.forEach(function(method) {
        HttpBackend.prototype['expect' + method] = function(url) {
            return this._expect(method, url);
        };
    })
};

generateWhenHelpers([
    'GET',
    'POST',
    'PUT',
    'DELETE',
    'HEAD',
    'PATCH',
    'JSONP'
]);

generateExpectHelpers([
    'GET',
    'POST',
    'PUT',
    'DELETE',
    'HEAD',
    'PATCH',
    'JSONP'
]);

HttpBackend.prototype.init = function() {

    this.isInitialized = true;
    var jsBuilder = new JavascriptBuilder();
    jsBuilder.responses = this.responses;
    jsBuilder.expectations = this.expectations;
    this.unSyncResponses = [];

    return this.browser.addMockModule('httpBackendMock', jsBuilder.toString(true));
};

HttpBackend.prototype.sync = function() {
    var jsBuilder = new JavascriptBuilder();
    jsBuilder.responses = this.unSyncResponses;
    jsBuilder.expectations = this.expectations;
    this.browser.driver.executeScript(jsBuilder.toString());
    this.unSyncResponses = [];
    this.expectations = [];
};

HttpBackend.prototype._init = function() {
    var _this = this;
    this.browser.get = function(destination, opt_timeout) {
        if (!_this.isInitialized) {
            _this.init();
        }

        return _this.browserGet.call(browser, destination, opt_timeout);
    };
};

HttpBackend.prototype._when = function(method, url) {
    var request = new HttpRequest(method, url);
    var response = new HttpResponse(request);

    var responseIndex = _.findIndex(this.responses, function(response) {
        return response.request.uid == request.uid;
    });

    if (responseIndex) {
        this.responses.splice(responseIndex, 1, response);
    }

    if (this.options.autoSync && this.isInitialized) {
        var _this = this;
        response.onResponse = function() {
            _this.sync();
        };
    }

    this.responses.push(response);
    this.unSyncResponses.push(response);

    return response;
};

HttpBackend.prototype.verifyNoOutstandingExpectation = function(){
    var jsBuilder = new JavascriptBuilder();
    this.browser.driver.executeScript(jsBuilder.verifyNoOutstandingExpectation());
    //func();
};

HttpBackend.prototype._expect = function(method, url) {
    var expectation = new HttpRequest(method, url);
    this.expectations.push(expectation);
    return expectation;
};

module.exports = HttpBackend;
<commit_msg>added new features explanation to readme<commit_after>var HttpRequest  = require('./httprequest.js'),
    HttpResponse = require('./httpresponse.js'),
    JavascriptBuilder = require('./javascriptbuilder.js'),
    _            = require('lodash');

/**
* HttpBackend
*/
var HttpBackend = function(browser, options) {
    this.browser = browser;
    this.browserGet = this.browser.get;
    this.options = options;
    this.flow = this.browser.driver.controlFlow;
    if (!this.options || this.options == 'undefined') {
        this.options = {};
    }

    if (!_.isBoolean(this.options.autoSync)) {
        this.options.autoSync = true;
    }

    this.reset();
};

HttpBackend.prototype.reset = function() {
    this.isInitialized = false;
    this.clear();
    this._init();
};

HttpBackend.prototype.clear = function() {
    this.responses = [];
    this.expectations = [];
    this.unSyncResponses = [];
    this.browser.get = this.browserGet;
    if (this.isInitialized) {
        this.browser.removeMockModule('httpBackendMock');
    }
};



HttpBackend.prototype.when = function(method, url) {
    return this._when(method, url);
};

HttpBackend.prototype.expect = function(method, url) {
    return this._expect(method, url);
};

var generateWhenHelpers = function(methods) {
    methods.forEach(function(method) {
        HttpBackend.prototype['when' + method] = function(url) {
            return this._when(method, url);
        };    
    })
};

var generateExpectHelpers = function(methods) {
    methods.forEach(function(method) {
        HttpBackend.prototype['expect' + method] = function(url) {
            return this._expect(method, url);
        };
    })
};

generateWhenHelpers([
    'GET',
    'POST',
    'PUT',
    'DELETE',
    'HEAD',
    'PATCH',
    'JSONP'
]);

generateExpectHelpers([
    'GET',
    'POST',
    'PUT',
    'DELETE',
    'HEAD',
    'PATCH',
    'JSONP'
]);//TODO Segudo?

HttpBackend.prototype.init = function() {

    this.isInitialized = true;
    var jsBuilder = new JavascriptBuilder();
    jsBuilder.responses = this.responses;
    jsBuilder.expectations = this.expectations;
    this.unSyncResponses = [];

    return this.browser.addMockModule('httpBackendMock', jsBuilder.toString(true));
};

HttpBackend.prototype.sync = function() {
    var jsBuilder = new JavascriptBuilder();
    jsBuilder.responses = this.unSyncResponses;
    jsBuilder.expectations = this.expectations;
    this.browser.driver.executeScript(jsBuilder.toString());
    this.unSyncResponses = [];
    this.expectations = [];
};

HttpBackend.prototype._init = function() {
    var _this = this;
    this.browser.get = function(destination, opt_timeout) {
        if (!_this.isInitialized) {
            _this.init();
        }

        return _this.browserGet.call(browser, destination, opt_timeout);
    };
};

HttpBackend.prototype._when = function(method, url) {
    var request = new HttpRequest(method, url);
    var response = new HttpResponse(request);

    var responseIndex = _.findIndex(this.responses, function(response) {
        return response.request.uid == request.uid;
    });

    if (responseIndex) {
        this.responses.splice(responseIndex, 1, response);
    }

    if (this.options.autoSync && this.isInitialized) {
        var _this = this;
        response.onResponse = function() {
            _this.sync();
        };
    }

    this.responses.push(response);
    this.unSyncResponses.push(response);

    return response;
};

HttpBackend.prototype.verifyNoOutstandingExpectation = function(){
    var jsBuilder = new JavascriptBuilder();
    this.browser.driver.executeScript(jsBuilder.verifyNoOutstandingExpectation());
    //func();
};

HttpBackend.prototype._expect = function(method, url) {
    var expectation = new HttpRequest(method, url);
    this.expectations.push(expectation);
    return expectation;
};

module.exports = HttpBackend;

{'fraction_non_alphanumeric': 0.13976555455365194, 'fraction_numerical': 0.0002576323586242432, 'mean_word_length': 3.1101111699311805, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4779597', 'n_tokens_mistral': 2309, 'n_tokens_neox': 2318, 'n_words': 401}