Functional Python Programming

(Wang) #1
Chapter 15

This will ensure that our routing works as expected. It's important to be able to
test this separately from the rest of the WSGI application. Testing a complete web
server means starting the server process and then trying to connect with a browser
or a test tool, such as Postman or Selenium. Visit http://www.getpostman.com or
http://www.seleniumhq.org to get more information on the usage of Postman and
Selenium. We prefer to test each feature in isolation.


Here's the overall WSGI application, with two lines of command highlighted:


import traceback


import urllib


def anscombe_app(environ, start_response):


log= environ['wsgi.errors']


try:


match= path_pat.match(environ['PATH_INFO'])


set_id= match.group('dataset').upper()


query= urllib.parse.parse_qs(environ['QUERY_STRING'])


print(environ['PATH_INFO'], environ['QUERY_STRING'],
match.groupdict(), file=log)


log.flush()


dataset= anscombe_filter(set_id, raw_data())


content, mime= serialize(query['form'][0], set_id, dataset)


headers= [


('Content-Type', mime),
('Content-Length', str(len(content))), ]


start_response(""200 OK"", headers)


return [content]


except Exception as e:


traceback.print_exc(file=log)


tb= traceback.format_exc()


page= error_page.substitute(title=""Error"",
message=repr(e), traceback=tb)


content= page.encode(""utf-8"")


headers = [


('Content-Type', ""text/html""),
('Content-Length', str(len(content))),
]


start_response(""404 NOT FOUND"", headers)


return [content]

Free download pdf