def test_handle_http_request_no_string_content():
    request = mock_http_request()
    response = mock_http_response()
    action = MagicMock()
    action.return_value = '123'

    resp = handle_action(action, request, response)

    action.assert_called_once_with(request, response)
    assert '123' == resp.content
示例#2
0
def test_handle_file(bytes_from_file_mock):
    bytes_from_file_mock.return_value = param = ['a', 'b', 'c', 'd']
    response = mock_http_response()

    content = handle_file('path/file.json', 1, response)

    bytes_from_file_mock.assert_called_once_with('path/file.json')
    assert None == content
    assert response.content == 'abcd'
    assert response.content_type == 'application/json'
示例#3
0
def test_handle_http_request_no_string_content():
    request = mock_http_request()
    response = mock_http_response()
    action = MagicMock()
    action.return_value = '123'

    resp = handle_action(action, request, response)

    action.assert_called_once_with(request, response)
    assert '123' == resp.content
示例#4
0
def test_handle_dir(listdir):
    listdir.return_value = ['test.css', 'dir1', 'dir2/']
    with patch('http.helpers.content_generator.generate_paths_index') as generate_paths_index:
        response = mock_http_response()
        generate_paths_index.return_value = 'sample'

        handle_dir('base', None, response)

        listdir.assert_called_once_with('base')
        generate_paths_index.assert_called_once_with(['/base/test.css', '/base/dir1', '/base/dir2/'])
        assert response.content == 'sample'
示例#5
0
def test_handle_http_request_no_content():
    request = mock_http_request()
    response = mock_http_response()

    action = MagicMock()
    action.return_value = None

    resp = handle_action(action, request, response)

    action.assert_called_once_with(request, response)
    assert resp == response
    assert response.status == 204
    assert response.content_type == 'text/html'
    assert response.content == ''
def test_handle_http_request_no_content():
    request = mock_http_request()
    response = mock_http_response()

    action = MagicMock()
    action.return_value = None

    resp = handle_action(action, request, response)

    action.assert_called_once_with(request, response)
    assert resp == response
    assert response.status == 204
    assert response.content_type == 'text/html'
    assert response.content == ''
示例#7
0
def test_handle_http_request_with_string_content_not_set_status():
    request = mock_http_request()
    response = mock_http_response()
    response.status = None
    action = MagicMock()
    action.return_value = 'response'
    assert response.status != 200

    resp = handle_action(action, request, response)

    action.assert_called_once_with(request, response)
    assert resp == response
    assert response.status == 200
    assert resp.content_type == 'text/html'
    assert resp.content == 'response'
def test_handle_http_request_with_string_content_not_set_status():
    request = mock_http_request()
    response = mock_http_response()
    response.status = None
    action = MagicMock()
    action.return_value = 'response'
    assert response.status != 200

    resp = handle_action(action, request, response)

    action.assert_called_once_with(request, response)
    assert resp == response
    assert response.status == 200
    assert resp.content_type == 'text/html'
    assert resp.content == 'response'
def test_handle_http_exception_with_handler():
    e = Mock(HttpExceptionBase)
    e.status = 123

    request = mock_http_request()

    with patch.multiple('http.handle_requests',
                has_error_handler=DEFAULT,
                handle_action=DEFAULT,
                get_error_handler=DEFAULT) as values:
        values['has_error_handler'].return_value = True
        values['get_error_handler'].return_value = handler = MagicMock()

        with patch('http.handle_requests.HTTPResponse') as HTTPResponse_Mock:
            HTTPResponse_Mock.return_value = response = mock_http_response()
            resp = handle_http_exception(request, e)

            values['has_error_handler'].assert_called_once_with(123)
            values['get_error_handler'].assert_called_once_with(123)
            values['handle_action'].assert_called_once_with(handler, request, response)
示例#10
0
def test_handle_http_exception_with_handler():
    e = Mock(HttpExceptionBase)
    e.status = 123

    request = mock_http_request()

    with patch.multiple('http.handle_requests',
                        has_error_handler=DEFAULT,
                        handle_action=DEFAULT,
                        get_error_handler=DEFAULT) as values:
        values['has_error_handler'].return_value = True
        values['get_error_handler'].return_value = handler = MagicMock()

        with patch('http.handle_requests.HTTPResponse') as HTTPResponse_Mock:
            HTTPResponse_Mock.return_value = response = mock_http_response()
            resp = handle_http_exception(request, e)

            values['has_error_handler'].assert_called_once_with(123)
            values['get_error_handler'].assert_called_once_with(123)
            values['handle_action'].assert_called_once_with(
                handler, request, response)