def test_execute_error_with_reason(requests_mock, encoding):

    requests_mock.request(
        'post',
        'https://localhost/resources',
        status_code=500,
        reason='Interñal Server Error'.encode(encoding),
    )

    c = ConnectClient('API_KEY', specs_location=None)

    with pytest.raises(HttpError):
        c.execute('post', 'https://localhost/resources', 201)
def test_execute_uparseable_connect_error(requests_mock):

    requests_mock.request(
        'post',
        'https://localhost/resources',
        text='error text',
        status_code=400,
    )

    c = ConnectClient('API_KEY', specs_location=None)

    with pytest.raises(HttpError):
        c.execute('post', 'https://localhost/resources', 201)
def test_execute_default_headers(requests_mock):
    mock = requests_mock.request(
        'get',
        'https://localhost/resources',
        json=[],
    )

    c = ConnectClient(
        'API_KEY',
        specs_location=None,
        default_headers={'X-Custom-Header': 'custom-header-value'},
    )

    c.execute('get', 'https://localhost/resources', 200)

    headers = mock.last_request.headers

    assert 'Authorization' in headers and headers['Authorization'] == 'API_KEY'
    assert 'User-Agent' in headers and headers['User-Agent'].startswith('connect-fluent')
    assert 'X-Custom-Header' in headers and headers['X-Custom-Header'] == 'custom-header-value'
def test_execute_connect_error(requests_mock):
    connect_error = {
        'error_code': 'code',
        'errors': ['first', 'second'],
    }

    requests_mock.request(
        'post',
        'https://localhost/resources',
        json=connect_error,
        status_code=400,
    )

    c = ConnectClient('API_KEY', specs_location=None)

    with pytest.raises(APIError) as cv:
        c.execute('post', 'https://localhost/resources', 201)

    assert cv.value.status_code == 400
    assert cv.value.error_code == 'code'
    assert cv.value.errors == ['first', 'second']
def test_execute_delete(requests_mock):

    requests_mock.request(
        'delete',
        'https://localhost/resources',
        text='error text',
        status_code=204,
    )

    c = ConnectClient('API_KEY', specs_location=None)

    results = c.execute('delete', 'https://localhost/resources', 204)

    assert results is None
def test_execute_with_kwargs(requests_mock):
    mock = requests_mock.request(
        'post',
        'https://localhost/resources',
        json=[],
        status_code=201,
    )

    c = ConnectClient('API_KEY', specs_location=None)
    kwargs = {
        'headers': {
            'X-Custom-Header': 'value',
        },
    }

    c.execute('post', 'https://localhost/resources', 201, **kwargs)

    assert mock.last_request.method == 'POST'

    headers = mock.last_request.headers

    assert 'Authorization' in headers and headers['Authorization'] == 'API_KEY'
    assert 'User-Agent' in headers and headers['User-Agent'].startswith('connect-fluent')
    assert 'X-Custom-Header' in headers and headers['X-Custom-Header'] == 'value'
def test_execute(requests_mock):
    expected = [{'id': i} for i in range(10)]
    mock = requests_mock.request(
        'get',
        'https://localhost/resources',
        json=expected,
    )

    c = ConnectClient('API_KEY', specs_location=None)

    results = c.execute('get', 'https://localhost/resources', 200)

    assert mock.last_request.method == 'GET'
    headers = mock.last_request.headers

    assert 'Authorization' in headers and headers['Authorization'] == 'API_KEY'
    assert 'User-Agent' in headers and headers['User-Agent'].startswith('connect-fluent')

    assert results == expected