|
|
@@ -17,11 +17,13 @@ class FakeHTTPRequest(object): |
|
|
|
|
|
|
|
code = 200 |
|
|
|
|
|
|
|
def __init__(self, meth, uri): |
|
|
|
def __init__(self, meth, uri, data): |
|
|
|
|
|
|
|
#self.requestHeaders = Headers() |
|
|
|
self.responseHeaders = Headers() |
|
|
|
|
|
|
|
self.content = StringIO(data) |
|
|
|
|
|
|
|
self.path = uri |
|
|
|
self.prepath = [] |
|
|
|
self.postpath = uri.split('/') |
|
|
@@ -44,9 +46,11 @@ class FakeHTTPRequest(object): |
|
|
|
def isSecure(self): |
|
|
|
return False |
|
|
|
|
|
|
|
#def processingFailed(self, failure): |
|
|
|
# print 'f:', `failure` |
|
|
|
# print 'b:', failure.getTraceback() |
|
|
|
def processingFailed(self, failure): |
|
|
|
self.setResponseCode(500, 'Internal Server Error') |
|
|
|
|
|
|
|
print 'f:', `failure` |
|
|
|
print 'b:', failure.getTraceback() |
|
|
|
|
|
|
|
def _cleanup(self): |
|
|
|
for d in self.notifications: |
|
|
@@ -61,9 +65,6 @@ class FakeHTTPRequest(object): |
|
|
|
|
|
|
|
self._cleanup() |
|
|
|
|
|
|
|
#def processingFailed(self, failure): |
|
|
|
# print 'pf:', failure.getTraceback() |
|
|
|
|
|
|
|
def notifyFinish(self): |
|
|
|
self.notifications.append(Deferred()) |
|
|
|
return self.notifications[-1] |
|
|
@@ -75,7 +76,7 @@ class FakeRequestsResponse(object): |
|
|
|
req.write = self.write |
|
|
|
|
|
|
|
def _finished(self, arg): |
|
|
|
if arg is not None: |
|
|
|
if arg is not None: # pragma: no cover |
|
|
|
raise NotImplementedError('cannot handle exceptions yet') |
|
|
|
|
|
|
|
self.status_code = self._req.code |
|
|
@@ -116,13 +117,11 @@ class FakeRequests(object): |
|
|
|
self._app = app |
|
|
|
self._res = app.resource() |
|
|
|
|
|
|
|
def get(self, url): |
|
|
|
'''Return a response for the passed in url.''' |
|
|
|
|
|
|
|
def _makerequest(self, method, url, data=''): |
|
|
|
if url[0] != '/': |
|
|
|
raise ValueError('url must be absolute (start w/ a slash)') |
|
|
|
|
|
|
|
req = FakeHTTPRequest('GET', url) |
|
|
|
req = FakeHTTPRequest('GET', url, data) |
|
|
|
resp = FakeRequestsResponse(req) |
|
|
|
|
|
|
|
req.notifyFinish().addBoth(resp._finished) |
|
|
@@ -131,8 +130,20 @@ class FakeRequests(object): |
|
|
|
|
|
|
|
return resp |
|
|
|
|
|
|
|
def get(self, url): |
|
|
|
'''Return a response for the passed in url.''' |
|
|
|
|
|
|
|
return self._makerequest('GET', url) |
|
|
|
|
|
|
|
def put(self, url, data=''): |
|
|
|
'''Make a put request to the provied URL w/ the body of data.''' |
|
|
|
|
|
|
|
return self._makerequest('PUT', url, data) |
|
|
|
|
|
|
|
class TestFakeRequests(unittest.TestCase): |
|
|
|
def setUp(self): |
|
|
|
self.putdata = [] |
|
|
|
|
|
|
|
app = Klein() |
|
|
|
|
|
|
|
@app.route('/') |
|
|
@@ -141,6 +152,17 @@ class TestFakeRequests(unittest.TestCase): |
|
|
|
|
|
|
|
return 'hello' |
|
|
|
|
|
|
|
@app.route('/500') |
|
|
|
def causeerror(request): |
|
|
|
raise ValueError('random exception') |
|
|
|
|
|
|
|
@app.route('/put') |
|
|
|
def putreq(request): |
|
|
|
self.putdata.append(request.content.read()) |
|
|
|
|
|
|
|
request.setResponseCode(201) |
|
|
|
return '' |
|
|
|
|
|
|
|
@app.route('/404') |
|
|
|
def notfound(request): |
|
|
|
request.setResponseCode(404) |
|
|
@@ -160,3 +182,14 @@ class TestFakeRequests(unittest.TestCase): |
|
|
|
r = self.requests.get('/404') |
|
|
|
self.assertEqual(r.status_code, 404) |
|
|
|
|
|
|
|
r = self.requests.get('/nonexistent') |
|
|
|
self.assertEqual(r.status_code, 404) |
|
|
|
|
|
|
|
r = self.requests.get('/500') |
|
|
|
self.assertEqual(r.status_code, 500) |
|
|
|
|
|
|
|
body = 'body' |
|
|
|
r = self.requests.put('/put', data=body) |
|
|
|
self.assertEqual(r.status_code, 201) |
|
|
|
self.assertEqual(r.text, '') |
|
|
|
self.assertEqual(''.join(self.putdata), body) |