OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 Google Inc. All Rights Reserved. |
| 2 # |
| 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 |
| 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # |
| 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 implied. |
| 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. |
| 14 |
| 15 # pylint: disable=missing-docstring,no-self-use,no-init,invalid-name |
| 16 """Unit tests for the path_template module.""" |
| 17 |
| 18 from __future__ import absolute_import |
| 19 import unittest2 |
| 20 |
| 21 from google.api.control.path_template import PathTemplate, ValidationException |
| 22 |
| 23 |
| 24 class TestPathTemplate(unittest2.TestCase): |
| 25 """Unit tests for PathTemplate.""" |
| 26 |
| 27 def test_len(self): |
| 28 self.assertEqual(len(PathTemplate('a/b/**/*/{a=hello/world}')), 6) |
| 29 |
| 30 def test_fail_invalid_token(self): |
| 31 self.assertRaises(ValidationException, |
| 32 PathTemplate, 'hello/wor*ld') |
| 33 |
| 34 def test_fail_when_impossible_match(self): |
| 35 template = PathTemplate('hello/world') |
| 36 self.assertRaises(ValidationException, |
| 37 template.match, 'hello') |
| 38 template = PathTemplate('hello/world') |
| 39 self.assertRaises(ValidationException, |
| 40 template.match, 'hello/world/fail') |
| 41 |
| 42 def test_fail_mismatched_literal(self): |
| 43 template = PathTemplate('hello/world') |
| 44 self.assertRaises(ValidationException, |
| 45 template.match, 'hello/world2') |
| 46 |
| 47 def test_fail_when_multiple_path_wildcards(self): |
| 48 self.assertRaises(ValidationException, |
| 49 PathTemplate, 'buckets/*/**/**/objects/*') |
| 50 |
| 51 def test_fail_if_inner_binding(self): |
| 52 self.assertRaises(ValidationException, |
| 53 PathTemplate, 'buckets/{hello={world}}') |
| 54 |
| 55 def test_fail_unexpected_eof(self): |
| 56 self.assertRaises(ValidationException, |
| 57 PathTemplate, 'a/{hello=world') |
| 58 |
| 59 def test_match_atomic_resource_name(self): |
| 60 template = PathTemplate('buckets/*/*/objects/*') |
| 61 self.assertEqual({'$0': 'f', '$1': 'o', '$2': 'bar'}, |
| 62 template.match('buckets/f/o/objects/bar')) |
| 63 template = PathTemplate('/buckets/{hello}') |
| 64 self.assertEqual({'hello': 'world'}, |
| 65 template.match('buckets/world')) |
| 66 template = PathTemplate('/buckets/{hello=*}') |
| 67 self.assertEqual({'hello': 'world'}, |
| 68 template.match('buckets/world')) |
| 69 |
| 70 def test_match_escaped_chars(self): |
| 71 template = PathTemplate('buckets/*/objects') |
| 72 self.assertEqual({'$0': 'hello%2F%2Bworld'}, |
| 73 template.match('buckets/hello%2F%2Bworld/objects')) |
| 74 |
| 75 def test_match_template_with_unbounded_wildcard(self): |
| 76 template = PathTemplate('buckets/*/objects/**') |
| 77 self.assertEqual({'$0': 'foo', '$1': 'bar/baz'}, |
| 78 template.match('buckets/foo/objects/bar/baz')) |
| 79 |
| 80 def test_match_with_unbound_in_middle(self): |
| 81 template = PathTemplate('bar/**/foo/*') |
| 82 self.assertEqual({'$0': 'foo/foo', '$1': 'bar'}, |
| 83 template.match('bar/foo/foo/foo/bar')) |
| 84 |
| 85 def test_render_atomic_resource(self): |
| 86 template = PathTemplate('buckets/*/*/*/objects/*') |
| 87 url = template.render({ |
| 88 '$0': 'f', '$1': 'o', '$2': 'o', '$3': 'google.com:a-b'}) |
| 89 self.assertEqual(url, 'buckets/f/o/o/objects/google.com') |
| 90 |
| 91 def test_render_fail_when_too_few_variables(self): |
| 92 template = PathTemplate('buckets/*/*/*/objects/*') |
| 93 self.assertRaises(ValidationException, |
| 94 template.render, |
| 95 {'$0': 'f', '$1': 'l', '$2': 'o'}) |
| 96 |
| 97 def test_render_with_unbound_in_middle(self): |
| 98 template = PathTemplate('bar/**/foo/*') |
| 99 url = template.render({'$0': '1/2', '$1': '3'}) |
| 100 self.assertEqual(url, 'bar/1/2/foo/3') |
| 101 |
| 102 def test_to_string(self): |
| 103 template = PathTemplate('bar/**/foo/*') |
| 104 self.assertEqual(str(template), 'bar/{$0=**}/foo/{$1=*}') |
| 105 template = PathTemplate('buckets/*/objects/*') |
| 106 self.assertEqual(str(template), 'buckets/{$0=*}/objects/{$1=*}') |
| 107 template = PathTemplate('/buckets/{hello}') |
| 108 self.assertEqual(str(template), 'buckets/{hello=*}') |
| 109 template = PathTemplate('/buckets/{hello=what}/{world}') |
| 110 self.assertEqual(str(template), 'buckets/{hello=what}/{world=*}') |
| 111 template = PathTemplate('/buckets/helloazAZ09-.~_what') |
| 112 self.assertEqual(str(template), 'buckets/helloazAZ09-.~_what') |
OLD | NEW |