class TestOpdsRequestHandler(RequestHandlerTestBase):

    def setUp(self):
        self.__initialise_app_mock()
        self.__loader = Jinja2Loader()
        self.__loader.load_template = Mock()
        self.__opds_request_handler = OpdsRequestHandler(self.app, self.__loader)

    def __initialise_app_mock(self):
        RequestHandlerTestBase.initialise_app_mock(self)
        b = self.__get_mock_book()
        self.app.search_books = Mock()
        self.app.get_book_details = Mock(return_value=b)
        self.app.get_book_type = Mock()
        self.app.list_books_by_first_letter = Mock()

    def __get_mock_book(self):
        b = Book()
        b.id = 1414
        b.title = "Title"
        b.author = "An Author"
        b.author_uri = "about:none"
        f = BookFormat()
        b.formats = [f, f, f]
        return b
        
    def test_index_handler_calls_template_loader(self):
        self.__opds_request_handler.index_handler()
        self.assert_called(self.__loader.load_template)

    def test_by_title_handler_calls_template_loader(self):
        self.__opds_request_handler.by_title_handler()
        self.assert_called(self.__loader.load_template)

    def test_first_letter_handler_gives_the_correct_feed_header(self):
        self.__opds_request_handler.first_letter_handler("")
        self.assert_called(self.app.list_books_by_first_letter)
        self.assert_called(self.__loader.load_template)

    def test_book_handler_calls_template_loader(self):
        self.__opds_request_handler.book_handler("1")
        self.assert_called(self.__loader.load_template)

    def test_search_calls_template_loader(self):
        self.__opds_request_handler.search_handler("oo")
        self.assert_called(self.app.search_books)
        self.assert_called(self.__loader.load_template)
Exemple #2
0
class RequestHandler(object):

    def __init__(self, app):
        self.__app = app
        self.__htmlHandler = HtmlRequestHandler(app)
        self.__opdsHandler = OpdsRequestHandler(app, Jinja2Loader())

    def index_handler(self, user_agent):
        if self.__is_opds_browser(user_agent):
            return self.__opdsHandler.index_handler()
        else:
            return self.__htmlHandler.index_handler()

    def by_title_handler(self, user_agent):
        if self.__is_opds_browser(user_agent):
            return self.__opdsHandler.by_title_handler()

    def first_letter_handler(self, user_agent, first_letter):
        if self.__is_opds_browser(user_agent):
            return self.__opdsHandler.first_letter_handler(first_letter)
        else:
            return self.__htmlHandler.first_letter_handler(first_letter)

    def book_handler(self, user_agent, book_id):
        if self.__is_opds_browser(user_agent):
            return self.__opdsHandler.book_handler(book_id)
        else:
            return self.__htmlHandler.book_handler(book_id)

    def download_handler(self, user_agent, book_id, book_format):
        if self.__is_opds_browser(user_agent):
            return self.__opdsHandler.download_handler(book_id, book_format)

    def search_handler(self, user_agent, search_term):
        if self.__is_opds_browser(user_agent):
            return self.__opdsHandler.search_handler(search_term)
        else:
            return self.__htmlHandler.search_handler(search_term)

    def harvest_handler(self):
        return self.__htmlHandler.harvest_handler()

    def set_html_request_handler(self, handler):
        self.__htmlHandler = handler

    def set_opds_request_handler(self, handler):
        self.__opdsHandler = handler

    @staticmethod
    def __is_opds_browser(user_agent):
        #Stanza iPhone/Aldiko/Moon+ Reader(Android)t.app)
        return user_agent.find("Aldiko") > -1