Exemplo 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)
Exemplo 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)
Exemplo 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
Exemplo 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
Exemplo 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
Exemplo 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)
Exemplo 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)
Exemplo 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)
Exemplo n.º 9
0
def test_get_default_encoder():
    codec = negotiate_encoder([CoreJSONCodec()])
    assert isinstance(codec, CoreJSONCodec)
Exemplo n.º 10
0
def json_codec():
    return CoreJSONCodec()
Exemplo n.º 11
0
def test_get_supported_decoder():
    codec = negotiate_decoder([CoreJSONCodec()],
                              'application/vnd.coreapi+json')
    assert isinstance(codec, CoreJSONCodec)
Exemplo n.º 12
0
 def render(self, data, media_type=None, renderer_context=None):
     codec = CoreJSONCodec()
     return codec.dump(data, indent=True)
Exemplo 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)
Exemplo n.º 14
0
def test_get_unsupported_encoder():
    with pytest.raises(NoCodecAvailable):
        negotiate_encoder([CoreJSONCodec()], 'application/csv')
Exemplo n.º 15
0
def test_get_accepted_encoder():
    codec = negotiate_encoder([CoreJSONCodec()],
                              accept='application/vnd.coreapi+json')
    assert isinstance(codec, CoreJSONCodec)
Exemplo n.º 16
0
def test_get_unsupported_decoder():
    with pytest.raises(UnsupportedContentType):
        negotiate_decoder([CoreJSONCodec()], 'application/csv')
Exemplo 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)
Exemplo n.º 18
0
 def render(self, data, media_type=None, renderer_context=None):
     codec = CoreJSONCodec()
     return codec.dump(data, indent=True)
Exemplo n.º 19
0
def test_get_underspecified_encoder():
    codec = negotiate_encoder([CoreJSONCodec()], accept='application/*')
    assert isinstance(codec, CoreJSONCodec)
Exemplo 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.

Exemplo n.º 21
0
def test_get_unsupported_encoder_with_fallback():
    codec = negotiate_encoder([CoreJSONCodec()], accept='application/csv, */*')
    assert isinstance(codec, CoreJSONCodec)
Exemplo 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)