'Test /get'

def test_get(self):

    with self.create_server() as server:
        db = server.handler.router.ctx.library_broker.get(None)
        conn = server.connect()
        def get(what, book_id, library_id=None, q=u''):
            q = ((u'?' + q) if q else q)
            conn.request(u'GET', (((u'/get/%s/%s' % (what, book_id)) + ((u'/' + library_id) if library_id else u'')) + q))
            r = conn.getresponse()
            return (r, r.read())
        def bad(*args):
            (r, data) = get(*args)
            self.ae(r.status, httplib.NOT_FOUND)
        bad(u'xxx', 1)
        bad(u'fmt1', 10)
        bad(u'fmt1', 1, u'zzzz')
        bad(u'fmt1', u'xx')
        (r, data) = get(u'fmt1', 1, db.server_library_id)
        self.ae(data, db.format(1, u'fmt1'))
        self.assertIsNotNone(r.getheader(u'Content-Disposition'))
        self.ae(r.getheader(u'Used-Cache'), u'no')
        (r, data) = get(u'fmt1', 1)
        self.ae(data, db.format(1, u'fmt1'))
        self.ae(r.getheader(u'Used-Cache'), u'yes')
        raw = P(u'quick_start/eng.epub', data=True)
        (r, data) = get(u'epub', 1)
        self.ae(r.status, httplib.OK)
        etag = r.getheader(u'ETag')
        self.assertIsNotNone(etag)
        self.ae(r.getheader(u'Used-Cache'), u'no')
        self.assertTrue(data.startswith('PK'))
        self.assertGreaterEqual(len(data), len(raw))
        db.set_field(u'title', {1: u'changed'})
        (r, data) = get(u'epub', 1)
        self.assertNotEqual(r.getheader(u'ETag'), etag)
        etag = r.getheader(u'ETag')
        self.ae(r.getheader(u'Used-Cache'), u'no')
        mi = get_metadata(BytesIO(data), extract_cover=False)
        self.ae(mi.title, u'changed')
        (r, data) = get(u'epub', 1)
        self.ae(r.getheader(u'Used-Cache'), u'yes')
        import calibre.library.save_to_disk as c
        (orig, c.DEBUG) = (c.DEBUG, False)
        try:
            db.set_pref(u'plugboards', {u'epub': {u'content_server': [[u'changed,    {title}', u'title']]}})
            db.set_field(u'title', {1: u'again'})
            (r, data) = get(u'epub', 1)
            self.assertNotEqual(r.getheader(u'ETag'), etag)
            etag = r.getheader(u'ETag')
            self.ae(r.getheader(u'Used-Cache'), u'no')
            mi = get_metadata(BytesIO(data), extract_cover=False)
            self.ae(mi.title, u'changed,    again')
        finally:
            c.DEBUG = orig
        def change_cover(count, book_id=2):
            cpath = db.format_abspath(book_id, u'__COVER_INTERNAL__')
            db.set_cover({2: I(u'lt.png', data=True)})
            t = ((time.time() + 1) + count)
            os.utime(cpath, (t, t))
        (r, data) = get(u'cover', 1)
        self.ae(r.status, httplib.OK)
        self.ae(data, db.cover(1))
        self.ae(r.getheader(u'Used-Cache'), u'no')
        self.ae(r.getheader(u'Content-Type'), u'image/jpeg')
        (r, data) = get(u'cover', 1)
        self.ae(r.status, httplib.OK)
        self.ae(data, db.cover(1))
        self.ae(r.getheader(u'Used-Cache'), u'yes')
        (r, data) = get(u'cover', 3)
        self.ae(r.status, httplib.OK)
        (r, data) = get(u'thumb', 1)
        self.ae(r.status, httplib.OK)
        self.ae(identify(data), (u'jpeg', 60, 60))
        self.ae(r.getheader(u'Used-Cache'), u'no')
        (r, data) = get(u'thumb', 1)
        self.ae(r.status, httplib.OK)
        self.ae(r.getheader(u'Used-Cache'), u'yes')
        (r, data) = get(u'thumb', 1, q=u'sz=100')
        self.ae(r.status, httplib.OK)
        self.ae(identify(data), (u'jpeg', 100, 100))
        self.ae(r.getheader(u'Used-Cache'), u'no')
        (r, data) = get(u'thumb', 1, q=u'sz=100x100')
        self.ae(r.status, httplib.OK)
        self.ae(r.getheader(u'Used-Cache'), u'yes')
        change_cover(1, 1)
        (r, data) = get(u'thumb', 1, q=u'sz=100')
        self.ae(r.status, httplib.OK)
        self.ae(identify(data), (u'jpeg', 100, 100))
        self.ae(r.getheader(u'Used-Cache'), u'no')
        (r, data) = get(u'cover', 2)
        self.ae(r.status, httplib.OK)
        self.ae(data, db.cover(2))
        self.ae(r.getheader(u'Used-Cache'), u'no')
        path = binascii.unhexlify(r.getheader(u'Tempfile')).decode(u'utf-8')
        (f, fdata) = (share_open(path, u'rb'), data)
        change_cover(1)
        (r, data) = get(u'cover', 2)
        self.ae(r.status, httplib.OK)
        self.ae(data, db.cover(2))
        self.ae(r.getheader(u'Used-Cache'), u'no')
        path = binascii.unhexlify(r.getheader(u'Tempfile')).decode(u'utf-8')
        (f2, f2data) = (share_open(path, u'rb'), data)
        change_cover(2)
        (r, data) = get(u'cover', 2)
        self.ae(r.status, httplib.OK)
        self.ae(data, db.cover(2))
        self.ae(r.getheader(u'Used-Cache'), u'no')
        self.ae(f.read(), fdata)
        self.ae(f2.read(), f2data)
        (r, data) = get(u'opf', 1)
        self.ae(r.status, httplib.OK)
        self.ae(r.getheader(u'Content-Type'), u'application/oebps-package+xml;    charset=UTF-8')
        self.assertIsNotNone(r.getheader(u'Last-Modified'))
        opf = OPF(BytesIO(data), populate_spine=False, try_to_guess_cover=False)
        self.ae(db.field_for(u'title', 1), opf.title)
        self.ae(db.field_for(u'authors', 1), tuple(opf.authors))
        conn.request(u'GET', u'/get/opf/1', headers={u'Accept-Encoding': u'gzip'})
        r = conn.getresponse()
        (self.ae(r.status, httplib.OK), self.ae(r.getheader(u'Content-Encoding'), u'gzip'))
        raw = r.read()
        self.ae(zlib.decompress(raw, (16 + zlib.MAX_WBITS)), data)
        (r, data) = get(u'json', 1)
        self.ae(r.status, httplib.OK)
        self.ae(db.field_for(u'title', 1), json.loads(data)[u'title'])
        conn.request(u'GET', u'/get/json/1', headers={u'Accept-Encoding': u'gzip'})
        r = conn.getresponse()
        (self.ae(r.status, httplib.OK), self.ae(r.getheader(u'Content-Encoding'), u'gzip'))
        raw = r.read()
        self.ae(zlib.decompress(raw, (16 + zlib.MAX_WBITS)), data)
