<s> import pytest <EOL> import asyncio <EOL> from collections import OrderedDict <EOL> from aiohttp import web <EOL> from aiohttp_utils import negotiation , CONFIG_KEY <EOL> from aiohttp_utils . negotiation import Response <EOL> @ pytest . fixture ( ) <EOL> def app ( loop ) : <EOL> return web . Application ( loop = loop ) <EOL> @ pytest . fixture ( ) <EOL> def client ( create_client , app ) : <EOL> return create_client ( app ) <EOL> def add_routes ( app ) : <EOL> def handler ( request ) : <EOL> return Response ( { '<STR_LIT:message>' : '<STR_LIT>' } ) <EOL> @ asyncio . coroutine <EOL> def coro_handler ( request ) : <EOL> return Response ( { '<STR_LIT:message>' : '<STR_LIT>' } ) <EOL> @ asyncio . coroutine <EOL> def post_coro_handler ( request ) : <EOL> return Response ( { '<STR_LIT:message>' : '<STR_LIT>' } , status = <NUM_LIT> ) <EOL> app . router . add_route ( '<STR_LIT:GET>' , '<STR_LIT>' , handler ) <EOL> app . router . add_route ( '<STR_LIT:GET>' , '<STR_LIT>' , coro_handler ) <EOL> app . router . add_route ( '<STR_LIT:POST>' , '<STR_LIT>' , post_coro_handler ) <EOL> def configure_app ( app , overrides = None , setup = False ) : <EOL> overrides = overrides or { } <EOL> add_routes ( app ) <EOL> if setup : <EOL> negotiation . setup ( app , ** overrides ) <EOL> else : <EOL> middleware = negotiation . negotiation_middleware ( ** overrides ) <EOL> app . middlewares . append ( middleware ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , <EOL> [ <EOL> True , <EOL> False <EOL> ] ) <EOL> def test_renders_to_json_by_default ( app , client , setup ) : <EOL> configure_app ( app , overrides = None , setup = setup ) <EOL> res = client . get ( '<STR_LIT>' ) <EOL> assert res . content_type == '<STR_LIT:application/json>' <EOL> assert res . json == { '<STR_LIT:message>' : '<STR_LIT>' } <EOL> res = client . get ( '<STR_LIT>' ) <EOL> assert res . content_type == '<STR_LIT:application/json>' <EOL> assert res . json == { '<STR_LIT:message>' : '<STR_LIT>' } <EOL> res = client . post ( '<STR_LIT>' ) <EOL> assert res . content_type == '<STR_LIT:application/json>' <EOL> assert res . status_code == <NUM_LIT> <EOL> assert res . json == { '<STR_LIT:message>' : '<STR_LIT>' } <EOL> def dummy_renderer ( request , data ) : <EOL> return web . Response ( <EOL> body = '<STR_LIT>' . format ( data [ '<STR_LIT:message>' ] ) . encode ( '<STR_LIT:utf-8>' ) , <EOL> content_type = '<STR_LIT>' <EOL> ) <EOL> def test_renderer_override ( app , client ) : <EOL> configure_app ( app , overrides = { <EOL> '<STR_LIT>' : OrderedDict ( [ <EOL> ( '<STR_LIT>' , dummy_renderer ) , <EOL> ] ) <EOL> } , setup = True ) <EOL> res = client . get ( '<STR_LIT>' ) <EOL> assert res . content_type == '<STR_LIT>' <EOL> assert res . body == b'<STR_LIT>' <EOL> def test_renderer_override_multiple_classes ( app , client ) : <EOL> configure_app ( app , overrides = { <EOL> '<STR_LIT>' : OrderedDict ( [ <EOL> ( '<STR_LIT>' , dummy_renderer ) , <EOL> ( '<STR_LIT:application/json>' , negotiation . render_json ) , <EOL> ( '<STR_LIT>' , negotiation . render_json ) , <EOL> ] ) <EOL> } , setup = True ) <EOL> res = client . get ( '<STR_LIT>' , headers = { '<STR_LIT>' : '<STR_LIT:application/json>' } ) <EOL> assert res . content_type == '<STR_LIT:application/json>' <EOL> assert res . json == { '<STR_LIT:message>' : '<STR_LIT>' } <EOL> res = client . get ( '<STR_LIT>' , headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert res . content_type == '<STR_LIT>' <EOL> assert res . json == { '<STR_LIT:message>' : '<STR_LIT>' } <EOL> res = client . get ( '<STR_LIT>' , headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert res . content_type == '<STR_LIT>' <EOL> assert res . body == b'<STR_LIT>' <EOL> def test_renderer_override_force ( app , client ) : <EOL> configure_app ( app , overrides = { <EOL> '<STR_LIT>' : False , <EOL> } , setup = True ) <EOL> res = client . get ( '<STR_LIT>' , headers = { '<STR_LIT>' : '<STR_LIT>' } , expect_errors = True ) <EOL> assert res . status_code == <NUM_LIT> <EOL> def test_nonordered_dict_of_renderers ( app , client ) : <EOL> configure_app ( app , overrides = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:application/json>' : negotiation . render_json <EOL> } <EOL> } , setup = True ) <EOL> res = client . get ( '<STR_LIT>' , headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert res . content_type == '<STR_LIT:application/json>' <EOL> def test_configuration_through_app_key ( app , client ) : <EOL> add_routes ( app ) <EOL> app [ CONFIG_KEY ] = { <EOL> '<STR_LIT>' : OrderedDict ( [ <EOL> ( '<STR_LIT>' , dummy_renderer ) , <EOL> ] ) <EOL> } <EOL> negotiation . setup ( app ) <EOL> res = client . get ( '<STR_LIT>' ) <EOL> assert res . content_type == '<STR_LIT>' <EOL> assert res . body == b'<STR_LIT>' </s>
