<s> import unittest <EOL> from wsgid . core import Wsgid <EOL> from wsgid . test import FakeOptions <EOL> import wsgid . conf <EOL> import urllib2 <EOL> import time <EOL> import logging <EOL> logging . basicConfig ( level = logging . DEBUG ) <EOL> import multiprocessing <EOL> logger = multiprocessing . log_to_stderr ( ) <EOL> logger . setLevel ( multiprocessing . SUBDEBUG ) <EOL> class WsgidServeTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> wsgid . conf . settings = FakeOptions ( mongrel2_chroot = None ) <EOL> def test_app_return_body ( self ) : <EOL> def app ( environ , start_response ) : <EOL> start_response ( '<STR_LIT>' , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> return [ '<STR_LIT>' ] <EOL> pid = self . _run_wsgid ( app ) <EOL> try : <EOL> r = urllib2 . urlopen ( '<STR_LIT>' ) <EOL> body = r . read ( ) <EOL> self . assertEquals ( body , '<STR_LIT>' ) <EOL> finally : <EOL> self . _kill_wsgid ( pid ) <EOL> def test_app_return_body_with_more_than_one_item ( self ) : <EOL> '''<STR_LIT>''' <EOL> def app ( env , start_response ) : <EOL> start_response ( '<STR_LIT>' , [ ] ) <EOL> return [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> pid = self . _run_wsgid ( app ) <EOL> try : <EOL> r = urllib2 . urlopen ( '<STR_LIT>' ) <EOL> self . assertEquals ( '<STR_LIT>' , r . read ( ) ) <EOL> finally : <EOL> self . _kill_wsgid ( pid ) <EOL> def test_app_use_write_callable ( self ) : <EOL> def app ( env , start_response ) : <EOL> write = start_response ( '<STR_LIT>' , [ ] ) <EOL> write ( '<STR_LIT>' ) <EOL> write ( '<STR_LIT>' ) <EOL> return None <EOL> pid = self . _run_wsgid ( app ) <EOL> try : <EOL> r = urllib2 . urlopen ( '<STR_LIT>' ) <EOL> self . assertEquals ( '<STR_LIT>' , r . read ( ) ) <EOL> finally : <EOL> self . _kill_wsgid ( pid ) <EOL> def test_app_return_an_iterable ( self ) : <EOL> '''<STR_LIT>''' <EOL> def app ( environ , start_response ) : <EOL> class Body ( object ) : <EOL> def __init__ ( self , parts ) : <EOL> self . parts = parts <EOL> def __iter__ ( self ) : <EOL> for a in self . parts : <EOL> yield a <EOL> start_response ( '<STR_LIT>' , [ ] ) <EOL> return Body ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> pid = self . _run_wsgid ( app ) <EOL> try : <EOL> r = urllib2 . urlopen ( '<STR_LIT>' ) <EOL> self . assertEquals ( '<STR_LIT>' , r . read ( ) ) <EOL> finally : <EOL> self . _kill_wsgid ( pid ) <EOL> def test_app_receives_a_post_request ( self ) : <EOL> '''<STR_LIT>''' <EOL> def app ( environ , start_response ) : <EOL> start_response ( '<STR_LIT>' , [ ] ) <EOL> if environ [ '<STR_LIT>' ] == '<STR_LIT:POST>' : <EOL> return [ environ [ '<STR_LIT>' ] . read ( ) ] <EOL> pid = self . _run_wsgid ( app ) <EOL> try : <EOL> r = urllib2 . urlopen ( '<STR_LIT>' , data = '<STR_LIT>' ) <EOL> self . assertEquals ( '<STR_LIT>' , r . read ( ) ) <EOL> finally : <EOL> self . _kill_wsgid ( pid ) <EOL> def _run_wsgid ( self , app ) : <EOL> def _serve ( app ) : <EOL> import multiprocessing <EOL> w = Wsgid ( app , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> w . log = multiprocessing . get_logger ( ) <EOL> w . log . setLevel ( logging . DEBUG ) <EOL> w . serve ( ) <EOL> import multiprocessing <EOL> p = multiprocessing . Process ( target = _serve , args = ( app , ) ) <EOL> p . start ( ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> return p . pid <EOL> def _kill_wsgid ( self , pid ) : <EOL> import os <EOL> os . kill ( pid , <NUM_LIT:15> ) </s>
