<s> import unittest <EOL> import six <EOL> from six . moves import http_cookies <EOL> from six . moves . urllib . parse import urlencode <EOL> from gunstar . http import Request <EOL> from gunstar . signals import template_rendered_signal <EOL> class Client ( object ) : <EOL> def __init__ ( self , app ) : <EOL> self . app = app <EOL> self . cookies = { } <EOL> self . template = None <EOL> self . context = None <EOL> template_rendered_signal . connect ( self . receive_template_rendered_signal ) <EOL> def get ( self , path , data = None , headers = None ) : <EOL> return self . get_request ( path , data = data , headers = headers ) <EOL> def post ( self , path , data = None , headers = None , <EOL> content_type = '<STR_LIT>' ) : <EOL> return self . post_request ( <EOL> path , data = data , headers = headers , <EOL> content_type = content_type , method = '<STR_LIT:POST>' <EOL> ) <EOL> def put ( self , path , data = None , headers = None , <EOL> content_type = '<STR_LIT>' ) : <EOL> return self . post_request ( <EOL> path , data = data , headers = headers , <EOL> content_type = content_type , method = '<STR_LIT>' <EOL> ) <EOL> def delete ( self , path , data = None , headers = None , <EOL> content_type = '<STR_LIT>' ) : <EOL> return self . post_request ( <EOL> path , data = data , headers = headers , <EOL> content_type = content_type , method = '<STR_LIT>' <EOL> ) <EOL> def options ( self , path , data = None , headers = None , <EOL> content_type = '<STR_LIT>' ) : <EOL> return self . post_request ( <EOL> path , data = data , headers = headers , <EOL> content_type = content_type , method = '<STR_LIT>' <EOL> ) <EOL> def head ( self , path , data = None , headers = None ) : <EOL> return self . get_request ( <EOL> path , data = data , headers = headers , method = '<STR_LIT>' <EOL> ) <EOL> def get_request ( self , path , data = None , headers = None , method = '<STR_LIT:GET>' ) : <EOL> if data is None : <EOL> data = { } <EOL> if headers is None : <EOL> headers = { } <EOL> if data : <EOL> path = path + '<STR_LIT:?>' + urlencode ( data ) <EOL> req = Request . blank ( path ) <EOL> req . method = method <EOL> req . headers . update ( headers ) <EOL> self . load_cookies ( req ) <EOL> resp = req . get_response ( self . app ) <EOL> self . store_cookies ( resp ) <EOL> resp . request_started = req <EOL> resp . template = self . template <EOL> resp . context = self . context <EOL> self . template = self . context = None <EOL> return resp <EOL> def post_request ( self , path , data = None , headers = None , content_type = '<STR_LIT>' , <EOL> method = '<STR_LIT:POST>' ) : <EOL> if data is None : <EOL> data = { } <EOL> if headers is None : <EOL> headers = { } <EOL> req = Request . blank ( path ) <EOL> req . content_type = content_type <EOL> req . method = method <EOL> req . body = six . b ( urlencode ( data ) ) <EOL> req . headers . update ( headers ) <EOL> self . load_cookies ( req ) <EOL> resp = req . get_response ( self . app ) <EOL> self . store_cookies ( resp ) <EOL> resp . request_started = req <EOL> resp . template = self . template <EOL> resp . context = self . context <EOL> self . template = self . context = None <EOL> return resp <EOL> def store_cookies ( self , resp ) : <EOL> cookies = http_cookies . SimpleCookie ( ) <EOL> try : <EOL> cookies . load ( resp . headers [ '<STR_LIT>' ] ) <EOL> for key in cookies : <EOL> self . cookies [ key ] = cookies [ key ] . value <EOL> except : <EOL> pass <EOL> def load_cookies ( self , req ) : <EOL> for key in self . cookies : <EOL> req . cookies [ key ] = self . cookies [ key ] <EOL> def receive_template_rendered_signal ( self , app , handler , template , <EOL> context ) : <EOL> self . template = template <EOL> self . context = context <EOL> class TestCase ( unittest . TestCase ) : <EOL> def get_app ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def _pre_setup ( self ) : <EOL> self . app = self . get_app ( ) <EOL> self . client = Client ( self . app ) <EOL> def __call__ ( self , result = None ) : <EOL> self . _pre_setup ( ) <EOL> super ( TestCase , self ) . __call__ ( result ) </s>
