<s> import functools <EOL> import httpretty . core as core <EOL> from httpretty . core import httpretty , URIInfo , fakesock , Entry <EOL> from httpretty . compat import ClassTypes , urlsplit <EOL> from httpretty . http import parse_requestline <EOL> from httpretty . utils import decode_utf8 , utf8 <EOL> old_fakesocket_sendall = fakesock . socket . sendall <EOL> def f_sendall ( self , data , * args , ** kw ) : <EOL> self . _sent_data . append ( data ) <EOL> try : <EOL> requestline , _ = data . split ( b'<STR_LIT:\r\n>' , <NUM_LIT:1> ) <EOL> method , path , version = parse_requestline ( decode_utf8 ( requestline ) ) <EOL> is_parsing_headers = True <EOL> except ValueError : <EOL> is_parsing_headers = False <EOL> if not self . _entry : <EOL> return self . real_sendall ( data , * args , ** kw ) <EOL> self . fd . seek ( <NUM_LIT:0> ) <EOL> if not is_parsing_headers : <EOL> if len ( self . _sent_data ) > <NUM_LIT:1> : <EOL> headers = utf8 ( last_requestline ( self . _sent_data ) ) <EOL> meta = self . _entry . request . headers <EOL> body = utf8 ( self . _sent_data [ - <NUM_LIT:1> ] ) <EOL> if meta . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' : <EOL> if not body . isdigit ( ) and body != b'<STR_LIT:\r\n>' and body != b'<STR_LIT>' : <EOL> self . _entry . request . body += body <EOL> else : <EOL> self . _entry . request . body += body <EOL> httpretty . historify_request ( headers , body , False ) <EOL> return <EOL> s = urlsplit ( path ) <EOL> core . POTENTIAL_HTTP_PORTS . add ( int ( s . port or <NUM_LIT> ) ) <EOL> headers , body = list ( map ( utf8 , data . split ( b'<STR_LIT>' , <NUM_LIT:1> ) ) ) <EOL> request = flaskhttpretty . historify_request ( headers , body ) <EOL> info = URIInfo ( hostname = self . _host , port = self . _port , <EOL> path = s . path , <EOL> query = s . query , <EOL> last_request = request ) <EOL> entry = flaskhttpretty . get_entry ( method , info , request ) <EOL> if not entry : <EOL> self . _entry = None <EOL> self . real_sendall ( data ) <EOL> return None <EOL> self . _entry = entry <EOL> class flaskhttpretty ( httpretty ) : <EOL> flask_app = None <EOL> _apps = [ ] <EOL> @ classmethod <EOL> def check_response ( cls , responses , method , info , request ) : <EOL> result_response = None <EOL> for response in responses : <EOL> if response . status_code != <NUM_LIT> : <EOL> result_response = response <EOL> return cls . flask_resp_to_entry ( result_response , method , info , request ) <EOL> @ classmethod <EOL> def get_entry ( cls , method , info , request ) : <EOL> if not cls . _apps : <EOL> return None <EOL> responses = [ ] <EOL> for app in cls . _apps : <EOL> with app . test_client ( ) as c : <EOL> http_request = getattr ( c , method . lower ( ) ) <EOL> content_type = request . headers . get ( '<STR_LIT:Content-Type>' ) <EOL> response = http_request ( '<STR_LIT>' . format ( info . path , info . query ) , <EOL> data = request . body , <EOL> content_type = content_type ) <EOL> responses . append ( response ) <EOL> return cls . check_response ( responses , method , info , request ) <EOL> @ classmethod <EOL> def register_app ( cls , app , uri = '<STR_LIT>' ) : <EOL> cls . uri = uri <EOL> cls . _apps . append ( app ) <EOL> @ classmethod <EOL> def enable ( cls ) : <EOL> fakesock . socket . sendall = f_sendall <EOL> httpretty . enable ( ) <EOL> @ classmethod <EOL> def reset ( cls ) : <EOL> cls . _apps = [ ] <EOL> httpretty . reset ( ) <EOL> @ classmethod <EOL> def flask_resp_to_entry ( cls , resp , method , info , request ) : <EOL> headers = { } <EOL> headers [ '<STR_LIT:Content-Type>' ] = resp . content_type <EOL> return Entry ( method , info . hostname , body = resp . data , <EOL> status = resp . status_code , <EOL> adding_headers = None , forcing_headers = None ) <EOL> def flaskhttprettified ( test ) : <EOL> "<STR_LIT>" <EOL> def decorate_class ( klass ) : <EOL> for attr in dir ( klass ) : <EOL> if not attr . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> attr_value = getattr ( klass , attr ) <EOL> if not hasattr ( attr_value , "<STR_LIT>" ) : <EOL> continue <EOL> setattr ( klass , attr , decorate_callable ( attr_value ) ) <EOL> return klass <EOL> def decorate_callable ( test ) : <EOL> @ functools . wraps ( test ) <EOL> def wrapper ( * args , ** kw ) : <EOL> flaskhttpretty . reset ( ) <EOL> flaskhttpretty . enable ( ) <EOL> try : <EOL> return test ( * args , ** kw ) <EOL> finally : <EOL> flaskhttpretty . disable ( ) <EOL> return wrapper <EOL> if isinstance ( test , ClassTypes ) : <EOL> return decorate_class ( test ) <EOL> return decorate_callable ( test ) </s>
