<s> from __future__ import unicode_literals <EOL> try : <EOL> from urllib . request import urlopen <EOL> import urllib . request as urllib2 <EOL> except ImportError : <EOL> import urllib2 <EOL> urlopen = urllib2 . urlopen <EOL> from sure import * <EOL> from httpretty import HTTPretty , httprettified <EOL> from httpretty . core import decode_utf8 <EOL> @ httprettified <EOL> @ within ( two = microseconds ) <EOL> def test_httpretty_should_mock_a_simple_get_with_urllib2_read ( ) : <EOL> "<STR_LIT>" <EOL> HTTPretty . register_uri ( HTTPretty . GET , "<STR_LIT>" , <EOL> body = "<STR_LIT>" ) <EOL> fd = urlopen ( '<STR_LIT>' ) <EOL> got = fd . read ( ) <EOL> fd . close ( ) <EOL> expect ( got ) . to . equal ( b'<STR_LIT>' ) <EOL> @ httprettified <EOL> @ within ( two = microseconds ) <EOL> def test_httpretty_provides_easy_access_to_querystrings ( now ) : <EOL> "<STR_LIT>" <EOL> HTTPretty . register_uri ( HTTPretty . GET , "<STR_LIT>" , <EOL> body = "<STR_LIT>" ) <EOL> fd = urllib2 . urlopen ( '<STR_LIT>' ) <EOL> fd . read ( ) <EOL> fd . close ( ) <EOL> expect ( HTTPretty . last_request . querystring ) . to . equal ( { <EOL> '<STR_LIT:foo>' : [ '<STR_LIT:bar>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> } ) <EOL> @ httprettified <EOL> @ within ( two = microseconds ) <EOL> def test_httpretty_should_mock_headers_urllib2 ( now ) : <EOL> "<STR_LIT>" <EOL> HTTPretty . register_uri ( HTTPretty . GET , "<STR_LIT>" , <EOL> body = "<STR_LIT>" , <EOL> status = <NUM_LIT> ) <EOL> request = urlopen ( '<STR_LIT>' ) <EOL> headers = dict ( request . headers ) <EOL> request . close ( ) <EOL> expect ( request . code ) . to . equal ( <NUM_LIT> ) <EOL> expect ( headers ) . to . equal ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:date>' : now . strftime ( '<STR_LIT>' ) , <EOL> } ) <EOL> @ httprettified <EOL> @ within ( two = microseconds ) <EOL> def test_httpretty_should_allow_adding_and_overwritting_urllib2 ( now ) : <EOL> "<STR_LIT>" <EOL> HTTPretty . register_uri ( HTTPretty . GET , "<STR_LIT>" , <EOL> body = "<STR_LIT>" , <EOL> adding_headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> } ) <EOL> request = urlopen ( '<STR_LIT>' ) <EOL> headers = dict ( request . headers ) <EOL> request . close ( ) <EOL> expect ( request . code ) . to . equal ( <NUM_LIT:200> ) <EOL> expect ( headers ) . to . equal ( { <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:date>' : now . strftime ( '<STR_LIT>' ) , <EOL> } ) <EOL> @ httprettified <EOL> @ within ( two = microseconds ) <EOL> def test_httpretty_should_allow_forcing_headers_urllib2 ( ) : <EOL> "<STR_LIT>" <EOL> HTTPretty . register_uri ( HTTPretty . GET , "<STR_LIT>" , <EOL> body = "<STR_LIT>" , <EOL> forcing_headers = { <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' , <EOL> } ) <EOL> request = urlopen ( '<STR_LIT>' ) <EOL> headers = dict ( request . headers ) <EOL> request . close ( ) <EOL> expect ( headers ) . to . equal ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> @ httprettified <EOL> @ within ( two = microseconds ) <EOL> def test_httpretty_should_allow_adding_and_overwritting_by_kwargs_u2 ( now ) : <EOL> "<STR_LIT>" "<STR_LIT>" <EOL> body = "<STR_LIT>" <EOL> HTTPretty . register_uri ( HTTPretty . GET , "<STR_LIT>" , <EOL> body = body , <EOL> server = '<STR_LIT>' , <EOL> content_length = len ( body ) , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> request = urlopen ( '<STR_LIT>' ) <EOL> headers = dict ( request . headers ) <EOL> request . close ( ) <EOL> expect ( request . code ) . to . equal ( <NUM_LIT:200> ) <EOL> expect ( headers ) . to . equal ( { <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : str ( len ( body ) ) , <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:date>' : now . strftime ( '<STR_LIT>' ) , <EOL> } ) <EOL> @ httprettified <EOL> @ within ( two = microseconds ) <EOL> def test_httpretty_should_support_a_list_of_successive_responses_urllib2 ( now ) : <EOL> "<STR_LIT>" "<STR_LIT>" <EOL> HTTPretty . register_uri ( <EOL> HTTPretty . GET , "<STR_LIT>" , <EOL> responses = [ <EOL> HTTPretty . Response ( body = "<STR_LIT>" , status = <NUM_LIT> ) , <EOL> HTTPretty . Response ( body = '<STR_LIT>' , status = <NUM_LIT> ) , <EOL> ] ) <EOL> request1 = urlopen ( '<STR_LIT>' ) <EOL> body1 = request1 . read ( ) <EOL> request1 . close ( ) <EOL> expect ( request1 . code ) . to . equal ( <NUM_LIT> ) <EOL> expect ( body1 ) . to . equal ( b'<STR_LIT>' ) <EOL> request2 = urlopen ( '<STR_LIT>' ) <EOL> body2 = request2 . read ( ) <EOL> request2 . close ( ) <EOL> expect ( request2 . code ) . to . equal ( <NUM_LIT> ) <EOL> expect ( body2 ) . to . equal ( b'<STR_LIT>' ) <EOL> request3 = urlopen ( '<STR_LIT>' ) <EOL> body3 = request3 . read ( ) <EOL> request3 . close ( ) <EOL> expect ( request3 . code ) . to . equal ( <NUM_LIT> ) <EOL> expect ( body3 ) . to . equal ( b'<STR_LIT>' ) <EOL> @ httprettified <EOL> @ within ( two = microseconds ) <EOL> def test_can_inspect_last_request ( now ) : <EOL> "<STR_LIT>" <EOL> HTTPretty . register_uri ( HTTPretty . POST , "<STR_LIT>" , <EOL> body = '<STR_LIT>' ) <EOL> request = urllib2 . Request ( <EOL> '<STR_LIT>' , <EOL> b'<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> ) <EOL> fd = urlopen ( request ) <EOL> got = fd . read ( ) <EOL> fd . close ( ) <EOL> expect ( HTTPretty . last_request . method ) . to . equal ( '<STR_LIT:POST>' ) <EOL> expect ( HTTPretty . last_request . body ) . to . equal ( <EOL> b'<STR_LIT>' , <EOL> ) <EOL> expect ( HTTPretty . last_request . headers [ '<STR_LIT>' ] ) . to . equal ( <EOL> '<STR_LIT>' , <EOL> ) <EOL> expect ( got ) . to . equal ( b'<STR_LIT>' ) <EOL> @ httprettified <EOL> @ within ( two = microseconds ) <EOL> def test_can_inspect_last_request_with_ssl ( now ) : <EOL> "<STR_LIT>" <EOL> HTTPretty . register_uri ( HTTPretty . POST , "<STR_LIT>" , <EOL> body = '<STR_LIT>' ) <EOL> request = urllib2 . Request ( <EOL> '<STR_LIT>' , <EOL> b'<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> ) <EOL> fd = urlopen ( request ) <EOL> got = fd . read ( ) <EOL> fd . close ( ) <EOL> expect ( HTTPretty . last_request . method ) . to . equal ( '<STR_LIT:POST>' ) <EOL> expect ( HTTPretty . last_request . body ) . to . equal ( <EOL> b'<STR_LIT>' , <EOL> ) <EOL> expect ( HTTPretty . last_request . headers [ '<STR_LIT>' ] ) . to . equal ( <EOL> '<STR_LIT>' , <EOL> ) <EOL> expect ( got ) . to . equal ( b'<STR_LIT>' ) <EOL> @ httprettified <EOL> @ within ( two = microseconds ) <EOL> def test_httpretty_ignores_querystrings_from_registered_uri ( ) : <EOL> "<STR_LIT>" <EOL> HTTPretty . register_uri ( HTTPretty . GET , "<STR_LIT>" , <EOL> body = "<STR_LIT>" ) <EOL> fd = urlopen ( '<STR_LIT>' ) <EOL> got = fd . read ( ) <EOL> fd . close ( ) <EOL> expect ( got ) . to . equal ( b'<STR_LIT>' ) <EOL> expect ( HTTPretty . last_request . method ) . to . equal ( '<STR_LIT:GET>' ) <EOL> expect ( HTTPretty . last_request . path ) . to . equal ( '<STR_LIT>' ) <EOL> @ httprettified <EOL> @ within ( two = microseconds ) <EOL> def test_callback_response ( now ) : <EOL> ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def request_callback ( request , uri , headers ) : <EOL> return [ <NUM_LIT:200> , headers , "<STR_LIT>" . format ( decode_utf8 ( request . method ) , uri ) ] <EOL> HTTPretty . register_uri ( <EOL> HTTPretty . GET , "<STR_LIT>" , <EOL> body = request_callback ) <EOL> fd = urllib2 . urlopen ( '<STR_LIT>' ) <EOL> got = fd . read ( ) <EOL> fd . close ( ) <EOL> expect ( got ) . to . equal ( b"<STR_LIT>" ) <EOL> HTTPretty . register_uri ( <EOL> HTTPretty . POST , "<STR_LIT>" , <EOL> body = request_callback ) <EOL> request = urllib2 . Request ( <EOL> "<STR_LIT>" , <EOL> b'<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> ) <EOL> fd = urllib2 . urlopen ( request ) <EOL> got = fd . read ( ) <EOL> fd . close ( ) <EOL> expect ( got ) . to . equal ( b"<STR_LIT>" ) <EOL> @ httprettified <EOL> def test_httpretty_should_allow_registering_regexes ( ) : <EOL> "<STR_LIT>" <EOL> HTTPretty . register_uri ( <EOL> HTTPretty . GET , <EOL> re . compile ( "<STR_LIT>" ) , <EOL> body = "<STR_LIT>" , <EOL> ) <EOL> request = urllib2 . Request ( <EOL> "<STR_LIT>" , <EOL> ) <EOL> fd = urllib2 . urlopen ( request ) <EOL> got = fd . read ( ) <EOL> fd . close ( ) <EOL> expect ( got ) . to . equal ( b"<STR_LIT>" ) </s>
