<commit_before>const { debug } = require('./debug');
const glob = require('glob-to-regexp');
const pathToRegexp = require('path-to-regexp');
const querystring = require('querystring');
const isSubset = require('is-subset');
const {
	headers: headerUtils,
	getPath,
	getQuery,
	normalizeUrl,
} = require('./request-utils');
const isEqual = require('lodash.isequal');

const debuggableUrlFunc = (func) => (url) => {
	debug('Actual url:', url);
	return func(url);
};

const stringMatchers = {
	begin: (targetString) =>
		debuggableUrlFunc((url) => url.indexOf(targetString) === 0),
	end: (targetString) =>
		debuggableUrlFunc(
			(url) => url.substr(-targetString.length) === targetString
		),
	glob: (targetString) => {
		const urlRX = glob(targetString);
		return debuggableUrlFunc((url) => urlRX.test(url));
	},
	express: (targetString) => {
		const urlRX = pathToRegexp(targetString);
		return debuggableUrlFunc((url) => urlRX.test(getPath(url)));
	},
	path: (targetString) =>
		debuggableUrlFunc((url) => getPath(url) === targetString),
};

const getHeaderMatcher = ({ headers: expectedHeaders }) => {
	debug('Generating header matcher');
	if (!expectedHeaders) {
		debug('  No header expectations defined - skipping');
		return;
	}
	const expectation = headerUtils.toLowerCase(expectedHeaders);
	debug('  Expected headers:', expectation);
	return (url, { headers = {} }) => {
		debug('Attempting to match headers');
		const lowerCaseHeaders = headerUtils.toLowerCase(
			headerUtils.normalize(headers)
		);
		debug('  Expected headers:', expectation);
		debug('  Actual headers:', lowerCaseHeaders);
		return Object.keys(expectation).every((headerName) =>
			headerUtils.equal(lowerCaseHeaders[headerName], expectation[headerName])
		);
	};
};

const getMethodMatcher = ({ method: expectedMethod }) => {
	debug('Generating method matcher');
	if (!expectedMethod) {
		debug('  No method expectations defined - skipping');
		return;
	}
	debug('  Expected method:', expectedMethod);
	return (url, { method }) => {
		debug('Attempting to match method');
		const actualMethod = method ? method.toLowerCase() : 'get';
		debug('  Expected method:', expectedMethod);
		debug('  Actual method:', actualMethod);
		return expectedMethod === actualMethod;
	};
};

const getQueryStringMatcher = ({ query: expectedQuery }) => {
	debug('Generating query parameters matcher');
	if (!expectedQuery) {
		debug('  No query parameters expectations defined - skipping');
		return;
	}
	debug('  Expected query parameters:', expectedQuery);
	const keys = Object.keys(expectedQuery);
	return (url) => {
		debug('Attempting to match query parameters');
		const query = querystring.parse(getQuery(url));
		debug('  Expected query parameters:', expectedQuery);
		debug('  Actual query parameters:', query);
		return keys.every((key) => query[key] === expectedQuery[key]);
	};
};

const getParamsMatcher = ({ params: expectedParams, url: matcherUrl }) => {
	debug('Generating path parameters matcher');
	if (!expectedParams) {
		debug('  No path parameters expectations defined - skipping');
		return;<commit_msg>fix: request with query is not validated correctly<commit_after>const { debug } = require('./debug');
const glob = require('glob-to-regexp');
const pathToRegexp = require('path-to-regexp');
const querystring = require('querystring');
const isSubset = require('is-subset');
const {
	headers: headerUtils,
	getPath,
	getQuery,
	normalizeUrl,
} = require('./request-utils');
const isEqual = require('lodash.isequal');

const debuggableUrlFunc = (func) => (url) => {
	debug('Actual url:', url);
	return func(url);
};

const stringMatchers = {
	begin: (targetString) =>
		debuggableUrlFunc((url) => url.indexOf(targetString) === 0),
	end: (targetString) =>
		debuggableUrlFunc(
			(url) => url.substr(-targetString.length) === targetString
		),
	glob: (targetString) => {
		const urlRX = glob(targetString);
		return debuggableUrlFunc((url) => urlRX.test(url));
	},
	express: (targetString) => {
		const urlRX = pathToRegexp(targetString);
		return debuggableUrlFunc((url) => urlRX.test(getPath(url)));
	},
	path: (targetString) =>
		debuggableUrlFunc((url) => getPath(url) === targetString),
};

const getHeaderMatcher = ({ headers: expectedHeaders }) => {
	debug('Generating header matcher');
	if (!expectedHeaders) {
		debug('  No header expectations defined - skipping');
		return;
	}
	const expectation = headerUtils.toLowerCase(expectedHeaders);
	debug('  Expected headers:', expectation);
	return (url, { headers = {} }) => {
		debug('Attempting to match headers');
		const lowerCaseHeaders = headerUtils.toLowerCase(
			headerUtils.normalize(headers)
		);
		debug('  Expected headers:', expectation);
		debug('  Actual headers:', lowerCaseHeaders);
		return Object.keys(expectation).every((headerName) =>
			headerUtils.equal(lowerCaseHeaders[headerName], expectation[headerName])
		);
	};
};

const getMethodMatcher = ({ method: expectedMethod }) => {
	debug('Generating method matcher');
	if (!expectedMethod) {
		debug('  No method expectations defined - skipping');
		return;
	}
	debug('  Expected method:', expectedMethod);
	return (url, { method }) => {
		debug('Attempting to match method');
		const actualMethod = method ? method.toLowerCase() : 'get';
		debug('  Expected method:', expectedMethod);
		debug('  Actual method:', actualMethod);
		return expectedMethod === actualMethod;
	};
};

const getQueryStringMatcher = ({ query: passedQuery }) => {
	debug('Generating query parameters matcher');
	if (!passedQuery) {
		debug('  No query parameters expectations defined - skipping');
		return;
	}
	const expectedQuery = querystring.parse(querystring.stringify(passedQuery))
	debug('  Expected query parameters:', passedQuery);
	const keys = Object.keys(passedQuery);
	return (url) => {
		debug('Attempting to match query parameters');
		const query = querystring.parse(getQuery(url));
		debug('  Expected query parameters:', expectedQuery);
		debug('  Actual query parameters:', query);
		return keys.every((key) => query[key] === expectedQuery[key]);
	};
};

const getParamsMatcher = ({ params: expectedParams, url: matcherUrl }) => {
	debug('Generating path parameters matcher');
	if (!expectedParams) {
		debug('  No path parameters expectations defined - skipping');
		return;
{'fraction_non_alphanumeric': 0.15504616364215218, 'fraction_numerical': 0.0003183699458771092, 'mean_word_length': 5.3528816986855405, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, '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': '3263905', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1852, 'n_words': 463}