<commit_before>import json
import unittest
from threading import Thread

from six.moves import BaseHTTPServer

import bugsnag


class IntegrationTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.server = FakeBugsnagServer()

    def setUp(self):
        self.server.received = []

    def tearDown(self):
        bugsnag.configuration = bugsnag.Configuration()
        bugsnag.configuration.api_key = 'some key'

    @classmethod
    def tearDownClass(cls):
        cls.server.shutdown()


class FakeBugsnagServer(object):
    """
    A server which accepts a single request, recording the JSON payload and
    other request information
    """
    host = 'localhost'

    def __init__(self, port=5555):
        self.received = []
        self.port = port

        class Handler(BaseHTTPServer.BaseHTTPRequestHandler):

            def do_POST(handler):
                handler.send_response(200)
                length = int(handler.headers['Content-Length'])
                raw_body = handler.rfile.read(length).decode('utf-8')
                self.received.append({'headers': handler.headers,
                                      'json_body': json.loads(raw_body),
                                      'path': handler.path,
                                      'method': handler.command})

            def log_request(self, *args):
                pass

        self.server = BaseHTTPServer.HTTPServer((self.host, self.port),
                                                Handler)
        self.server.timeout = 0.5
        self.thread = Thread(target=self.server.serve_forever, args=(0.1,))
        self.thread.daemon = True
        self.thread.start()

    @property
    def address(self):
        return '%s:%d' % (self.host, self.port)

    @property
    def url(self):
        return 'http://%s' % self.address

    def shutdown(self):
        self.server.shutdown()
        self.thread.join()
        self.server.server_close()


class ScaryException(Exception):
    pass<commit_msg>Randomise integration test server port<commit_after>import json
import unittest
from threading import Thread

from six.moves import BaseHTTPServer

import bugsnag


class IntegrationTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.server = FakeBugsnagServer()

    def setUp(self):
        self.server.received = []

    def tearDown(self):
        bugsnag.configuration = bugsnag.Configuration()
        bugsnag.configuration.api_key = 'some key'

    @classmethod
    def tearDownClass(cls):
        cls.server.shutdown()


class FakeBugsnagServer(object):
    """
    A server which accepts a single request, recording the JSON payload and
    other request information
    """

    def __init__(self):
        self.received = []

        class Handler(BaseHTTPServer.BaseHTTPRequestHandler):

            def do_POST(handler):
                handler.send_response(200)
                length = int(handler.headers['Content-Length'])
                raw_body = handler.rfile.read(length).decode('utf-8')
                self.received.append({'headers': handler.headers,
                                      'json_body': json.loads(raw_body),
                                      'path': handler.path,
                                      'method': handler.command})

            def log_request(self, *args):
                pass

        self.server = BaseHTTPServer.HTTPServer(('localhost', 0), Handler)
        self.server.timeout = 0.5
        self.thread = Thread(target=self.server.serve_forever, args=(0.1,))
        self.thread.daemon = True
        self.thread.start()

    @property
    def address(self):
        return '{0}:{1}'.format(*self.server.server_address)

    @property
    def url(self):
        return 'http://%s' % self.address

    def shutdown(self):
        self.server.shutdown()
        self.thread.join()
        self.server.server_close()


class ScaryException(Exception):
    pass
{'fraction_non_alphanumeric': 0.1048002010555416, 'fraction_numerical': 0.005780346820809248, 'mean_word_length': 2.045141545524101, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5954325', 'n_tokens_mistral': 1132, 'n_tokens_neox': 1067, 'n_words': 224}