<commit_before>import Moon from "moon/src/index";

const open = jest.fn();
const send = jest.fn(function() {
	if (this.error) {
		this.onerror();
	} else {
		this.onload();
	}
});
const setRequestHeader = jest.fn();
let error;

window.XMLHttpRequest = function() {
	this.responseType = null;
	this.status = 200;
	this.response = "Moon Test";

	this.setRequestHeader = setRequestHeader;
	this.getAllResponseHeaders = () => `test: moon
content-length: 1084
date: Fri, 25 Jun 04 00:00:00 +0000`;
	this.open = open;
	this.send = send;

	this.error = error;
};

test("successful http request", () => {
	let run = false;
	error = false;

	Moon.use({
		http: Moon.http.driver
	});

	Moon.run(() => ({
		http: [{
			method: "GET",
			responseType: "text",
			url: "https://example.com",
			headers: {
				test: "moon request",
				foo: "bar",
				date: "Fri, 25 Jun 04 00:00:00 +0000"
			},
			body: "Moon Test Request",
			onLoad: ({ http }) => {
				run = true;
				expect(setRequestHeader).toBeCalledWith("test", "moon request");
				expect(setRequestHeader).toBeCalledWith("foo", "bar");
				expect(setRequestHeader).toBeCalledWith("date", "Fri, 25 Jun 04 00:00:00 +0000");
				expect(open).toBeCalledWith("GET", "https://example.com");
				expect(send).toBeCalledWith("Moon Test Request");
				expect(http).toEqual({
					status: 200,
					headers: {"content-length": "1084", "date": "Fri, 25 Jun 04 00:00:00 +0000", "test": "moon"},
					body: "Moon Test"
				});
				return {};
			}
		}]
	}));

	expect(run).toEqual(true);

	setRequestHeader.mockClear();
	open.mockClear();
	send.mockClear();
});

test("failing http request", () => {
	let run = false;
	error = true;

	Moon.use({
		http: Moon.http.driver
	});

	Moon.run(() => ({
		http: [{
			method: "GET",
			responseType: "text",
			url: "https://example.com",
			headers: {
				test: "moon request",
				foo: "bar",
				date: "Fri, 25 Jun 04 00:00:00 +0000"
			},
			body: "Moon Test Request",
			onError: ({ http }) => {
				run = true;
				expect(setRequestHeader).toBeCalledWith("test", "moon request");
				expect(setRequestHeader).toBeCalledWith("foo", "bar");
				expect(setRequestHeader).toBeCalledWith("date", "Fri, 25 Jun 04 00:00:00 +0000");
				expect(open).toBeCalledWith("GET", "https://example.com");
				expect(send).toBeCalledWith("Moon Test Request");
				expect(http).toEqual(null);
				return {};
			}
		}]
	}));

	expect(run).toEqual(true);

	setRequestHeader.mockClear();
	open.mockClear();
	send.mockClear();
});

test("default http request", () => {
	error = false;

	Moon.use({
		http: Moon.http.driver
	});

	Moon.run(() => ({
		http: [{
			url: "https://example.com"
		}]
	}));

	expect(setRequestHeader).not.toBeCalled();
	expect(open).toBeCalledWith("GET", "https://example.com");
	expect(send).toBeCalledWith(null);

	setRequestHeader.mockClear();
	open.mockClear();
	send.mockClear();
});

test("default http request with error", () => {
	error = true;

	Moon.use({
		http: Moon.http.driver
	});

	Moon.run(() => ({
		http: [{
			url: "https://example.com"
		}]
	}));

	expect(setRequestHeader).not.toBeCalled();
	expect(open).toBeCalledWith("GET", "https://example.com");
	expect(send).toBeCalledWith(null);

	setRequestHeader.mockClear();
	open.mockClear();
	send.mockClear();
});<commit_msg>add tests for multiple HTTP requests<commit_after>import Moon from "moon/src/index";

const open = jest.fn();
const send = jest.fn(function() {
	if (this.error) {
		this.onerror();
	} else {
		this.onload();
	}
});
const setRequestHeader = jest.fn();
let error;

window.XMLHttpRequest = function() {
	this.responseType = null;
	this.status = 200;
	this.response = "Moon Test";

	this.setRequestHeader = setRequestHeader;
	this.getAllResponseHeaders = () => `test: moon
content-length: 1084
date: Fri, 25 Jun 04 00:00:00 +0000`;
	this.open = open;
	this.send = send;

	this.error = error;
};

test("successful http request", () => {
	let run = false;
	error = false;

	Moon.use({
		http: Moon.http.driver
	});

	Moon.run(() => ({
		http: [{
			method: "GET",
			responseType: "text",
			url: "https://example.com",
			headers: {
				test: "moon request",
				foo: "bar",
				date: "Fri, 25 Jun 04 00:00:00 +0000"
			},
			body: "Moon Test Request",
			onLoad: ({ http }) => {
				run = true;
				expect(setRequestHeader).toBeCalledWith("test", "moon request");
				expect(setRequestHeader).toBeCalledWith("foo", "bar");
				expect(setRequestHeader).toBeCalledWith("date", "Fri, 25 Jun 04 00:00:00 +0000");
				expect(open).toBeCalledWith("GET", "https://example.com");
				expect(send).toBeCalledWith("Moon Test Request");
				expect(http).toEqual({
					status: 200,
					headers: {"content-length": "1084", "date": "Fri, 25 Jun 04 00:00:00 +0000", "test": "moon"},
					body: "Moon Test"
				});
				return {};
			}
		}]
	}));

	expect(run).toEqual(true);

	setRequestHeader.mockClear();
	open.mockClear();
	send.mockClear();
});

test("failing http request", () => {
	let run = false;
	error = true;

	Moon.use({
		http: Moon.http.driver
	});

	Moon.run(() => ({
		http: [{
			method: "GET",
			responseType: "text",
			url: "https://example.com",
			headers: {
				test: "moon request",
				foo: "bar",
				date: "Fri, 25 Jun 04 00:00:00 +0000"
			},
			body: "Moon Test Request",
			onError: ({ http }) => {
				run = true;
				expect(setRequestHeader).toBeCalledWith("test", "moon request");
				expect(setRequestHeader).toBeCalledWith("foo", "bar");
				expect(setRequestHeader).toBeCalledWith("date", "Fri, 25 Jun 04 00:00:00 +0000");
				expect(open).toBeCalledWith("GET", "https://example.com");
				expect(send).toBeCalledWith("Moon Test Request");
				expect(http).toEqual(null);
				return {};
			}
		}]
	}));

	expect(run).toEqual(true);

	setRequestHeader.mockClear();
	open.mockClear();
	send.mockClear();
});

test("default http request", () => {
	error = false;

	Moon.use({
		http: Moon.http.driver
	});

	Moon.run(() => ({
		http: [{
			url: "https://example.com"
		}]
	}));

	expect(setRequestHeader).not.toBeCalled();
	expect(open).toBeCalledWith("GET", "https://example.com");
	expect(send).toBeCalledWith(null);

	setRequestHeader.mockClear();
	open.mockClear();
	send.mockClear();
});

test("default http request with error", () => {
	error = true;

	Moon.use({
		http: Moon.http.driver
	});

	Moon.run(() => ({
		http: [{
			url: "https://example.com"
		}]
	}));

	expect(setRequestHeader).not.toBeCalled();
	expect(open).toBeCalledWith("GET", "https://example.com");
	expect(send).toBeCalledWith(null);

	setRequestHeader.mockClear();
	open.mockClear();
	send.mockClear();
});

test("multiple http requests", () => {
	let run = [false, false, false];
	error = false;

	Moon.use({
		http: Moon.http.driver
	});

	Moon.run(() => ({
		http: [{
			url: "https://example.com/1",
			onLoad: ({ http }) => {
				run[0] = true;
				expect(setRequestHeader).not.toBeCalled();
				expect(open).toBeCalledWith("GET", "https://example.com/1");
				expect(send).toBeCalledWith(null);
				expect(http).toEqual({
					status: 200,
					headers: {"content-length": "1084", "date": "Fri, 25 Jun 04 00:00:00 +0000", "test": "moon"},
					body: "Moon Test"
				});
				setRequestHeader.mockClear();
				open.mockClear();
				send.mockClear();
				return {};
			}
		}, {
			url: "https://example.com/2",
			onLoad: ({ http }) => {
				run[1] = true;
				expect(setRequestHeader).not.toBeCalled();
				expect(open).toBeCalledWith("GET", "https://example.com/2");
				expect(send).toBeCalledWith(null);
				expect(http).toEqual({
					status: 200,
					headers: {"content-length": "1084", "date": "Fri, 25 Jun 04 00:00:00 +0000", "test": "moon"},
					body: "Moon Test"
				});
				setRequestHeader.mockClear();
				open.mockClear();
				send.mockClear();
				return {};
			}
		}, {
			url: "https://example.com/3",
			onLoad: ({ http }) => {
				run[2] = true;
				expect(setRequestHeader).not.toBeCalled();
				expect(open).toBeCalledWith("GET", "https://example.com/3");
				expect(send).toBeCalledWith(null);
				expect(http).toEqual({
					status: 200,
					headers: {"content-length": "1084", "date": "Fri, 25 Jun 04 00:00:00 +0000", "test": "moon"},
					body: "Moon Test"
				});
				setRequestHeader.mockClear();
				open.mockClear();
				send.mockClear();
				return {};
			}
		}]
	}));

	expect(run).toEqual([true, true, true]);

	setRequestHeader.mockClear();
	open.mockClear();
	send.mockClear();
});
{'fraction_non_alphanumeric': 0.2023582658408766, 'fraction_numerical': 0.031919961886612676, 'mean_word_length': 3.683212493028444, 'pattern_counts': {'":': 15, '<': 3, '<?xml version=': 0, '>': 30, 'https://': 22, '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': '6947171', 'n_tokens_mistral': 3599, 'n_tokens_neox': 2985, 'n_words': 586}