Example #1
0
def test_only_bytes_allowed():
    with pytest.raises(TypeError):
        rj.RawJSON(b'A bytes string')
    with pytest.raises(TypeError):
        rj.RawJSON({})
    with pytest.raises(TypeError):
        rj.RawJSON(None)
Example #2
0
def test_rawjson_new():
    rc0 = sys.gettotalrefcount()
    for i in range(1000):
        raw_json = rj.RawJSON('["foo", "bar"]')
        del raw_json
    rc1 = sys.gettotalrefcount()
    assert (rc1 - rc0) < THRESHOLD
Example #3
0
def get_response(request, **additional_kwargs):
    """
    Given a WSGI request, makes a call to a corresponding view
    function and returns the response.
    """
    # Get the view / handler for this request
    try:
        resolver_match = resolve(request.path_info)
    except Resolver404:
        return {'code': 404, 'content': 'Invalid endpoint specified.'}

    request.resolver_match = resolver_match
    view, args, kwargs = resolver_match
    kwargs.update(additional_kwargs)

    # Let the view do his task.
    try:
        resp = view(request, *args, **kwargs)
    except Exception as exc:
        resp = HttpResponseServerError(content=str(exc))

    # Convert HTTP response into simple dict type.
    response = {'code': resp.status_code}
    response_len = 0
    if getattr(resp, 'data', None):
        data = json.dumps(resp.data, number_mode=json.NM_NATIVE)
        response_len = len(data)
        data = json.RawJSON(data)
        if not settings.RESPONSE_ENCODED:
            data = resp.data
        response['content'] = data
    return response, response_len
Example #4
0
def test_rawjson_constructor():
    raw_json = rj.RawJSON('["foo", "bar"]')
    rc0 = sys.gettotalrefcount()
    for i in range(1000):
        value = '"foobar"'
        raw_json.__init__(value)
        del value
    rc1 = sys.gettotalrefcount()
    assert (rc1 - rc0) < THRESHOLD
Example #5
0
UUIDS = {'uuid_mode': rj.UM_CANONICAL}
FOOS_DUMP = {'default': default}
FOOS_LOAD = {'object_hook': hook}
ARRAY_DUMP = {'datetime_mode': rj.DM_ISO8601,
              'uuid_mode': rj.UM_CANONICAL,
              'default': default}
ARRAY_LOAD = {'datetime_mode': rj.DM_ISO8601,
              'uuid_mode': rj.UM_CANONICAL,
              'object_hook': hook}

@pytest.mark.skipif(not hasattr(sys, 'gettotalrefcount'), reason='Non-debug Python')
@pytest.mark.parametrize('value,dumps_options,loads_options', [
    ( plain_string, NO_OPTION, NO_OPTION ),
    ( bigint, NO_OPTION, NO_OPTION ),
    ( pi, NO_OPTION, NO_OPTION ),
    ( rj.RawJSON(' "foo" '), NO_OPTION, NO_OPTION),
    ( right_now, DATETIMES, DATETIMES ),
    ( date, DATETIMES, DATETIMES ),
    ( time, DATETIMES, DATETIMES ),
    ( nil, UUIDS, UUIDS ),
    ( foo, FOOS_DUMP, FOOS_LOAD ),
    ( array, ARRAY_DUMP, ARRAY_LOAD ),
])
def test_leaks(value, dumps_options, loads_options):
    rc0 = sys.gettotalrefcount()
    for i in range(1000):
        asjson = rj.dumps(value, **dumps_options)
        aspython = rj.loads(asjson, **loads_options)
        del asjson
        del aspython
    rc1 = sys.gettotalrefcount()
Example #6
0
def test_mix_preserialized():
    assert rj.dumps({'a': rj.RawJSON('{1 : 2}')}) == '{"a":{1 : 2}}'
Example #7
0
def test_instantiation_named():
    value = 'a string'
    raw = rj.RawJSON(value=value)
    assert raw.value == value
Example #8
0
def test_instantiation_positional():
    value = 'a string'
    raw = rj.RawJSON(value)
    assert raw.value == value