2017-08-05 10:29:40 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2014-08-27 15:17:06 +00:00
|
|
|
from __future__ import unicode_literals
|
2013-08-03 21:21:25 +00:00
|
|
|
import sure # noqa
|
2013-03-05 13:14:43 +00:00
|
|
|
|
2017-09-16 13:38:40 +00:00
|
|
|
from flask.testing import FlaskClient
|
2013-03-05 13:14:43 +00:00
|
|
|
import moto.server as server
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
"""
|
2013-03-05 13:14:43 +00:00
|
|
|
Test the different server responses
|
2019-10-31 15:44:26 +00:00
|
|
|
"""
|
2013-03-05 13:14:43 +00:00
|
|
|
|
|
|
|
|
2017-09-16 13:38:40 +00:00
|
|
|
class AuthenticatedClient(FlaskClient):
|
|
|
|
def open(self, *args, **kwargs):
|
2019-10-31 15:44:26 +00:00
|
|
|
kwargs["headers"] = kwargs.get("headers", {})
|
|
|
|
kwargs["headers"]["Authorization"] = "Any authorization header"
|
|
|
|
kwargs["content_length"] = 0 # Fixes content-length complaints.
|
2017-09-16 13:38:40 +00:00
|
|
|
return super(AuthenticatedClient, self).open(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def authenticated_client():
|
2013-12-29 01:15:37 +00:00
|
|
|
backend = server.create_backend_app("s3")
|
2017-09-16 13:38:40 +00:00
|
|
|
backend.test_client_class = AuthenticatedClient
|
|
|
|
return backend.test_client()
|
2013-12-29 01:15:37 +00:00
|
|
|
|
2017-09-16 13:38:40 +00:00
|
|
|
|
|
|
|
def test_s3_server_get():
|
|
|
|
test_client = authenticated_client()
|
2019-10-31 15:44:26 +00:00
|
|
|
res = test_client.get("/")
|
2013-03-05 13:14:43 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
res.data.should.contain(b"ListAllMyBucketsResult")
|
2013-03-05 13:14:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_s3_server_bucket_create():
|
2017-09-16 13:38:40 +00:00
|
|
|
test_client = authenticated_client()
|
2013-12-29 01:15:37 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
res = test_client.put("/", "http://foobaz.localhost:5000/")
|
2013-03-05 13:14:43 +00:00
|
|
|
res.status_code.should.equal(200)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
res = test_client.get("/")
|
|
|
|
res.data.should.contain(b"<Name>foobaz</Name>")
|
2013-03-05 13:14:43 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
res = test_client.get("/", "http://foobaz.localhost:5000/")
|
2013-03-05 13:14:43 +00:00
|
|
|
res.status_code.should.equal(200)
|
2014-08-26 17:25:50 +00:00
|
|
|
res.data.should.contain(b"ListBucketResult")
|
2013-03-05 13:14:43 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
res = test_client.put("/bar", "http://foobaz.localhost:5000/", data="test value")
|
2013-03-05 13:14:43 +00:00
|
|
|
res.status_code.should.equal(200)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert "ETag" in dict(res.headers)
|
2013-03-05 13:14:43 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
res = test_client.get("/bar", "http://foobaz.localhost:5000/")
|
2013-03-05 13:14:43 +00:00
|
|
|
res.status_code.should.equal(200)
|
2014-08-26 17:25:50 +00:00
|
|
|
res.data.should.equal(b"test value")
|
2013-05-17 23:41:39 +00:00
|
|
|
|
|
|
|
|
2015-02-22 15:13:34 +00:00
|
|
|
def test_s3_server_bucket_versioning():
|
2017-09-16 13:38:40 +00:00
|
|
|
test_client = authenticated_client()
|
2015-02-22 15:13:34 +00:00
|
|
|
|
|
|
|
# Just enough XML to enable versioning
|
2019-10-31 15:44:26 +00:00
|
|
|
body = "<Status>Enabled</Status>"
|
|
|
|
res = test_client.put("/?versioning", "http://foobaz.localhost:5000", data=body)
|
2015-02-22 15:13:34 +00:00
|
|
|
res.status_code.should.equal(200)
|
|
|
|
|
|
|
|
|
2013-05-17 23:41:39 +00:00
|
|
|
def test_s3_server_post_to_bucket():
|
2017-09-16 13:38:40 +00:00
|
|
|
test_client = authenticated_client()
|
2013-12-29 01:15:37 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
res = test_client.put("/", "http://tester.localhost:5000/")
|
2013-05-17 23:41:39 +00:00
|
|
|
res.status_code.should.equal(200)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
test_client.post(
|
|
|
|
"/",
|
|
|
|
"https://tester.localhost:5000/",
|
|
|
|
data={"key": "the-key", "file": "nothing"},
|
|
|
|
)
|
2013-05-17 23:41:39 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
res = test_client.get("/the-key", "http://tester.localhost:5000/")
|
2013-05-17 23:41:39 +00:00
|
|
|
res.status_code.should.equal(200)
|
2014-08-26 17:25:50 +00:00
|
|
|
res.data.should.equal(b"nothing")
|
2017-05-01 19:13:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_s3_server_post_without_content_length():
|
2017-09-16 13:38:40 +00:00
|
|
|
test_client = authenticated_client()
|
2017-05-01 19:13:12 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
res = test_client.put(
|
|
|
|
"/", "http://tester.localhost:5000/", environ_overrides={"CONTENT_LENGTH": ""}
|
|
|
|
)
|
2017-05-01 19:13:12 +00:00
|
|
|
res.status_code.should.equal(411)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
res = test_client.post(
|
|
|
|
"/", "https://tester.localhost:5000/", environ_overrides={"CONTENT_LENGTH": ""}
|
|
|
|
)
|
2017-05-01 19:13:12 +00:00
|
|
|
res.status_code.should.equal(411)
|
2017-08-05 10:29:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_s3_server_post_unicode_bucket_key():
|
|
|
|
# Make sure that we can deal with non-ascii characters in request URLs (e.g., S3 object names)
|
|
|
|
dispatcher = server.DomainDispatcherApplication(server.create_backend_app)
|
2019-10-31 15:44:26 +00:00
|
|
|
backend_app = dispatcher.get_application(
|
|
|
|
{"HTTP_HOST": "s3.amazonaws.com", "PATH_INFO": "/test-bucket/test-object-てすと"}
|
|
|
|
)
|
2017-08-05 10:29:40 +00:00
|
|
|
assert backend_app
|
2019-10-31 15:44:26 +00:00
|
|
|
backend_app = dispatcher.get_application(
|
|
|
|
{
|
|
|
|
"HTTP_HOST": "s3.amazonaws.com",
|
|
|
|
"PATH_INFO": "/test-bucket/test-object-てすと".encode("utf-8"),
|
|
|
|
}
|
|
|
|
)
|
2017-08-05 10:29:40 +00:00
|
|
|
assert backend_app
|
2020-09-19 09:07:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_s3_server_post_cors():
|
|
|
|
test_client = authenticated_client()
|
|
|
|
|
|
|
|
preflight_headers = {
|
|
|
|
"Access-Control-Request-Method": "POST",
|
|
|
|
"Access-Control-Request-Headers": "origin, x-requested-with",
|
|
|
|
"Origin": "https://localhost:9000",
|
|
|
|
}
|
|
|
|
|
|
|
|
res = test_client.options(
|
|
|
|
"/", "http://tester.localhost:5000/", headers=preflight_headers
|
|
|
|
)
|
|
|
|
assert res.status_code in [200, 204]
|
|
|
|
|
|
|
|
expected_methods = set(["DELETE", "PATCH", "PUT", "GET", "HEAD", "POST", "OPTIONS"])
|
|
|
|
assert set(res.headers["Allow"].split(", ")) == expected_methods
|
|
|
|
assert (
|
|
|
|
set(res.headers["Access-Control-Allow-Methods"].split(", ")) == expected_methods
|
|
|
|
)
|
|
|
|
|
|
|
|
res.headers.should.have.key("Access-Control-Allow-Origin").which.should.equal(
|
|
|
|
"https://localhost:9000"
|
|
|
|
)
|
|
|
|
res.headers.should.have.key("Access-Control-Allow-Headers").which.should.equal(
|
|
|
|
"origin, x-requested-with"
|
|
|
|
)
|