OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved |
| 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: |
| 10 # |
| 11 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. |
| 13 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. |
| 21 # |
| 22 |
| 23 from tests.unit import unittest |
| 24 import xml.dom.minidom |
| 25 |
| 26 from boto.s3.website import WebsiteConfiguration |
| 27 from boto.s3.website import RedirectLocation |
| 28 from boto.s3.website import RoutingRules |
| 29 from boto.s3.website import Condition |
| 30 from boto.s3.website import RoutingRules |
| 31 from boto.s3.website import RoutingRule |
| 32 from boto.s3.website import Redirect |
| 33 |
| 34 |
| 35 def pretty_print_xml(text): |
| 36 text = ''.join(t.strip() for t in text.splitlines()) |
| 37 x = xml.dom.minidom.parseString(text) |
| 38 return x.toprettyxml() |
| 39 |
| 40 |
| 41 class TestS3WebsiteConfiguration(unittest.TestCase): |
| 42 maxDiff = None |
| 43 |
| 44 def setUp(self): |
| 45 pass |
| 46 |
| 47 def tearDown(self): |
| 48 pass |
| 49 |
| 50 def test_suffix_only(self): |
| 51 config = WebsiteConfiguration(suffix='index.html') |
| 52 xml = config.to_xml() |
| 53 self.assertIn( |
| 54 '<IndexDocument><Suffix>index.html</Suffix></IndexDocument>', xml) |
| 55 |
| 56 def test_suffix_and_error(self): |
| 57 config = WebsiteConfiguration(suffix='index.html', |
| 58 error_key='error.html') |
| 59 xml = config.to_xml() |
| 60 self.assertIn( |
| 61 '<ErrorDocument><Key>error.html</Key></ErrorDocument>', xml) |
| 62 |
| 63 def test_redirect_all_request_to_with_just_host(self): |
| 64 location = RedirectLocation(hostname='example.com') |
| 65 config = WebsiteConfiguration(redirect_all_requests_to=location) |
| 66 xml = config.to_xml() |
| 67 self.assertIn( |
| 68 ('<RedirectAllRequestsTo><HostName>' |
| 69 'example.com</HostName></RedirectAllRequestsTo>'), xml) |
| 70 |
| 71 def test_redirect_all_requests_with_protocol(self): |
| 72 location = RedirectLocation(hostname='example.com', protocol='https') |
| 73 config = WebsiteConfiguration(redirect_all_requests_to=location) |
| 74 xml = config.to_xml() |
| 75 self.assertIn( |
| 76 ('<RedirectAllRequestsTo><HostName>' |
| 77 'example.com</HostName>\n<Protocol>https</Protocol>' |
| 78 '</RedirectAllRequestsTo>'), xml) |
| 79 |
| 80 def test_routing_rules_key_prefix(self): |
| 81 x = pretty_print_xml |
| 82 # This rule redirects requests for docs/* to documentation/* |
| 83 rules = RoutingRules() |
| 84 condition = Condition(key_prefix='docs/') |
| 85 redirect = Redirect(replace_key_prefix='documents/') |
| 86 rules.add_rule(RoutingRule(condition, redirect)) |
| 87 config = WebsiteConfiguration(suffix='index.html', routing_rules=rules) |
| 88 xml = config.to_xml() |
| 89 |
| 90 expected_xml = """<?xml version="1.0" encoding="UTF-8"?> |
| 91 <WebsiteConfiguration xmlns='http://s3.amazonaws.com/doc/2006-03-01/
'> |
| 92 <IndexDocument> |
| 93 <Suffix>index.html</Suffix> |
| 94 </IndexDocument> |
| 95 <RoutingRules> |
| 96 <RoutingRule> |
| 97 <Condition> |
| 98 <KeyPrefixEquals>docs/</KeyPrefixEquals> |
| 99 </Condition> |
| 100 <Redirect> |
| 101 <ReplaceKeyPrefixWith>documents/</ReplaceKeyPrefixWith> |
| 102 </Redirect> |
| 103 </RoutingRule> |
| 104 </RoutingRules> |
| 105 </WebsiteConfiguration> |
| 106 """ |
| 107 self.assertEqual(x(expected_xml), x(xml)) |
| 108 |
| 109 def test_routing_rules_to_host_on_404(self): |
| 110 x = pretty_print_xml |
| 111 # Another example from the docs: |
| 112 # Redirect requests to a specific host in the event of a 404. |
| 113 # Also, the redirect inserts a report-404/. For example, |
| 114 # if you request a page ExamplePage.html and it results |
| 115 # in a 404, the request is routed to a page report-404/ExamplePage.html |
| 116 rules = RoutingRules() |
| 117 condition = Condition(http_error_code=404) |
| 118 redirect = Redirect(hostname='example.com', |
| 119 replace_key_prefix='report-404/') |
| 120 rules.add_rule(RoutingRule(condition, redirect)) |
| 121 config = WebsiteConfiguration(suffix='index.html', routing_rules=rules) |
| 122 xml = config.to_xml() |
| 123 |
| 124 expected_xml = """<?xml version="1.0" encoding="UTF-8"?> |
| 125 <WebsiteConfiguration xmlns='http://s3.amazonaws.com/doc/2006-03-01/
'> |
| 126 <IndexDocument> |
| 127 <Suffix>index.html</Suffix> |
| 128 </IndexDocument> |
| 129 <RoutingRules> |
| 130 <RoutingRule> |
| 131 <Condition> |
| 132 <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals> |
| 133 </Condition> |
| 134 <Redirect> |
| 135 <HostName>example.com</HostName> |
| 136 <ReplaceKeyPrefixWith>report-404/</ReplaceKeyPrefixWith> |
| 137 </Redirect> |
| 138 </RoutingRule> |
| 139 </RoutingRules> |
| 140 </WebsiteConfiguration> |
| 141 """ |
| 142 self.assertEqual(x(expected_xml), x(xml)) |
| 143 |
| 144 def test_key_prefix(self): |
| 145 x = pretty_print_xml |
| 146 rules = RoutingRules() |
| 147 condition = Condition(key_prefix="images/") |
| 148 redirect = Redirect(replace_key='folderdeleted.html') |
| 149 rules.add_rule(RoutingRule(condition, redirect)) |
| 150 config = WebsiteConfiguration(suffix='index.html', routing_rules=rules) |
| 151 xml = config.to_xml() |
| 152 |
| 153 expected_xml = """<?xml version="1.0" encoding="UTF-8"?> |
| 154 <WebsiteConfiguration xmlns='http://s3.amazonaws.com/doc/2006-03-01/
'> |
| 155 <IndexDocument> |
| 156 <Suffix>index.html</Suffix> |
| 157 </IndexDocument> |
| 158 <RoutingRules> |
| 159 <RoutingRule> |
| 160 <Condition> |
| 161 <KeyPrefixEquals>images/</KeyPrefixEquals> |
| 162 </Condition> |
| 163 <Redirect> |
| 164 <ReplaceKeyWith>folderdeleted.html</ReplaceKeyWith> |
| 165 </Redirect> |
| 166 </RoutingRule> |
| 167 </RoutingRules> |
| 168 </WebsiteConfiguration> |
| 169 """ |
| 170 self.assertEqual(x(expected_xml), x(xml)) |
| 171 |
| 172 def test_builders(self): |
| 173 x = pretty_print_xml |
| 174 # This is a more declarative way to create rules. |
| 175 # First the long way. |
| 176 rules = RoutingRules() |
| 177 condition = Condition(http_error_code=404) |
| 178 redirect = Redirect(hostname='example.com', |
| 179 replace_key_prefix='report-404/') |
| 180 rules.add_rule(RoutingRule(condition, redirect)) |
| 181 xml = rules.to_xml() |
| 182 |
| 183 # Then the more concise way. |
| 184 rules2 = RoutingRules().add_rule( |
| 185 RoutingRule.when(http_error_code=404).then_redirect( |
| 186 hostname='example.com', replace_key_prefix='report-404/')) |
| 187 xml2 = rules2.to_xml() |
| 188 self.assertEqual(x(xml), x(xml2)) |
OLD | NEW |