def test_request_id_in_response_headers():
    app = Sanic()

    @app.post('/post')
    def handler(r):
        return sanic.response.text('post')

    @app.get('/get')
    def handler(r):
        return sanic.response.text('get')

    @app.head('/head')
    def handler(r):
        return sanic.response.text('head')

    @app.options('/options')
    def handler(r):
        return sanic.response.text('options')

    app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False)
    app.request_middleware.append(convert_to_jussi_request)
    app.response_middleware.append(finalize_jussi_response)

    _, response = app.test_client.get('/get')
    assert 'x-jussi-request-id' in response.headers

    _, response = app.test_client.post('/post')
    assert 'x-jussi-request-id' in response.headers

    _, response = app.test_client.head('/head')
    assert 'x-jussi-request-id' in response.headers

    _, response = app.test_client.options('/options')
    assert 'x-jussi-request-id' in response.headers
Exemple #2
0
def test_upstream_url(urn_test_request_dicts):
    os.environ['JUSSI_ACCOUNT_TRANSFER_STEEMD_URL'] = 'account_transfer_url'
    upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False)
    jsonrpc_request, urn, url, ttl, timeout = urn_test_request_dicts
    test_urn = URN.from_request(jsonrpc_request)
    upstream = Upstream.from_urn(test_urn, upstreams=upstreams)
    del os.environ['JUSSI_ACCOUNT_TRANSFER_STEEMD_URL']
    assert upstream.url == url
Exemple #3
0
def test_request_urn(urn_test_request_dicts):
    jsonrpc_request, urn, url, ttl, timeout = urn_test_request_dicts
    dummy_request = AttrDict()
    dummy_request.headers = dict()
    dummy_request['jussi_request_id'] = '123456789012345'
    dummy_request.app = AttrDict()
    dummy_request.app.config = AttrDict()
    dummy_request.app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG,
                                                    validate=False)
    jussi_request = JussiJSONRPCRequest.from_request(dummy_request, 0,
                                                     jsonrpc_request)
    assert jussi_request.urn == urn
Exemple #4
0
def test_request_upstream(urn_test_request_dicts):
    jsonrpc_request, urn, url, ttl, timeout = urn_test_request_dicts
    dummy_request = AttrDict()
    dummy_request.headers = dict()
    dummy_request['jussi_request_id'] = '123456789012345'
    dummy_request.app = AttrDict()
    dummy_request.app.config = AttrDict()
    dummy_request.app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG,
                                                    validate=False)
    jussi_request = JussiJSONRPCRequest.from_request(dummy_request, 0,
                                                     jsonrpc_request)
    os.environ['JUSSI_ACCOUNT_TRANSFER_STEEMD_URL'] = 'account_transfer_url'
    assert jussi_request.upstream.url == url
Exemple #5
0
def test_urn_parts_not_in_options_response_headers():
    app = sanic.Sanic('testApp', request_class=HTTPRequest)

    @app.options('/options')
    def handler(r):
        _ = r.jsonrpc  # trigger lazy parsing
        return sanic.response.text('options')

    app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False)
    app.response_middleware.append(finalize_jussi_response)

    _, response = app.test_client.options('/options')
    assert 'x-jussi-request-namespace' not in response.headers
    assert 'x-jussi-request-api' not in response.headers
    assert 'x-jussi-request-method' not in response.headers
    assert 'x-jussi-request-params' not in response.headers
def test_urn_parts_not_in_options_response_headers():
    app = Sanic()

    @app.options('/options')
    def handler(r):
        return sanic.response.text('options')

    app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False)
    app.request_middleware.append(convert_to_jussi_request)
    app.response_middleware.append(finalize_jussi_response)

    _, response = app.test_client.options('/options')
    assert 'x-jussi-request-namespace' not in response.headers
    assert 'x-jussi-request-api' not in response.headers
    assert 'x-jussi-request-method' not in response.headers
    assert 'x-jussi-request-params' not in response.headers
Exemple #7
0
def test_urn_parts_in_post_response_headers():
    app = sanic.Sanic('testApp', request_class=HTTPRequest)

    @app.post('/post')
    def handler(r):
        _ = r.jsonrpc  # trigger lazy parsing
        return sanic.response.text('post')

    app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False)
    app.response_middleware.append(finalize_jussi_response)

    _, response = app.test_client.post('/post', json=req)
    assert 'x-jussi-request-id' in response.headers
    assert response.headers['x-jussi-namespace'] == 'steemd', f'{response.headers}'
    assert response.headers['x-jussi-api'] == 'database_api', f'{response.headers}'
    assert response.headers['x-jussi-method'] == 'get_block', f'{response.headers}'
    assert response.headers['x-jussi-params'] == '[1000]', f'{response.headers}'
Exemple #8
0
def test_jussi_request_ids_equal():
    app = sanic.Sanic('testApp', request_class=HTTPRequest)

    @app.post('/post')
    def handler(r):
        return sanic.response.text('post')

    @app.get('/get')
    def handler(r):
        return sanic.response.text('get')

    @app.head('/head')
    def handler(r):
        return sanic.response.text('head')

    @app.options('/options')
    def handler(r):
        return sanic.response.text('options')

    app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False)
    app.response_middleware.append(finalize_jussi_response)

    _, response = app.test_client.get('/get',
                                      headers={
                                          'x-jussi-request-id': '123456789012345'
                                      })
    assert response.headers['x-jussi-request-id'] == '123456789012345'

    _, response = app.test_client.post('/post',
                                       headers={
                                           'x-jussi-request-id': '123456789012345'
                                       })
    assert response.headers['x-jussi-request-id'] == '123456789012345'

    _, response = app.test_client.head('/head',
                                       headers={
                                           'x-jussi-request-id': '123456789012345'
                                       })
    assert response.headers['x-jussi-request-id'] == '123456789012345'

    _, response = app.test_client.options('/options',
                                          headers={
                                              'x-jussi-request-id': '123456789012345'
                                          })
    assert response.headers['x-jussi-request-id'] == '123456789012345'
def test_urn_parts_in_post_response_headers():
    app = Sanic()

    @app.post('/post')
    def handler(r):
        return sanic.response.text('post')

    app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False)
    app.request_middleware.append(convert_to_jussi_request)
    app.response_middleware.append(finalize_jussi_response)

    _, response = app.test_client.post('/post', json=req)
    assert 'x-jussi-request-id' in response.headers

    assert response.headers['x-jussi-namespace'] == 'steemd'
    assert response.headers['x-jussi-api'] == 'database_api'
    assert response.headers['x-jussi-method'] == 'get_block'
    assert response.headers['x-jussi-params'] == '[1000]'
Exemple #10
0
def test_response_time_in_response_headers():
    app = sanic.Sanic('testApp', request_class=HTTPRequest)

    @app.post('/post')
    def handler(r):
        return sanic.response.text('post')

    @app.get('/get')
    def handler(r):
        return sanic.response.text('get')

    @app.head('/head')
    def handler(r):
        return sanic.response.text('head')

    @app.options('/options')
    def handler(r):
        return sanic.response.text('options')

    app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False)
    app.response_middleware.append(finalize_jussi_response)
    _, response = app.test_client.post('/post')

    assert 'x-jussi-response-time' in response.headers
    assert float(response.headers['x-jussi-response-time']) > 0

    _, response = app.test_client.get('/get')
    assert 'x-jussi-response-time' in response.headers
    assert float(response.headers['x-jussi-response-time']) > 0

    _, response = app.test_client.head('/head')
    assert 'x-jussi-response-time' in response.headers
    assert float(response.headers['x-jussi-response-time']) > 0

    _, response = app.test_client.options('/options')
    assert 'x-jussi-response-time' in response.headers
    assert float(response.headers['x-jussi-response-time']) > 0
Exemple #11
0
def upstream_request(urn_test_request_dicts):
    jsonrpc_request, urn, url, ttl, timeout = urn_test_request_dicts
    dummy_request = AttrDict()
    dummy_request.headers = dict()
    dummy_request['jussi_request_id'] = '123456789012345'
    dummy_request.app = AttrDict()
    dummy_request.app.config = AttrDict()
    dummy_request.app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG,
                                                    validate=False)
    jussi_request = JussiJSONRPCRequest.from_request(dummy_request, 0,
                                                     jsonrpc_request)

    cpy = deepcopy(jussi_request)
    cpy['id'] = 123456789012345
    assert jussi_request.to_upstream_request(as_json=False) == cpy
    assert jussi_request.to_upstream_request() == ujson.dumps(
        cpy, ensure_ascii=False)

    cpy = deepcopy(jussi_request)
    cpy['id'] = 123456789012346
    jussi_request = JussiJSONRPCRequest.from_request(dummy_request, 1, cpy)
    assert jussi_request.to_upstream_request(as_json=False) == cpy
    assert jussi_request.to_upstream_request() == ujson.dumps(
        cpy, ensure_ascii=False)
Exemple #12
0
def test_ttl_object():
    from jussi.urn import URN
    urn = URN('test2', 'api', 'method', False)
    upstreams = _Upstreams(SIMPLE_CONFIG, validate=False)
    assert upstreams.ttl(urn) == 2
Exemple #13
0
def test_namespaces_config_ends_with_api():
    with pytest.raises(AssertionError):
        upstreams = _Upstreams(INVALID_NAMESPACE1_CONFIG, validate=False)
Exemple #14
0
def test_namespaces_config_is_jsonrpc():
    with pytest.raises(AssertionError):
        upstreams = _Upstreams(INVALID_NAMESPACE2_CONFIG, validate=False)
Exemple #15
0
def test_namespaces_config():
    upstreams = _Upstreams(SIMPLE_CONFIG, validate=False)
    assert upstreams.namespaces == frozenset(['test', 'test2'])
Exemple #16
0
def test_hash_ineq():
    upstreams1 = _Upstreams(SIMPLE_CONFIG, validate=False)
    upstreams2 = _Upstreams(VALID_HOSTNAME_CONFIG, validate=False)

    assert hash(upstreams1) != hash(upstreams2)
Exemple #17
0
def test_hash():
    upstreams = _Upstreams(SIMPLE_CONFIG, validate=False)
    upstreams_hash = hash(ujson.dumps(SIMPLE_CONFIG))
    assert hash(upstreams) == upstreams_hash
Exemple #18
0
def test_urls_config():
    upstreams = _Upstreams(SIMPLE_CONFIG, validate=False)
    assert upstreams.urls == frozenset(['http://test.com', 'http://test2.com'])
Exemple #19
0
from jussi.upstream import _Upstreams
from .conftest import TEST_UPSTREAM_CONFIG


class AttrDict(dict):
    def __init__(self, *args, **kwargs):
        super(AttrDict, self).__init__(*args, **kwargs)
        self.__dict__ = self


dummy_request = AttrDict()
dummy_request.headers = dict()
dummy_request['jussi_request_id'] = '123456789012345'
dummy_request.app = AttrDict()
dummy_request.app.config = AttrDict()
dummy_request.app.config.upstreams = _Upstreams(TEST_UPSTREAM_CONFIG,
                                                validate=False)


@pytest.mark.parametrize('cache_cls', [SimpleMaxTTLMemoryCache])
async def test_cache_clear(cache_cls):
    cache = cache_cls()
    await cache.clear()
    assert await cache.get('key') is None
    await cache.set('key', 'value', ttl=None)
    assert await cache.get('key') == 'value'
    await cache.clear()
    assert await cache.get('key') is None


@pytest.mark.parametrize('cache_cls', [SimpleMaxTTLMemoryCache])
async def test_cache_set_get(cache_cls):
Exemple #20
0
def test_timeout_pair():
    from jussi.urn import URN
    urn = URN('test', 'api', 'method', False)
    upstreams = _Upstreams(SIMPLE_CONFIG, validate=False)
    assert upstreams.timeout(urn) == 1
Exemple #21
0
def test_url_pair():
    from jussi.urn import URN
    urn = URN('test', 'api', 'method', False)
    upstreams = _Upstreams(SIMPLE_CONFIG, validate=False)
    assert upstreams.url(urn) == 'http://test.com'
Exemple #22
0
def test_translate_to_appbase_config_false():
    from jussi.urn import URN
    urn = URN('test2', 'api', 'method', False)
    upstreams = _Upstreams(SIMPLE_CONFIG, validate=False)
    assert upstreams.translate_to_appbase(urn) is False
Exemple #23
0
def test_upstream_timeout(urn_test_request_dicts):
    upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False)
    jsonrpc_request, urn, url, ttl, timeout = urn_test_request_dicts
    test_urn = URN.from_request(jsonrpc_request)
    upstream = Upstream.from_urn(test_urn, upstreams=upstreams)
    assert upstream.timeout == timeout
Exemple #24
0
def test_validate_urls():
    upstreams = _Upstreams(VALID_HOSTNAME_CONFIG, validate=True)
    assert isinstance(upstreams, _Upstreams)
Exemple #25
0
def test_validate_urls_raises():
    with pytest.raises(InvalidUpstreamHost):
        upstreams = _Upstreams(SIMPLE_CONFIG)
# -*- coding: utf-8 -*-
from jussi.urn import URN
import pytest
from jussi.errors import InvalidNamespaceAPIError
from jussi.upstream import _Upstreams
from .conftest import TEST_UPSTREAM_CONFIG
upstreams = _Upstreams(TEST_UPSTREAM_CONFIG, validate=False)
namespaces = upstreams.namespaces
from jussi.urn import from_request
from jussi.urn import _parse_jrpc_method


def test_urns(urn_test_request_dict):
    jsonrpc_request, urn, url, ttl, timeout = urn_test_request_dict
    result = str(from_request(jsonrpc_request))
    assert result == urn


@pytest.mark.parametrize(
    "jsonrpc_request,expected",
    [
        # steemd, bare_method
        ({
            'id': 1,
            'jsonrpc': '2.0',
            'method': 'get_account_count',
            'params': []
        }, 'steemd.database_api.get_account_count.params=[]'),
        # steemd, method=call
        ({
            'id': 1,