allowing the bypass of an unregistered URI
This commit is contained in:
@@ -33,9 +33,8 @@ from urlparse import urlsplit
|
||||
old_socket = socket.socket
|
||||
old_create_connection = socket.create_connection
|
||||
|
||||
|
||||
class fakesock(object):
|
||||
|
||||
|
||||
class socket(object):
|
||||
_entry = None
|
||||
debuglevel = 0
|
||||
@@ -44,27 +43,45 @@ class fakesock(object):
|
||||
self.type = type
|
||||
self.protocol = protocol
|
||||
self.truesock = old_socket(family, type, protocol)
|
||||
|
||||
self._closed = True
|
||||
self.fd = StringIO()
|
||||
def connect(self, address):
|
||||
|
||||
self._address = (self._host, self._port) = address
|
||||
self._closed = False
|
||||
|
||||
def close(self):
|
||||
if not self._closed:
|
||||
self.truesock.close()
|
||||
self._closed = True
|
||||
|
||||
def makefile(self, mode, bufsize):
|
||||
self._mode = mode
|
||||
self._bufsize = bufsize
|
||||
fd = StringIO()
|
||||
|
||||
if self._entry:
|
||||
self._entry.fill_filekind(fd)
|
||||
self._entry.fill_filekind(self.fd)
|
||||
|
||||
return fd
|
||||
return self.fd
|
||||
|
||||
def _true_sendall(self, data):
|
||||
self.truesock.connect(self._address)
|
||||
self.truesock.sendall(data)
|
||||
_d = self.truesock.recv(255)
|
||||
self.fd.seek(0)
|
||||
self.fd.write(_d)
|
||||
while _d:
|
||||
_d = self.truesock.recv(255)
|
||||
self.fd.write(_d)
|
||||
|
||||
self.fd.seek(0)
|
||||
self.truesock.close()
|
||||
|
||||
def sendall(self, data):
|
||||
verb, headers_string = data.split('\n', 1)
|
||||
try:
|
||||
verb, headers_string = data.split('\n', 1)
|
||||
except ValueError:
|
||||
return self._true_sendall(data)
|
||||
|
||||
method, path, version = re.split('\s+', verb.strip(), 3)
|
||||
|
||||
info = URIInfo(hostname=self._host, port=self._port, path=path)
|
||||
@@ -77,6 +94,7 @@ class fakesock(object):
|
||||
break
|
||||
|
||||
if not entries:
|
||||
self._true_sendall(data)
|
||||
return
|
||||
|
||||
entry = info.get_next_entry()
|
||||
|
||||
@@ -39,19 +39,19 @@ def stop_server(context):
|
||||
HTTPretty.enable()
|
||||
|
||||
@that_with_context(start_server, stop_server)
|
||||
def test_httpretty_bypasses_a_unregistered_request():
|
||||
u"HTTPretty should bypass a unregistered request by disabling it"
|
||||
def test_httpretty_bypasses_when_disabled():
|
||||
u"HTTPretty should bypass all requests by disabling it"
|
||||
|
||||
HTTPretty.register_uri(HTTPretty.GET, "http://localhost:9999/go-for-bubbles/",
|
||||
body="glub glub")
|
||||
|
||||
HTTPretty.disable()
|
||||
|
||||
fd = urllib2.urlopen('http://localhost:9999/go-for-bubbles/')
|
||||
got1 = fd.read()
|
||||
fd.close()
|
||||
|
||||
assert that(got1).equals('glub glub')
|
||||
|
||||
HTTPretty.disable()
|
||||
assert that(got1).equals('. o O 0 O o . o O 0 O o . o O 0 O o . o O 0 O o . o O 0 O o .')
|
||||
|
||||
fd = urllib2.urlopen('http://localhost:9999/come-again/')
|
||||
got2 = fd.read()
|
||||
@@ -68,3 +68,21 @@ def test_httpretty_bypasses_a_unregistered_request():
|
||||
assert that(got3).equals('glub glub')
|
||||
|
||||
|
||||
@that_with_context(start_server, stop_server)
|
||||
def test_httpretty_bypasses_a_unregistered_request():
|
||||
u"HTTPretty should bypass a unregistered request by disabling it"
|
||||
|
||||
HTTPretty.register_uri(HTTPretty.GET, "http://localhost:9999/go-for-bubbles/",
|
||||
body="glub glub")
|
||||
|
||||
fd = urllib2.urlopen('http://localhost:9999/go-for-bubbles/')
|
||||
got1 = fd.read()
|
||||
fd.close()
|
||||
|
||||
assert that(got1).equals('glub glub')
|
||||
|
||||
fd = urllib2.urlopen('http://localhost:9999/come-again/')
|
||||
got2 = fd.read()
|
||||
fd.close()
|
||||
|
||||
assert that(got2).equals('<- HELLO WORLD ->')
|
||||
|
||||
@@ -68,6 +68,8 @@ class Server(object):
|
||||
args = (app, self.port, data)
|
||||
self.process = Process(target=go, args=args)
|
||||
self.process.start()
|
||||
import time;
|
||||
time.sleep(0.4)
|
||||
|
||||
def stop(self):
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user