<commit_before>import io
import os
import shutil

import web
import web.file
import web.fancyindex

from http.client import HTTPConnection, HTTPSConnection

from nose.tools import nottest, with_setup

test_message = b'This is a test sentence!'

class RootHandler(web.HTTPHandler):
	def do_get(self):
		return 200, test_message

class IOHandler(web.HTTPHandler):
	def do_get(self):
		self.response.headers.set('content-length', str(len(test_message)))
		return 200, io.BytesIO(test_message)

class ChunkedHandler(web.HTTPHandler):
	def do_get(self):
		#Create a multichunked 'aaaaaaa...' message
		return 200, io.BytesIO(test_message + b'a' * (web.stream_chunk_size) + test_message)

class ExceptionHandler(web.HTTPHandler):
	def do_get(self):
		raise Exception()

string = b''

class EchoHandler(web.HTTPHandler):
	def do_get(self):
		global string

		return 200, string

	def do_put(self):
		global string

		string = self.request.body

		return 204, ''

saved = {}

class AuthHandler(web.HTTPHandler):
	def do_get(self):
		if not self.request.headers.get('Authorization'):
			auth_headers = web.HTTPHeaders()
			auth_headers.set('WWW-Authenticate', 'Any')
			raise web.HTTPError(401, headers=auth_headers)

		try:
			return 200, saved[self.groups[0]]
		except KeyError:
			raise web.HTTPError(404)

	def do_put(self):
		saved[self.groups[0]] = self.request.body

		return 200, 'Accepted'

error_message = b'Oh noes, there was an error!'

class ErrorHandler(web.HTTPErrorHandler):
	def respond(self):
		return 203, error_message

routes = { '/': RootHandler, '/io': IOHandler, '/chunked': ChunkedHandler, '/error': ExceptionHandler, '/echo': EchoHandler, '/auth/(.*)': AuthHandler }

routes.update(web.file.new('tmp', '/tmpro', dir_index=False, modify=False))
routes.update(web.file.new('tmp', '/tmp', dir_index=True, modify=True))

routes.update(web.fancyindex.new('tmp', '/tmpfancy'))

def setup_integration():
	if os.path.exists('tmp'):
		shutil.rmtree('tmp')

	os.mkdir('tmp')

def teardown_integration():
	shutil.rmtree('tmp')

@with_setup(setup_integration, teardown_integration)
def test_integration_http():
	#create
	httpd = web.HTTPServer(('localhost', 0), routes, { '500': ErrorHandler }, log=web.HTTPLog('tmp/httpd.log', 'tmp/access.log'))

	#start
	httpd.start()

	#test_running
	assert httpd.is_running()

	#test
	try:
		run_conn_tests(HTTPConnection('localhost', httpd.server_address[1]))
	#close
	finally:
		httpd.close()

@with_setup(setup_integration, teardown_integration)
def test_integration_https():
	#create
	httpsd = web.HTTPServer(('localhost', 0), routes, { '500': ErrorHandler }, keyfile='tests/ssl/ssl.key', certfile='tests/ssl/ssl.crt', log=web.HTTPLog('tmp/httpd_ssl.log', 'tmp/access_ssl.log'))

	#start
	httpsd.start()

	#test_running
	assert httpsd.is_running()

	#test
	try:
		run_conn_tests(HTTPSConnection('localhost', httpsd.server_address[1]))
	#close
	finally:
		httpsd.close()

@nottest
def run_conn_tests(conn):
	#test_root<commit_msg>fix ssl certificate validaton in https test<commit_after>import io
import os
import shutil
import ssl

import web
import web.file
import web.fancyindex

from http.client import HTTPConnection, HTTPSConnection

from nose.tools import nottest, with_setup

test_message = b'This is a test sentence!'

class RootHandler(web.HTTPHandler):
	def do_get(self):
		return 200, test_message

class IOHandler(web.HTTPHandler):
	def do_get(self):
		self.response.headers.set('content-length', str(len(test_message)))
		return 200, io.BytesIO(test_message)

class ChunkedHandler(web.HTTPHandler):
	def do_get(self):
		#Create a multichunked 'aaaaaaa...' message
		return 200, io.BytesIO(test_message + b'a' * (web.stream_chunk_size) + test_message)

class ExceptionHandler(web.HTTPHandler):
	def do_get(self):
		raise Exception()

string = b''

class EchoHandler(web.HTTPHandler):
	def do_get(self):
		global string

		return 200, string

	def do_put(self):
		global string

		string = self.request.body

		return 204, ''

saved = {}

class AuthHandler(web.HTTPHandler):
	def do_get(self):
		if not self.request.headers.get('Authorization'):
			auth_headers = web.HTTPHeaders()
			auth_headers.set('WWW-Authenticate', 'Any')
			raise web.HTTPError(401, headers=auth_headers)

		try:
			return 200, saved[self.groups[0]]
		except KeyError:
			raise web.HTTPError(404)

	def do_put(self):
		saved[self.groups[0]] = self.request.body

		return 200, 'Accepted'

error_message = b'Oh noes, there was an error!'

class ErrorHandler(web.HTTPErrorHandler):
	def respond(self):
		return 203, error_message

routes = { '/': RootHandler, '/io': IOHandler, '/chunked': ChunkedHandler, '/error': ExceptionHandler, '/echo': EchoHandler, '/auth/(.*)': AuthHandler }

routes.update(web.file.new('tmp', '/tmpro', dir_index=False, modify=False))
routes.update(web.file.new('tmp', '/tmp', dir_index=True, modify=True))

routes.update(web.fancyindex.new('tmp', '/tmpfancy'))

def setup_integration():
	if os.path.exists('tmp'):
		shutil.rmtree('tmp')

	os.mkdir('tmp')

def teardown_integration():
	shutil.rmtree('tmp')

@with_setup(setup_integration, teardown_integration)
def test_integration_http():
	#create
	httpd = web.HTTPServer(('localhost', 0), routes, { '500': ErrorHandler }, log=web.HTTPLog('tmp/httpd.log', 'tmp/access.log'))

	#start
	httpd.start()

	#test_running
	assert httpd.is_running()

	#test
	try:
		run_conn_tests(HTTPConnection('localhost', httpd.server_address[1]))
	#close
	finally:
		httpd.close()

@with_setup(setup_integration, teardown_integration)
def test_integration_https():
	#create
	httpsd = web.HTTPServer(('localhost', 0), routes, { '500': ErrorHandler }, keyfile='tests/ssl/ssl.key', certfile='tests/ssl/ssl.crt', log=web.HTTPLog('tmp/httpd_ssl.log', 'tmp/access_ssl.log'))

	#start
	httpsd.start()

	#test_running
	assert httpsd.is_running()

	#test
	try:
		context = ssl.SSLContext()
		context.load_cert_chain('tests/ssl/ssl.crt')

		run_conn_tests(HTTPSConnection('localhost', httpsd.server_address[1], context=context))
	#close
	finally:
		httpsd.close()

@nottest
def run_conn_tests(conn):
	#test_root
{'fraction_non_alphanumeric': 0.14358468868079513, 'fraction_numerical': 0.013799901429275506, 'mean_word_length': 5.588744588744588, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '4091519', 'n_tokens_mistral': 2291, 'n_tokens_neox': 2137, 'n_words': 441}