Example #1
0
    def testHTTPError(self):  # issue147
        def mypost(url, data, headers):
            class Response(object): status_code=500
            if mod.requests.__build__ > 0x020000:
                # Only newer requests exceptions inherit from IOError
                e = mod.requests.exceptions.HTTPError('bad', response=Response())
            else:
                # older inherit from RuntimeError (no kwargs)
                e = mod.requests.exceptions.HTTPError('bad')
            raise e

        mod.requests.post = mypost

        T_ID = 'abcd'
        D = MyDongle(0, 0)
        d = MyMegaDump('YWJjZA==')
        gc = RemoteXMLDatabase('a', 'b', 'c', 0)
        with self.assertRaises(SyncError) as cm:
            gc.sync(D, T_ID, d)
        self.assertEqual(cm.exception.errorstring, 'HTTPError: bad (500)')
Example #2
0
    def testOk(self):
        T_ID = 'abcd'
        D = MyDongle(0, 0)
        d = MyMegaDump('YWJjZA==')
        def mypost(url, data, headers):
            self.assertEqual(url, 'a://b:0/c')
            self.assertEqual(data.decode('utf-8'), """\
<?xml version='1.0' encoding='utf-8'?>
<galileo-client version="2.0"><client-info><client-id>%(id)s</client-id><client-version>%(version)s</client-version><client-mode>sync</client-mode><dongle-version major="%(M)d" minor="%(m)d" /></client-info><tracker tracker-id="%(t_id)s"><data>%(b64dump)s</data></tracker></galileo-client>""" % {
    'id': RemoteXMLDatabase.ID,
    'version': __version__,
    'M': D.major,
    'm': D.minor,
    't_id': T_ID,
    'b64dump': d.toBase64()})
            self.assertEqual(headers['Content-Type'], 'text/xml')
            return requestResponse('<tracker tracker-id="abcd" type="megadumpresponse"><data>ZWZnaA==</data></tracker>')

        mod.requests.post = mypost
        gc = RemoteXMLDatabase('a', 'b', 'c', 0)
        self.assertEqual(gc.sync(D, T_ID, d),
                         [101, 102, 103, 104])