def RequestMock(response='', headers=None):
 res = mock.Mock()
 res.read.return_value = response
 res.contents = response
 res.text = response
 res.iter_lines.side_effect = (lambda chunk_size=1: response.split('\n').__iter__())
 res.iter_content.side_effect = (lambda chunk_size=1: (response,).__iter__())
 def lines():
  return [(l + '\n') for l in response.split('\n')[:(-1)]]
 res.readlines.side_effect = lines
 res.iter_lines.side_effect = (lambda : lines().__iter__())
 res.headers = (headers or {})
 res.headers['content-length'] = len(response)
 return res

def RequestMock ( response = '' , headers = None ) : res = mock . Mock ( ) res . read . return_value = response res . contents = response res . text = response res . iter_lines . side_effect = ( lambda chunk_size = 1 : response . split ( '\n' ) . __iter__ ( ) ) res . iter_content . side_effect = ( lambda chunk_size = 1 : ( response , ) . __iter__ ( ) ) def lines ( ) : return [ ( l + '\n' ) for l in response . split ( '\n' ) [ : ( - 1 ) ] ] res . readlines . side_effect = lines res . iter_lines . side_effect = ( lambda : lines ( ) . __iter__ ( ) ) res . headers = ( headers or { } ) res . headers [ 'content-length' ] = len ( response ) return res
mocks the request objects of urllib2 and requests modules .