Beispiel #1
0
    def test_routing_rules_key_prefix(self):
        x = pretty_print_xml
        # This rule redirects requests for docs/* to documentation/*
        rules = RoutingRules()
        condition = Condition(key_prefix='docs/')
        redirect = Redirect(replace_key_prefix='documents/')
        rules.add_rule(RoutingRule(condition, redirect))
        config = WebsiteConfiguration(suffix='index.html', routing_rules=rules)
        xml = config.to_xml()

        expected_xml = """<?xml version="1.0" encoding="UTF-8"?>
            <WebsiteConfiguration xmlns='http://s3.amazonaws.com/doc/2006-03-01/'>
              <IndexDocument>
                <Suffix>index.html</Suffix>
              </IndexDocument>
              <RoutingRules>
                <RoutingRule>
                <Condition>
                  <KeyPrefixEquals>docs/</KeyPrefixEquals>
                </Condition>
                <Redirect>
                  <ReplaceKeyPrefixWith>documents/</ReplaceKeyPrefixWith>
                </Redirect>
                </RoutingRule>
              </RoutingRules>
            </WebsiteConfiguration>
        """
        self.assertEqual(x(expected_xml), x(xml))
Beispiel #2
0
 def test_redirect_all_request_to_with_just_host(self):
     location = RedirectLocation(hostname='example.com')
     config = WebsiteConfiguration(redirect_all_requests_to=location)
     xml = config.to_xml()
     self.assertIn(
         ('<RedirectAllRequestsTo><HostName>'
          'example.com</HostName></RedirectAllRequestsTo>'), xml)
Beispiel #3
0
    def test_routing_rules_to_host_on_404(self):
        x = pretty_print_xml
        # Another example from the docs:
        # Redirect requests to a specific host in the event of a 404.
        # Also, the redirect inserts a report-404/.  For example,
        # if you request a page ExamplePage.html and it results
        # in a 404, the request is routed to a page report-404/ExamplePage.html
        rules = RoutingRules()
        condition = Condition(http_error_code=404)
        redirect = Redirect(hostname='example.com',
                            replace_key_prefix='report-404/')
        rules.add_rule(RoutingRule(condition, redirect))
        config = WebsiteConfiguration(suffix='index.html', routing_rules=rules)
        xml = config.to_xml()

        expected_xml = """<?xml version="1.0" encoding="UTF-8"?>
            <WebsiteConfiguration xmlns='http://s3.amazonaws.com/doc/2006-03-01/'>
              <IndexDocument>
                <Suffix>index.html</Suffix>
              </IndexDocument>
              <RoutingRules>
                <RoutingRule>
                <Condition>
                  <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
                </Condition>
                <Redirect>
                  <HostName>example.com</HostName>
                  <ReplaceKeyPrefixWith>report-404/</ReplaceKeyPrefixWith>
                </Redirect>
                </RoutingRule>
              </RoutingRules>
            </WebsiteConfiguration>
        """
        self.assertEqual(x(expected_xml), x(xml))
Beispiel #4
0
 def test_redirect_all_requests_with_protocol(self):
     location = RedirectLocation(hostname='example.com', protocol='https')
     config = WebsiteConfiguration(redirect_all_requests_to=location)
     xml = config.to_xml()
     self.assertIn(('<RedirectAllRequestsTo><HostName>'
                    'example.com</HostName><Protocol>https</Protocol>'
                    '</RedirectAllRequestsTo>'), xml)
Beispiel #5
0
    def test_routing_rules_key_prefix(self):
        x = pretty_print_xml
        # This rule redirects requests for docs/* to documentation/*
        rules = RoutingRules()
        condition = Condition(key_prefix='docs/')
        redirect = Redirect(replace_key_prefix='documents/')
        rules.add_rule(RoutingRule(condition, redirect))
        config = WebsiteConfiguration(suffix='index.html', routing_rules=rules)
        xml = config.to_xml()

        expected_xml = """<?xml version="1.0" encoding="UTF-8"?>
            <WebsiteConfiguration xmlns='http://s3.amazonaws.com/doc/2006-03-01/'>
              <IndexDocument>
                <Suffix>index.html</Suffix>
              </IndexDocument>
              <RoutingRules>
                <RoutingRule>
                <Condition>
                  <KeyPrefixEquals>docs/</KeyPrefixEquals>
                </Condition>
                <Redirect>
                  <ReplaceKeyPrefixWith>documents/</ReplaceKeyPrefixWith>
                </Redirect>
                </RoutingRule>
              </RoutingRules>
            </WebsiteConfiguration>
        """
        self.assertEqual(x(expected_xml), x(xml))
Beispiel #6
0
    def test_routing_rules_to_host_on_404(self):
        x = pretty_print_xml
        # Another example from the docs:
        # Redirect requests to a specific host in the event of a 404.
        # Also, the redirect inserts a report-404/.  For example,
        # if you request a page ExamplePage.html and it results
        # in a 404, the request is routed to a page report-404/ExamplePage.html
        rules = RoutingRules()
        condition = Condition(http_error_code=404)
        redirect = Redirect(hostname='example.com',
                            replace_key_prefix='report-404/')
        rules.add_rule(RoutingRule(condition, redirect))
        config = WebsiteConfiguration(suffix='index.html', routing_rules=rules)
        xml = config.to_xml()

        expected_xml = """<?xml version="1.0" encoding="UTF-8"?>
            <WebsiteConfiguration xmlns='http://s3.amazonaws.com/doc/2006-03-01/'>
              <IndexDocument>
                <Suffix>index.html</Suffix>
              </IndexDocument>
              <RoutingRules>
                <RoutingRule>
                <Condition>
                  <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
                </Condition>
                <Redirect>
                  <HostName>example.com</HostName>
                  <ReplaceKeyPrefixWith>report-404/</ReplaceKeyPrefixWith>
                </Redirect>
                </RoutingRule>
              </RoutingRules>
            </WebsiteConfiguration>
        """
        self.assertEqual(x(expected_xml), x(xml))
Beispiel #7
0
 def test_redirect_all_request_to_with_just_host(self):
     location = RedirectLocation(hostname='example.com')
     config = WebsiteConfiguration(redirect_all_requests_to=location)
     xml = config.to_xml()
     self.assertIn(
         ('<RedirectAllRequestsTo><HostName>'
          'example.com</HostName></RedirectAllRequestsTo>'), xml)
Beispiel #8
0
 def test_redirect_all_requests_with_protocol(self):
     location = RedirectLocation(hostname='example.com', protocol='https')
     config = WebsiteConfiguration(redirect_all_requests_to=location)
     xml = config.to_xml()
     self.assertIn(
         ('<RedirectAllRequestsTo><HostName>'
          'example.com</HostName><Protocol>https</Protocol>'
          '</RedirectAllRequestsTo>'), xml)
Beispiel #9
0
def test_set_bucket_website():
    buckets, zone_bucket = create_bucket_per_zone_in_realm()
    for _, bucket in zone_bucket:
        website_cfg = WebsiteConfiguration(suffix='index.html',error_key='error.html')
        try:
            bucket.set_website_configuration(website_cfg)
        except boto.exception.S3ResponseError as e:
            if e.error_code == 'MethodNotAllowed':
                raise SkipTest("test_set_bucket_website skipped. Requires rgw_enable_static_website = 1.")
        assert(bucket.get_website_configuration_with_xml()[1] == website_cfg.to_xml())
Beispiel #10
0
def test_set_bucket_website():
    buckets, zone_bucket = create_bucket_per_zone_in_realm()
    for _, bucket in zone_bucket:
        website_cfg = WebsiteConfiguration(suffix='index.html',error_key='error.html')
        try:
            bucket.set_website_configuration(website_cfg)
        except boto.exception.S3ResponseError as e:
            if e.error_code == 'MethodNotAllowed':
                raise SkipTest("test_set_bucket_website skipped. Requires rgw_enable_static_website = 1.")
        assert(bucket.get_website_configuration_with_xml()[1] == website_cfg.to_xml())
Beispiel #11
0
 def test_parse_xml(self):
     x = pretty_print_xml
     xml_in = """<?xml version="1.0" encoding="UTF-8"?>
         <WebsiteConfiguration xmlns='http://s3.amazonaws.com/doc/2006-03-01/'>
           <IndexDocument>
             <Suffix>index.html</Suffix>
           </IndexDocument>
           <ErrorDocument>
             <Key>error.html</Key>
           </ErrorDocument>
           <RoutingRules>
             <RoutingRule>
             <Condition>
               <KeyPrefixEquals>docs/</KeyPrefixEquals>
             </Condition>
             <Redirect>
               <Protocol>https</Protocol>
               <HostName>www.example.com</HostName>
               <ReplaceKeyWith>documents/</ReplaceKeyWith>
               <HttpRedirectCode>302</HttpRedirectCode>
             </Redirect>
             </RoutingRule>
             <RoutingRule>
             <Condition>
               <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
             </Condition>
             <Redirect>
               <HostName>example.com</HostName>
               <ReplaceKeyPrefixWith>report-404/</ReplaceKeyPrefixWith>
             </Redirect>
             </RoutingRule>
           </RoutingRules>
         </WebsiteConfiguration>
     """
     webconfig = WebsiteConfiguration()
     h = handler.XmlHandler(webconfig, None)
     xml.sax.parseString(xml_in.encode('utf-8'), h)
     xml_out = webconfig.to_xml()
     self.assertEqual(x(xml_in), x(xml_out))
Beispiel #12
0
 def test_parse_xml(self):
     x = pretty_print_xml
     xml_in = """<?xml version="1.0" encoding="UTF-8"?>
         <WebsiteConfiguration xmlns='http://s3.amazonaws.com/doc/2006-03-01/'>
           <IndexDocument>
             <Suffix>index.html</Suffix>
           </IndexDocument>
           <ErrorDocument>
             <Key>error.html</Key>
           </ErrorDocument>
           <RoutingRules>
             <RoutingRule>
             <Condition>
               <KeyPrefixEquals>docs/</KeyPrefixEquals>
             </Condition>
             <Redirect>
               <Protocol>https</Protocol>
               <HostName>www.example.com</HostName>
               <ReplaceKeyWith>documents/</ReplaceKeyWith>
               <HttpRedirectCode>302</HttpRedirectCode>
             </Redirect>
             </RoutingRule>
             <RoutingRule>
             <Condition>
               <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
             </Condition>
             <Redirect>
               <HostName>example.com</HostName>
               <ReplaceKeyPrefixWith>report-404/</ReplaceKeyPrefixWith>
             </Redirect>
             </RoutingRule>
           </RoutingRules>
         </WebsiteConfiguration>
     """
     webconfig = WebsiteConfiguration()
     h = handler.XmlHandler(webconfig, None)
     xml.sax.parseString(xml_in.encode('utf-8'), h)
     xml_out = webconfig.to_xml()
     self.assertEqual(x(xml_in), x(xml_out))
def main(arg):
    try:
        bucket=conn.create_bucket(arg[0])
        k=bucket.new_key('error.html')
        k.set_contents_from_string('404testchttl<br><title>chttl</title>')
        k.set_acl('public-read')
        k=bucket.new_key('index.html')
        k.set_contents_from_string('<title>chttl</title>Hello World!')
        k.set_acl('public-read')
        
        config=WebsiteConfiguration()
        #Routing rules
        config.suffix='index.html'
        config.error_key='error.html'
        
        RoutingRules=boto.s3.website.RoutingRules()
        rule = boto.s3.website.RoutingRule()
        rule.redirect=boto.s3.website.Redirect(
                    hostname='www.google.com', protocol=None,
                    replace_key=None,
                    replace_key_prefix=None,
                    http_redirect_code=None)
        RoutingRules.add_rule(rule)
        
        #get bucket url
        #print bucket.get_website_endpoint()
        
        config.routing_rules=RoutingRules
        #print "Put Website Configurations.."
        bucket.set_website_configuration(config)
        #print "Get Website Configurations.."
        #print repr(bucket.get_website_configuration())
        #print "Delete Website Configurations..\n"
        bucket.delete_website_configuration()
        
        
        
        #If 'redirect_all_requests_to' is set, all other attribute will be ignored!
        config2=WebsiteConfiguration()
        config2.redirect_all_requests_to=boto.s3.website.RedirectLocation(hostname='www.google.com')
        #print "Put Website Configurations with redirect all request"
        bucket.set_website_configuration(config2)
        #print "Get Website Configurations.."
        #print repr(bucket.get_website_configuration())
        #print "Delete Website Configurations..\n"
        bucket.delete_website_configuration()
        
        #print "\nClean up.."
        # clear bucket
        result = bucket.get_all_versions()
        for v in result:
            v.delete()
        
        conn.delete_bucket(bucket)
        #print " - Website Serial Test Done ! \n"
    except boto.exception.S3ResponseError, e:
        xmldoc = minidom.parseString(e.body)
        itemlist = xmldoc.getElementsByTagName('Message')
        print "Status Code: " + repr(e.status)
        print "Reason: " + repr(e.reason)
        print "Message: " + itemlist[0].childNodes[0].nodeValue
Beispiel #14
0
 def test_suffix_only(self):
     config = WebsiteConfiguration(suffix='index.html')
     xml = config.to_xml()
     self.assertIn(
         '<IndexDocument><Suffix>index.html</Suffix></IndexDocument>', xml)
Beispiel #15
0
 def test_suffix_and_error(self):
     config = WebsiteConfiguration(suffix='index.html',
                                   error_key='error.html')
     xml = config.to_xml()
     self.assertIn('<ErrorDocument><Key>error.html</Key></ErrorDocument>',
                   xml)
Beispiel #16
0
 def test_suffix_only(self):
     config = WebsiteConfiguration(suffix='index.html')
     xml = config.to_xml()
     self.assertIn(
         '<IndexDocument><Suffix>index.html</Suffix></IndexDocument>', xml)
Beispiel #17
0
 def test_suffix_and_error(self):
     config = WebsiteConfiguration(suffix='index.html',
                                   error_key='error.html')
     xml = config.to_xml()
     self.assertIn(
         '<ErrorDocument><Key>error.html</Key></ErrorDocument>', xml)