1
# Copyright (c) 2012 OpenStack, LLC.
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
17
Unit tests for Name_check filter
19
Created on February 29, 2012
21
@author: eamonn-otoole
25
from webob import Request, Response
26
from swift.common.middleware import name_check
29
FORBIDDEN_CHARS = '\'\"<>`'
32
class FakeApp(object):
34
def __call__(self, env, start_response):
35
return Response(body="OK")(env, start_response)
38
class TestNameCheckMiddleware(unittest.TestCase):
41
self.conf = {'maximum_length': MAX_LENGTH, 'forbidden_chars':
43
self.test_check = name_check.filter_factory(self.conf)(FakeApp())
45
def test_valid_length_and_character(self):
46
path = '/V1.0/' + 'c' * (MAX_LENGTH - 6)
47
resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'}
48
).get_response(self.test_check)
49
self.assertEquals(resp.body, 'OK')
51
def test_invalid_character(self):
52
for c in self.conf['forbidden_chars']:
53
path = '/V1.0/1234' + c + '5'
54
resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'}
55
).get_response(self.test_check)
56
self.assertEquals(resp.body,
57
("Object/Container name contains forbidden chars from %s"
58
% self.conf['forbidden_chars']))
59
self.assertEquals(resp.status_int, 400)
61
def test_invalid_length(self):
62
path = '/V1.0/' + 'c' * (MAX_LENGTH - 5)
63
resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'}
64
).get_response(self.test_check)
65
self.assertEquals(resp.body,
66
("Object/Container name longer than the allowed maximum %s"
67
% self.conf['maximum_length']))
68
self.assertEquals(resp.status_int, 400)
71
if __name__ == '__main__':