forked from qiniu/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
336 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# -*- coding: utf-8 -*- | ||
import unittest | ||
|
||
import auth | ||
import config | ||
|
||
def round_tripper(client, method, path, body): | ||
pass | ||
|
||
class ClsTestClient(auth.Client): | ||
def round_tripper(self, method, path, body): | ||
round_tripper(self, method, path, body) | ||
return super(ClsTestClient, self).round_tripper(method, path, body) | ||
|
||
client = ClsTestClient(config.RS_HOST) | ||
|
||
class TestClient(unittest.TestCase): | ||
def test_call(self): | ||
global round_tripper | ||
|
||
def tripper(client, method, path, body): | ||
self.assertEqual(path, "/hello") | ||
self.assertIsNone(body) | ||
|
||
round_tripper = tripper | ||
client.call("/hello") | ||
|
||
def test_call_with(self): | ||
global round_tripper | ||
def tripper(client, method, path, body): | ||
self.assertEqual(body, "body") | ||
|
||
round_tripper = tripper | ||
client.call_with("/hello", "body") | ||
|
||
def test_call_with_multipart(self): | ||
global round_tripper | ||
def tripper(client, method, path, body): | ||
self.assertEqual(len(body), client._header["Content-Length"]) | ||
target_type = "multipart/form-data" | ||
self.assertTrue(client._header["Content-Type"].startswith(target_type)) | ||
start_index = client._header["Content-Type"].find("boundary") | ||
boundary = client._header["Content-Type"][start_index + 9: ] | ||
dispostion = 'Content-Disposition: form-data; name="auth"' | ||
tpl = "--%s\r\n%s\r\n\r\n%s\r\n--%s--\r\n" % (boundary, dispostion, | ||
"auth_string", boundary) | ||
self.assertEqual(tpl, body) | ||
|
||
round_tripper = tripper | ||
client.call_with_multipart("/hello", fields=[("auth", "auth_string")]) | ||
|
||
def test_call_with_form(self): | ||
global round_tripper | ||
def tripper(client, method, path, body): | ||
self.assertEqual(body, "action=a&op=a&op=b") | ||
target_type = "application/x-www-form-urlencoded" | ||
self.assertEqual(client._header["Content-Type"], target_type) | ||
self.assertEqual(client._header["Content-Length"], len(body)) | ||
|
||
round_tripper = tripper | ||
client.call_with_form("/hello", dict(op=["a", "b"], action="a")) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# -*- coding: utf-8 -*- | ||
import unittest | ||
import config | ||
import os | ||
import json | ||
from base64 import urlsafe_b64decode as decode | ||
from base64 import urlsafe_b64encode as encode | ||
from hashlib import sha1 | ||
import hmac | ||
|
||
import auth | ||
import auth_token | ||
|
||
config.ACCESS_KEY = os.getenv("QINIU_ACCESS_KEY") | ||
config.SECRET_KEY = os.getenv("QINIU_SECRET_KEY") | ||
bucket_name = os.getenv("QINIU_BUCKET_NAME") | ||
|
||
class TestToken(unittest.TestCase): | ||
def test_put_policy(self): | ||
policy = auth_token.PutPolicy(bucket_name) | ||
policy.customer = "hello!" | ||
tokens = policy.token().split(':') | ||
self.assertEqual(config.ACCESS_KEY, tokens[0]) | ||
data = json.loads(decode(tokens[2])) | ||
self.assertEqual(data["scope"], bucket_name) | ||
self.assertEqual(data["customer"], policy.customer) | ||
|
||
new_hmac = encode(hmac.new(config.SECRET_KEY, tokens[2], sha1).digest()) | ||
self.assertEqual(new_hmac, tokens[1]) | ||
|
||
def test_get_policy(self): | ||
policy = auth_token.GetPolicy(bucket_name) | ||
tokens = policy.token().split(':') | ||
data = json.loads(decode(tokens[2])) | ||
self.assertEqual(data["S"], bucket_name) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import unittest | ||
import os | ||
import fop | ||
|
||
access_key = os.getenv("QINIU_ACCESS_KEY") | ||
secret_key = os.getenv("QINIU_SECRET_KEY") | ||
pic = os.getenv("QINIU_TEST_PIC_1") | ||
noexist_pic = os.getenv("QINIU_NOEXIST_PIC") | ||
|
||
class TestFop(unittest.TestCase): | ||
def test_imageExif(self): | ||
ie = fop.ImageExif() | ||
ret, err = ie.call(pic) | ||
self.assertIsNone(err) | ||
self.assertIsNotNone(ret) | ||
|
||
# error | ||
ret, err = ie.call(noexist_pic) | ||
self.assertIsNotNone(err) | ||
self.assertIsNone(ret) | ||
|
||
def test_imageView(self): | ||
iv = fop.ImageView() | ||
iv.height = 100 | ||
ret = iv.make_request(pic) | ||
self.assertEqual(ret, "%s?imageView/1/h/100/" % pic) | ||
|
||
iv.quality = 20 | ||
iv.format = "png" | ||
ret = iv.make_request(pic) | ||
self.assertEqual(ret, "%s?imageView/1/h/100/q/20/format/png/" % pic) | ||
|
||
def test_imageInfo(self): | ||
ii = fop.ImageInfo() | ||
ret, err = ii.call(pic) | ||
self.assertIsNone(err) | ||
self.assertIsNotNone(ret) | ||
|
||
# error | ||
ret, err = ii.call(noexist_pic) | ||
self.assertIsNotNone(err) | ||
|
||
def test_imageMogr(self): | ||
im = fop.ImageMogr() | ||
im.auto_orient = True | ||
im.quality = 200 | ||
im.rotate = 90 | ||
target = "%s?imageMogr/auto-orient/quality/200/rotate/90" % pic | ||
self.assertEqual(im.make_request(pic), target) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.