diff --git a/__pycache__/bookapp.cpython-37.pyc b/__pycache__/bookapp.cpython-37.pyc new file mode 100644 index 0000000..730cce4 Binary files /dev/null and b/__pycache__/bookapp.cpython-37.pyc differ diff --git a/__pycache__/bookdb.cpython-37.pyc b/__pycache__/bookdb.cpython-37.pyc new file mode 100644 index 0000000..3a66262 Binary files /dev/null and b/__pycache__/bookdb.cpython-37.pyc differ diff --git a/__pycache__/tests.cpython-37.pyc b/__pycache__/tests.cpython-37.pyc new file mode 100644 index 0000000..e9d5b3b Binary files /dev/null and b/__pycache__/tests.cpython-37.pyc differ diff --git a/bookapp.py b/bookapp.py index d2284c6..a05d5f7 100644 --- a/bookapp.py +++ b/bookapp.py @@ -1,4 +1,5 @@ import re +import traceback from bookdb import BookDB @@ -6,18 +7,69 @@ def book(book_id): - return "

a book with id %s

" % book_id - + page = """ +

{title}

+ + + + +
Author{author}
Publisher{publisher}
ISBN{isbn}
+Back to the list +""" + book = DB.title_info(book_id) + if book is None: + raise NameError + return page.format(**book) + def books(): - return "

a list of books

" + all_books = DB.titles() + body = ['

My Bookshelf

', '') + return '\n'.join(body) + + +def resolve_path(path): + funcs = { + '': books, + 'book': book, + } + + path = path.strip('/').split('/') + + func_name = path[0] + args = path[1:] + + try: + func = funcs[func_name] + except KeyError: + raise NameError + return func, args def application(environ, start_response): - status = "200 OK" - headers = [('Content-type', 'text/html')] - start_response(status, headers) - return ["

No Progress Yet

".encode('utf8')] + headers = [("Content-type", "text/html")] + try: + path = environ.get('PATH_INFO', None) + if path is None: + raise NameError + func, args = resolve_path(path) + body = func(*args) + status = "200 OK" + except NameError: + status = "404 Not Found" + body = "

Not Found

" + except Exception: + status = "500 Internal Server Error" + body = "

Internal Server Error

" + print(traceback.format_exc()) + finally: + headers.append(('Content-length', str(len(body)))) + start_response(status, headers) + return [body.encode('utf8')] if __name__ == '__main__': diff --git a/do_Testing.bat b/do_Testing.bat new file mode 100644 index 0000000..6497dbb --- /dev/null +++ b/do_Testing.bat @@ -0,0 +1,4 @@ +python -m unittest -vv tests.py > testing_results.txt 2>&1 +type testing_results.txt + + diff --git a/testing_results.txt b/testing_results.txt new file mode 100644 index 0000000..b0e8470 --- /dev/null +++ b/testing_results.txt @@ -0,0 +1,21 @@ +test_all_titles_correct (tests.BookDBTestCase) ... ok +test_all_titles_returned (tests.BookDBTestCase) ... ok +test_title_info_complete (tests.BookDBTestCase) ... ok +test_title_info_correct (tests.BookDBTestCase) ... ok +test_all_ids_have_results (tests.BookTestCase) ... ok +test_bad_id_raises_name_error (tests.BookTestCase) ... ok +test_id_returns_correct_results (tests.BookTestCase) ... ok +test_all_book_ids_in_result (tests.BooksTestCase) ... ok +test_all_book_titles_in_result (tests.BooksTestCase) ... ok +test_bad_path_raises_name_error (tests.ResolvePathTestCase) ... ok +test_book_path_returns_book_function (tests.ResolvePathTestCase) ... ok +test_book_path_returns_bookid_in_args (tests.ResolvePathTestCase) ... ok +test_root_returns_books_function (tests.ResolvePathTestCase) +verify that the correct function is returned by the root path ... ok +test_root_returns_no_args (tests.ResolvePathTestCase) +verify that no args are returned for the root path ... ok + +---------------------------------------------------------------------- +Ran 14 tests in 0.006s + +OK diff --git a/wsgi_1.py b/wsgi_1.py index 85498d1..fc1f9da 100644 --- a/wsgi_1.py +++ b/wsgi_1.py @@ -21,21 +21,18 @@ def application(environ, start_response): response_body = body.format( software=environ.get('SERVER_SOFTWARE', default), - path="aaaa", - month="bbbb", - date="cccc", - year="dddd", - client_ip="eeee" + path=environ.get('PATH_INFO', default), + month=datetime.datetime.now().strftime('%B'), + date=datetime.datetime.now().day, + year=datetime.datetime.now().year, + client_ip=environ.get('REMOTE_ADDR', default), ) status = '200 OK' - response_headers = [('Content-Type', 'text/html'), ('Content-Length', str(len(response_body)))] start_response(status, response_headers) - return [response_body.encode('utf8')] - if __name__ == '__main__': from wsgiref.simple_server import make_server srv = make_server('localhost', 8080, application)