<s> """<STR_LIT>""" <EOL> from nose . tools import eq_ , ok_ , assert_false , assert_raises <EOL> from django_wsgi . embedded_wsgi import call_wsgi_app , make_wsgi_view <EOL> from django_wsgi . handler import DjangoWSGIRequest <EOL> from django_wsgi . exc import ApplicationCallError <EOL> from tests import ( BaseDjangoTestCase , MockApp , MockClosingApp , MockWriteApp , <EOL> MockGeneratorApp , complete_environ ) <EOL> class TestCallWSGIApp ( BaseDjangoTestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_original_environ_not_modified ( self ) : <EOL> """<STR_LIT>""" <EOL> original_environ = complete_environ ( SCRIPT_NAME = "<STR_LIT>" , PATH_INFO = "<STR_LIT>" ) <EOL> expected_environ = original_environ . copy ( ) <EOL> request = _make_request ( ** original_environ ) <EOL> app = MockApp ( "<STR_LIT>" , [ ] ) <EOL> call_wsgi_app ( app , request , "<STR_LIT>" ) <EOL> eq_ ( expected_environ . keys ( ) , request . environ . keys ( ) ) <EOL> for variable_name , expected_variable_value in expected_environ . items ( ) : <EOL> if variable_name == '<STR_LIT>' : <EOL> actual_input = request . environ [ '<STR_LIT>' ] <EOL> eq_ ( <NUM_LIT:0> , actual_input . tell ( ) ) <EOL> eq_ ( b"<STR_LIT>" , actual_input . read ( ) ) <EOL> else : <EOL> eq_ ( expected_variable_value , request . environ [ variable_name ] ) <EOL> def test_routing_args_are_removed ( self ) : <EOL> """<STR_LIT>""" <EOL> environ = { <EOL> '<STR_LIT>' : ( ( ) , { } ) , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } <EOL> environ = complete_environ ( ** environ ) <EOL> request = _make_request ( ** environ ) <EOL> app = MockApp ( "<STR_LIT>" , [ ] ) <EOL> call_wsgi_app ( app , request , "<STR_LIT>" ) <EOL> ok_ ( "<STR_LIT>" not in app . environ ) <EOL> def test_webob_adhoc_attrs_are_removed ( self ) : <EOL> """<STR_LIT>""" <EOL> environ = { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : ( ( ) , { } ) , <EOL> '<STR_LIT>' : { '<STR_LIT:foo>' : "<STR_LIT:bar>" } , <EOL> } <EOL> environ = complete_environ ( ** environ ) <EOL> request = _make_request ( ** environ ) <EOL> app = MockApp ( "<STR_LIT>" , [ ] ) <EOL> call_wsgi_app ( app , request , "<STR_LIT>" ) <EOL> ok_ ( "<STR_LIT>" not in app . environ ) <EOL> def test_mount_point ( self ) : <EOL> environ = complete_environ ( SCRIPT_NAME = "<STR_LIT>" , PATH_INFO = "<STR_LIT>" ) <EOL> request = _make_request ( ** environ ) <EOL> app = MockApp ( "<STR_LIT>" , [ ] ) <EOL> call_wsgi_app ( app , request , "<STR_LIT>" ) <EOL> eq_ ( app . environ [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> eq_ ( app . environ [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> def test_incorrect_mount_point ( self ) : <EOL> """<STR_LIT>""" <EOL> environ = complete_environ ( SCRIPT_NAME = "<STR_LIT>" , <EOL> PATH_INFO = "<STR_LIT>" ) <EOL> request = _make_request ( ** environ ) <EOL> path_info = "<STR_LIT>" <EOL> app = MockApp ( "<STR_LIT>" , [ ] ) <EOL> assert_raises ( ApplicationCallError , call_wsgi_app , app , request , <EOL> path_info ) <EOL> def test_headers_are_copied_over ( self ) : <EOL> environ = complete_environ ( SCRIPT_NAME = "<STR_LIT>" , PATH_INFO = "<STR_LIT>" ) <EOL> request = _make_request ( ** environ ) <EOL> headers = [ <EOL> ( "<STR_LIT>" , "<STR_LIT:bar>" ) , <EOL> ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) , <EOL> ] <EOL> expected_headers = { <EOL> '<STR_LIT>' : ( "<STR_LIT>" , "<STR_LIT:bar>" ) , <EOL> '<STR_LIT>' : ( "<STR_LIT:Content-Type>" , "<STR_LIT>" ) , <EOL> } <EOL> app = MockApp ( "<STR_LIT>" , headers ) <EOL> django_response = call_wsgi_app ( app , request , "<STR_LIT>" ) <EOL> eq_ ( expected_headers , django_response . _headers ) <EOL> def test_authenticated_user ( self ) : <EOL> environ = complete_environ ( SCRIPT_NAME = "<STR_LIT>" , PATH_INFO = "<STR_LIT>" ) <EOL> request = _make_request ( authenticated = True , ** environ ) <EOL> app = MockApp ( "<STR_LIT>" , [ ] ) <EOL> call_wsgi_app ( app , request , "<STR_LIT>" ) <EOL> eq_ ( "<STR_LIT>" , app . environ [ '<STR_LIT>' ] ) <EOL> def test_cookies_sent ( self ) : <EOL> environ = complete_environ ( SCRIPT_NAME = "<STR_LIT>" , PATH_INFO = "<STR_LIT>" ) <EOL> request = _make_request ( ** environ ) <EOL> headers = [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> ) , <EOL> ( "<STR_LIT>" , u"<STR_LIT>" ) , <EOL> ] <EOL> expected_cookies = { <EOL> '<STR_LIT>' : { '<STR_LIT:value>' : "<STR_LIT>" } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:value>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } , <EOL> '<STR_LIT>' : { '<STR_LIT:value>' : "<STR_LIT>" , '<STR_LIT:path>' : "<STR_LIT:/>" } , <EOL> '<STR_LIT>' : { '<STR_LIT:value>' : "<STR_LIT>" , '<STR_LIT:path>' : "<STR_LIT>" } , <EOL> '<STR_LIT>' : { '<STR_LIT:value>' : "<STR_LIT>" , '<STR_LIT>' : "<STR_LIT>" } , <EOL> '<STR_LIT>' : { '<STR_LIT:value>' : "<STR_LIT>" , '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:value>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT:path>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } , <EOL> '<STR_LIT>' : { '<STR_LIT:value>' : "<STR_LIT>" , '<STR_LIT>' : <NUM_LIT> } , <EOL> } <EOL> app = MockApp ( "<STR_LIT>" , headers ) <EOL> django_response = call_wsgi_app ( app , request , "<STR_LIT>" ) <EOL> eq_ ( len ( expected_cookies ) , len ( django_response . cookies ) ) <EOL> for ( cookie_set_name , cookie_set ) in django_response . cookies . items ( ) : <EOL> expected_cookie = expected_cookies [ cookie_set_name ] <EOL> expected_cookie_value = expected_cookie . pop ( "<STR_LIT:value>" ) <EOL> eq_ ( expected_cookie_value , cookie_set . value , <EOL> '<STR_LIT>' % <EOL> ( cookie_set_name , cookie_set . value ) ) <EOL> for ( attr_key , attr_val ) in expected_cookie . items ( ) : <EOL> eq_ ( <EOL> cookie_set [ attr_key ] , <EOL> attr_val , <EOL> '<STR_LIT>' % <EOL> ( attr_key , cookie_set_name , cookie_set [ attr_key ] ) , <EOL> ) <EOL> def test_string_as_response ( self ) : <EOL> app = MockApp ( "<STR_LIT>" , [ ( "<STR_LIT>" , "<STR_LIT>" ) ] ) <EOL> environ = complete_environ ( SCRIPT_NAME = "<STR_LIT>" , PATH_INFO = "<STR_LIT>" ) <EOL> request = _make_request ( ** environ ) <EOL> django_response = call_wsgi_app ( app , request , "<STR_LIT>" ) <EOL> http_response_content = b"<STR_LIT:body>" <EOL> eq_ ( http_response_content , _resolve_response_body ( django_response ) ) <EOL> def test_iterable_as_response ( self ) : <EOL> app = MockGeneratorApp ( "<STR_LIT>" , [ ( "<STR_LIT>" , "<STR_LIT>" ) ] ) <EOL> environ = complete_environ ( SCRIPT_NAME = "<STR_LIT>" , PATH_INFO = "<STR_LIT>" ) <EOL> request = _make_request ( ** environ ) <EOL> django_response = call_wsgi_app ( app , request , "<STR_LIT>" ) <EOL> http_response_content = b"<STR_LIT>" <EOL> eq_ ( http_response_content , _resolve_response_body ( django_response ) ) <EOL> def test_write_response ( self ) : <EOL> app = MockWriteApp ( "<STR_LIT>" , [ ( "<STR_LIT>" , "<STR_LIT>" ) ] ) <EOL> environ = complete_environ ( SCRIPT_NAME = "<STR_LIT>" , PATH_INFO = "<STR_LIT>" ) <EOL> request = _make_request ( ** environ ) <EOL> django_response = call_wsgi_app ( app , request , "<STR_LIT>" ) <EOL> http_response_content = b"<STR_LIT>" <EOL> eq_ ( http_response_content , _resolve_response_body ( django_response ) ) <EOL> def test_closure_response ( self ) : <EOL> """<STR_LIT>""" <EOL> app = MockClosingApp ( "<STR_LIT>" , [ ] ) <EOL> environ = complete_environ ( SCRIPT_NAME = "<STR_LIT>" , PATH_INFO = "<STR_LIT>" ) <EOL> request = _make_request ( ** environ ) <EOL> django_response = call_wsgi_app ( app , request , "<STR_LIT>" ) <EOL> assert_false ( app . app_iter . closed ) <EOL> django_response . close ( ) <EOL> ok_ ( app . app_iter . closed ) <EOL> class TestWSGIView ( BaseDjangoTestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_right_path ( self ) : <EOL> """<STR_LIT>""" <EOL> headers = [ ( "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> app = MockApp ( "<STR_LIT>" , headers ) <EOL> django_view = make_wsgi_view ( app ) <EOL> environ = complete_environ ( SCRIPT_NAME = "<STR_LIT>" , <EOL> PATH_INFO = "<STR_LIT>" ) <EOL> request = _make_request ( ** environ ) <EOL> django_response = django_view ( request , "<STR_LIT>" ) <EOL> eq_ ( django_response . status_code , <NUM_LIT> ) <EOL> eq_ ( ( "<STR_LIT>" , "<STR_LIT>" ) , django_response . _headers [ '<STR_LIT>' ] ) <EOL> eq_ ( app . environ [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> eq_ ( app . environ [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> def test_not_final_path ( self ) : <EOL> """<STR_LIT>""" <EOL> headers = [ ( "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> app = MockApp ( "<STR_LIT>" , headers ) <EOL> django_view = make_wsgi_view ( app ) <EOL> environ = complete_environ ( SCRIPT_NAME = "<STR_LIT>" , <EOL> PATH_INFO = "<STR_LIT>" ) <EOL> request = _make_request ( ** environ ) <EOL> assert_raises ( ApplicationCallError , django_view , request , "<STR_LIT>" ) <EOL> def _make_request ( authenticated = False , ** environ ) : <EOL> """<STR_LIT>""" <EOL> class MockDjangoUser ( object ) : <EOL> def __init__ ( self , authenticated ) : <EOL> self . username = "<STR_LIT>" <EOL> self . authenticated = authenticated <EOL> def is_authenticated ( self ) : <EOL> return self . authenticated <EOL> request = DjangoWSGIRequest ( environ ) <EOL> request . user = MockDjangoUser ( authenticated ) <EOL> return request <EOL> def _resolve_response_body ( response ) : <EOL> body_parts = tuple ( response ) <EOL> body_text = b"<STR_LIT>" . join ( body_parts ) <EOL> return body_text </s>
