<s> import time <EOL> import multiprocessing <EOL> import pytest <EOL> import socket <EOL> try : <EOL> from urllib2 import urlopen <EOL> except ImportError : <EOL> from urllib . request import urlopen <EOL> from flask import _request_ctx_stack <EOL> @ pytest . yield_fixture <EOL> def client ( app ) : <EOL> """<STR_LIT>""" <EOL> with app . test_client ( ) as client : <EOL> yield client <EOL> @ pytest . fixture <EOL> def client_class ( request , client ) : <EOL> """<STR_LIT>""" <EOL> if request . cls is not None : <EOL> request . cls . client = client <EOL> class LiveServer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app , port ) : <EOL> self . app = app <EOL> self . port = port <EOL> self . _process = None <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> worker = lambda app , port : app . run ( port = port , use_reloader = False ) <EOL> self . _process = multiprocessing . Process ( <EOL> target = worker , <EOL> args = ( self . app , self . port ) <EOL> ) <EOL> self . _process . start ( ) <EOL> timeout = <NUM_LIT:5> <EOL> while timeout > <NUM_LIT:0> : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> try : <EOL> urlopen ( self . url ( ) ) <EOL> timeout = <NUM_LIT:0> <EOL> except : <EOL> timeout -= <NUM_LIT:1> <EOL> def url ( self , url = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( self . port , url ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _process : <EOL> self . _process . terminate ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . url ( ) <EOL> def _rewrite_server_name ( server_name , new_port ) : <EOL> """<STR_LIT>""" <EOL> sep = '<STR_LIT::>' <EOL> if sep in server_name : <EOL> server_name , port = server_name . split ( sep , <NUM_LIT:1> ) <EOL> return sep . join ( ( server_name , new_port ) ) <EOL> @ pytest . fixture ( scope = '<STR_LIT>' ) <EOL> def live_server ( request , app , monkeypatch ) : <EOL> """<STR_LIT>""" <EOL> s = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> s . bind ( ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> port = s . getsockname ( ) [ <NUM_LIT:1> ] <EOL> s . close ( ) <EOL> server_name = app . config [ '<STR_LIT>' ] or '<STR_LIT:localhost>' <EOL> monkeypatch . setitem ( app . config , '<STR_LIT>' , <EOL> _rewrite_server_name ( server_name , str ( port ) ) ) <EOL> server = LiveServer ( app , port ) <EOL> if request . config . getvalue ( '<STR_LIT>' ) : <EOL> server . start ( ) <EOL> request . addfinalizer ( server . stop ) <EOL> return server <EOL> @ pytest . fixture <EOL> def config ( app ) : <EOL> """<STR_LIT>""" <EOL> return app . config <EOL> @ pytest . fixture <EOL> def request_ctx ( app ) : <EOL> """<STR_LIT>""" <EOL> return _request_ctx_stack . top <EOL> @ pytest . fixture ( params = [ '<STR_LIT:application/json>' , '<STR_LIT>' ] ) <EOL> def mimetype ( request ) : <EOL> return request . param <EOL> @ pytest . fixture <EOL> def accept_mimetype ( mimetype ) : <EOL> return [ ( '<STR_LIT>' , mimetype ) ] <EOL> @ pytest . fixture <EOL> def accept_json ( request ) : <EOL> return accept_mimetype ( '<STR_LIT:application/json>' ) <EOL> @ pytest . fixture <EOL> def accept_jsonp ( ) : <EOL> return accept_mimetype ( '<STR_LIT>' ) <EOL> @ pytest . fixture ( params = [ '<STR_LIT:*>' , '<STR_LIT>' ] ) <EOL> def accept_any ( request ) : <EOL> return accept_mimetype ( request . param ) </s>
