<s> import base64 <EOL> import logging <EOL> import os <EOL> import unittest <EOL> import urllib <EOL> import urllib2 <EOL> import urlparse <EOL> import wptserve <EOL> logging . basicConfig ( ) <EOL> here = os . path . split ( __file__ ) [ <NUM_LIT:0> ] <EOL> doc_root = os . path . join ( here , "<STR_LIT>" ) <EOL> class Request ( urllib2 . Request ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> urllib2 . Request . __init__ ( self , * args , ** kwargs ) <EOL> self . method = "<STR_LIT:GET>" <EOL> def get_method ( self ) : <EOL> return self . method <EOL> def add_data ( self , data ) : <EOL> if hasattr ( data , "<STR_LIT>" ) : <EOL> data = urllib . urlencode ( data ) <EOL> print data <EOL> self . add_header ( "<STR_LIT>" , str ( len ( data ) ) ) <EOL> urllib2 . Request . add_data ( self , data ) <EOL> class TestUsingServer ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . server = wptserve . server . WebTestHttpd ( host = "<STR_LIT:localhost>" , <EOL> port = <NUM_LIT:0> , <EOL> use_ssl = False , <EOL> certificate = None , <EOL> doc_root = doc_root ) <EOL> self . server . start ( False ) <EOL> def tearDown ( self ) : <EOL> self . server . stop ( ) <EOL> def abs_url ( self , path , query = None ) : <EOL> return urlparse . urlunsplit ( ( "<STR_LIT:http>" , "<STR_LIT>" % ( self . server . host , self . server . port ) , path , query , None ) ) <EOL> def request ( self , path , query = None , method = "<STR_LIT:GET>" , headers = None , body = None , auth = None ) : <EOL> req = Request ( self . abs_url ( path , query ) ) <EOL> req . method = method <EOL> if headers is None : <EOL> headers = { } <EOL> for name , value in headers . iteritems ( ) : <EOL> req . add_header ( name , value ) <EOL> if body is not None : <EOL> req . add_data ( body ) <EOL> if auth is not None : <EOL> req . add_header ( "<STR_LIT>" , "<STR_LIT>" % base64 . encodestring ( '<STR_LIT>' % auth ) ) <EOL> return urllib2 . urlopen ( req ) </s>
