<s> from __future__ import print_function <EOL> from collections import namedtuple <EOL> from responses import RequestsMock <EOL> from tornado import ioloop <EOL> from tornado . concurrent import Future <EOL> from tornado . httpclient import HTTPRequest , HTTPResponse , HTTPError <EOL> Request = namedtuple ( "<STR_LIT>" , [ "<STR_LIT:url>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:body>" ] ) <EOL> class ResponseGenerator ( object ) : <EOL> def get_adapter ( self , url ) : <EOL> return self <EOL> def build_response ( self , request , response ) : <EOL> resp = HTTPResponse ( request , response . status , headers = response . headers , <EOL> effective_url = request . url , error = None , buffer = "<STR_LIT>" ) <EOL> resp . _body = response . data <EOL> f = Future ( ) <EOL> f . content = None <EOL> if response . status < <NUM_LIT:200> or response . status >= <NUM_LIT> : <EOL> resp . error = HTTPError ( response . status , response = resp ) <EOL> ioloop . IOLoop ( ) . current ( ) . add_callback ( f . set_exception , resp . error ) <EOL> else : <EOL> ioloop . IOLoop ( ) . current ( ) . add_callback ( f . set_result , resp ) <EOL> return f <EOL> class AsyncClientMock ( RequestsMock ) : <EOL> def start ( self ) : <EOL> import mock <EOL> def unbound_on_send ( client , request , callback = None , ** kwargs ) : <EOL> if not isinstance ( request , HTTPRequest ) : <EOL> request = Request ( request , <EOL> kwargs . get ( "<STR_LIT>" , "<STR_LIT:GET>" ) , <EOL> kwargs . get ( "<STR_LIT>" , [ ] ) , <EOL> kwargs . get ( "<STR_LIT:body>" , "<STR_LIT>" ) ) <EOL> return self . _on_request ( ResponseGenerator ( ) , request ) <EOL> self . _patcher = mock . patch ( '<STR_LIT>' , <EOL> unbound_on_send ) <EOL> self . _patcher . start ( ) <EOL> def stop ( self ) : <EOL> self . _patcher . stop ( ) </s>
