Esempio n. 1
0
def serve_schema_js(schema: APISchema, templates: Templates) -> http.Response:
    codec = CoreJSONCodec()
    base64_schema = base64.b64encode(codec.encode(schema)).decode('latin1')
    template = templates.get_template('apistar/schema.js')
    content = template.render(base64_schema=base64_schema)
    headers = {'Content-Type': 'application/javascript'}
    return http.Response(content, headers=headers)
Esempio n. 2
0
def serve_schema_js(schema: APISchema, templates: Templates) -> http.Response:
    codec = CoreJSONCodec()
    base64_schema = base64.b64encode(codec.encode(schema)).decode('latin1')
    template = templates.get_template('apistar/schema.js')
    content = template.render(base64_schema=base64_schema)
    headers = {'Content-Type': 'application/javascript'}
    return http.Response(content, headers=headers)
Esempio n. 3
0
def test_serve_schema():
    response = client.get('/schema/')
    codec = CoreJSONCodec()
    document = codec.decode(response.content)
    assert document.url == 'http://testserver/schema/'
    for name, link in expected.links.items():
        assert name in document
        assert link.action == document[name].action
        assert link.fields == document[name].fields
Esempio n. 4
0
def test_serve_schema():
    response = client.get('/schema/')
    codec = CoreJSONCodec()
    document = codec.decode(response.content)
    assert document.url == '/schema/'
    for name, link in expected.links.items():
        assert name in document
        assert link.action == document[name].action
        assert link.fields == document[name].fields
Esempio n. 5
0
def corejson_renderer(verbose=False):
    codec = CoreJSONCodec()

    @annotate(media_type='application/vnd.coreapi+json', charset=None, format='coreapi')
    def renderer(data, **context):
        return codec.dump(data, indent=verbose)

    return renderer
Esempio n. 6
0
class CoreClient(object):
    decoders = [CoreJSONCodec(), JSONCodec()]

    def __init__(self, base_url) -> None:
        super().__init__()
        self.base_url = base_url
        self.client = Client(decoders=self.decoders)

    def auth_token(self, app_key):
        self.client = Client(auth=auth.TokenAuthentication(token=app_key,
                                                           scheme="Token"),
                             decoders=self.decoders)
Esempio n. 7
0
def test_encoder_preference():
    codec = negotiate_encoder(
        [CoreJSONCodec()],
        accept='text/html; q=1.0, application/vnd.coreapi+json; q=1.0')
    assert isinstance(codec, CoreJSONCodec)
Esempio n. 8
0
def test_get_supported_decoder_with_parameters():
    codec = negotiate_decoder([CoreJSONCodec()],
                              'application/vnd.coreapi+json; verison=1.0')
    assert isinstance(codec, CoreJSONCodec)
Esempio n. 9
0
def test_get_default_encoder():
    codec = negotiate_encoder([CoreJSONCodec()])
    assert isinstance(codec, CoreJSONCodec)
Esempio n. 10
0
def json_codec():
    return CoreJSONCodec()
Esempio n. 11
0
def test_get_supported_decoder():
    codec = negotiate_decoder([CoreJSONCodec()],
                              'application/vnd.coreapi+json')
    assert isinstance(codec, CoreJSONCodec)
Esempio n. 12
0
 def render(self, data, media_type=None, renderer_context=None):
     codec = CoreJSONCodec()
     return codec.dump(data, indent=True)
Esempio n. 13
0
 def mockreturn(self, request):
     codec = CoreJSONCodec()
     body = force_text(request.body)
     content = codec.encode(Document(content={'data': json.loads(body)}))
     return MockResponse(content)
Esempio n. 14
0
def test_get_unsupported_encoder():
    with pytest.raises(NoCodecAvailable):
        negotiate_encoder([CoreJSONCodec()], 'application/csv')
Esempio n. 15
0
def test_get_accepted_encoder():
    codec = negotiate_encoder([CoreJSONCodec()],
                              accept='application/vnd.coreapi+json')
    assert isinstance(codec, CoreJSONCodec)
Esempio n. 16
0
def test_get_unsupported_decoder():
    with pytest.raises(UnsupportedContentType):
        negotiate_decoder([CoreJSONCodec()], 'application/csv')
Esempio n. 17
0
def serve_schema(schema: APISchema) -> http.Response:
    codec = CoreJSONCodec()
    content = codec.encode(schema)
    headers = {'Content-Type': codec.media_type}
    return http.Response(content, headers=headers)
Esempio n. 18
0
 def render(self, data, media_type=None, renderer_context=None):
     codec = CoreJSONCodec()
     return codec.dump(data, indent=True)
Esempio n. 19
0
def test_get_underspecified_encoder():
    codec = negotiate_encoder([CoreJSONCodec()], accept='application/*')
    assert isinstance(codec, CoreJSONCodec)
Esempio n. 20
0
# coding: utf-8
from coreapi import Document, Link, Field
from coreapi.codecs import CoreJSONCodec
from coreapi.compat import force_text
from coreapi.exceptions import NetworkError
from coreapi.transports import HTTPTransport
from coreapi.utils import determine_transport
import pytest
import requests
import json

decoders = [CoreJSONCodec()]
transports = [HTTPTransport()]


@pytest.fixture
def http():
    return HTTPTransport()


class MockResponse(object):
    def __init__(self, content):
        self.content = content
        self.headers = {}
        self.url = 'http://example.org'
        self.status_code = 200


# Test transport errors.

Esempio n. 21
0
def test_get_unsupported_encoder_with_fallback():
    codec = negotiate_encoder([CoreJSONCodec()], accept='application/csv, */*')
    assert isinstance(codec, CoreJSONCodec)
Esempio n. 22
0
def serve_schema(schema: APISchema) -> http.Response:
    codec = CoreJSONCodec()
    content = codec.encode(schema)
    headers = {'Content-Type': codec.media_type}
    return http.Response(content, headers=headers)