def test_http_request_error_code_cases(mox, response, exception_caught,
                                       prefix):
    """Test the cases in which http_request throws an exception as a result of receiving an error status."""
    creds = Credentials({'url': 'https://example.com', 'token': 'ABCDEFGH'})
    conn = Connection(creds)
    mox.StubOutWithMock(conn.session, 'request')
    conn.session.request('GET',
                         'https://example.com/path',
                         headers=IgnoreArg(),
                         verify=True,
                         proxies=conn.proxies,
                         timeout=conn._timeout).AndReturn(response)
    mox.ReplayAll()
    with pytest.raises(exception_caught) as excinfo:
        conn.http_request('get', '/path')
    assert excinfo.value.message.startswith(prefix)
    mox.VerifyAll()
def test_http_request_exception_cases(mox, exception_raised, exception_caught,
                                      prefix):
    """Test the cases in which the underlying session object throws an exception as a result of a request."""
    creds = Credentials({'url': 'https://example.com', 'token': 'ABCDEFGH'})
    conn = Connection(creds)
    mox.StubOutWithMock(conn.session, 'request')
    conn.session.request('GET',
                         'https://example.com/path',
                         headers=IgnoreArg(),
                         verify=True,
                         proxies=conn.proxies,
                         timeout=conn._timeout).AndRaise(exception_raised)
    mox.ReplayAll()
    with pytest.raises(exception_caught) as excinfo:
        conn.http_request('get', '/path')
    if prefix:
        assert excinfo.value.message.startswith(prefix)
    mox.VerifyAll()
def test_http_request_happy_path(mox):
    """Test the happy-path case of http_request."""
    def validate_headers(hdrs):
        assert hdrs['X-Auth-Token'] == 'ABCDEFGH'
        assert hdrs['User-Agent'].startswith("CBC_SDK/")
        assert hdrs['X-Test'] == 'yes'
        return True

    creds = Credentials({'url': 'https://example.com', 'token': 'ABCDEFGH'})
    conn = Connection(creds)
    mox.StubOutWithMock(conn.session, 'request')
    conn.session.request('GET',
                         'https://example.com/path',
                         headers=Func(validate_headers),
                         verify=True,
                         proxies=conn.proxies,
                         timeout=conn._timeout).AndReturn(
                             StubResponse({'ok': True}))
    mox.ReplayAll()
    resp = conn.http_request('get', '/path', headers={'X-Test': 'yes'})
    assert resp.json()['ok']
    mox.VerifyAll()