예제 #1
0
def test_base_client():
    public_key = 'public'
    secret_key = 'secret'

    bc = BaseClient(public_key, secret_key)

    #test urlencode
    urlresult = bc.urlencode({'spam': 42, 'foo': 'bar'})
    encodedkey = 'api_sig=8a0da3cab1dbf7451f38fb5f5aec129c&api_key=public&foo=bar&spam=42'
    assert urlresult == encodedkey, urlresult
예제 #2
0
def test_base_client():
    public_key = 'public'
    secret_key = 'secret'

    bc = BaseClient(public_key, secret_key)

    #test urlencode
    urlresult = bc.urlencode({'spam': 42, 'foo': 'bar'})
    encodedkey = 'api_sig=8a0da3cab1dbf7451f38fb5f5aec129c&api_key=public&foo=bar&spam=42'
    assert urlresult == encodedkey, urlresult
예제 #3
0
def test_base_client_urlopen():
    public_key = 'public'
    secret_key = 'secret'

    bc = BaseClient(public_key, secret_key)

    #test urlopen
    data = [{'url': 'http://test.url',
             'data': {'foo': 'bar'},
             'method': 'GET',
             'result_data': None,
             'result_url': 'http://test.url?api_sig=ddbf4b10a47ca8300554441dc7c9042b&api_key=public&foo=bar',
             'result_method': 'GET'},
             {'url': 'http://test.url',
             'data': {},
             'method': 'POST',
             'result_data': b'api_sig=ba343f176db8166c4b7e88911e7e46ec&api_key=public',
             'result_url': 'http://test.url',
             'result_method': 'POST'},
             {'url': 'http://test.url',
             'data': {},
             'method': 'PUT',
             'result_data': b'api_sig=52cbaea073a5d47abdffc7fc8ccd839b&api_key=public&http_method=put',
             'result_url': 'http://test.url',
             'result_method': 'POST'},
             {'url': 'http://test.url',
             'data': {},
             'method': 'DELETE',
             'result_data': b'api_sig=8621f072b1492fbd164d808307ba72b9&api_key=public&http_method=delete',
             'result_url': 'http://test.url',
             'result_method': 'POST'},
             ]

    for params in data:
        result = bc.urlopen(url=params['url'],
                            data=params['data'],
                            method=params['method'])
        assert isinstance(result, HttpRequest), type(result)
        assert result.get_data() == params["result_data"], (result.get_data(),
                                                        params["result_data"])
        assert result.get_full_url() == params["result_url"], \
                                                         (result.get_full_url(),
                                                          params["result_url"])
        assert result.get_method() == params["result_method"], \
                                                         (result.get_method(),
                                                          params["result_method"])
예제 #4
0
def test_base_client_read():
    """
    test cases:
      method default (get) - other we already tested
      format json|yaml ( should produce error)
      codes 200|400|401|403|404|500
    """
    public_key = 'public'
    secret_key = 'secret'

    bc = BaseClient(public_key, secret_key)
    test_url = 'http://test.url'

    #produce error on format other then json
    class NotJsonException(Exception):
        pass

    try:
        bc.read(url=test_url, format='yaml')
        raise NotJsonException()
    except NotJsonException, e:
        assert 0, "BaseClient.read() doesn't produce error on yaml format"
예제 #5
0
def test_base_client_read():
    """
    test cases:
      method default (get) - other we already tested
      format json|yaml ( should produce error)
      codes 200|400|401|403|404|500
    """
    public_key = 'public'
    secret_key = 'secret'

    bc = BaseClient(public_key, secret_key)
    test_url = 'http://test.url'

    #produce error on format other then json
    class NotJsonException(Exception):
        pass

    try:
        bc.read(url=test_url, format='yaml')
        raise NotJsonException()
    except NotJsonException, e:
        assert 0, "BaseClient.read() doesn't produce error on yaml format"
예제 #6
0
def test_base_client_read():
    """
    test cases:
      method default (get) - other we already tested
      format json|yaml ( should produce error)
      codes 200|400|401|403|404|500
    """
    public_key = 'public'
    secret_key = 'secret'

    bc = BaseClient(public_key, secret_key)
    test_url = 'http://test.url'

    #produce error on format other then json
    class NotJsonException(Exception):
        pass

    try:
        bc.read(url=test_url, format='yaml')
        raise NotJsonException()
    except NotJsonException as e:
        assert 0, "BaseClient.read() doesn't produce error on yaml format"
    except:
        pass

    #test get, all ok
    result = bc.read(url=test_url)
    assert result == sample_json_dict, result

    #test get, 400 error
    try:
        result = base_client_read_400(bc=bc, url=test_url)
    except HTTP400BadRequestError as e:
        pass
    except Exception as e:
        assert 0, "Incorrect exception raised for 400 code: " + str(e)

    #test get, 401 error
    try:
        result = base_client_read_401(bc=bc, url=test_url)
    except HTTP401UnauthorizedError as e:
        pass
    except Exception as e:
        assert 0, "Incorrect exception raised for 401 code: " + str(e)

    #test get, 403 error
    try:
        result = base_client_read_403(bc=bc, url=test_url)
    except HTTP403ForbiddenError as e:
        pass
    except Exception as e:
        assert 0, "Incorrect exception raised for 403 code: " + str(e)

    #test get, 404 error
    try:
        result = base_client_read_404(bc=bc, url=test_url)
    except HTTP404NotFoundError as e:
        pass
    except Exception as e:
        assert 0, "Incorrect exception raised for 404 code: " + str(e)

    #test get, 500 error
    try:
        result = base_client_read_500(bc=bc, url=test_url)
    except urllib2.HTTPError as e:
        if e.code == 500:
            pass
        else:
            assert 0, "Incorrect exception raised for 500 code: " + str(e)
    except Exception as e:
        assert 0, "Incorrect exception raised for 500 code: " + str(e)
예제 #7
0
def test_base_client_read():
    """
    test cases:
      method default (get) - other we already tested
      format json|yaml ( should produce error)
      codes 200|400|401|403|404|500
    """
    public_key = 'public'
    secret_key = 'secret'

    bc = BaseClient(public_key, secret_key)
    test_url = 'http://test.url'

    #produce error on format other then json
    class NotJsonException(Exception):
        pass

    try:
        bc.read(url=test_url, format='yaml')
        raise NotJsonException()
    except NotJsonException as e:
        assert 0, "BaseClient.read() doesn't produce error on yaml format"
    except:
        pass

    #test get, all ok
    result = bc.read(url=test_url)
    assert result == sample_json_dict, result

    #test get, 400 error
    try:
        result = base_client_read_400(bc=bc, url=test_url)
    except HTTP400BadRequestError as e:
        pass
    except Exception as e:
        assert 0, "Incorrect exception raised for 400 code: " + str(e)

    #test get, 401 error
    try:
        result = base_client_read_401(bc=bc, url=test_url)
    except HTTP401UnauthorizedError as e:
        pass
    except Exception as e:
        assert 0, "Incorrect exception raised for 401 code: " + str(e)

    #test get, 403 error
    try:
        result = base_client_read_403(bc=bc, url=test_url)
    except HTTP403ForbiddenError as e:
        pass
    except Exception as e:
        assert 0, "Incorrect exception raised for 403 code: " + str(e)

    #test get, 404 error
    try:
        result = base_client_read_404(bc=bc, url=test_url)
    except HTTP404NotFoundError as e:
        pass
    except Exception as e:
        assert 0, "Incorrect exception raised for 404 code: " + str(e)

    #test get, 500 error
    try:
        result = base_client_read_500(bc=bc, url=test_url)
    except urllib2.HTTPError as e:
        if e.code == 500:
            pass
        else:
            assert 0, "Incorrect exception raised for 500 code: " + str(e)
    except Exception as e:
        assert 0, "Incorrect exception raised for 500 code: " + str(e)
예제 #8
0
def test_base_client_urlopen():
    public_key = 'public'
    secret_key = 'secret'

    bc = BaseClient(public_key, secret_key)

    #test urlopen
    data = [
        {
            'url': 'http://test.url',
            'data': {
                'foo': 'bar'
            },
            'method': 'GET',
            'result_data': None,
            'result_url':
            'http://test.url?api_sig=ddbf4b10a47ca8300554441dc7c9042b&api_key=public&foo=bar',
            'result_method': 'GET'
        },
        {
            'url': 'http://test.url',
            'data': {},
            'method': 'POST',
            'result_data':
            b'api_sig=ba343f176db8166c4b7e88911e7e46ec&api_key=public',
            'result_url': 'http://test.url',
            'result_method': 'POST'
        },
        {
            'url': 'http://test.url',
            'data': {},
            'method': 'PUT',
            'result_data':
            b'api_sig=52cbaea073a5d47abdffc7fc8ccd839b&api_key=public&http_method=put',
            'result_url': 'http://test.url',
            'result_method': 'POST'
        },
        {
            'url': 'http://test.url',
            'data': {},
            'method': 'DELETE',
            'result_data':
            b'api_sig=8621f072b1492fbd164d808307ba72b9&api_key=public&http_method=delete',
            'result_url': 'http://test.url',
            'result_method': 'POST'
        },
    ]

    for params in data:
        result = bc.urlopen(url=params['url'],
                            data=params['data'],
                            method=params['method'])
        assert isinstance(result, HttpRequest), type(result)
        assert result.get_data() == params["result_data"], (
            result.get_data(), params["result_data"])
        assert result.get_full_url() == params["result_url"], \
                                                         (result.get_full_url(),
                                                          params["result_url"])
        assert result.get_method() == params["result_method"], \
                                                         (result.get_method(),
                                                          params["result_method"])