<commit_before>import stream from 'stream';
import test from 'ava';
import getStream from 'get-stream';
import PCancelable from 'p-cancelable';
import got from '..';
import {createServer} from './helpers/server';

const Readable = stream.Readable;

async function createAbortServer() {
	const s = await createServer();
	const aborted = new Promise((resolve, reject) => {
		s.on('/abort', (req, res) => {
			req.on('aborted', resolve);
			res.on('finish', reject.bind(null, new Error('Request finished instead of aborting.')));

			getStream(req).then(() => {
				res.end();
			});
		});
	});

	await s.listen(s.port);

	return {
		aborted,
		url: `${s.url}/abort`
	};
}

test('cancel in-progress request', async t => {
	const helper = await createAbortServer();
	const body = new Readable({
		read() {}
	});
	body.push('1');

	const p = got(helper.url, {body});

	// Wait for the stream to be established before canceling
	setTimeout(() => {
		p.cancel();
		body.push(null);
	}, 100);

	await t.throws(p, PCancelable.CancelError);
	await t.notThrows(helper.aborted, 'Request finished instead of aborting.');
});

test('cancel in-progress request with timeout', async t => {
	const helper = await createAbortServer();
	const body = new Readable({
		read() {}
	});
	body.push('1');

	const p = got(helper.url, {body, timeout: 10000});

	// Wait for the stream to be established before canceling
	setTimeout(() => {
		p.cancel();
		body.push(null);
	}, 100);

	await t.throws(p, PCancelable.CancelError);
	await t.notThrows(helper.aborted, 'Request finished instead of aborting.');
});

test('cancel immediately', async t => {
	const s = await createServer();
	const aborted = new Promise((resolve, reject) => {
		// We won't get an abort or even a connection
		// We assume no request within 1000ms equals a (client side) aborted request
		s.on('/abort', (req, res) => {
			res.on('finish', reject.bind(this, new Error('Request finished instead of aborting.')));
			res.end();
		});<commit_msg>Ensure cancel tests wait for a connection before cancelling (#414)<commit_after>import EventEmitter from 'events';
import stream from 'stream';
import test from 'ava';
import getStream from 'get-stream';
import PCancelable from 'p-cancelable';
import got from '..';
import {createServer} from './helpers/server';

const Readable = stream.Readable;

async function createAbortServer() {
	const s = await createServer();
	const ee = new EventEmitter();
	ee.aborted = new Promise((resolve, reject) => {
		s.on('/abort', (req, res) => {
			ee.emit('connection');
			req.on('aborted', resolve);
			res.on('finish', reject.bind(null, new Error('Request finished instead of aborting.')));

			getStream(req).then(() => {
				res.end();
			});
		});
	});

	await s.listen(s.port);
	ee.url = `${s.url}/abort`;

	return ee;
}

test('cancel in-progress request', async t => {
	const helper = await createAbortServer();
	const body = new Readable({
		read() {}
	});
	body.push('1');

	const p = got(helper.url, {body});

	// Wait for the connection to be established before canceling
	helper.on('connection', () => {
		p.cancel();
		body.push(null);
	});

	await t.throws(p, PCancelable.CancelError);
	await t.notThrows(helper.aborted, 'Request finished instead of aborting.');
});

test('cancel in-progress request with timeout', async t => {
	const helper = await createAbortServer();
	const body = new Readable({
		read() {}
	});
	body.push('1');

	const p = got(helper.url, {body, timeout: 10000});

	// Wait for the connection to be established before canceling
	helper.on('connection', () => {
		p.cancel();
		body.push(null);
	});

	await t.throws(p, PCancelable.CancelError);
	await t.notThrows(helper.aborted, 'Request finished instead of aborting.');
});

test('cancel immediately', async t => {
	const s = await createServer();
	const aborted = new Promise((resolve, reject) => {
		// We won't get an abort or even a connection
		// We assume no request within 1000ms equals a (client side) aborted request
		s.on('/abort', (req, res) => {
			res.on('finish', reject.bind(this, new Error('Request finished instead of aborting.')));
			res.end();
		});
{'fraction_non_alphanumeric': 0.16658619024625784, 'fraction_numerical': 0.00748430709802028, 'mean_word_length': 4.159402241594022, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '7609514', 'n_tokens_mistral': 1404, 'n_tokens_neox': 1324, 'n_words': 401}