Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
looyolo
scrapy
提交
7748ee6b
S
scrapy
项目概览
looyolo
/
scrapy
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
scrapy
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7748ee6b
编写于
2月 05, 2016
作者:
K
Konstantin Lopuhin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
mock date in s3 tests when using botocore
上级
c3fec83e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
67 addition
and
34 deletion
+67
-34
scrapy/core/downloader/handlers/s3.py
scrapy/core/downloader/handlers/s3.py
+4
-4
tests/test_downloader_handlers.py
tests/test_downloader_handlers.py
+63
-30
未找到文件。
scrapy/core/downloader/handlers/s3.py
浏览文件 @
7748ee6b
...
...
@@ -68,10 +68,10 @@ class S3DownloadHandler(object):
except
Exception
as
ex
:
raise
NotConfigured
(
str
(
ex
))
else
:
SignerCls
=
botocore
.
auth
.
AUTH_TYPE_MAPS
[
's3'
]
# TODO - anon
self
.
_signer
=
SignerCls
(
botocore
.
credentials
.
Credentials
(
aws_access_key_id
,
aws_secret_access_key
))
if
not
self
.
anon
:
SignerCls
=
botocore
.
auth
.
AUTH_TYPE_MAPS
[
's3'
]
self
.
_signer
=
SignerCls
(
botocore
.
credentials
.
Credentials
(
aws_access_key_id
,
aws_secret_access_key
))
self
.
_download_http
=
httpdownloadhandler
(
settings
).
download_request
...
...
tests/test_downloader_handlers.py
浏览文件 @
7748ee6b
import
os
import
six
import
contextlib
try
:
from
unittest
import
mock
except
ImportError
:
import
mock
from
twisted.trial
import
unittest
from
twisted.protocols.policies
import
WrappingFactory
...
...
@@ -433,16 +438,16 @@ class HttpDownloadHandlerMock(object):
class
BaseS3TestCase
(
unittest
.
TestCase
):
if
six
.
PY3
:
try
:
import
botocore
except
ImportError
:
try
:
import
botocore
except
ImportError
:
if
six
.
PY2
:
try
:
import
boto
except
ImportError
:
skip
=
'missing botocore or boto library'
else
:
skip
=
'missing botocore library'
else
:
try
:
import
boto
except
ImportError
:
skip
=
'missing boto library'
class
S3AnonTestCase
(
BaseS3TestCase
):
...
...
@@ -464,7 +469,7 @@ class S3AnonTestCase(BaseS3TestCase):
httpreq
.
url
,
'http://aws-publicdatasets.s3.amazonaws.com/'
)
class
S3TestCase
(
unittest
.
TestCase
):
class
S3TestCase
(
BaseS3
TestCase
):
download_handler_cls
=
S3DownloadHandler
# test use same example keys than amazon developer guide
...
...
@@ -481,63 +486,89 @@ class S3TestCase(unittest.TestCase):
self
.
download_request
=
s3reqh
.
download_request
self
.
spider
=
Spider
(
'foo'
)
@
contextlib
.
contextmanager
def
_mocked_date
(
self
,
date
):
try
:
import
botocore.auth
except
ImportError
:
yield
else
:
# We need to mock botocore.auth.formatdate, because otherwise
# botocore overrides Date header with current date and time
# and Authorization header is different each time
with
mock
.
patch
(
'botocore.auth.formatdate'
)
as
mock_formatdate
:
mock_formatdate
.
return_value
=
date
yield
def
test_request_signing1
(
self
):
# gets an object from the johnsmith bucket.
req
=
Request
(
's3://johnsmith/photos/puppy.jpg'
,
headers
=
{
'Date'
:
'Tue, 27 Mar 2007 19:36:42 +0000'
})
httpreq
=
self
.
download_request
(
req
,
self
.
spider
)
date
=
'Tue, 27 Mar 2007 19:36:42 +0000'
req
=
Request
(
's3://johnsmith/photos/puppy.jpg'
,
headers
=
{
'Date'
:
date
})
with
self
.
_mocked_date
(
date
):
httpreq
=
self
.
download_request
(
req
,
self
.
spider
)
self
.
assertEqual
(
httpreq
.
headers
[
'Authorization'
],
\
b
'AWS 0PN5J17HBGZHT7JJ3X82:xXjDGYUmKxnwqr5KXNPGldn5LbA='
)
def
test_request_signing2
(
self
):
# puts an object into the johnsmith bucket.
date
=
'Tue, 27 Mar 2007 21:15:45 +0000'
req
=
Request
(
's3://johnsmith/photos/puppy.jpg'
,
method
=
'PUT'
,
headers
=
{
'Content-Type'
:
'image/jpeg'
,
'Date'
:
'Tue, 27 Mar 2007 21:15:45 +0000'
,
'Date'
:
date
,
'Content-Length'
:
'94328'
,
})
httpreq
=
self
.
download_request
(
req
,
self
.
spider
)
with
self
.
_mocked_date
(
date
):
httpreq
=
self
.
download_request
(
req
,
self
.
spider
)
self
.
assertEqual
(
httpreq
.
headers
[
'Authorization'
],
\
b
'AWS 0PN5J17HBGZHT7JJ3X82:hcicpDDvL9SsO6AkvxqmIWkmOuQ='
)
def
test_request_signing3
(
self
):
# lists the content of the johnsmith bucket.
date
=
'Tue, 27 Mar 2007 19:42:41 +0000'
req
=
Request
(
's3://johnsmith/?prefix=photos&max-keys=50&marker=puppy'
,
\
method
=
'GET'
,
headers
=
{
'User-Agent'
:
'Mozilla/5.0'
,
'Date'
:
'Tue, 27 Mar 2007 19:42:41 +0000'
,
'Date'
:
date
,
})
httpreq
=
self
.
download_request
(
req
,
self
.
spider
)
with
self
.
_mocked_date
(
date
):
httpreq
=
self
.
download_request
(
req
,
self
.
spider
)
self
.
assertEqual
(
httpreq
.
headers
[
'Authorization'
],
\
b
'AWS 0PN5J17HBGZHT7JJ3X82:jsRt/rhG+Vtp88HrYL706QhE4w4='
)
def
test_request_signing4
(
self
):
# fetches the access control policy sub-resource for the 'johnsmith' bucket.
req
=
Request
(
's3://johnsmith/?acl'
,
\
method
=
'GET'
,
headers
=
{
'Date'
:
'Tue, 27 Mar 2007 19:44:46 +0000'
})
httpreq
=
self
.
download_request
(
req
,
self
.
spider
)
date
=
'Tue, 27 Mar 2007 19:44:46 +0000'
req
=
Request
(
's3://johnsmith/?acl'
,
method
=
'GET'
,
headers
=
{
'Date'
:
date
})
with
self
.
_mocked_date
(
date
):
httpreq
=
self
.
download_request
(
req
,
self
.
spider
)
self
.
assertEqual
(
httpreq
.
headers
[
'Authorization'
],
\
b
'AWS 0PN5J17HBGZHT7JJ3X82:thdUi9VAkzhkniLj96JIrOPGi0g='
)
def
test_request_signing5
(
self
):
# deletes an object from the 'johnsmith' bucket using the
# path-style and Date alternative.
date
=
'Tue, 27 Mar 2007 21:20:27 +0000'
req
=
Request
(
's3://johnsmith/photos/puppy.jpg'
,
\
method
=
'DELETE'
,
headers
=
{
'Date'
:
'Tue, 27 Mar 2007 21:20:27 +0000'
,
'Date'
:
date
,
'x-amz-date'
:
'Tue, 27 Mar 2007 21:20:26 +0000'
,
})
httpreq
=
self
.
download_request
(
req
,
self
.
spider
)
self
.
assertEqual
(
httpreq
.
headers
[
'Authorization'
],
\
b
'AWS 0PN5J17HBGZHT7JJ3X82:k3nL7gH3+PadhTEVn5Ip83xlYzk='
)
with
self
.
_mocked_date
(
date
):
httpreq
=
self
.
download_request
(
req
,
self
.
spider
)
# botocore does not override Date with x-amz-date
self
.
assertIn
(
httpreq
.
headers
[
'Authorization'
],
[
b
'AWS 0PN5J17HBGZHT7JJ3X82:k3nL7gH3+PadhTEVn5Ip83xlYzk='
,
b
'AWS 0PN5J17HBGZHT7JJ3X82:otYM2krxnuHhAofO4oqIV7wcfdU='
])
def
test_request_signing6
(
self
):
# uploads an object to a CNAME style virtual hosted bucket with metadata.
date
=
'Tue, 27 Mar 2007 21:06:08 +0000'
req
=
Request
(
's3://static.johnsmith.net:8080/db-backup.dat.gz'
,
\
method
=
'PUT'
,
headers
=
{
'User-Agent'
:
'curl/7.15.5'
,
'Host'
:
'static.johnsmith.net:8080'
,
'Date'
:
'Tue, 27 Mar 2007 21:06:08 +0000'
,
'Date'
:
date
,
'x-amz-acl'
:
'public-read'
,
'content-type'
:
'application/x-download'
,
'Content-MD5'
:
'4gJE4saaMU4BqNR0kLY+lw=='
,
...
...
@@ -548,20 +579,22 @@ class S3TestCase(unittest.TestCase):
'Content-Encoding'
:
'gzip'
,
'Content-Length'
:
'5913339'
,
})
httpreq
=
self
.
download_request
(
req
,
self
.
spider
)
with
self
.
_mocked_date
(
date
):
httpreq
=
self
.
download_request
(
req
,
self
.
spider
)
self
.
assertEqual
(
httpreq
.
headers
[
'Authorization'
],
\
b
'AWS 0PN5J17HBGZHT7JJ3X82:C0FlOtU8Ylb9KDTpZqYkZPX91iI='
)
def
test_request_signing7
(
self
):
# ensure that spaces are quoted properly before signing
date
=
'Tue, 27 Mar 2007 19:42:41 +0000'
req
=
Request
(
(
"s3://johnsmith/photos/my puppy.jpg"
"?response-content-disposition=my puppy.jpg"
),
method
=
'GET'
,
headers
=
{
'Date'
:
'Tue, 27 Mar 2007 19:42:41 +0000'
,
})
httpreq
=
self
.
download_request
(
req
,
self
.
spider
)
headers
=
{
'Date'
:
date
},
)
with
self
.
_mocked_date
(
date
):
httpreq
=
self
.
download_request
(
req
,
self
.
spider
)
self
.
assertEqual
(
httpreq
.
headers
[
'Authorization'
],
b
'AWS 0PN5J17HBGZHT7JJ3X82:+CfvG8EZ3YccOrRVMXNaK2eKZmM='
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录