def test_handle_user_request():
    with patch.multiple('http.handle_requests',
                process_http_message=DEFAULT,
                handle_http_request=DEFAULT,
                HTTPRequest=DEFAULT,
                ServerRequestLogger=DEFAULT,
                handle_response=DEFAULT) as values:
        values['process_http_message'].return_value = request = mock_http_request()
        values['HTTPRequest'].return_value = request = mock_http_request()
        values['handle_http_request'].return_value = response = Mock(HTTPResponse)

        handle_user_request('abc')

        values['process_http_message'].assert_called_once_with(request, 'abc')
        values['handle_http_request'].assert_called_once_with(request)
        values['handle_response'].assert_called_once_with(response)
        values['ServerRequestLogger'].log.assert_called_once_with(request, response)
def test_handle_user_request_with_exception():
    with patch.multiple('http.handle_requests',
                process_http_message=DEFAULT,
                handle_http_exception=DEFAULT,
                ServerRequestLogger=DEFAULT,
                HTTPRequest=DEFAULT,
                handle_response=DEFAULT) as values:
        values['process_http_message'].side_effect = e = BadRequestHttpException()
        values['handle_http_exception'].return_value = response = Mock(HTTPResponse)
        values['ServerRequestLogger'].log.return_value = ''
        values['HTTPRequest'].return_value = request = mock_http_request()

        handle_user_request('abc')

        values['ServerRequestLogger'].log.assert_called_once()
        values['process_http_message'].assert_called_once_with(request, 'abc')
        values['handle_http_exception'].assert_called_once_with(request, e)
        values['handle_response'].assert_called_once_with(response)
Пример #3
0
def test_handle_user_request():
    with patch.multiple('http.handle_requests',
                        process_http_message=DEFAULT,
                        handle_http_request=DEFAULT,
                        HTTPRequest=DEFAULT,
                        ServerRequestLogger=DEFAULT,
                        handle_response=DEFAULT) as values:
        values[
            'process_http_message'].return_value = request = mock_http_request(
            )
        values['HTTPRequest'].return_value = request = mock_http_request()
        values['handle_http_request'].return_value = response = Mock(
            HTTPResponse)

        handle_user_request('abc')

        values['process_http_message'].assert_called_once_with(request, 'abc')
        values['handle_http_request'].assert_called_once_with(request)
        values['handle_response'].assert_called_once_with(response)
        values['ServerRequestLogger'].log.assert_called_once_with(
            request, response)
Пример #4
0
def test_handle_user_request_with_exception():
    with patch.multiple('http.handle_requests',
                        process_http_message=DEFAULT,
                        handle_http_exception=DEFAULT,
                        ServerRequestLogger=DEFAULT,
                        HTTPRequest=DEFAULT,
                        handle_response=DEFAULT) as values:
        values[
            'process_http_message'].side_effect = e = BadRequestHttpException(
            )
        values['handle_http_exception'].return_value = response = Mock(
            HTTPResponse)
        values['ServerRequestLogger'].log.return_value = ''
        values['HTTPRequest'].return_value = request = mock_http_request()

        handle_user_request('abc')

        values['ServerRequestLogger'].log.assert_called_once()
        values['process_http_message'].assert_called_once_with(request, 'abc')
        values['handle_http_exception'].assert_called_once_with(request, e)
        values['handle_response'].assert_called_once_with(response)
Пример #5
0
def client_thread(conn, client_ip, client_port):
    # Receiving from client, handle request and back the resposne
    message = ""
    while True:
        data = conn.recv(1024)
        if not data:
            break
        message += data
        if "\r\n\r\n" in message or "\n\n" in message:
            break

    reply = handle_user_request(message, client_ip=client_ip, client_port=client_port)
    if data:
        conn.sendall(reply)

    conn.close()
Пример #6
0
def client_thread(conn, client_ip, client_port):
    # Receiving from client, handle request and back the resposne
    message = ''
    while True:
        data = conn.recv(1024)
        if not data:
            break
        message += data
        if '\r\n\r\n' in message or '\n\n' in message:
            break

    reply = handle_user_request(message,
                                client_ip=client_ip,
                                client_port=client_port)
    if data:
        conn.sendall(reply)

    conn.close()