<s> from base64 import b64encode <EOL> from werkzeug . exceptions import HTTPException <EOL> import json <EOL> class TestClient ( ) : <EOL> def __init__ ( self , app , username , password ) : <EOL> self . app = app <EOL> self . auth = '<STR_LIT>' + b64encode ( ( username + '<STR_LIT::>' + password ) <EOL> . encode ( '<STR_LIT:utf-8>' ) ) . decode ( '<STR_LIT:utf-8>' ) <EOL> def send ( self , url , method = '<STR_LIT:GET>' , data = None , headers = { } ) : <EOL> headers = headers . copy ( ) <EOL> headers [ '<STR_LIT>' ] = self . auth <EOL> headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT:application/json>' <EOL> if data : <EOL> data = json . dumps ( data ) <EOL> with self . app . test_request_context ( url , method = method , data = data , <EOL> headers = headers ) : <EOL> try : <EOL> rv = self . app . preprocess_request ( ) <EOL> if rv is None : <EOL> rv = self . app . dispatch_request ( ) <EOL> rv = self . app . make_response ( rv ) <EOL> rv = self . app . process_response ( rv ) <EOL> except HTTPException as e : <EOL> rv = self . app . handle_user_exception ( e ) <EOL> return rv , json . loads ( rv . data . decode ( '<STR_LIT:utf-8>' ) ) <EOL> def get ( self , url , headers = { } ) : <EOL> return self . send ( url , '<STR_LIT:GET>' , headers = headers ) <EOL> def post ( self , url , data , headers = { } ) : <EOL> return self . send ( url , '<STR_LIT:POST>' , data , headers = headers ) <EOL> def put ( self , url , data , headers = { } ) : <EOL> return self . send ( url , '<STR_LIT>' , data , headers = headers ) <EOL> def delete ( self , url , headers = { } ) : <EOL> return self . send ( url , '<STR_LIT>' , headers = headers ) </s>
