<commit_before>const DEFAULT_ENCODING = 'UTF8';
const FILESYSTEM_DELAY = 1100;

var path = require('path');
var assert = require('assert');
var async = require('async');
var fs = require('fs');
var request = require('supertest');
var nock = require('nock');
var zlib = require('zlib');
var solidus = require('../solidus.js');

var original_path = __dirname;
var site1_path = path.join( original_path, 'fixtures', 'site 1' );
var site2_path = path.join( original_path, 'fixtures', 'site2' );

var normalizePath = function( file_path ){
	return file_path.replace( /\//g, path.sep );
};

describe( 'Solidus', function(){

	describe( 'production', function(){

		var solidus_server;

		beforeEach( function( done ){
			process.chdir( site1_path );
			solidus_server = solidus.start({
				log_level: 0,
				port: 9009
			});
			// mock http endpoints for resources
			nock('https://solid.us').get('/basic/1').reply( 200, { test: true } );
			nock('https://solid.us').get('/basic/2').reply( 200, { test: true } );
			nock('https://solid.us').get('/dynamic/segment/3').reply( 200, { test: true } );
			nock('https://solid.us').get('/resource/options/url').reply( 200, { test: true } );
			nock('https://solid.us').get('/resource/options/query?test=true').reply( 200, { test: true } );
			nock('https://solid.us').get('/resource/options/dynamic/query?test=3').reply( 200, { test: true } );
			nock('https://solid.us').get('/resource/options/double/dynamic/query?test2=4&test=3').reply( 200, { test: true } );
			nock('https://solid.us').get('/centralized/auth/query').reply( 200, { test: true } );
			nock('https://solid.us').get('/resource/options/headers').matchHeader( 'key', '12345' ).reply( 200, { test: true } );
			nock('https://a.solid.us').get('/centralized/auth').matchHeader( 'key', '12345' ).reply( 200, { test: true } );
			nock('https://b.solid.us').get('/centralized/auth/query?key=12345').reply( 200, { test: true } );
			// empty dynamic segments
			nock('https://solid.us').get('/dynamic/segment/').reply( 200, { test: false } );
			nock('https://solid.us').get('/resource/options/dynamic/query?test=').reply( 200, { test: false } );
			nock('https://solid.us').get('/resource/options/double/dynamic/query?test2=&test=').reply( 200, { test: false } );

			async.parallel(
				[
					// compressed resources
					function( callback ){
						zlib.gzip( '{"test":true}', function( _, result ){
							nock('https://solid.us').get('/compressed/gzip').reply( 200, result, { 'Content-Encoding': 'gzip' } );
							callback();
						});
					},
					function( callback ){
						zlib.deflate( '{"test":true}', function( _, result ){
							nock('https://solid.us').get('/compressed/deflate').reply( 200, result, { 'Content-Encoding': 'deflate' } );
							callback();
						});
					},
					// hack that will work until .start callback is complete
					function( callback ){
						solidus_server.on( 'ready', callback );
					}
				],
				function(){
					done();
				}
			);
		});

		afterEach( function(){
			solidus_server.stop();
			process.chdir( original_path );
		});

		it( 'Starts a new http server', function( done ){
			request( solidus_server.router )
				.get('/')
				.end( function( err, res ){
					if( err ) throw err;
					done();
				});
		});

		it( 'Creates routes based on the contents of /views', function( done ){
			var s_request = request( solidus_server.router );
			async.parallel([
				function( callback ){
					s_request.get('/').expect( 200, callback );
				},
				function( callback ){
					s_request.get('/layout').expect( 200, callback );
				}
			], function( err, results ){
				if( err ) throw err;
				done();
			});
		});

		it( 'Creates routes with dynamic segments', function( done ){
			var s_request = request( solidus_server.router );
			async.parallel([
				function( callback ){
					s_request.get('/dynamic/1').expect( 200, callback );
				},
				function( callback ){
					s_request.get('/dynamic/2').expect( 200, callback );
				}
			], function( err, results ){
				if( err ) throw err;
				done();
			});
		});

		it( 'Creates routes for page contexts', function( done ){
			var s_request = request( solidus_server.router );
			async.parallel([
				function( callback ){
					s_request.get('/.json')
						.expect( 'Content-Type', /json/ )
						.expect( 200 )
						.end( function( err, res ){
							assert( res.body.page.title === 'test' );
							assert( res.body.parameters );
							assert( res.body.query );
							callback( err );
						});
				},
				function( callback ){
					s_request.get('/layout.json?test=true')
						.expect( 'Content-Type', /json/ )
						.expect( 200 )
						.end( function( err, res ){
							assert( res.body.page );
							assert( res.body.parameters );
							assert( res.body.query.test );
							callback( err );
						});
				},
				function( callback ){
					s_request.get('/dynamic/1.json')
						.expect( 'Content-Type', /json/ )
						.expect( 200 )
						.end( function( err, res ){
							assert( res.body.page );
							assert( res.body.parameters.segment == '1' );
							assert( res.body.query );
							callback( err );
						});
				},
				function( callback ){
					s_request.get('/dynamic/2.json')
						.expect( 'Content-Type', /json/ )
						.expect( 200 )
						.end( function( err, res ){
							assert( res.body.page );
							assert( res.body.parameters.segment == '2' );
							assert( res.body.query );
							callback( err );
						});
				}
			], function( err, results ){
				if( err ) throw err;
				done();
			});
		});

		it( 'Makes URL information available in page context', function( done ){
			var s_request = request( solidus_server.router );
			s_request.get('/.json')
				.expect( 'Content-Type', /json/ )
				.expect( 200 )
				.end( function( err, res ){
					assert( res.body.url );
					assert( res.body.url.path === '/.json' );
					if( err ) throw err;
					done();
				});
		});

		it( 'Fetches resources and adds them to the page context', function( done ){
			var s_request = request( solidus_server.router );
			async.parallel([
				function( callback ){
					s_request.get('/.json?resource_test=3&resource_test2=4')<commit_msg>add basic 404 tests<commit_after>const DEFAULT_ENCODING = 'UTF8';
const FILESYSTEM_DELAY = 1100;

var path = require('path');
var assert = require('assert');
var async = require('async');
var fs = require('fs');
var request = require('supertest');
var nock = require('nock');
var zlib = require('zlib');
var solidus = require('../solidus.js');

var original_path = __dirname;
var site1_path = path.join( original_path, 'fixtures', 'site 1' );
var site2_path = path.join( original_path, 'fixtures', 'site2' );

var normalizePath = function( file_path ){
	return file_path.replace( /\//g, path.sep );
};

describe( 'Solidus', function(){

	describe( 'production', function(){

		var solidus_server;

		beforeEach( function( done ){
			process.chdir( site1_path );
			solidus_server = solidus.start({
				log_level: 0,
				port: 9009
			});
			// mock http endpoints for resources
			nock('https://solid.us').get('/basic/1').reply( 200, { test: true } );
			nock('https://solid.us').get('/basic/2').reply( 200, { test: true } );
			nock('https://solid.us').get('/dynamic/segment/3').reply( 200, { test: true } );
			nock('https://solid.us').get('/resource/options/url').reply( 200, { test: true } );
			nock('https://solid.us').get('/resource/options/query?test=true').reply( 200, { test: true } );
			nock('https://solid.us').get('/resource/options/dynamic/query?test=3').reply( 200, { test: true } );
			nock('https://solid.us').get('/resource/options/double/dynamic/query?test2=4&test=3').reply( 200, { test: true } );
			nock('https://solid.us').get('/centralized/auth/query').reply( 200, { test: true } );
			nock('https://solid.us').get('/resource/options/headers').matchHeader( 'key', '12345' ).reply( 200, { test: true } );
			nock('https://a.solid.us').get('/centralized/auth').matchHeader( 'key', '12345' ).reply( 200, { test: true } );
			nock('https://b.solid.us').get('/centralized/auth/query?key=12345').reply( 200, { test: true } );
			// empty dynamic segments
			nock('https://solid.us').get('/dynamic/segment/').reply( 200, { test: false } );
			nock('https://solid.us').get('/resource/options/dynamic/query?test=').reply( 200, { test: false } );
			nock('https://solid.us').get('/resource/options/double/dynamic/query?test2=&test=').reply( 200, { test: false } );

			async.parallel(
				[
					// compressed resources
					function( callback ){
						zlib.gzip( '{"test":true}', function( _, result ){
							nock('https://solid.us').get('/compressed/gzip').reply( 200, result, { 'Content-Encoding': 'gzip' } );
							callback();
						});
					},
					function( callback ){
						zlib.deflate( '{"test":true}', function( _, result ){
							nock('https://solid.us').get('/compressed/deflate').reply( 200, result, { 'Content-Encoding': 'deflate' } );
							callback();
						});
					},
					// hack that will work until .start callback is complete
					function( callback ){
						solidus_server.on( 'ready', callback );
					}
				],
				function(){
					done();
				}
			);
		});

		afterEach( function(){
			solidus_server.stop();
			process.chdir( original_path );
		});

		it( 'Starts a new http server', function( done ){
			request( solidus_server.router )
				.get('/')
				.end( function( err, res ){
					if( err ) throw err;
					done();
				});
		});

		it( 'Creates routes based on the contents of /views', function( done ){
			var s_request = request( solidus_server.router );
			async.parallel([
				function( callback ){
					s_request.get('/').expect( 200, callback );
				},
				function( callback ){
					s_request.get('/layout').expect( 200, callback );
				}
			], function( err, results ){
				if( err ) throw err;
				done();
			});
		});

		it( 'Creates routes with dynamic segments', function( done ){
			var s_request = request( solidus_server.router );
			async.parallel([
				function( callback ){
					s_request.get('/dynamic/1').expect( 200, callback );
				},
				function( callback ){
					s_request.get('/dynamic/2').expect( 200, callback );
				}
			], function( err, results ){
				if( err ) throw err;
				done();
			});
		});

		it( 'Creates routes for page contexts', function( done ){
			var s_request = request( solidus_server.router );
			async.parallel([
				function( callback ){
					s_request.get('/.json')
						.expect( 'Content-Type', /json/ )
						.expect( 200 )
						.end( function( err, res ){
							assert( res.body.page.title === 'test' );
							assert( res.body.parameters );
							assert( res.body.query );
							callback( err );
						});
				},
				function( callback ){
					s_request.get('/layout.json?test=true')
						.expect( 'Content-Type', /json/ )
						.expect( 200 )
						.end( function( err, res ){
							assert( res.body.page );
							assert( res.body.parameters );
							assert( res.body.query.test );
							callback( err );
						});
				},
				function( callback ){
					s_request.get('/dynamic/1.json')
						.expect( 'Content-Type', /json/ )
						.expect( 200 )
						.end( function( err, res ){
							assert( res.body.page );
							assert( res.body.parameters.segment == '1' );
							assert( res.body.query );
							callback( err );
						});
				},
				function( callback ){
					s_request.get('/dynamic/2.json')
						.expect( 'Content-Type', /json/ )
						.expect( 200 )
						.end( function( err, res ){
							assert( res.body.page );
							assert( res.body.parameters.segment == '2' );
							assert( res.body.query );
							callback( err );
						});
				}
			], function( err, results ){
				if( err ) throw err;
				done();
			});
		});

		it( 'Returns 404s for unmatched routes', function( done ){
			var s_request = request( solidus_server.router );
			s_request.get('/nonexistent-url')
				.expect( 404 )
				.end( function( err, res ){
					assert( res.text === '404 Not Found' );
					if( err ) throw err;
					done();
				});
		});

		it( 'Makes URL information available in page context', function( done ){
			var s_request = request( solidus_server.router );
			s_request.get('/.json')
				.expect( 'Content-Type', /json/ )
				.expect( 200 )
				.end( function( err, res ){
					assert( res.body.url );
					assert( res.body.url.path === '/.json' );
					if( err ) throw err;
					done();
				});
		});

		it( 'Fetches resources and adds them to the page context', function( done ){
			var s_request = request( solidus_server.router );
			async.parallel([
				function( callback ){
					s_request.get('/.json?resource_test=3&resource_test2=4')
{'fraction_non_alphanumeric': 0.19133117139919323, 'fraction_numerical': 0.020248358775607055, 'mean_word_length': 2.8338386901152215, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 32, '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': '3167736', 'n_tokens_mistral': 5347, 'n_tokens_neox': 4021, 'n_words': 899}