<s> import json <EOL> from mock import patch <EOL> import pytest <EOL> from werkzeug . wrappers import Response <EOL> from nameko . testing . utils import get_extension <EOL> from nameko . web . handlers import http , HttpRequestHandler <EOL> class ExampleService ( object ) : <EOL> name = "<STR_LIT>" <EOL> @ http ( '<STR_LIT:GET>' , '<STR_LIT>' ) <EOL> def do_get ( self , request , bar ) : <EOL> return '<STR_LIT>' . format ( bar ) <EOL> @ http ( '<STR_LIT:POST>' , '<STR_LIT>' ) <EOL> def do_post ( self , request ) : <EOL> data = json . loads ( request . get_data ( as_text = True ) ) <EOL> value = data [ '<STR_LIT:value>' ] <EOL> return value <EOL> @ http ( '<STR_LIT:GET>' , '<STR_LIT>' ) <EOL> def do_custom ( self , request ) : <EOL> return Response ( '<STR_LIT>' ) <EOL> @ http ( '<STR_LIT:GET>' , '<STR_LIT>' ) <EOL> def do_status_code ( self , request ) : <EOL> return <NUM_LIT> , '<STR_LIT>' <EOL> @ http ( '<STR_LIT:GET>' , '<STR_LIT>' ) <EOL> def do_headers ( self , request ) : <EOL> return <NUM_LIT> , { '<STR_LIT>' : '<STR_LIT:bar>' } , '<STR_LIT>' <EOL> @ http ( '<STR_LIT:GET>' , '<STR_LIT>' ) <EOL> def fail ( self , request ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> @ http ( '<STR_LIT:GET>' , '<STR_LIT>' , expected_exceptions = ValueError ) <EOL> def fail_expected ( self , request ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> class SimpleService ( object ) : <EOL> name = "<STR_LIT>" <EOL> @ http ( '<STR_LIT:GET>' , '<STR_LIT>' ) <EOL> def method ( self , request ) : <EOL> pass <EOL> @ pytest . fixture <EOL> def web_session ( container_factory , web_config , web_session ) : <EOL> container = container_factory ( ExampleService , web_config ) <EOL> container . start ( ) <EOL> return web_session <EOL> def test_get ( web_session ) : <EOL> rv = web_session . get ( '<STR_LIT>' ) <EOL> assert rv . text == '<STR_LIT>' <EOL> rv = web_session . get ( '<STR_LIT>' ) <EOL> assert rv . status_code == <NUM_LIT> <EOL> def test_post ( web_session ) : <EOL> rv = web_session . post ( '<STR_LIT>' , data = json . dumps ( { <EOL> '<STR_LIT:value>' : '<STR_LIT:foo>' , <EOL> } ) ) <EOL> assert rv . text == "<STR_LIT:foo>" <EOL> def test_custom_response ( web_session ) : <EOL> rv = web_session . get ( '<STR_LIT>' ) <EOL> assert rv . text == '<STR_LIT>' <EOL> def test_custom_status_code ( web_session ) : <EOL> rv = web_session . get ( '<STR_LIT>' ) <EOL> assert rv . text == '<STR_LIT>' <EOL> assert rv . status_code == <NUM_LIT> <EOL> def test_custom_headers ( web_session ) : <EOL> rv = web_session . get ( '<STR_LIT>' ) <EOL> assert rv . text == '<STR_LIT>' <EOL> assert rv . status_code == <NUM_LIT> <EOL> assert rv . headers [ '<STR_LIT>' ] == '<STR_LIT:bar>' <EOL> def test_broken_method ( web_session ) : <EOL> rv = web_session . get ( '<STR_LIT>' ) <EOL> assert rv . status_code == <NUM_LIT> <EOL> assert "<STR_LIT>" in rv . text <EOL> def test_broken_method_expected ( web_session ) : <EOL> rv = web_session . get ( '<STR_LIT>' ) <EOL> assert rv . status_code == <NUM_LIT> <EOL> assert "<STR_LIT>" in rv . text <EOL> def test_bad_payload ( web_session ) : <EOL> rv = web_session . post ( '<STR_LIT>' , data = json . dumps ( { '<STR_LIT:value>' : <NUM_LIT> } ) ) <EOL> assert rv . status_code == <NUM_LIT> <EOL> assert "<STR_LIT>" in rv . text <EOL> def test_lifecycle ( container_factory , web_config ) : <EOL> container = container_factory ( SimpleService , web_config ) <EOL> http = get_extension ( container , HttpRequestHandler ) <EOL> with patch . object ( http . server , '<STR_LIT>' ) as register : <EOL> container . start ( ) <EOL> register . assert_called_with ( http ) <EOL> with patch . object ( http . server , '<STR_LIT>' ) as unregister : <EOL> container . stop ( ) <EOL> unregister . assert_called_with ( http ) </s>
