2012-06-18 22:42:25 +00:00
|
|
|
import cStringIO, sys
|
2012-06-19 02:23:22 +00:00
|
|
|
from netlib import wsgi, odict
|
|
|
|
|
|
|
|
|
2014-09-03 15:15:50 +00:00
|
|
|
def tflow():
|
2012-06-19 02:23:22 +00:00
|
|
|
h = odict.ODictCaseless()
|
|
|
|
h["test"] = ["value"]
|
2014-09-03 15:15:50 +00:00
|
|
|
req = wsgi.Request("http", "GET", "/", h, "")
|
|
|
|
return wsgi.Flow(("127.0.0.1", 8888), req)
|
2012-06-18 22:42:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestApp:
|
|
|
|
def __init__(self):
|
|
|
|
self.called = False
|
|
|
|
|
|
|
|
def __call__(self, environ, start_response):
|
|
|
|
self.called = True
|
|
|
|
status = '200 OK'
|
|
|
|
response_headers = [('Content-type', 'text/plain')]
|
|
|
|
start_response(status, response_headers)
|
|
|
|
return ['Hello', ' world!\n']
|
|
|
|
|
|
|
|
|
2012-06-19 02:23:22 +00:00
|
|
|
class TestWSGI:
|
2012-06-18 22:42:25 +00:00
|
|
|
def test_make_environ(self):
|
2012-06-19 02:23:22 +00:00
|
|
|
w = wsgi.WSGIAdaptor(None, "foo", 80, "version")
|
2014-09-03 15:15:50 +00:00
|
|
|
tf = tflow()
|
|
|
|
assert w.make_environ(tf, None)
|
2012-06-18 22:42:25 +00:00
|
|
|
|
2014-09-03 15:15:50 +00:00
|
|
|
tf.request.path = "/foo?bar=voing"
|
|
|
|
r = w.make_environ(tf, None)
|
2012-06-18 22:42:25 +00:00
|
|
|
assert r["QUERY_STRING"] == "bar=voing"
|
|
|
|
|
|
|
|
def test_serve(self):
|
|
|
|
ta = TestApp()
|
2012-06-19 02:23:22 +00:00
|
|
|
w = wsgi.WSGIAdaptor(ta, "foo", 80, "version")
|
2014-09-03 15:15:50 +00:00
|
|
|
f = tflow()
|
|
|
|
f.request.host = "foo"
|
|
|
|
f.request.port = 80
|
2012-06-18 22:42:25 +00:00
|
|
|
|
|
|
|
wfile = cStringIO.StringIO()
|
2014-09-03 15:15:50 +00:00
|
|
|
err = w.serve(f, wfile)
|
2012-06-18 22:42:25 +00:00
|
|
|
assert ta.called
|
|
|
|
assert not err
|
|
|
|
|
|
|
|
val = wfile.getvalue()
|
|
|
|
assert "Hello world" in val
|
|
|
|
assert "Server:" in val
|
|
|
|
|
|
|
|
def _serve(self, app):
|
2012-06-19 02:23:22 +00:00
|
|
|
w = wsgi.WSGIAdaptor(app, "foo", 80, "version")
|
2014-09-03 15:15:50 +00:00
|
|
|
f = tflow()
|
|
|
|
f.request.host = "foo"
|
|
|
|
f.request.port = 80
|
2012-06-18 22:42:25 +00:00
|
|
|
wfile = cStringIO.StringIO()
|
2014-09-03 15:15:50 +00:00
|
|
|
err = w.serve(f, wfile)
|
2012-06-18 22:42:25 +00:00
|
|
|
return wfile.getvalue()
|
|
|
|
|
|
|
|
def test_serve_empty_body(self):
|
|
|
|
def app(environ, start_response):
|
|
|
|
status = '200 OK'
|
|
|
|
response_headers = [('Foo', 'bar')]
|
|
|
|
start_response(status, response_headers)
|
|
|
|
return []
|
|
|
|
assert self._serve(app)
|
|
|
|
|
|
|
|
def test_serve_double_start(self):
|
|
|
|
def app(environ, start_response):
|
|
|
|
try:
|
|
|
|
raise ValueError("foo")
|
|
|
|
except:
|
|
|
|
ei = sys.exc_info()
|
|
|
|
status = '200 OK'
|
|
|
|
response_headers = [('Content-type', 'text/plain')]
|
|
|
|
start_response(status, response_headers)
|
|
|
|
start_response(status, response_headers)
|
|
|
|
assert "Internal Server Error" in self._serve(app)
|
|
|
|
|
|
|
|
def test_serve_single_err(self):
|
|
|
|
def app(environ, start_response):
|
|
|
|
try:
|
|
|
|
raise ValueError("foo")
|
|
|
|
except:
|
|
|
|
ei = sys.exc_info()
|
|
|
|
status = '200 OK'
|
|
|
|
response_headers = [('Content-type', 'text/plain')]
|
|
|
|
start_response(status, response_headers, ei)
|
|
|
|
assert "Internal Server Error" in self._serve(app)
|
|
|
|
|
|
|
|
def test_serve_double_err(self):
|
|
|
|
def app(environ, start_response):
|
|
|
|
try:
|
|
|
|
raise ValueError("foo")
|
|
|
|
except:
|
|
|
|
ei = sys.exc_info()
|
|
|
|
status = '200 OK'
|
|
|
|
response_headers = [('Content-type', 'text/plain')]
|
|
|
|
start_response(status, response_headers)
|
|
|
|
yield "aaa"
|
|
|
|
start_response(status, response_headers, ei)
|
|
|
|
yield "bbb"
|
|
|
|
assert "Internal Server Error" in self._serve(app)
|
|
|
|
|