<commit_before>/* globals describe, beforeEach, afterEach, it */
var assert = require('assert');
var request = require('request');
var Q = require('q');

request.defaults({
	json:true
});

var get = Q.denodeify(request.get);
var post = Q.denodeify(request.post);
var put = Q.denodeify(request.put);
var del = Q.denodeify(request.del);
var patch = Q.denodeify(request.patch);

// The thing we're testing
var Interfake = require('..');
var interfake;

describe('Interfake Fluent JavaScript API', function () {
	beforeEach(function () {
		interfake = new Interfake();
	});
	afterEach(function () {
		if (interfake) {
			interfake.stop();
		}
	});
	
	// Testing the fluent interface
	describe('#get()', function () {
		it('should create one GET endpoint', function (done) {
			interfake.get('/fluent');
			interfake.listen(3000);

			request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response, body) {
				assert.equal(response.statusCode, 200);
				done();
			});
		});

		it('should create one GET endpoint with a querystring', function (done) {
			interfake.get('/fluent?query=1');
			interfake.listen(3000);

			request({ url : 'http://localhost:3000/fluent?query=1', json : true }, function (error, response, body) {
				assert.equal(response.statusCode, 200);
				done();
			});
		});

		describe('#responseHeaders()', function () {
			it('should create one GET endpoint which returns custom headers', function (done) {
				interfake.get('/fluent').responseHeaders({ 'X-Request-Type': 'test', 'X-lol-TEST': 'bleep' });
				interfake.listen(3000);

				request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response) {
					assert.equal(response.statusCode, 200);
					assert.equal(response.headers['x-request-type'], 'test');
					assert.equal(response.headers['x-lol-test'], 'bleep');
					assert.equal(response.headers['x-undef'], undefined);
					done();
				});
			});

			describe('#status()', function () {
				it('should return a 300 status', function (done) {
					interfake.get('/fluent').responseHeaders({ 'X-Request-Type': 'test', 'X-lol-TEST': 'bleep' }).status(300);
					interfake.listen(3000);

					request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response) {
						assert.equal(response.statusCode, 300);
						assert.equal(response.headers['x-request-type'], 'test');
						assert.equal(response.headers['x-lol-test'], 'bleep');
						assert.equal(response.headers['x-undef'], undefined);
						done();
					});
				});
			});
		});

		describe('#proxy()', function () {
			it('should create one GET endpoint which acts as a proxy for another', function (done) {
				var proxiedInterfake = new Interfake();
				proxiedInterfake.get('/whatever').status(404).body({
					message: 'This is something you proxied!'
				});
				proxiedInterfake.listen(3051);
				interfake.get('/proxy').proxy('http://localhost:3051/whatever');
				interfake.listen(3000);

				request('http://localhost:3000/stuff', function (error, response, body) {
					assert.equal(response.statusCode, 404);
					assert.equal(body.message, 'This is something you proxied!');
					proxiedInterfake.stop();
					done();
				});
			});
		});

		describe('#query()', function () {
			it('should use the query object as priority', function (done) {
				interfake.get('/fluent?query=1').query({ query: 2 }).status(200);
				interfake.listen(3000);

				Q.all([get({url:'http://localhost:3000/fluent?query=1',json:true}), get({url:'http://localhost:3000/fluent?query=2',json:true})])
					.then(function (results) {
						assert.equal(results[0][0].statusCode, 404);
						assert.equal(results[1][0].statusCode, 200);
						done();
					});
			});

			it('should use a RegExp to find a partially-matched query string param', function (done) {<commit_msg>#29 proxy() support in the fluent interface<commit_after>/* globals describe, beforeEach, afterEach, it */
var assert = require('assert');
var request = require('request');
var Q = require('q');

request = request.defaults({
	json: true
});

var get = Q.denodeify(request.get);
var post = Q.denodeify(request.post);
var put = Q.denodeify(request.put);
var del = Q.denodeify(request.del);
var patch = Q.denodeify(request.patch);

// The thing we're testing
var Interfake = require('..');
var interfake;

describe('Interfake Fluent JavaScript API', function () {
	beforeEach(function () {
		interfake = new Interfake();
	});
	afterEach(function () {
		if (interfake) {
			interfake.stop();
		}
	});
	
	// Testing the fluent interface
	describe('#get()', function () {
		it('should create one GET endpoint', function (done) {
			interfake.get('/fluent');
			interfake.listen(3000);

			request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response, body) {
				assert.equal(response.statusCode, 200);
				done();
			});
		});

		it('should create one GET endpoint with a querystring', function (done) {
			interfake.get('/fluent?query=1');
			interfake.listen(3000);

			request({ url : 'http://localhost:3000/fluent?query=1', json : true }, function (error, response, body) {
				assert.equal(response.statusCode, 200);
				done();
			});
		});

		describe('#responseHeaders()', function () {
			it('should create one GET endpoint which returns custom headers', function (done) {
				interfake.get('/fluent').responseHeaders({ 'X-Request-Type': 'test', 'X-lol-TEST': 'bleep' });
				interfake.listen(3000);

				request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response) {
					assert.equal(response.statusCode, 200);
					assert.equal(response.headers['x-request-type'], 'test');
					assert.equal(response.headers['x-lol-test'], 'bleep');
					assert.equal(response.headers['x-undef'], undefined);
					done();
				});
			});

			describe('#status()', function () {
				it('should return a 300 status', function (done) {
					interfake.get('/fluent').responseHeaders({ 'X-Request-Type': 'test', 'X-lol-TEST': 'bleep' }).status(300);
					interfake.listen(3000);

					request({ url : 'http://localhost:3000/fluent', json : true }, function (error, response) {
						assert.equal(response.statusCode, 300);
						assert.equal(response.headers['x-request-type'], 'test');
						assert.equal(response.headers['x-lol-test'], 'bleep');
						assert.equal(response.headers['x-undef'], undefined);
						done();
					});
				});
			});
		});

		describe('#proxy()', function () {
			it('should create one GET endpoint which acts as a proxy for another', function (done) {
				var proxiedInterfake = new Interfake();
				proxiedInterfake.get('/whatever').status(404).body({
					message: 'This is something you proxied!'
				});
				proxiedInterfake.listen(3051);
				interfake = new Interfake({debug:true});
				interfake.get('/proxy').proxy('http://localhost:3051/whatever');
				interfake.listen(3000);

				request('http://localhost:3000/proxy', function (error, response, body) {
					assert.equal(response.statusCode, 404);
					assert.equal(body.message, 'This is something you proxied!');
					proxiedInterfake.stop();
					done();
				});
			});
		});

		describe('#query()', function () {
			it('should use the query object as priority', function (done) {
				interfake.get('/fluent?query=1').query({ query: 2 }).status(200);
				interfake.listen(3000);

				Q.all([get({url:'http://localhost:3000/fluent?query=1',json:true}), get({url:'http://localhost:3000/fluent?query=2',json:true})])
					.then(function (results) {
						assert.equal(results[0][0].statusCode, 404);
						assert.equal(results[1][0].statusCode, 200);
						done();
					});
			});

			it('should use a RegExp to find a partially-matched query string param', function (done) {
{'fraction_non_alphanumeric': 0.18465320910973085, 'fraction_numerical': 0.026915113871635612, 'mean_word_length': 3.9544871794871796, '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': '1317066', 'n_tokens_mistral': 2994, 'n_tokens_neox': 2354, 'n_words': 525}