<commit_before>"use strict";

var _ = require('lodash');
var request = require('request');
var debug = require('debug')('http-status-check:uriCheck');
require('sugar');
var outAdapter;

var init = function(output){
	outAdapter = output;
};

function startsWith(testString, protocols) {
	for(var i= 0,n=protocols.length; i<n; i++) {
		if(testString.toLowerCase().startsWith(protocols[i].toLowerCase())) {
			return true;
		}
	}
	return false;
}

var checkUri = function(site, callback) {
	if (site.disabled) {
		outAdapter.writeResult('disabled', site);
		return callback(null);
	}

	var options = {
		followRedirect: false,
		uri: site.requestUrl
	};
	// Check that uri has a protocol
	if(!startsWith(options.uri,['http://','https://']))
	{
		options.uri = 'http://' + options.uri;
	}

	if(site.requestHeaders) {
		options.headers = site.requestHeaders;
	}

	request.get(options, function(error, response, body) {
		if(error) {
			site.errors = error;
			outAdapter.writeResult('fail', site);
			return callback(null);
		} else {
			if(response.statusCode !== site.expectedStatus) {
				var err = 'Expected HTTP status of ' + site.expectedStatus + ' and got ' + response.statusCode + '.';
        if(response.statusCode >= 300 && response.statusCode < 400) {
          // Some type of redirect. Let's add the location to the error result to help the user
          // determine what the problem is.
          if(response.headers && response.headers.location) {
            err += ' Location: ' + response.headers.location;
          }
        }
				site.errors = [err];
				outAdapter.writeResult('fail', site);
				return callback(null);
			} else {

        var accumulatedFails = [];

        // If responseHeaders is not empty then evaluate that they all have the
        // expected values.
				if(site.responseHeaders) {
					for (var header in site.responseHeaders) {
						if (site.responseHeaders.hasOwnProperty(header)) {
							var actualHeaderValue = response.headers[header];
							var expectedHeaderValue = site.responseHeaders[header];
							if(actualHeaderValue !== expectedHeaderValue) {
								accumulatedFails.push(
												'Expected header \'' + header + '\' to be ' +
												expectedHeaderValue + ' but instead it was ' +
												actualHeaderValue
								);
							}
						}
					}
				}

        // If excludedHeaders is not empty then ensure that none of them have been
        // returned by the server.
        if(site.excludedHeaders) {
          _.each(site.excludedHeaders, function(header) {
            header = header.toLowerCase();
            if (response.headers[header]) {
              var headerValue = response.headers[header];
              accumulatedFails.push(
                'Expected header \'' + header +
                '\' to NOT be present but it is set to: ' +
                headerValue
              );
            }
          });
        }

        // expectedText is not empty then check that the expected text is in
        // the response body received back from server.
        if(site.expectedText) {
          if(body) {
            var lowerCaseBody; // Don't populate it until we need it.
            _.forEach(site.expectedText, function (textObj) {
              if (textObj.caseSensitive) {
                if (!~body.indexOf(textObj.text)){
                  accumulatedFails.push(
                    'Expected (case sensitive) text in response body not found: ' +
                    textObj.text
                  );
                }
              } else {
                if(!lowerCaseBody) {
                  lowerCaseBody = body.toLowerCase();
                }
                if(!~lowerCaseBody.indexOf(textObj.text.toLowerCase())){
                  accumulatedFails.push(
                    'Expected (case insensitive) text in response body not found: ' +
                    textObj.text.toLowerCase()
                  );
                }
              }
            });
          } else {
            accumulatedFails.push('Expected a response body from the request but it was empty.');
          }
        }

        if(accumulatedFails.length === 0) {
          outAdapter.writeResult('success', site);
          return callback(null);
        } else {
          site.errors = accumulatedFails;
          outAdapter.writeResult('fail', site);<commit_msg>Removed usage of ~ for "contains" so that jshint can continue to be enabled for bitwise warnings and run clean.<commit_after>"use strict";

var _ = require('lodash');
var request = require('request');
var debug = require('debug')('http-status-check:uriCheck');
require('sugar');
var outAdapter;

var init = function(output){
	outAdapter = output;
};

function startsWith(testString, protocols) {
	for(var i= 0,n=protocols.length; i<n; i++) {
		if(testString.toLowerCase().startsWith(protocols[i].toLowerCase())) {
			return true;
		}
	}
	return false;
}

var checkUri = function(site, callback) {
	if (site.disabled) {
		outAdapter.writeResult('disabled', site);
		return callback(null);
	}

	var options = {
		followRedirect: false,
		uri: site.requestUrl
	};
	// Check that uri has a protocol
	if(!startsWith(options.uri,['http://','https://']))
	{
		options.uri = 'http://' + options.uri;
	}

	if(site.requestHeaders) {
		options.headers = site.requestHeaders;
	}

	request.get(options, function(error, response, body) {
		if(error) {
      debug('error in request GET:');
      debug(error);
			site.errors = error;
			outAdapter.writeResult('fail', site);
			return callback(null);
		} else {
			if(response.statusCode !== site.expectedStatus) {
				var err = 'Expected HTTP status of ' + site.expectedStatus + ' and got ' + response.statusCode + '.';
        if(response.statusCode >= 300 && response.statusCode < 400) {
          // Some type of redirect. Let's add the location to the error result to help the user
          // determine what the problem is.
          if(response.headers && response.headers.location) {
            err += ' Location: ' + response.headers.location;
          }
        }
				site.errors = [err];
				outAdapter.writeResult('fail', site);
				return callback(null);
			} else {

        var accumulatedFails = [];

        // If responseHeaders is not empty then evaluate that they all have the
        // expected values.
				if(site.responseHeaders) {
					for (var header in site.responseHeaders) {
						if (site.responseHeaders.hasOwnProperty(header)) {
							var actualHeaderValue = response.headers[header];
							var expectedHeaderValue = site.responseHeaders[header];
							if(actualHeaderValue !== expectedHeaderValue) {
								accumulatedFails.push(
												'Expected header \'' + header + '\' to be ' +
												expectedHeaderValue + ' but instead it was ' +
												actualHeaderValue
								);
							}
						}
					}
				}

        // If excludedHeaders is not empty then ensure that none of them have been
        // returned by the server.
        if(site.excludedHeaders) {
          _.each(site.excludedHeaders, function(header) {
            header = header.toLowerCase();
            if (response.headers[header]) {
              var headerValue = response.headers[header];
              accumulatedFails.push(
                'Expected header \'' + header +
                '\' to NOT be present but it is set to: ' +
                headerValue
              );
            }
          });
        }

        // expectedText is not empty then check that the expected text is in
        // the response body received back from server.
        if(site.expectedText) {
          if(body) {
            var lowerCaseBody; // Don't populate it until we need it.
            _.forEach(site.expectedText, function (textObj) {
              if (textObj.caseSensitive) {
                if (body.indexOf(textObj.text) === -1){
                  accumulatedFails.push(
                    'Expected (case sensitive) text in response body not found: ' +
                    textObj.text
                  );
                }
              } else {
                if(!lowerCaseBody) {
                  lowerCaseBody = body.toLowerCase();
                }
                if(lowerCaseBody.indexOf(textObj.text.toLowerCase()) === -1) {
                  accumulatedFails.push(
                    'Expected (case insensitive) text in response body not found: ' +
                    textObj.text.toLowerCase()
                  );
                }
              }
            });
          } else {
            accumulatedFails.push('Expected a response body from the request but it was empty.');
          }
        }

        if(accumulatedFails.length === 0) {
          outAdapter.writeResult('success', site);
          return callback(null);
        } else {
          site.errors = accumulatedFails;
          outAdapter.writeResult('fail', site);
{'fraction_non_alphanumeric': 0.11473933115640131, 'fraction_numerical': 0.0020267987839207298, 'mean_word_length': 1.9855462184873949, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '1303541', 'n_tokens_mistral': 2660, 'n_tokens_neox': 2353, 'n_words': 610}