1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
import io
import wsgiref.util
from webview.wsgi import send_simple_text, Routing, StaticContentsApp
def make_wsgi_get(app, path):
environ = {
'SCRIPT_NAME': '',
'PATH_INFO': path
}
wsgiref.util.setup_testing_defaults(environ)
resp_status = None
def sr(status, headers):
nonlocal resp_status
resp_status = status
resp_body = b''.join(app(environ, sr))
return resp_status, resp_body
def basic_app(response):
def app(environ, start_response):
return send_simple_text(environ, start_response, "200 OK", response)
return app
def test_routing_simple():
app = Routing({
'/': basic_app("root"),
'/spam': basic_app("spam"),
'/eggs': basic_app("eggs"),
'/spam/eggs': basic_app("vikings"),
})
assert make_wsgi_get(app, "/") == ('200 OK', b'root')
assert make_wsgi_get(app, "/spam") == ('200 OK', b'spam')
assert make_wsgi_get(app, "/eggs") == ('200 OK', b'eggs')
assert make_wsgi_get(app, "/spam/eggs") == ('200 OK', b'vikings')
assert make_wsgi_get(app, "/nope") == ('200 OK', b'root')
def test_routing_404():
app = Routing({
'/spam': basic_app("spam"),
'/eggs': basic_app("eggs"),
})
assert make_wsgi_get(app, "/spam") == ('200 OK', b'spam')
assert make_wsgi_get(app, "/eggs") == ('200 OK', b'eggs')
assert make_wsgi_get(app, "/nope")[0] == '404 Not Found'
def test_basic_static():
class Echo(StaticContentsApp):
def open(self, path):
return io.BytesIO(path.encode('utf-8'))
app = Echo()
assert make_wsgi_get(app, "/spam") == ('200 OK', b'/spam')
assert make_wsgi_get(app, "/eggs") == ('200 OK', b'/eggs')
assert make_wsgi_get(app, "/foo.txt") == ('200 OK', b'/foo.txt')
|