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)
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
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
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
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)
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)
def test_get_supported_decoder_with_parameters(): codec = negotiate_decoder([CoreJSONCodec()], 'application/vnd.coreapi+json; verison=1.0') assert isinstance(codec, CoreJSONCodec)
def test_get_default_encoder(): codec = negotiate_encoder([CoreJSONCodec()]) assert isinstance(codec, CoreJSONCodec)
def json_codec(): return CoreJSONCodec()
def test_get_supported_decoder(): codec = negotiate_decoder([CoreJSONCodec()], 'application/vnd.coreapi+json') assert isinstance(codec, CoreJSONCodec)
def render(self, data, media_type=None, renderer_context=None): codec = CoreJSONCodec() return codec.dump(data, indent=True)
def mockreturn(self, request): codec = CoreJSONCodec() body = force_text(request.body) content = codec.encode(Document(content={'data': json.loads(body)})) return MockResponse(content)
def test_get_unsupported_encoder(): with pytest.raises(NoCodecAvailable): negotiate_encoder([CoreJSONCodec()], 'application/csv')
def test_get_accepted_encoder(): codec = negotiate_encoder([CoreJSONCodec()], accept='application/vnd.coreapi+json') assert isinstance(codec, CoreJSONCodec)
def test_get_unsupported_decoder(): with pytest.raises(UnsupportedContentType): negotiate_decoder([CoreJSONCodec()], 'application/csv')
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)
def test_get_underspecified_encoder(): codec = negotiate_encoder([CoreJSONCodec()], accept='application/*') assert isinstance(codec, CoreJSONCodec)
# 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.
def test_get_unsupported_encoder_with_fallback(): codec = negotiate_encoder([CoreJSONCodec()], accept='application/csv, */*') assert isinstance(codec, CoreJSONCodec)