# Path: server/websockets/exceptions.py
# class ConnectionClosed(InvalidState):
#     """
#     Exception raised when trying to read or write on a closed connection.
# 
#     Provides the connection close code and reason in its ``code`` and
#     ``reason`` attributes respectively.
# 
#     """
#     def __init__(self, code, reason):
#         self.code = code
#         self.reason = reason
#         message = 'WebSocket connection is closed: '
#         message += 'code = {}, '.format(code) if code else 'no code, '
#         message += 'reason = {}.'.format(reason) if reason else 'no reason.'
#         super().__init__(message)
# 
# class InvalidHandshake(Exception):
#     """
#     Exception raised when a handshake request or response is invalid.
# 
#     """
# 
# Path: server/websockets/http.py
# USER_AGENT = ' '.join((
#     'Python/{}'.format(sys.version[:3]),
#     'websockets/{}'.format(websockets_version),
# ))
# 
# @asyncio.coroutine
# def read_response(stream):
#     """
#     Read an HTTP/1.1 response from ``stream``.
# 
#     Return ``(status, headers)`` where ``status`` is a :class:`int` and
#     ``headers`` is a :class:`~email.message.Message`.
# 
#     Raise an exception if the request isn't well formatted.
# 
#     The response is assumed not to contain a body.
# 
#     """
#     status_line, headers = yield from read_message(stream)
#     version, status, reason = status_line[:-2].decode().split(None, 2)
#     if version != 'HTTP/1.1':
#         raise ValueError("Unsupported HTTP version")
#     return int(status), headers


import asyncio
import logging
import os
import ssl
import unittest
import unittest.mock
from .client import *
from .exceptions import ConnectionClosed, InvalidHandshake
from .http import USER_AGENT, read_response
from .server import *
    from .py35.client_server import ClientServerContextManager
    secure = False

    def setUp(self):
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)

    def tearDown(self):
        self.loop.close()

    def run_loop_once(self):
        # Process callbacks scheduled with call_soon by appending a callback
        # to stop the event loop then running it until it hits that callback.
        self.loop.call_soon(self.loop.stop)
        self.loop.run_forever()

    def start_server(self, **kwds):
        server = serve(handler, 'localhost', 8642, **kwds)
        self.server = self.loop.run_until_complete(server)

    def start_client(self, path='', **kwds):
        client = connect('ws://localhost:8642/' + path, **kwds)
        self.client = self.loop.run_until_complete(client)

    def stop_client(self):
        try:
            self.loop.run_until_complete(
                asyncio.wait_for(self.client.worker_task, timeout=1))
        except asyncio.TimeoutError:                # pragma: no cover
            self.fail("Client failed to stop")


# Path: server/websockets/exceptions.py
# class ConnectionClosed(InvalidState):
#     """
#     Exception raised when trying to read or write on a closed connection.
# 
#     Provides the connection close code and reason in its ``code`` and
#     ``reason`` attributes respectively.
# 
#     """
#     def __init__(self, code, reason):
#         self.code = code
#         self.reason = reason
#         message = 'WebSocket connection is closed: '
#         message += 'code = {}, '.format(code) if code else 'no code, '
#         message += 'reason = {}.'.format(reason) if reason else 'no reason.'
#         super().__init__(message)
# 
# class InvalidHandshake(Exception):
#     """
#     Exception raised when a handshake request or response is invalid.
# 
#     """
# 
# Path: server/websockets/http.py
# USER_AGENT = ' '.join((
#     'Python/{}'.format(sys.version[:3]),
#     'websockets/{}'.format(websockets_version),
# ))
# 
# @asyncio.coroutine
# def read_response(stream):
#     """
#     Read an HTTP/1.1 response from ``stream``.
# 
#     Return ``(status, headers)`` where ``status`` is a :class:`int` and
#     ``headers`` is a :class:`~email.message.Message`.
# 
#     Raise an exception if the request isn't well formatted.
# 
#     The response is assumed not to contain a body.
# 
#     """
#     status_line, headers = yield from read_message(stream)
#     version, status, reason = status_line[:-2].decode().split(None, 2)
#     if version != 'HTTP/1.1':
#         raise ValueError("Unsupported HTTP version")
#     return int(status), headers


# Path: server/websockets/test_client_server.py
import asyncio
import logging
import os
import ssl
import unittest
import unittest.mock
from .client import *
from .exceptions import ConnectionClosed, InvalidHandshake
from .http import USER_AGENT, read_response
from .server import *
    from .py35.client_server import ClientServerContextManager

    secure = False

    def setUp(self):
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)

    def tearDown(self):
        self.loop.close()

    def run_loop_once(self):
        # Process callbacks scheduled with call_soon by appending a callback
        # to stop the event loop then running it until it hits that callback.
        self.loop.call_soon(self.loop.stop)
        self.loop.run_forever()

    def start_server(self, **kwds):
        server = serve(handler, 'localhost', 8642, **kwds)
        self.server = self.loop.run_until_complete(server)

    def start_client(self, path='', **kwds):
        client = connect('ws://localhost:8642/' + path, **kwds)
        self.client = self.loop.run_until_complete(client)

    def stop_client(self):
        try:
            self.loop.run_until_complete(
                asyncio.wait_for(self.client.worker_task, timeout=1))
        except asyncio.TimeoutError:                # pragma: no cover
            self.fail("Client failed to stop")


    def stop_server(self):