예제 #1
0
def test_pyramid_json_encoder_with_adapter(app):
    """Test we can define a custom adapter using global json_renderer_factory"""
    from pyramid.renderers import json_renderer_factory

    def serialize_anyclass(obj, request):
        return obj.__class__.__name__

    json_renderer_factory.add_adapter(NonSerializable, serialize_anyclass)

    response = app.get("/extra_claims")
    token = str(response.json_body["token"])

    response = app.get("/dump_claims", headers={"X-Token": token})
    assert response.json_body["extra_claim"] == "NonSerializable"
예제 #2
0
def test_pyramid_custom_json_encoder(app_config: Configurator):
    """Test we can still use user-defined custom adapter"""
    from pyramid.renderers import json_renderer_factory

    def serialize_anyclass(obj, request):
        assert False  # This asserts this method will not be called

    json_renderer_factory.add_adapter(NonSerializable, serialize_anyclass)

    def other_serializer(obj, request):
        return "other_serializer"

    my_renderer = JSON()
    my_renderer.add_adapter(NonSerializable, other_serializer)
    app_config.add_renderer("json", my_renderer)
    app = TestApp(app_config.make_wsgi_app())

    response = app.get("/extra_claims")
    token = str(response.json_body["token"])

    response = app.get("/dump_claims", headers={"X-Token": token})
    assert response.json_body["extra_claim"] == "other_serializer"