<s> from stango import Stango <EOL> from stango . files import Files <EOL> import functools <EOL> from threading import Thread <EOL> from urllib . request import urlopen <EOL> from urllib . error import HTTPError <EOL> from . import StangoTestCase , make_suite , view_value , view_template <EOL> class ServerTestCase ( StangoTestCase ) : <EOL> def setup ( self ) : <EOL> self . manager = Stango ( ) <EOL> self . manager . index_file = '<STR_LIT>' <EOL> def serve ( func ) : <EOL> @ functools . wraps ( func ) <EOL> def wrapper ( self ) : <EOL> gen = func ( self ) <EOL> httpd = next ( gen ) <EOL> httpd . allow_reuse_address = True <EOL> server_thread = Thread ( target = httpd . serve_forever ) <EOL> server_thread . start ( ) <EOL> try : <EOL> self . assert_raises ( StopIteration , gen . send , None ) <EOL> finally : <EOL> httpd . shutdown ( ) <EOL> server_thread . join ( ) <EOL> httpd . socket . close ( ) <EOL> return wrapper <EOL> @ serve <EOL> def test_simple ( self ) : <EOL> self . manager . files = Files ( <EOL> ( '<STR_LIT>' , view_value ( '<STR_LIT>' ) ) , <EOL> ) <EOL> yield self . manager . make_server ( '<STR_LIT:127.0.0.1>' , <NUM_LIT> ) <EOL> data = urlopen ( '<STR_LIT>' ) <EOL> self . eq ( data . read ( ) , b'<STR_LIT>' ) <EOL> self . eq ( data . info ( ) [ '<STR_LIT:Content-Type>' ] , '<STR_LIT>' ) <EOL> @ serve <EOL> def test_real_path ( self ) : <EOL> self . manager . files = Files ( <EOL> ( '<STR_LIT>' , view_value ( '<STR_LIT>' ) ) , <EOL> ) <EOL> yield self . manager . make_server ( '<STR_LIT:127.0.0.1>' , <NUM_LIT> ) <EOL> data = urlopen ( '<STR_LIT>' ) <EOL> self . eq ( data . read ( ) , b'<STR_LIT>' ) <EOL> self . eq ( data . info ( ) [ '<STR_LIT:Content-Type>' ] , '<STR_LIT>' ) <EOL> @ serve <EOL> def test_404 ( self ) : <EOL> self . manager . files = Files ( <EOL> ( '<STR_LIT>' , view_value ( '<STR_LIT>' ) ) , <EOL> ) <EOL> yield self . manager . make_server ( '<STR_LIT:127.0.0.1>' , <NUM_LIT> ) <EOL> url = '<STR_LIT>' <EOL> exc = self . assert_raises ( HTTPError , urlopen , url ) <EOL> self . eq ( exc . code , <NUM_LIT> ) <EOL> def suite ( ) : <EOL> return make_suite ( ServerTestCase ) </s>
