# 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
    def test_client_sends_invalid_handshake_request(self, _build_request):
        def wrong_build_request(set_header):
            return '42'
        _build_request.side_effect = wrong_build_request

        self.start_server()
        with self.assertRaises(InvalidHandshake):
            self.start_client()
        self.stop_server()

    @unittest.mock.patch('websockets.server.build_response')
    def test_server_sends_invalid_handshake_response(self, _build_response):
        def wrong_build_response(set_header, key):
            return build_response(set_header, '42')
        _build_response.side_effect = wrong_build_response

        self.start_server()
        with self.assertRaises(InvalidHandshake):
            self.start_client()
        self.stop_server()

    @unittest.mock.patch('websockets.client.read_response')
    def test_server_does_not_switch_protocols(self, _read_response):
        @asyncio.coroutine
        def wrong_read_response(stream):
            code, headers = yield from read_response(stream)
            return 400, headers
        _read_response.side_effect = wrong_read_response

        self.start_server()

# 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

    def test_client_sends_invalid_handshake_request(self, _build_request):
        def wrong_build_request(set_header):
            return '42'
        _build_request.side_effect = wrong_build_request

        self.start_server()
        with self.assertRaises(InvalidHandshake):
            self.start_client()
        self.stop_server()

    @unittest.mock.patch('websockets.server.build_response')
    def test_server_sends_invalid_handshake_response(self, _build_response):
        def wrong_build_response(set_header, key):
            return build_response(set_header, '42')
        _build_response.side_effect = wrong_build_response

        self.start_server()
        with self.assertRaises(InvalidHandshake):
            self.start_client()
        self.stop_server()

    @unittest.mock.patch('websockets.client.read_response')
    def test_server_does_not_switch_protocols(self, _read_response):
        @asyncio.coroutine
        def wrong_read_response(stream):
            code, headers = yield from read_response(stream)
            return 400, headers
        _read_response.side_effect = wrong_read_response

        self.start_server()

        with self.assertRaises(InvalidHandshake):