コード例 #1
0
ファイル: int.py プロジェクト: MaxCDN/python-maxcdn
class MaxCDNIntegration(unittest.TestCase):
    def setUp(self):
        self.alias = os.environ["ALIAS"]
        self.key = os.environ["KEY"]
        self.secret = os.environ["SECRET"]
        self.time = str(int(time.mktime(time.gmtime())))

        self.max = MaxCDN(self.alias, self.key, self.secret)

    def test_get(self):
        for end_point in ["account.json",
                          "account.json/address",
                          "users.json",
                          "zones.json"]:
            if "/" in end_point:
                key = end_point.split("/")[1]
            else:
                key = end_point.replace(".json", "")

            rsp = self.max.get(end_point)
            self.assertTrue(rsp["data"][key], "get " + key + " with data")

    def test_get_logs(self):
        rsp = self.max.get("v3/reporting/logs.json")
        self.assertTrue(rsp["next_page_key"],
                        "get v3/reporting/logs.json with data")

    def test_post_and_delete(self):
        data = {"name": self.time, "url": "http://www.example.com/"}
        res = self.max.post("/zones/pull.json", data=data)
        zid = str(res["data"]["pullzone"]["id"])

        rsp = self.max.delete("/zones/pull.json/" + zid)
        self.assertTrue(zid, "post")
        self.assertEqual(200, rsp["code"], "delete")

    def test_put(self):
        street = self.time + "_put"
        rsp = self.max.put("/account.json/address", {"street1": street})
        self.assertEqual(street, str(rsp["data"]["address"]["street1"]))

    def test_purge(self):
        rsp = self.max.get("zones/pull.json")
        zones = rsp["data"]["pullzones"]
        zone = zones[len(zones) - 1]["id"]

        rsp = self.max.purge(zone)
        self.assertEqual(200, rsp["code"])

        rsp = self.max.get("reports/popularfiles.json")
        popularfiles = rsp["data"]["popularfiles"]

        rsp = self.max.purge(zone, popularfiles[0]["uri"])
        self.assertEqual(200, rsp["code"])

        files = [popularfiles[0]["uri"], popularfiles[1]["uri"]]
        rsp = self.max.purge(zone, files)
        self.assertEqual(200, rsp["code"])
コード例 #2
0
ファイル: int.py プロジェクト: sladjan2015/python-maxcdn
class MaxCDNIntegration(unittest.TestCase):
    def setUp(self):
        self.alias = os.environ["ALIAS"]
        self.key = os.environ["KEY"]
        self.secret = os.environ["SECRET"]
        self.time = str(int(time.mktime(time.gmtime())))

        self.max = MaxCDN(self.alias, self.key, self.secret)

    def test_get(self):
        for end_point in [
                "account.json", "account.json/address", "users.json",
                "zones.json"
        ]:
            if "/" in end_point:
                key = end_point.split("/")[1]
            else:
                key = end_point.replace(".json", "")

            rsp = self.max.get(end_point)
            self.assertTrue(rsp["data"][key], "get " + key + " with data")

    def test_get_logs(self):
        rsp = self.max.get("v3/reporting/logs.json")
        self.assertTrue(rsp["next_page_key"],
                        "get v3/reporting/logs.json with data")

    def test_post_and_delete(self):
        data = {"name": self.time, "url": "http://www.example.com/"}
        res = self.max.post("/zones/pull.json", data=data)
        zid = str(res["data"]["pullzone"]["id"])

        rsp = self.max.delete("/zones/pull.json/" + zid)
        self.assertTrue(zid, "post")
        self.assertEqual(200, rsp["code"], "delete")

    def test_put(self):
        street = self.time + "_put"
        rsp = self.max.put("/account.json/address", {"street1": street})
        self.assertEqual(street, str(rsp["data"]["address"]["street1"]))

    def test_purge(self):
        rsp = self.max.get("zones/pull.json")
        zones = rsp["data"]["pullzones"]
        zone = zones[len(zones) - 1]["id"]

        rsp = self.max.purge(zone)
        self.assertEqual(200, rsp["code"])

        rsp = self.max.get("reports/popularfiles.json")
        popularfiles = rsp["data"]["popularfiles"]

        rsp = self.max.purge(zone, popularfiles[0]["uri"])
        self.assertEqual(200, rsp["code"])

        files = [popularfiles[0]["uri"], popularfiles[1]["uri"]]
        rsp = self.max.purge(zone, files)
        self.assertEqual(200, rsp["code"])
コード例 #3
0
from maxcdn import MaxCDN

api = MaxCDN("windsmobi", "", "")

# Purge All Cache
api.delete("/zones/pull.json/470828/cache")
コード例 #4
0
ファイル: test.py プロジェクト: sladjan2015/python-maxcdn
class MaxCDNTests(unittest.TestCase):
    def setUp(self):
        self.alias = "test_alias"
        self.key = "test_key"
        self.secret = "test_secret"
        self.server = "rws.example.com"
        self.maxcdn = MaxCDN(self.alias,
                             self.key,
                             self.secret,
                             server=self.server)

    def test_init(self):
        self.assertTrue(self.maxcdn)
        self.assertEqual(self.maxcdn.url, "https://rws.example.com/test_alias")

    def test_get_url(self):
        self.assertEqual(self.maxcdn._get_url("/foo.json"),
                         "https://rws.example.com/test_alias/foo.json")

        self.assertEqual(self.maxcdn._get_url("foo.json"),
                         "https://rws.example.com/test_alias/foo.json")

    def test_data_request(self):
        for meth in ["post", "put", "delete"]:
            requests.Session.request = mock.create_autospec(
                mock_request, return_value=response(meth))

            data = {"foo": "bar"}
            rsp = self.maxcdn._data_request(meth, meth + ".json", data=data)
            expected = {"code": 200, "method": meth, "data": {"foo": "bar"}}
            self.assertEqual(rsp, expected)

            requests.Session.request = mock.create_autospec(
                mock_request, return_value=error_response())
        with self.assertRaises(MaxCDN.ServerError, None):
            self.maxcdn._data_request(meth,
                                      meth + ".json",
                                      data={"foo": "bar"})

    def test_get(self):
        requests.Session.request = mock.create_autospec(
            mock_request, return_value=response("get"))

        expected = {"code": 200, "method": "get", "data": {"foo": "bar"}}
        self.assertEqual(self.maxcdn.get("/get.json"), expected)

        requests.Session.request = mock.create_autospec(
            mock_request, return_value=error_response())
        with self.assertRaises(MaxCDN.ServerError, None):
            self.maxcdn.get("/get.json")

    def test_post(self):
        requests.Session.request = mock.create_autospec(
            mock_request, return_value=response("post"))

        rsp = self.maxcdn.post("/post.json", data={"foo": "bar"})
        expected = {"code": 200, "method": "post", "data": {"foo": "bar"}}
        self.assertEqual(rsp, expected)

        rsp = self.maxcdn.post("/post.json", params={"foo": "bar"})
        self.assertEqual(rsp, expected)

        rsp = self.maxcdn.post("/post.json", params="foo=bar")
        self.assertEqual(rsp, expected)

    def test_put(self):
        requests.Session.request = mock.create_autospec(
            mock_request, return_value=response("put"))

        expected = {"code": 200, "method": "put", "data": {"foo": "bar"}}
        self.assertEqual(self.maxcdn.put("/put.json"), expected)

    def test_delete(self):
        requests.Session.request = mock.create_autospec(
            mock_request, return_value=response("delete"))

        expected = {"code": 200, "method": "delete", "data": {"foo": "bar"}}
        self.assertEqual(self.maxcdn.delete("/delete.json"), expected)

        rsp = self.maxcdn.patch("/delete.json", file_or_files="/foo.css")
        self.assertEqual(rsp, expected)

        files = ["/foo.css", "/bar.css"]
        expected = {"code": 200, "method": "delete", "data": {"foo": "bar"}}
        rsp = self.maxcdn.patch("/delete.json", file_or_files=files)
        self.assertEqual(rsp, expected)

    def test_purge(self):
        requests.Session.request = mock.create_autospec(
            mock_request, return_value=response("delete"))

        expected = {"code": 200, "method": "delete", "data": {"foo": "bar"}}
        self.assertEqual(self.maxcdn.purge(12345), expected)

        self.assertEqual(self.maxcdn.purge(12345, "/master.css"), expected)

        files = ["/master.css", "/other.css"]
        self.assertEqual(self.maxcdn.purge(12345, files), expected)
コード例 #5
0
ファイル: test.py プロジェクト: dave-shawley/python-maxcdn
class MaxCDNTests(unittest.TestCase):

    def setUp(self):
        self.alias  = "test_alias"
        self.key    = "test_key"
        self.secret = "test_secret"
        self.server = "rws.example.com"
        self.maxcdn = MaxCDN(self.alias, self.key, self.secret, server=self.server)

    def test_init(self):
        self.assertTrue(self.maxcdn)
        self.assertEqual(self.maxcdn.url, "https://rws.example.com/test_alias")

    def test_get_url(self):
        self.assertEqual(self.maxcdn._get_url("/foo.json"),
                "https://rws.example.com/test_alias/foo.json")

        self.assertEqual(self.maxcdn._get_url("foo.json"),
                "https://rws.example.com/test_alias/foo.json")

    def test_data_request(self):
        for meth in [ "post", "put", "delete" ]:
            requests.Session.request = mock.create_autospec(mock_request,
                    return_value=response(meth))
            self.assertEqual(self.maxcdn._data_request(meth, meth+".json",
                data={"foo":"bar"}), { "code": 200, "method": meth, "data": { "foo":"bar" } })

    def test_get(self):
        requests.Session.request = mock.create_autospec(mock_request,
                return_value=response("get"))
        self.assertEqual(self.maxcdn.get("/get.json"),
                { "code": 200, "method": "get", "data": { "foo":"bar" } })

    def test_post(self):
        requests.Session.request = mock.create_autospec(mock_request,
                return_value=response("post"))
        self.assertEqual(self.maxcdn.post("/post.json", data={ "foo": "bar" }),
                { "code": 200, "method": "post", "data": { "foo":"bar" } })
        self.assertEqual(self.maxcdn.post("/post.json", params={ "foo": "bar" }),
                { "code": 200, "method": "post", "data": { "foo":"bar" } })
        self.assertEqual(self.maxcdn.post("/post.json", params="foo=bar"),
                { "code": 200, "method": "post", "data": { "foo":"bar" } })

    def test_put(self):
        requests.Session.request = mock.create_autospec(mock_request,
                return_value=response("put"))
        self.assertEqual(self.maxcdn.put("/put.json"),
                { "code": 200, "method": "put", "data": { "foo":"bar" } })

    def test_purge(self):
        requests.Session.request = mock.create_autospec(mock_request,
                return_value=response("purge"))
        self.assertEqual(self.maxcdn.purge("/purge.json"),
                { "code": 200, "method": "purge", "data": { "foo":"bar" } })

    def test_delete(self):
        requests.Session.request = mock.create_autospec(mock_request,
                return_value=response("delete"))
        self.assertEqual(self.maxcdn.delete("/delete.json"),
                { "code": 200, "method": "delete", "data": { "foo":"bar" } })

        self.assertEqual(self.maxcdn.patch("/delete.json",
                file_or_files="/foo.css"),
                { "code": 200, "method": "delete", "data": { "foo":"bar" } })

        self.assertEqual(self.maxcdn.patch("/delete.json",
                file_or_files=["/foo.css", "/bar.css"]),
                { "code": 200, "method": "delete", "data": { "foo":"bar" } })

    def test_purge(self):
        requests.Session.request = mock.create_autospec(mock_request,
                return_value=response("delete"))
        self.assertEqual(self.maxcdn.purge(12345),
                { "code": 200, "method": "delete", "data": { "foo":"bar" } })
        self.assertEqual(self.maxcdn.purge(12345, "/master.css"),
                { "code": 200, "method": "delete", "data": { "foo":"bar" } })
        self.assertEqual(self.maxcdn.purge(12345, ["/master.css", "/other.css"]),
                { "code": 200, "method": "delete", "data": { "foo":"bar" } })
コード例 #6
0
from maxcdn import MaxCDN

api = MaxCDN('windsmobi', '', '')

# Purge All Cache
api.delete('/zones/pull.json/470828/cache')
print('MaxCDN cache purged')