From 042477ac62de6ded0a7b5e9f402735c1f590382f Mon Sep 17 00:00:00 2001 From: yangpeng <183851063@qq.com> Date: Mon, 3 Apr 2023 11:19:39 +0800 Subject: [PATCH] Add phpunit test and modify some bugs --- samples/BucketWebsite.php | 450 +++++++++++++++++- src/OSS/Model/WebsiteCondition.php | 122 +++++ src/OSS/Model/WebsiteConfig.php | 348 +++++++++++++- src/OSS/Model/WebsiteErrorDocument.php | 69 +++ src/OSS/Model/WebsiteIncludeHeader.php | 59 +++ src/OSS/Model/WebsiteIndexDocument.php | 86 ++++ src/OSS/Model/WebsiteMirrorHeaders.php | 136 ++++++ src/OSS/Model/WebsiteMirrorHeadersSet.php | 61 +++ src/OSS/Model/WebsiteRedirect.php | 315 ++++++++++++ src/OSS/Model/WebsiteRoutingRule.php | 85 ++++ src/OSS/OssClient.php | 8 +- src/OSS/Result/GetWebsiteResult.php | 14 +- tests/OSS/Tests/GetWebsiteResultTest.php | 246 ++++++++++ .../OSS/Tests/OssClientBucketWebsiteTest.php | 225 ++++++++- tests/OSS/Tests/WebsiteConfigTest.php | 275 ++++++++++- 15 files changed, 2457 insertions(+), 42 deletions(-) create mode 100644 src/OSS/Model/WebsiteCondition.php create mode 100644 src/OSS/Model/WebsiteErrorDocument.php create mode 100644 src/OSS/Model/WebsiteIncludeHeader.php create mode 100644 src/OSS/Model/WebsiteIndexDocument.php create mode 100644 src/OSS/Model/WebsiteMirrorHeaders.php create mode 100644 src/OSS/Model/WebsiteMirrorHeadersSet.php create mode 100644 src/OSS/Model/WebsiteRedirect.php create mode 100644 src/OSS/Model/WebsiteRoutingRule.php diff --git a/samples/BucketWebsite.php b/samples/BucketWebsite.php index 6c387e68..addf28fa 100644 --- a/samples/BucketWebsite.php +++ b/samples/BucketWebsite.php @@ -1,9 +1,18 @@ putBucketWebsite($bucket, $websiteConfig); Common::println("bucket $bucket websiteConfig created:" . $websiteConfig->serializeToXml()); @@ -24,12 +36,239 @@ $ossClient->deleteBucketWebsite($bucket); Common::println("bucket $bucket websiteConfig deleted"); -//******************************* For complete usage, see the following functions **************************************************** +// Set Mirror back to source configuration +$index->setSupportSubDir(false); +$index->setType(0); +$error->setHttpStatus(404); + +$websiteConfig = new WebsiteConfig($index, $error); + +$routingRule = new WebsiteRoutingRule(); + +$routingRule->setNumber(1); +$websiteCondition = new WebsiteCondition(); +$websiteCondition->setKeyPrefixEquals("examplebucket"); +$websiteCondition->setHttpErrorCodeReturnedEquals(404); + +$websiteRedirect = new WebsiteRedirect(); +$websiteRedirect->setRedirectType(WebsiteRedirect::MIRROR); +$websiteRedirect->setMirrorURL('https://www.example.com/'); +$websiteRedirect->setPassQueryString(true); +$websiteRedirect->setMirrorPassQueryString(true); +// Specifies the status code returned when jumping +//$websiteRedirect->setHttpRedirectCode(302); +// Specifies the domain name at jump time +//$websiteRedirect->setHostName("oss.aliyuncs.com"); +// Specifies the protocol at jump time. It takes effect only when redirecttype is set to external or alicdn. +//$websiteRedirect->setProtocol(WebsiteRedirect::HTTPS); +// During redirect, the object name will be replaced with the value specified by replacekeywith. Replacekeywith supports setting variables. +$websiteRedirect->setReplaceKeyWith('${key}.jpg'); +// If this field is set to true, the prefix of the object will be replaced with the value specified by replacekeyprefixwith. +$websiteRedirect->setEnableReplacePrefix(false); +// When redirect, the prefix of the object name will be replaced with this value. +//$websiteRedirect->setReplaceKeyPrefixWith('examplebucket'); +// Check MD5 of the source body +$websiteRedirect->setMirrorCheckMd5(true); + +$mirrorHeaders = new WebsiteMirrorHeaders(); +// Whether to transparently transmit headers other than the following headers to the source station + +$mirrorHeaders->setPassAll(true); + +$pass = 'cache-control-one'; +$passOne = 'pass-one'; +$mirrorHeaders->addPass($pass); +$mirrorHeaders->addPass($passOne); +$remove = 'remove-one'; +$removeOne = 'test-two'; +$mirrorHeaders->addRemove($remove); +$mirrorHeaders->addRemove($removeOne); + +$set = new WebsiteMirrorHeadersSet(); +$set->setKey("key1"); +$set->setValue("val1"); + +$mirrorHeaders->addSet($set); + +$setOne = new WebsiteMirrorHeadersSet(); +$setOne->setKey("key2"); +$setOne->setValue("val2"); + +$mirrorHeaders->addSet($setOne); + +$websiteRedirect->setMirrorHeaders($mirrorHeaders); + +$routingRule->setRedirect($websiteRedirect); +$routingRule->setCondition($websiteCondition); + +$websiteConfig->addRule($routingRule); + +$routingRuleOne = new WebsiteRoutingRule(); + +$routingRuleOne->setNumber(2); + +$websiteCondition = new WebsiteCondition(); + +$includeHeader = new WebsiteIncludeHeader(); +$includeHeader->setKey('host'); +$includeHeader->setEquals('test.oss-cn-beijing-internal.aliyuncs.com'); +$websiteCondition->addIncludeHeader($includeHeader); + +$includeHeaderOne = new WebsiteIncludeHeader(); +$includeHeaderOne->setKey('host_two'); +$includeHeaderOne->setEquals('demo.oss-cn-beijing-internal.aliyuncs.com'); +$websiteCondition->addIncludeHeader($includeHeaderOne); +$websiteCondition->setKeyPrefixEquals('abc/'); +$websiteCondition->setHttpErrorCodeReturnedEquals(404); +$routingRuleOne->setCondition($websiteCondition); + +$websiteRedirect = new WebsiteRedirect(); +$websiteRedirect->setRedirectType(WebsiteRedirect::ALICDN); +$websiteRedirect->setProtocol(WebsiteRedirect::HTTP); +$websiteRedirect->setPassQueryString(false); +$websiteRedirect->setReplaceKeyWith('prefix/${key}.jpg'); +$websiteRedirect->setEnableReplacePrefix(false); +$websiteRedirect->setHttpRedirectCode(301); +$routingRuleOne->setRedirect($websiteRedirect); +$websiteConfig->addRule($routingRuleOne); + +$routingRuleTwo = new WebsiteRoutingRule(); +$routingRuleTwo->setNumber(3); +$websiteCondition = new WebsiteCondition(); +$websiteCondition->setKeyPrefixEquals("abc/"); +$websiteCondition->setHttpErrorCodeReturnedEquals(404); +$routingRuleTwo->setCondition($websiteCondition); + +$websiteRedirect = new WebsiteRedirect(); +$websiteRedirect->setRedirectType(WebsiteRedirect::EXTERNAL); +$websiteRedirect->setProtocol(WebsiteRedirect::HTTPS); +$websiteRedirect->setHostName("demo.com"); +$websiteRedirect->setPassQueryString(false); +$websiteRedirect->setReplaceKeyWith('prefix/${key}'); +$websiteRedirect->setEnableReplacePrefix(false); +$websiteRedirect->setHttpRedirectCode(302); + +$routingRuleTwo->setRedirect($websiteRedirect); +$websiteConfig->addRule($routingRuleTwo); +$ossClient->putBucketWebsite($bucket,$websiteConfig); +Common::println("bucket $bucket websiteConfig created:" . $websiteConfig->serializeToXml()); + +// Get Mirror back to source configuration +$result = $ossClient->getBucketWebsite($bucket); + +if ($result->getIndexDocument() !== null){ + if ($result->getIndexDocument()->getSuffix()){ + Common::println("Index Document:" . $result->getIndexDocument()->getSuffix().PHP_EOL); + } + if ($result->getIndexDocument()->getSupportSubDir()){ + Common::println("Index Document Support Sub Dir:" . $result->getIndexDocument()->getSupportSubDir().PHP_EOL); + } + if ($result->getIndexDocument()->getType()){ + Common::println("Index Document Type:" . $result->getIndexDocument()->getType().PHP_EOL); + } +} + +if ($result->getErrorDocument() !== null){ + if ($result->getErrorDocument()->getKey()){ + Common::println("Error Document Key:" . $result->getErrorDocument()->getKey().PHP_EOL); + } + if ($result->getErrorDocument()->getHttpStatus()){ + Common::println("Error Document Http Status:" . $result->getErrorDocument()->getHttpStatus().PHP_EOL); + } + +} + + +if($result->getRoutingRules() !== null){ + foreach ($result->getRoutingRules() as $rule){ + Common::println("Routing Rule Number:" . $rule->getRuleNumber().PHP_EOL); + if($rule->getCondition()){ + if($rule->getCondition()->getKeyPrefixEquals()){ + Common::println("Routing Rule Condition Key Prefix Equals:" . $rule->getCondition()->getKeyPrefixEquals().PHP_EOL); + } + if($rule->getCondition()->getKeySuffixEquals()){ + Common::println("Routing Rule Condition Key Suffix Equals:" . $rule->getCondition()->getKeySuffixEquals().PHP_EOL); + } + if($rule->getCondition()->getHttpErrorCodeReturnedEquals()){ + Common::println("Routing Rule Condition Http Error Code Returned Equals:" . $rule->getCondition()->getHttpErrorCodeReturnedEquals().PHP_EOL); + } + if($rule->getCondition()->getIncludeHeader()){ + foreach ($rule->getCondition()->getIncludeHeader() as $headers){ + Common::println("Routing Rule Condition Include Headers Key:" . $headers->getKey().PHP_EOL); + Common::println("Routing Rule Condition Include Headers Equals:" . $headers->getEquals().PHP_EOL); + } + } + } + if($rule->getRedirect()){ + if($rule->getRedirect()->getRedirectType()){ + Common::println("Routing Rule Redirect Redirect Type:" . $rule->getRedirect()->getRedirectType().PHP_EOL); + } + if($rule->getRedirect()->getPassQueryString()){ + Common::println("Routing Rule Redirect Pass Query String:" . $rule->getRedirect()->getPassQueryString().PHP_EOL); + } + if($rule->getRedirect()->getProtocol()){ + Common::println("Routing Rule Redirect Protocol:" . $rule->getRedirect()->getProtocol().PHP_EOL); + } + if($rule->getRedirect()->getHostName()){ + Common::println("Routing Rule Redirect Host Name:" . $rule->getRedirect()->getHostName().PHP_EOL); + } + if($rule->getRedirect()->getReplaceKeyWith()){ + Common::println("Routing Rule Redirect Replace Key:" . $rule->getRedirect()->getReplaceKeyWith().PHP_EOL); + } + if($rule->getRedirect()->getEnableReplacePrefix()){ + Common::println("Routing Rule Redirect Replace Prefix:" . $rule->getRedirect()->getEnableReplacePrefix().PHP_EOL); + } + if($rule->getRedirect()->getReplaceKeyPrefixWith()){ + Common::println("Routing Rule Redirect Replace Key Prefix With:" . $rule->getRedirect()->getReplaceKeyPrefixWith().PHP_EOL); + } + if($rule->getRedirect()->getMirrorURL()){ + Common::println("Routing Rule Redirect Mirror Url:" . $rule->getRedirect()->getMirrorURL().PHP_EOL); + } + if($rule->getRedirect()->getHttpRedirectCode()){ + Common::println("Routing Rule Redirect Http Redirect Code:" . $rule->getRedirect()->getHttpRedirectCode().PHP_EOL); + } + if($rule->getRedirect()->getMirrorPassQueryString()){ + Common::println("Routing Rule Redirect Mirror Pass Query String:" . $rule->getRedirect()->getMirrorPassQueryString().PHP_EOL); + } + if($rule->getRedirect()->getMirrorFollowRedirect()){ + Common::println("Routing Rule Redirect Mirror Follow Redirect:" . $rule->getRedirect()->getMirrorFollowRedirect().PHP_EOL); + } + if($rule->getRedirect()->getMirrorCheckMd5()){ + Common::println("Routing Rule Redirect Mirror Check Md5:" . $rule->getRedirect()->getMirrorCheckMd5().PHP_EOL); + } + if($rule->getRedirect()->getMirrorHeaders()){ + $headerObject = $rule->getRedirect()->getMirrorHeaders(); + Common::println("Routing Rule Redirect Mirror Headers Pass All:" . $headerObject->getPassAll().PHP_EOL); + if($headerObject->getPass()){ + foreach ($headerObject->getPass() as $pass){ + Common::println("Routing Rule Redirect Mirror Headers Pass:" . $pass.PHP_EOL); + } + } + if($headerObject->getRemove()){ + foreach ($headerObject->getRemove() as $remove){ + Common::println("Routing Rule Redirect Mirror Headers Remove:" . $remove.PHP_EOL); + } + } + if($headerObject->getSet()){ + foreach ($headerObject->getSet() as $set){ + Common::println("Routing Rule Redirect Mirror Headers Set Key:" . $set->getKey().PHP_EOL); + Common::println("Routing Rule Redirect Mirror Headers Set Value:" . $set->getValue().PHP_EOL); + } + } + } + + } + } +} + +//******************************* For complete usage, see the following functions **************************************************** putBucketWebsite($ossClient, $bucket); getBucketWebsite($ossClient, $bucket); deleteBucketWebsite($ossClient, $bucket); getBucketWebsite($ossClient, $bucket); +putBucketWebsiteMirror($ossClient, $bucket); +getBucketWebsiteMirror($ossClient, $bucket); /** * Sets bucket static website configuration @@ -37,6 +276,7 @@ * @param $ossClient OssClient * @param $bucket string bucket name * @return null + * @throws RequestCore_Exception */ function putBucketWebsite($ossClient, $bucket) { @@ -57,10 +297,10 @@ function putBucketWebsite($ossClient, $bucket) * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null + * @throws RequestCore_Exception|OssException */ function getBucketWebsite($ossClient, $bucket) { - $websiteConfig = null; try { $websiteConfig = $ossClient->getBucketWebsite($bucket); } catch (OssException $e) { @@ -78,6 +318,7 @@ function getBucketWebsite($ossClient, $bucket) * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null + * @throws RequestCore_Exception */ function deleteBucketWebsite($ossClient, $bucket) { @@ -90,3 +331,206 @@ function deleteBucketWebsite($ossClient, $bucket) } print(__FUNCTION__ . ": OK" . "\n"); } + +/** + * Sets bucket static website configuration mirror + * @param $ossClient OssClient + * @param $bucket string bucket name + * @throws OssException|RequestCore_Exception + */ +function putBucketWebsiteMirror($ossClient, $bucket) +{ + try { + $index = new WebsiteIndexDocument("index.html"); + $error = new WebsiteErrorDocument("error.html"); + $websiteConfig = new WebsiteConfig($index, $error); + + $index->setSupportSubDir(false); + $index->setType(0); + $error->setHttpStatus(404); + + $routingRule = new WebsiteRoutingRule(); + $routingRule->setNumber(1); + $websiteCondition = new WebsiteCondition(); + $websiteCondition->setKeyPrefixEquals("examplebucket"); + $websiteCondition->setHttpErrorCodeReturnedEquals(404); + + $websiteRedirect = new WebsiteRedirect(); + $websiteRedirect->setRedirectType(WebsiteRedirect::MIRROR); + $websiteRedirect->setMirrorURL('https://www.example.com/'); + $websiteRedirect->setPassQueryString(true); + $websiteRedirect->setMirrorPassQueryString(true); + // Specifies the status code returned when jumping + //$websiteRedirect->setHttpRedirectCode(302); + // Specifies the domain name at jump time +// $websiteRedirect->setHostName("oss.aliyuncs.com"); + // Specifies the protocol at jump time. It takes effect only when redirecttype is set to external or alicdn. + //$websiteRedirect->setProtocol(WebsiteRedirect::HTTPS); + // During redirect, the object name will be replaced with the value specified by replacekeywith. Replacekeywith supports setting variables. + $websiteRedirect->setReplaceKeyWith('${key}.jpg'); + // If this field is set to true, the prefix of the object will be replaced with the value specified by replacekeyprefixwith. + $websiteRedirect->setEnableReplacePrefix(false); + // When redirect, the prefix of the object name will be replaced with this value. +// $websiteRedirect->setReplaceKeyPrefixWith('examplebucket'); + // Check MD5 of the source body + $websiteRedirect->setMirrorCheckMd5(true); + + $mirrorHeaders = new WebsiteMirrorHeaders(); + // Whether to transparently transmit headers other than the following headers to the source station + + $mirrorHeaders->setPassAll(true); + $pass = 'cache-control-one'; + $passOne = 'pass-one'; + $mirrorHeaders->addPass($pass); + $mirrorHeaders->addPass($passOne); + $remove = 'remove-one'; + $removeOne = 'test-two'; + $mirrorHeaders->addRemove($remove); + $mirrorHeaders->addRemove($removeOne); + + $set = new WebsiteMirrorHeadersSet(); + $set->setKey("key1"); + $set->setValue("val1"); + $mirrorHeaders->addSet($set); + + $setOne = new WebsiteMirrorHeadersSet(); + $setOne->setKey("key2"); + $setOne->setValue("val2"); + $mirrorHeaders->addSet($setOne); + + $websiteRedirect->setMirrorHeaders($mirrorHeaders); + $routingRule->setRedirect($websiteRedirect); + $routingRule->setCondition($websiteCondition); + + $websiteConfig->addRule($routingRule); + $ossClient->putBucketWebsite($bucket, $websiteConfig); + } catch (OssException $e) { + printf(__FUNCTION__ . ": FAILED\n"); + printf($e->getMessage() . "\n"); + return; + } + print(__FUNCTION__ . ": OK" . "\n"); + printf("bucket $bucket websiteConfig created:" . $websiteConfig->serializeToXml() . "\n"); +} + + +/** + * Get bucket static website configuration mirror + * @param $ossClient OssClient + * @param $bucket string bucket name + * @throws RequestCore_Exception + */ +function getBucketWebsiteMirror($ossClient, $bucket) +{ + try { + $result = $ossClient->getBucketWebsite($bucket); + if ($result->getIndexDocument() !== null){ + if ($result->getIndexDocument()->getSuffix()){ + Common::println("Index Document:" . $result->getIndexDocument()->getSuffix().PHP_EOL); + } + if ($result->getIndexDocument()->getSupportSubDir()){ + Common::println("Index Document Support Sub Dir:" . $result->getIndexDocument()->getSupportSubDir().PHP_EOL); + } + if ($result->getIndexDocument()->getType()){ + Common::println("Index Document Type:" . $result->getIndexDocument()->getType().PHP_EOL); + } + } + + if ($result->getErrorDocument() !== null){ + if ($result->getErrorDocument()->getKey()){ + Common::println("Error Document Key:" . $result->getErrorDocument()->getKey().PHP_EOL); + } + if ($result->getErrorDocument()->getHttpStatus()){ + Common::println("Error Document Http Status:" . $result->getErrorDocument()->getHttpStatus().PHP_EOL); + } + + } + + if($result->getRoutingRules() !== null){ + foreach ($result->getRoutingRules() as $rule){ + Common::println("Routing Rule Number:" . $rule->getRuleNumber().PHP_EOL); + if($rule->getCondition()){ + if($rule->getCondition()->getKeyPrefixEquals()){ + Common::println("Routing Rule Condition Key Prefix Equals:" . $rule->getCondition()->getKeyPrefixEquals().PHP_EOL); + } + if($rule->getCondition()->getKeySuffixEquals()){ + Common::println("Routing Rule Condition Key Suffix Equals:" . $rule->getCondition()->getKeySuffixEquals().PHP_EOL); + } + if($rule->getCondition()->getHttpErrorCodeReturnedEquals()){ + Common::println("Routing Rule Condition Http Error Code Returned Equals:" . $rule->getCondition()->getHttpErrorCodeReturnedEquals().PHP_EOL); + } + if($rule->getCondition()->getIncludeHeader()){ + foreach ($rule->getCondition()->getIncludeHeader() as $headers){ + Common::println("Routing Rule Condition Include Headers Key:" . $headers->getKey().PHP_EOL); + Common::println("Routing Rule Condition Include Headers Equals:" . $headers->getEquals().PHP_EOL); + } + } + } + if($rule->getRedirect()){ + if($rule->getRedirect()->getRedirectType()){ + Common::println("Routing Rule Redirect Redirect Type:" . $rule->getRedirect()->getRedirectType().PHP_EOL); + } + if($rule->getRedirect()->getPassQueryString()){ + Common::println("Routing Rule Redirect Pass Query String:" . $rule->getRedirect()->getPassQueryString().PHP_EOL); + } + if($rule->getRedirect()->getProtocol()){ + Common::println("Routing Rule Redirect Protocol:" . $rule->getRedirect()->getProtocol().PHP_EOL); + } + if($rule->getRedirect()->getHostName()){ + Common::println("Routing Rule Redirect Host Name:" . $rule->getRedirect()->getHostName().PHP_EOL); + } + if($rule->getRedirect()->getReplaceKeyWith()){ + Common::println("Routing Rule Redirect Replace Key:" . $rule->getRedirect()->getReplaceKeyWith().PHP_EOL); + } + if($rule->getRedirect()->getEnableReplacePrefix()){ + Common::println("Routing Rule Redirect Replace Prefix:" . $rule->getRedirect()->getEnableReplacePrefix().PHP_EOL); + } + if($rule->getRedirect()->getReplaceKeyPrefixWith()){ + Common::println("Routing Rule Redirect Replace Key Prefix With:" . $rule->getRedirect()->getReplaceKeyPrefixWith().PHP_EOL); + } + if($rule->getRedirect()->getMirrorURL()){ + Common::println("Routing Rule Redirect Mirror Url:" . $rule->getRedirect()->getMirrorURL().PHP_EOL); + } + if($rule->getRedirect()->getHttpRedirectCode()){ + Common::println("Routing Rule Redirect Http Redirect Code:" . $rule->getRedirect()->getHttpRedirectCode().PHP_EOL); + } + if($rule->getRedirect()->getMirrorPassQueryString()){ + Common::println("Routing Rule Redirect Mirror Pass Query String:" . $rule->getRedirect()->getMirrorPassQueryString().PHP_EOL); + } + if($rule->getRedirect()->getMirrorFollowRedirect()){ + Common::println("Routing Rule Redirect Mirror Follow Redirect:" . $rule->getRedirect()->getMirrorFollowRedirect().PHP_EOL); + } + if($rule->getRedirect()->getMirrorCheckMd5()){ + Common::println("Routing Rule Redirect Mirror Check Md5:" . $rule->getRedirect()->getMirrorCheckMd5().PHP_EOL); + } + if($rule->getRedirect()->getMirrorHeaders()){ + $headerObject = $rule->getRedirect()->getMirrorHeaders(); + Common::println("Routing Rule Redirect Mirror Headers Pass All:" . $headerObject->getPassAll().PHP_EOL); + if($headerObject->getPass()){ + foreach ($headerObject->getPass() as $pass){ + Common::println("Routing Rule Redirect Mirror Headers Pass:" . $pass.PHP_EOL); + } + } + if($headerObject->getRemove()){ + foreach ($headerObject->getRemove() as $remove){ + Common::println("Routing Rule Redirect Mirror Headers Remove:" . $remove.PHP_EOL); + } + } + if($headerObject->getSet()){ + foreach ($headerObject->getSet() as $set){ + Common::println("Routing Rule Redirect Mirror Headers Set Key:" . $set->getKey().PHP_EOL); + Common::println("Routing Rule Redirect Mirror Headers Set Value:" . $set->getValue().PHP_EOL); + } + } + } + + } + } + } + } catch (OssException $e) { + printf(__FUNCTION__ . ": FAILED\n"); + printf($e->getMessage() . "\n"); + return; + } + print(__FUNCTION__ . ": OK" . "\n"); +} diff --git a/src/OSS/Model/WebsiteCondition.php b/src/OSS/Model/WebsiteCondition.php new file mode 100644 index 00000000..8e088812 --- /dev/null +++ b/src/OSS/Model/WebsiteCondition.php @@ -0,0 +1,122 @@ +httpErrorCodeReturnedEquals; + } + + /** + * @param $httpErrorCodeReturnedEquals int + */ + public function setHttpErrorCodeReturnedEquals($httpErrorCodeReturnedEquals){ + $this->httpErrorCodeReturnedEquals = $httpErrorCodeReturnedEquals; + } + + /** + * @return string + */ + public function getKeyPrefixEquals(){ + return $this->keyPrefixEquals; + } + + /** + * @param $keyPrefixEquals string + */ + public function setKeyPrefixEquals($keyPrefixEquals){ + $this->keyPrefixEquals = $keyPrefixEquals; + } + + /** + * @return string + */ + public function getKeySuffixEquals(){ + return $this->keySuffixEquals; + } + + + /** + * @param $keySuffixEquals string + */ + public function setKeySuffixEquals($keySuffixEquals){ + $this->keySuffixEquals = $keySuffixEquals; + } + + /** + * @return WebsiteIncludeHeader[] + */ + public function getIncludeHeader(){ + return $this->includeHeader; + } + + + /** + * @param $includeHeaders WebsiteIncludeHeader + * @throws OssException + */ + public function addIncludeHeader($includeHeaders){ + if (isset($this->includeHeader) && count($this->includeHeader) >= self::OSS_MAX_HEADER) { + throw new OssException( + "num of include header in the config exceeds : " . strval(self::OSS_MAX_HEADER)); + } + $this->includeHeader[] = $includeHeaders; + } + + /** + * @param \SimpleXMLElement $xmlCondition + */ + public function appendToXml(&$xmlCondition) + { + if (isset($this->keyPrefixEquals)){ + $xmlCondition->addChild('KeyPrefixEquals', $this->keyPrefixEquals); + } + if (isset($this->httpErrorCodeReturnedEquals)){ + $xmlCondition->addChild('HttpErrorCodeReturnedEquals', $this->httpErrorCodeReturnedEquals); + } + if (isset($this->includeHeader)){ + foreach ($this->includeHeader as $includeHeader){ + $xmlIncludeHeader = $xmlCondition->addChild('IncludeHeader'); + $includeHeader->appendToXml($xmlIncludeHeader); + } + } + if (isset($this->keySuffixEquals)){ + $xmlCondition->addChild('KeySuffixEquals', $this->keySuffixEquals); + } + } + +} \ No newline at end of file diff --git a/src/OSS/Model/WebsiteConfig.php b/src/OSS/Model/WebsiteConfig.php index e298eb46..b4f01889 100644 --- a/src/OSS/Model/WebsiteConfig.php +++ b/src/OSS/Model/WebsiteConfig.php @@ -5,38 +5,298 @@ use OSS\Core\OssException; - /** * Class WebsiteConfig * @package OSS\Model - * @link http://help.aliyun.com/document_detail/oss/api-reference/bucket/PutBucketWebsite.html + * @link https://help.aliyun.com/document_detail/31970.html */ class WebsiteConfig implements XmlConfig { + /** + * @var WebsiteIndexDocument + */ + private $indexDocument; + /** + * @var WebsiteErrorDocument + */ + private $errorDocument; + /** + * @var WebsiteRoutingRule[] + */ + private $routingRules; + + const OSS_MAX_RULES = 20; + + private $suffix; + private $key; + /** * WebsiteConfig constructor. - * @param string $indexDocument - * @param string $errorDocument + * @param string|WebsiteIndexDocument $indexDocument + * @param string|WebsiteErrorDocument $errorDocument */ - public function __construct($indexDocument = "", $errorDocument = "") + public function __construct($indexDocument = null, $errorDocument = null) { + if (is_string($indexDocument)){ + if ($indexDocument != ""){ + $this->suffix = $indexDocument; + } + $this->indexDocument = new WebsiteIndexDocument(); + } + if (is_string($errorDocument)){ + if ($errorDocument != ""){ + $this->key = $errorDocument; + } + $this->errorDocument = new WebsiteErrorDocument(); + } + + if (is_object($indexDocument)){ + $this->indexDocument = $indexDocument; + } + if (is_object($errorDocument)){ + $this->errorDocument = $errorDocument; + } + + } + + /** + * @param WebsiteIndexDocument $indexDocument + */ + public function setIndexDocument($indexDocument){ $this->indexDocument = $indexDocument; + } + + /** + * @param WebsiteErrorDocument $errorDocument + */ + public function setErrorDocument($errorDocument){ $this->errorDocument = $errorDocument; } /** * @param string $strXml * @return null + * @throws \Exception */ public function parseFromXml($strXml) { - $xml = simplexml_load_string($strXml); - if (isset($xml->IndexDocument) && isset($xml->IndexDocument->Suffix)) { - $this->indexDocument = strval($xml->IndexDocument->Suffix); + $xml = new \SimpleXMLElement($strXml); + if (!isset($xml->IndexDocument) && !isset($xml->ErrorDocument) && !isset($xml->RoutingRules)) return; + if (isset($xml->IndexDocument)){ + $this->parseIndexDocument($xml->IndexDocument); + } + if (isset($xml->ErrorDocument)){ + $this->parseErrorDocument($xml->ErrorDocument); + } + if (isset($xml->RoutingRules)){ + $this->parseRoutingRules($xml->RoutingRules); + } + } + + /** + * @param $indexDocument \SimpleXMLElement + */ + private function parseIndexDocument($indexDocument) + { + if(isset($indexDocument)){ + $index = new WebsiteIndexDocument(); + if (isset($indexDocument->Suffix)){ + $index->setSuffix(strval($indexDocument->Suffix)); + } + if (isset($indexDocument->SupportSubDir)){ + $index->setSupportSubDir(strval($indexDocument->SupportSubDir) === "TRUE" || strval($indexDocument->SupportSubDir) === "true"); + } + if (isset($indexDocument->Type)){ + $index->setType(strval($indexDocument->Type)); + } + $this->setIndexDocument($index); + } + } + + /** + * @param $errorDocument \SimpleXMLElement + */ + private function parseErrorDocument($errorDocument) + { + if(isset($errorDocument)){ + $error = new WebsiteErrorDocument(); + if (isset($errorDocument->Key)){ + $error->setKey(strval($errorDocument->Key)); + } + if (isset($errorDocument->HttpStatus)){ + $error->setHttpStatus(intval($errorDocument->HttpStatus)); + } + $this->setErrorDocument($error); } - if (isset($xml->ErrorDocument) && isset($xml->ErrorDocument->Key)) { - $this->errorDocument = strval($xml->ErrorDocument->Key); + } + + /** + * @param $routingRules + * @throws OssException + */ + private function parseRoutingRules($routingRules) + { + if(isset($routingRules)){ + foreach ($routingRules->RoutingRule as $rule){ + $routingRule = new WebsiteRoutingRule(); + if (isset($rule->RuleNumber)){ + $routingRule->setNumber(intval($rule->RuleNumber)); + } + if (isset($rule->Condition)){ + $this->parseCondition($rule->Condition,$routingRule); + } + if (isset($rule->Redirect)){ + $this->parseRedirect($rule->Redirect,$routingRule); + } + $this->addRule($routingRule); + } } + + } + + /** + * @param $condition \SimpleXMLElement + * @param $websiteRoutingRule WebsiteRoutingRule + */ + private function parseCondition($condition,&$websiteRoutingRule) + { + $websiteCondition = new WebsiteCondition(); + if (isset($condition->KeyPrefixEquals)){ + $websiteCondition->setKeyPrefixEquals(strval($condition->KeyPrefixEquals)); + } + if (isset($condition->HttpErrorCodeReturnedEquals)){ + $websiteCondition->setHttpErrorCodeReturnedEquals(strval($condition->HttpErrorCodeReturnedEquals)); + } + if (isset($condition->KeySuffixEquals)){ + $websiteCondition->setKeySuffixEquals(strval($condition->KeySuffixEquals)); + } + if (isset($condition->IncludeHeader)){ + $this->parseIncludeHeaders($condition->IncludeHeader,$websiteCondition); + } + $websiteRoutingRule->setCondition($websiteCondition); + } + + + /** + * @param $includeHeaders \SimpleXMLElement + * @param $websiteCondition WebsiteCondition + */ + private function parseIncludeHeaders($includeHeaders,&$websiteCondition) + { + foreach ($includeHeaders as $header){ + $websiteIncludeHeader = new WebsiteIncludeHeader(); + if (isset($header->Key)){ + $websiteIncludeHeader->setKey(strval($header->Key)); + } + + if (isset($header->Equals)){ + $websiteIncludeHeader->setEquals(strval($header->Equals)); + } + + $websiteCondition->addIncludeHeader($websiteIncludeHeader); + } + } + + + /** + * @param $mirrorHeaders + * @param $websiteRedirect WebsiteRedirect + * @throws OssException + */ + private function parseMirrorHeaders($mirrorHeaders,&$websiteRedirect) + { + $websiteMirrorHeaders= new WebsiteMirrorHeaders(); + if (isset($mirrorHeaders->PassAll)){ + $websiteMirrorHeaders->setPassAll(strval($mirrorHeaders->PassAll) === 'true' || strval($mirrorHeaders->PassAll) === 'TRUE'); + } + + if (isset($mirrorHeaders->Pass)){ + foreach ($mirrorHeaders->Pass as $pass){ + $websiteMirrorHeaders->addPass(strval($pass)); + } + + } + + if (isset($mirrorHeaders->Remove)){ + foreach ($mirrorHeaders->Remove as $remove){ + $websiteMirrorHeaders->addRemove(strval($remove)); + } + } + + if (isset($mirrorHeaders->Set)){ + $this->parseSet($mirrorHeaders->Set,$websiteMirrorHeaders); + } + $websiteRedirect->setMirrorHeaders($websiteMirrorHeaders); + } + + + /** + * @param $set \SimpleXMLElement + * @param $websiteMirrorHeaders WebsiteMirrorHeaders + * @throws OssException + */ + private function parseSet($set,&$websiteMirrorHeaders) + { + if (isset($set)){ + foreach ($set as $headerSet){ + $websiteMirrorHeadersSet = new WebsiteMirrorHeadersSet(); + if (isset($headerSet->Key)){ + $websiteMirrorHeadersSet->setKey(strval($headerSet->Key)); + } + if (isset($headerSet->Value)){ + $websiteMirrorHeadersSet->setValue(strval($headerSet->Value)); + } + $websiteMirrorHeaders->addSet($websiteMirrorHeadersSet); + } + } + } + + /** + * @param $redirect \SimpleXMLElement + * @param $websiteRoutingRule WebsiteRoutingRule + * @throws OssException + */ + private function parseRedirect($redirect,&$websiteRoutingRule) + { + $websiteRedirect = new WebsiteRedirect(); + if (isset($redirect->RedirectType)){ + $websiteRedirect->setRedirectType(strval($redirect->RedirectType)); + } + if (isset($redirect->MirrorURL)){ + $websiteRedirect->setMirrorURL(strval($redirect->MirrorURL)); + } + if (isset($redirect->PassQueryString)){ + $websiteRedirect->setPassQueryString(strval($redirect->PassQueryString) === 'true' || strval($redirect->PassQueryString) === 'TRUE'); + } + if (isset($redirect->Protocol)){ + $websiteRedirect->setProtocol(strval($redirect->Protocol)); + } + if (isset($redirect->ReplaceKeyWith)){ + $websiteRedirect->setReplaceKeyWith(strval($redirect->ReplaceKeyWith)); + } + if (isset($redirect->HostName)){ + $websiteRedirect->setHostName(strval($redirect->HostName)); + } + if (isset($redirect->EnableReplacePrefix)){ + $websiteRedirect->setEnableReplacePrefix(strval($redirect->EnableReplacePrefix) === 'TRUE' || strval($redirect->EnableReplacePrefix) === 'true'); + } + if (isset($redirect->HttpRedirectCode)){ + $websiteRedirect->setHttpRedirectCode(strval($redirect->HttpRedirectCode)); + } + if (isset($redirect->MirrorPassQueryString)){ + $websiteRedirect->setMirrorPassQueryString(strval($redirect->MirrorPassQueryString) === "TRUE" || strval($redirect->MirrorPassQueryString) === "true"); + } + if (isset($redirect->MirrorFollowRedirect)){ + $websiteRedirect->setMirrorFollowRedirect(strval($redirect->MirrorFollowRedirect) === "TRUE" || strval($redirect->MirrorFollowRedirect) === "true" ); + } + if (isset($redirect->MirrorCheckMd5)){ + $websiteRedirect->setMirrorCheckMd5(strval($redirect->MirrorCheckMd5) === 'true' || strval($redirect->MirrorCheckMd5) === 'TRUE'); + } + + if (isset($redirect->MirrorHeaders)){ + $this->parseMirrorHeaders($redirect->MirrorHeaders,$websiteRedirect); + } + $websiteRoutingRule->setRedirect($websiteRedirect); } /** @@ -48,29 +308,73 @@ public function parseFromXml($strXml) public function serializeToXml() { $xml = new \SimpleXMLElement(''); - $index_document_part = $xml->addChild('IndexDocument'); - $error_document_part = $xml->addChild('ErrorDocument'); - $index_document_part->addChild('Suffix', $this->indexDocument); - $error_document_part->addChild('Key', $this->errorDocument); + + if (isset($this->indexDocument) || isset($this->suffix)){ + $xmlIndexDocument = $xml->addChild('IndexDocument'); + if (isset($this->suffix)){ + $xmlIndexDocument->addChild('Suffix', $this->suffix); + } + $this->indexDocument->appendToXml($xmlIndexDocument); + } + if (isset($this->errorDocument) || isset($this->key)) { + $xmlErrorDocument = $xml->addChild('ErrorDocument'); + if (isset($this->key)){ + $xmlErrorDocument->addChild('Key', $this->key); + } + $this->errorDocument->appendToXml($xmlErrorDocument); + } + if (isset($this->routingRules)){ + $xmlRoutingRules = $xml->addChild('RoutingRules'); + foreach ($this->routingRules as $rule){ + $xmlRoutingRule = $xmlRoutingRules->addChild('RoutingRule'); + $rule->appendToXml($xmlRoutingRule); + } + + } return $xml->asXML(); } /** - * @return string + * @param $rule WebsiteRoutingRule + * @throws OssException */ - public function getIndexDocument() - { + public function addRule($rule){ + if (isset($this->routingRules) && count($this->routingRules) >= self::OSS_MAX_RULES) { + throw new OssException( + "num of routing rules in the config exceeds : " . strval(self::OSS_MAX_RULES)); + } + $this->routingRules[] = $rule; + } + + /** + * @return WebsiteIndexDocument|null + */ + public function getIndexDocument(){ return $this->indexDocument; } /** - * @return string + * @return WebsiteErrorDocument|null */ - public function getErrorDocument() - { + public function getErrorDocument(){ return $this->errorDocument; } - private $indexDocument = ""; - private $errorDocument = ""; + /** + * @return WebsiteRoutingRule[] + */ + public function getRoutingRules(){ + return $this->routingRules; + } + + /** + * Serialize the object into xml string. + * + * @return string + * @throws OssException + */ + public function __toString() + { + return $this->serializeToXml(); + } } \ No newline at end of file diff --git a/src/OSS/Model/WebsiteErrorDocument.php b/src/OSS/Model/WebsiteErrorDocument.php new file mode 100644 index 00000000..9f2b916f --- /dev/null +++ b/src/OSS/Model/WebsiteErrorDocument.php @@ -0,0 +1,69 @@ +key = $key; + $this->httpStatus = $httpStatus; + } + + /** + * @param $key string + */ + public function setKey($key){ + $this->key = $key; + } + + + /** + * @return string + */ + public function getKey(){ + return $this->key; + } + + /** + * @param $httpStatus int + */ + public function setHttpStatus($httpStatus){ + $this->httpStatus = $httpStatus; + } + + /** + * @return string + */ + public function getHttpStatus(){ + return $this->httpStatus; + } + + /** + * @param \SimpleXMLElement $xmlErrorDocument + */ + public function appendToXml(&$xmlErrorDocument) + { + if (isset($this->key)){ + $xmlErrorDocument->addChild('Key', $this->key); + } + if (isset($this->httpStatus)){ + $xmlErrorDocument->addChild('HttpStatus', $this->httpStatus); + } + } + +} \ No newline at end of file diff --git a/src/OSS/Model/WebsiteIncludeHeader.php b/src/OSS/Model/WebsiteIncludeHeader.php new file mode 100644 index 00000000..b923dc18 --- /dev/null +++ b/src/OSS/Model/WebsiteIncludeHeader.php @@ -0,0 +1,59 @@ +key = $key; + } + + /** + * @param $equals string + */ + public function setEquals($equals){ + $this->equals = $equals; + } + + + /** + * @return string + */ + public function getKey(){ + return $this->key; + } + + /** + * @return string + */ + public function getEquals(){ + return $this->equals; + } + + + /** + * @param \SimpleXMLElement $xmlIncludeHeader + */ + public function appendToXml(&$xmlIncludeHeader) + { + if (isset($this->key)){ + $xmlIncludeHeader->addChild('Key', $this->key); + } + if (isset($this->equals)){ + $xmlIncludeHeader->addChild('Equals', $this->equals); + } + } + +} \ No newline at end of file diff --git a/src/OSS/Model/WebsiteIndexDocument.php b/src/OSS/Model/WebsiteIndexDocument.php new file mode 100644 index 00000000..5489746e --- /dev/null +++ b/src/OSS/Model/WebsiteIndexDocument.php @@ -0,0 +1,86 @@ +suffix = $suffix; + $this->supportSubDir = $supportSubDir; + $this->type = $type; + } + + /** + * @param $suffix string + */ + public function setSuffix($suffix){ + $this->suffix = $suffix; + } + + + /** + * @return string + */ + public function getSuffix(){ + return $this->suffix; + } + + /** + * @param $supportSubDir bool + */ + public function setSupportSubDir($supportSubDir){ + $this->supportSubDir = $supportSubDir; + } + + /** + * @return boolean + */ + public function getSupportSubDir(){ + return $this->supportSubDir; + } + + /** + * @param $type int + */ + public function setType($type){ + $this->type = $type; + } + + /** + * @return int + */ + public function getType(){ + return $this->type; + } + + /** + * @param \SimpleXMLElement $xmlIndexDocument + */ + public function appendToXml(&$xmlIndexDocument) + { + if (isset($this->suffix)){ + $xmlIndexDocument->addChild('Suffix', $this->suffix); + } + if (isset($this->supportSubDir)){ + $xmlIndexDocument->addChild('SupportSubDir', json_encode($this->supportSubDir)); + } + if (isset($this->type)){ + $xmlIndexDocument->addChild('Type', $this->type); + } + } + +} \ No newline at end of file diff --git a/src/OSS/Model/WebsiteMirrorHeaders.php b/src/OSS/Model/WebsiteMirrorHeaders.php new file mode 100644 index 00000000..637b9686 --- /dev/null +++ b/src/OSS/Model/WebsiteMirrorHeaders.php @@ -0,0 +1,136 @@ +passAll = $passAll; + } + + /** + * @param $pass string + * @throws OssException + */ + public function addPass($pass){ + if (isset($this->pass) && count($this->pass) >= self::OSS_MAX) { + throw new OssException( + "num of pass in the config exceeds : " . strval(self::OSS_MAX)); + } + $this->pass[] = $pass; + } + + /** + * @param string $remove + * @throws OssException + */ + public function addRemove($remove){ + if (isset($this->remove) && count($this->remove) >= self::OSS_MAX) { + throw new OssException( + "num of remove in the config exceeds : " . strval(self::OSS_MAX)); + } + $this->remove[] = $remove; + } + + /** + * @param $set WebsiteMirrorHeadersSet + * @throws OssException + */ + public function addSet($set){ + if (isset($this->set) && count($this->set) >= self::OSS_MAX) { + throw new OssException( + "num of set in the config exceeds : " . strval(self::OSS_MAX)); + } + $this->set[] = $set; + } + + /** + * @return boolean + */ + public function getPassAll(){ + return $this->passAll; + } + + /** + * @return array + */ + public function getPass(){ + return $this->pass; + } + + + /** + * @return array + */ + public function getRemove(){ + return $this->remove; + } + + + /** + * @return WebsiteMirrorHeadersSet[] + */ + public function getSet(){ + return $this->set; + } + + /** + * @param \SimpleXMLElement $xmlMirrorHeaders + */ + public function appendToXml(&$xmlMirrorHeaders) + { + if (isset($this->passAll)){ + $xmlMirrorHeaders->addChild('PassAll', json_encode($this->passAll)); + } + if (isset($this->pass)){ + foreach ($this->pass as $pass){ + $xmlMirrorHeaders->addChild('Pass', $pass); + } + } + if (isset($this->remove)){ + foreach ($this->remove as $remove){ + $xmlMirrorHeaders->addChild('Remove', $remove); + } + + } + if (isset($this->set)){ + foreach ($this->set as $set){ + $xmlSet = $xmlMirrorHeaders->addChild('Set'); + $set->appendToXml($xmlSet); + } + } + } + +} \ No newline at end of file diff --git a/src/OSS/Model/WebsiteMirrorHeadersSet.php b/src/OSS/Model/WebsiteMirrorHeadersSet.php new file mode 100644 index 00000000..d8cf4322 --- /dev/null +++ b/src/OSS/Model/WebsiteMirrorHeadersSet.php @@ -0,0 +1,61 @@ +key = $key; + } + + /** + * @return string + */ + public function getKey(){ + return $this->key; + } + /** + * @param $value string + */ + public function setValue($value){ + $this->value = $value; + } + + /** + * @return string + */ + public function getValue(){ + return $this->value; + } + + /** + * @param \SimpleXMLElement $xmlSet + */ + public function appendToXml(&$xmlSet) + { + if (isset($this->key)){ + $xmlSet->addChild('Key', $this->key); + } + if (isset($this->value)){ + $xmlSet->addChild('Value', $this->value); + } + } + +} \ No newline at end of file diff --git a/src/OSS/Model/WebsiteRedirect.php b/src/OSS/Model/WebsiteRedirect.php new file mode 100644 index 00000000..87398190 --- /dev/null +++ b/src/OSS/Model/WebsiteRedirect.php @@ -0,0 +1,315 @@ +redirectType; + } + + + /** + * @param string $type Mirror|External|AliCDN + */ + public function setRedirectType($type){ + $this->redirectType = $type; + } + + /** + * @param $mirrorUrl + */ + public function setMirrorURL($mirrorUrl){ + $this->mirrorURL= $mirrorUrl; + } + + /** + * @return boolean + */ + public function getPassQueryString(){ + return $this->passQueryString; + } + + /** + * @param $passQueryString bool + */ + public function setPassQueryString($passQueryString){ + $this->passQueryString = $passQueryString; + } + + /** + * @return string + */ + public function getProtocol(){ + return $this->protocol; + } + + /** + * @param $protocol string + */ + public function setProtocol($protocol){ + $this->protocol = $protocol; + } + + + /** + * @return string + */ + public function getHostName(){ + return $this->hostName; + } + + /** + * @param $hostName string + */ + public function setHostName($hostName){ + $this->hostName = $hostName; + } + + /** + * @return string + */ + public function getReplaceKeyWith(){ + return $this->replaceKeyWith; + } + + /** + * @param $replaceKeyWith string + */ + public function setReplaceKeyWith($replaceKeyWith){ + $this->replaceKeyWith = $replaceKeyWith; + } + + /** + * @param $replaceKeyPrefixWith string + */ + public function setReplaceKeyPrefixWith($replaceKeyPrefixWith){ + $this->replaceKeyPrefixWith = $replaceKeyPrefixWith; + } + + /** + * @return string + */ + public function getReplaceKeyPrefixWith(){ + return $this->replaceKeyPrefixWith; + } + + /** + * @return boolean + */ + public function getEnableReplacePrefix(){ + return $this->enableReplacePrefix; + } + + + /** + * @param $enableReplacePrefix boolean + */ + public function setEnableReplacePrefix($enableReplacePrefix){ + $this->enableReplacePrefix = $enableReplacePrefix; + } + + /** + * @return string + */ + public function getMirrorURL(){ + return $this->mirrorURL; + } + + /** + * @return int + */ + public function getHttpRedirectCode(){ + return $this->httpRedirectCode; + } + + + /** + * @param $httpRedirectCode int + */ + public function setHttpRedirectCode($httpRedirectCode){ + $this->httpRedirectCode = $httpRedirectCode; + } + + /** + * @return boolean + */ + public function getMirrorPassQueryString(){ + return $this->mirrorPassQueryString; + } + + /** + * @param $mirrorPassQueryString boolean + */ + public function setMirrorPassQueryString($mirrorPassQueryString){ + $this->mirrorPassQueryString = $mirrorPassQueryString; + } + + /** + * @return boolean + */ + public function getMirrorFollowRedirect(){ + return $this->mirrorFollowRedirect; + } + + /** + * @param $mirrorFollowRedirect boolean + */ + public function setMirrorFollowRedirect($mirrorFollowRedirect){ + $this->mirrorFollowRedirect = $mirrorFollowRedirect; + } + + /** + * @return boolean + */ + public function getMirrorCheckMd5(){ + return $this->mirrorCheckMd5; + } + + /** + * @param $mirrorCheckMd5 boolean + */ + public function setMirrorCheckMd5($mirrorCheckMd5){ + $this->mirrorCheckMd5 = $mirrorCheckMd5; + } + + /** + * @return WebsiteMirrorHeaders + */ + public function getMirrorHeaders(){ + return $this->mirrorHeaders; + } + /** + * @param $mirrorHeaders WebsiteMirrorHeaders + */ + public function setMirrorHeaders($mirrorHeaders){ + $this->mirrorHeaders = $mirrorHeaders; + } + + /** + * @param \SimpleXMLElement $xmlRedirect + */ + public function appendToXml(&$xmlRedirect) + { + + if (isset($this->redirectType)){ + $xmlRedirect->addChild("RedirectType",$this->redirectType); + } + if (isset($this->passQueryString)){ + $xmlRedirect->addChild("PassQueryString",json_encode($this->passQueryString)); + } + if (isset($this->mirrorURL)){ + $xmlRedirect->addChild("MirrorURL",$this->mirrorURL); + } + if (isset($this->mirrorPassQueryString)){ + $xmlRedirect->addChild("MirrorPassQueryString",json_encode($this->mirrorPassQueryString)); + } + if (isset($this->mirrorFollowRedirect)){ + $xmlRedirect->addChild("MirrorFollowRedirect",json_encode($this->mirrorFollowRedirect)); + } + if (isset($this->mirrorCheckMd5)){ + $xmlRedirect->addChild("MirrorCheckMd5",json_encode($this->mirrorCheckMd5)); + } + if (isset($this->mirrorHeaders)){ + $xmlMirrorHeaders = $xmlRedirect->addChild("MirrorHeaders"); + $this->mirrorHeaders->appendToXml($xmlMirrorHeaders); + } + if (isset($this->protocol)){ + $xmlRedirect->addChild("Protocol",$this->protocol); + } + if (isset($this->hostName)){ + $xmlRedirect->addChild("HostName",$this->hostName); + } + if (isset($this->replaceKeyPrefixWith)){ + $xmlRedirect->addChild("ReplaceKeyPrefixWith",$this->replaceKeyPrefixWith); + } + if (isset($this->enableReplacePrefix)){ + $xmlRedirect->addChild("EnableReplacePrefix",json_encode($this->enableReplacePrefix)); + } + if (isset($this->replaceKeyWith)){ + $xmlRedirect->addChild("ReplaceKeyWith",$this->replaceKeyWith); + } + if (isset($this->httpRedirectCode)){ + $xmlRedirect->addChild("HttpRedirectCode",$this->httpRedirectCode); + } + } +} diff --git a/src/OSS/Model/WebsiteRoutingRule.php b/src/OSS/Model/WebsiteRoutingRule.php new file mode 100644 index 00000000..94ed9d5f --- /dev/null +++ b/src/OSS/Model/WebsiteRoutingRule.php @@ -0,0 +1,85 @@ +ruleNumber = $number; + } + + /** + * @return int + */ + public function getRuleNumber(){ + return $this->ruleNumber; + } + + /** + * @param $condition WebsiteCondition + */ + public function setCondition($condition){ + $this->condition = $condition; + } + + /** + * @return WebsiteCondition + */ + public function getCondition(){ + return $this->condition; + } + + /** + * @param $redirect WebsiteRedirect + */ + public function setRedirect($redirect){ + $this->redirect = $redirect; + } + + /** + * @return WebsiteRedirect + */ + public function getRedirect(){ + return $this->redirect; + } + + /** + * @param \SimpleXMLElement $xmlRoutingRule + */ + public function appendToXml(&$xmlRoutingRule) + { + if (isset($this->ruleNumber)){ + $xmlRoutingRule->addChild('RuleNumber', $this->ruleNumber); + } + if (isset($this->condition)){ + $xmlCondition = $xmlRoutingRule->addChild('Condition'); + $this->condition->appendToXml($xmlCondition); + + } + if (isset($this->redirect)){ + $xmlRedirect = $xmlRoutingRule->addChild('Redirect'); + $this->redirect->appendToXml($xmlRedirect); + } + } + +} \ No newline at end of file diff --git a/src/OSS/OssClient.php b/src/OSS/OssClient.php index 0922a0b7..33cfbbb3 100644 --- a/src/OSS/OssClient.php +++ b/src/OSS/OssClient.php @@ -407,8 +407,8 @@ public function deleteBucketLogging($bucket, $options = NULL) * @param string $bucket bucket name * @param WebsiteConfig $websiteConfig * @param array $options - * @throws OssException * @return null + * @throws OssException|RequestCore_Exception */ public function putBucketWebsite($bucket, $websiteConfig, $options = NULL) { @@ -429,8 +429,8 @@ public function putBucketWebsite($bucket, $websiteConfig, $options = NULL) * * @param string $bucket bucket name * @param array $options - * @throws OssException - * @return WebsiteConfig + * @return WebsiteConfig|null + * @throws OssException|RequestCore_Exception */ public function getBucketWebsite($bucket, $options = NULL) { @@ -449,8 +449,8 @@ public function getBucketWebsite($bucket, $options = NULL) * * @param string $bucket bucket name * @param array $options - * @throws OssException * @return null + * @throws OssException|RequestCore_Exception */ public function deleteBucketWebsite($bucket, $options = NULL) { diff --git a/src/OSS/Result/GetWebsiteResult.php b/src/OSS/Result/GetWebsiteResult.php index 64d54fac..ab32383e 100644 --- a/src/OSS/Result/GetWebsiteResult.php +++ b/src/OSS/Result/GetWebsiteResult.php @@ -2,6 +2,7 @@ namespace OSS\Result; +use OSS\Core\OssException; use OSS\Model\WebsiteConfig; /** @@ -11,16 +12,19 @@ class GetWebsiteResult extends Result { /** - * Parse WebsiteConfig data - * * @return WebsiteConfig + * @throws OssException */ protected function parseDataFromResponse() { $content = $this->rawResponse->body; - $config = new WebsiteConfig(); - $config->parseFromXml($content); - return $config; + if (empty($content)) { + throw new OssException("body is null"); + } + + $websiteConfig = new WebsiteConfig(); + $websiteConfig->parseFromXml($content); + return $websiteConfig; } /** diff --git a/tests/OSS/Tests/GetWebsiteResultTest.php b/tests/OSS/Tests/GetWebsiteResultTest.php index d8533066..bb018a50 100644 --- a/tests/OSS/Tests/GetWebsiteResultTest.php +++ b/tests/OSS/Tests/GetWebsiteResultTest.php @@ -3,9 +3,12 @@ namespace OSS\Tests; +use OSS\Model\WebsiteRedirect; +use OSS\Model\WebsiteRoutingRules; use OSS\Result\GetWebsiteResult; use OSS\Http\ResponseCore; use OSS\Core\OssException; +use OSS\Result\Result; class GetWebsiteResultTest extends \PHPUnit\Framework\TestCase { @@ -37,6 +40,249 @@ private function cleanXml($xml) return str_replace("\n", "", str_replace("\r", "", $xml)); } + + private $oneXml = << + + + index.html + 0 + false + + + error.html + 404 + + + + 1 + + examplebucket + 404 + + + Mirror + https://www.example.com/ + true + true + key.jpg + false + true + + true + cache-control-one + pass-one + remove-one + test-two + + key1 + val1 + + + + + + 2 + + abc/ + 404 + + host + test.oss-cn-beijing-internal.aliyuncs.com + + + + AliCDN + http + false + prefix/key.jpg + 301 + + true + cache-control-one + pass-one + remove-one + test-two + + key1 + val1 + + + key2 + val2 + + + + + + +BBBB; + + + public function testValidWithMirrorOne(){ + $response = new ResponseCore(array(), $this->oneXml, 200); + $result = new GetWebsiteResult($response); + $this->assertTrue($result->isOK()); + $this->assertNotNull($result->getData()); + $this->assertNotNull($result->getRawResponse()); + $websiteConfig2 = $result->getData(); + + $this->assertEquals("index.html", $websiteConfig2->getIndexDocument()->getSuffix()); + $this->assertEquals("error.html", $websiteConfig2->getErrorDocument()->getKey()); + $this->assertEquals(false,$websiteConfig2->getIndexDocument()->getSupportSubDir()); + $this->assertEquals(0, $websiteConfig2->getIndexDocument()->getType()); + $this->assertEquals(404, $websiteConfig2->getErrorDocument()->getHttpStatus()); + + $rule = $websiteConfig2->getRoutingRules(); + + // 1.test Condition + $this->assertEquals("examplebucket", $rule[0]->getCondition()->getKeyPrefixEquals()); + $this->assertEquals(404, $rule[0]->getCondition()->getHttpErrorCodeReturnedEquals()); + + $includeObject = $rule[1]->getCondition()->getIncludeHeader(); + $this->assertEquals('host',$includeObject[0]->getKey()); + $this->assertEquals('test.oss-cn-beijing-internal.aliyuncs.com',$includeObject[0]->getEquals()); + + // 2.test Redirect + $this->assertEquals("Mirror", $rule[0]->getRedirect()->getRedirectType()); + $this->assertEquals(true,$rule[0]->getRedirect()->getMirrorPassQueryString()); + $this->assertEquals("https://www.example.com/", $rule[0]->getRedirect()->getMirrorURL()); + $this->assertEquals(true,$rule[0]->getRedirect()->getMirrorPassQueryString()); + $this->assertEquals('key.jpg', $rule[0]->getRedirect()->getReplaceKeyWith()); + $this->assertEquals(false,$rule[0]->getRedirect()->getEnableReplacePrefix()); + $this->assertEquals(true,$rule[0]->getRedirect()->getMirrorCheckMd5()); + + + // 2.1 test Redirect mirror header + $headerObject = $rule[0]->getRedirect()->getMirrorHeaders(); + $this->assertEquals('pass-one',$headerObject->getPass()[1]); + $this->assertEquals('remove-one',$headerObject->getRemove()[0]); + $this->assertEquals('key1',$headerObject->getSet()[0]->getKey()); + $this->assertEquals('val1',$headerObject->getSet()[0]->getValue()); + + $headerObject2 = $rule[1]->getRedirect()->getMirrorHeaders(); + + $this->assertEquals('key2',$headerObject2->getSet()[1]->getKey()); + $this->assertEquals('val2',$headerObject2->getSet()[1]->getValue()); + + } + +private $twoXml = << + + +index.html + + +error.html + + + +1 + +abc/ +404 + +host +test.oss-cn-beijing-internal.aliyuncs.com + + +host_two +demo.oss-cn-beijing-internal.aliyuncs.com + + + +AliCDN +http +false +prefix/key.jpg +301 + + + + +BBBB; + + public function testValidWithMirrorTwo(){ + $response = new ResponseCore(array(), $this->twoXml, 200); + $result = new GetWebsiteResult($response); + $this->assertTrue($result->isOK()); + $this->assertNotNull($result->getData()); + $this->assertNotNull($result->getRawResponse()); + $websiteConfig2 = $result->getData(); + $this->assertEquals("index.html", $websiteConfig2->getIndexDocument()->getSuffix()); + $this->assertEquals("error.html", $websiteConfig2->getErrorDocument()->getKey()); + + $rule = $websiteConfig2->getRoutingRules(); + // 1.test Condition + $this->assertEquals("abc/", $rule[0]->getCondition()->getKeyPrefixEquals()); + $this->assertEquals(404, $rule[0]->getCondition()->getHttpErrorCodeReturnedEquals()); + // 1.1 test Condition Inclue Header + $headerObject = $rule[0]->getCondition()->getIncludeHeader(); + $this->assertEquals('host',$headerObject[0]->getKey()); + $this->assertEquals('demo.oss-cn-beijing-internal.aliyuncs.com',$headerObject[1]->getEquals()); + + // 2.test Redirect + $this->assertEquals(WebsiteRedirect::ALICDN, $rule[0]->getRedirect()->getRedirectType()); + $this->assertEquals(WebsiteRedirect::HTTP, $rule[0]->getRedirect()->getProtocol()); + $this->assertEquals(false,$rule[0]->getRedirect()->getPassQueryString()); + $this->assertEquals(301, $rule[0]->getRedirect()->getHttpRedirectCode()); + $this->assertEquals('prefix/key.jpg', $rule[0]->getRedirect()->getReplaceKeyWith()); + } + + + private $xml3 = << + + + index.html + + + error.html + + + + 1 + + 404 + + + prefix/key + 302 + false + false + External + https + demo.com + + + + +bbbbb; + + public function testXmlWithMirrorThree(){ + $response = new ResponseCore(array(), $this->xml3, 200); + $result = new GetWebsiteResult($response); + $this->assertTrue($result->isOK()); + $this->assertNotNull($result->getData()); + $this->assertNotNull($result->getRawResponse()); + $websiteConfig2 = $result->getData(); + $this->assertEquals("index.html", $websiteConfig2->getIndexDocument()->getSuffix()); + $this->assertEquals("error.html", $websiteConfig2->getErrorDocument()->getKey()); + $rule = $websiteConfig2->getRoutingRules(); + // 1.test Condition + $this->assertEquals(404, $rule[0]->getCondition()->getHttpErrorCodeReturnedEquals()); + // 2.test Redirect + $this->assertEquals(WebsiteRedirect::EXTERNAL, $rule[0]->getRedirect()->getRedirectType()); + $this->assertEquals(WebsiteRedirect::HTTPS, $rule[0]->getRedirect()->getProtocol()); + $this->assertEquals("demo.com", $rule[0]->getRedirect()->getHostName()); + $this->assertEquals(false,$rule[0]->getRedirect()->getPassQueryString()); + $this->assertEquals(false,$rule[0]->getRedirect()->getEnableReplacePrefix()); + $this->assertEquals(302, $rule[0]->getRedirect()->getHttpRedirectCode()); + $this->assertEquals('prefix/key', $rule[0]->getRedirect()->getReplaceKeyWith()); + + } + public function testInvalidResponse() { $response = new ResponseCore(array(), $this->validXml, 300); diff --git a/tests/OSS/Tests/OssClientBucketWebsiteTest.php b/tests/OSS/Tests/OssClientBucketWebsiteTest.php index dfa9cc17..0f04b7dc 100644 --- a/tests/OSS/Tests/OssClientBucketWebsiteTest.php +++ b/tests/OSS/Tests/OssClientBucketWebsiteTest.php @@ -3,17 +3,109 @@ namespace OSS\Tests; use OSS\Core\OssException; +use OSS\Model\WebsiteCondition; use OSS\Model\WebsiteConfig; +use OSS\Model\WebsiteErrorDocument; +use OSS\Model\WebsiteIncludeHeader; +use OSS\Model\WebsiteIndexDocument; +use OSS\Model\WebsiteMirrorHeaders; +use OSS\Model\WebsiteMirrorHeadersSet; +use OSS\Model\WebsiteRedirect; +use OSS\Model\WebsiteRoutingRule; require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php'; class OssClientBucketWebsiteTest extends TestOssClientBase { - public function testBucket() + public function testBucketSample() { - $websiteConfig = new WebsiteConfig("index.html", "error.html"); + $index = new WebsiteIndexDocument("index.html"); + $error = new WebsiteErrorDocument("error.html"); + $websiteConfig = new WebsiteConfig($index, $error); + try { + $this->ossClient->putBucketWebsite($this->bucket, $websiteConfig); + } catch (OssException $e) { + var_dump($e->getMessage()); + $this->assertTrue(false); + } + + try { + Common::waitMetaSync(); + $websiteConfig2 = $this->ossClient->getBucketWebsite($this->bucket); + $this->assertEquals("index.html", $websiteConfig2->getIndexDocument()->getSuffix()); + $this->assertEquals("error.html", $websiteConfig2->getErrorDocument()->getKey()); + } catch (OssException $e) { + $this->assertTrue(false); + } + try { + Common::waitMetaSync(); + $this->ossClient->deleteBucketWebsite($this->bucket); + } catch (OssException $e) { + $this->assertTrue(false); + } + try { + Common::waitMetaSync(); + $websiteConfig3 = $this->ossClient->getBucketLogging($this->bucket); + $this->assertNotEquals($websiteConfig->serializeToXml(), $websiteConfig3->serializeToXml()); + } catch (OssException $e) { + $this->assertTrue(false); + } + } + + + public function testBucketWithMirrorOne(){ + $index = new WebsiteIndexDocument("index.html"); + $error = new WebsiteErrorDocument("error.html"); + $index->setSupportSubDir(false); + $index->setType(0); + $error->setHttpStatus(404); + $websiteConfig = new WebsiteConfig($index, $error); + + $routingRule = new WebsiteRoutingRule(); + $routingRule->setNumber(1); + $websiteCondition = new WebsiteCondition(); + $websiteCondition->setKeyPrefixEquals("abc"); + $websiteCondition->setHttpErrorCodeReturnedEquals(404); + $routingRule->setCondition($websiteCondition); + + $websiteRedirect = new WebsiteRedirect(); + $websiteRedirect->setRedirectType(WebsiteRedirect::MIRROR); + $websiteRedirect->setPassQueryString(true); + $websiteRedirect->setMirrorURL('https://www.example.com/'); + $websiteRedirect->setMirrorPassQueryString(true); + $websiteRedirect->setMirrorFollowRedirect(true); + $websiteRedirect->setMirrorCheckMd5(true); + + $mirrorHeaders = new WebsiteMirrorHeaders(); + $mirrorHeaders->setPassAll(true); + $pass = 'cache-control-one'; + $passOne = 'pass-one'; + $mirrorHeaders->addPass($pass); + $mirrorHeaders->addPass($passOne); + $remove = 'remove-one'; + $removeOne = 'test-two'; + $mirrorHeaders->addRemove($remove); + $mirrorHeaders->addRemove($removeOne); + + $set = new WebsiteMirrorHeadersSet(); + $set->setKey("key1"); + $set->setValue("val1"); + + $mirrorHeaders->addSet($set); + + $setOne = new WebsiteMirrorHeadersSet(); + $setOne->setKey("key2"); + $setOne->setValue("val2"); + + $mirrorHeaders->addSet($setOne); + $websiteRedirect->setMirrorHeaders($mirrorHeaders); +// $websiteRedirect->setReplaceKeyPrefixWith('def/'); + $websiteRedirect->setEnableReplacePrefix(false); + $routingRule->setRedirect($websiteRedirect); + + $websiteConfig->addRule($routingRule); try { $this->ossClient->putBucketWebsite($this->bucket, $websiteConfig); @@ -25,10 +117,13 @@ public function testBucket() try { Common::waitMetaSync(); $websiteConfig2 = $this->ossClient->getBucketWebsite($this->bucket); + print_r($websiteConfig2->serializeToXml()); + print_r($websiteConfig->serializeToXml()); $this->assertEquals($websiteConfig->serializeToXml(), $websiteConfig2->serializeToXml()); } catch (OssException $e) { $this->assertTrue(false); } + try { Common::waitMetaSync(); $this->ossClient->deleteBucketWebsite($this->bucket); @@ -42,5 +137,131 @@ public function testBucket() } catch (OssException $e) { $this->assertTrue(false); } + + } + + + public function testBucketWithMirrorTwo(){ + $index = new WebsiteIndexDocument("index.html"); + $error = new WebsiteErrorDocument("error.html"); + $index->setSupportSubDir(false); + $index->setType(0); + $error->setHttpStatus(404); + $websiteConfig = new WebsiteConfig($index, $error); + + $routingRule = new WebsiteRoutingRule(); + + $routingRule->setNumber(1); + $websiteCondition = new WebsiteCondition(); + $websiteCondition->setKeyPrefixEquals("examplebucket"); + $websiteCondition->setHttpErrorCodeReturnedEquals(404); + $routingRule->setCondition($websiteCondition); + + $websiteRedirect = new WebsiteRedirect(); + $websiteRedirect->setRedirectType(WebsiteRedirect::MIRROR); + $websiteRedirect->setPassQueryString(true); + $websiteRedirect->setMirrorURL('https://www.example.com/'); + $websiteRedirect->setMirrorPassQueryString(true); + $websiteRedirect->setMirrorFollowRedirect(true); + $websiteRedirect->setMirrorCheckMd5(true); + + $mirrorHeaders = new WebsiteMirrorHeaders(); + $mirrorHeaders->setPassAll(true); + + $mirrorHeaders = new WebsiteMirrorHeaders(); + $mirrorHeaders->setPassAll(true); + $pass = 'cache-control-one'; + $passOne = 'pass-one'; + $mirrorHeaders->addPass($pass); + $mirrorHeaders->addPass($passOne); + $remove = 'remove-one'; + $removeOne = 'test-two'; + $mirrorHeaders->addRemove($remove); + $mirrorHeaders->addRemove($removeOne); + + $set = new WebsiteMirrorHeadersSet(); + $set->setKey("key1"); + $set->setValue("val1"); + + $mirrorHeaders->addSet($set); + $websiteRedirect->setMirrorHeaders($mirrorHeaders); +// $websiteRedirect->setReplaceKeyPrefixWith('examplebucket'); + $websiteRedirect->setEnableReplacePrefix(true); + $routingRule->setRedirect($websiteRedirect); + $websiteConfig->addRule($routingRule); + + $routingRuleOne = new WebsiteRoutingRule(); + $routingRuleOne->setNumber(2); + $websiteCondition = new WebsiteCondition(); + $includeHeader = new WebsiteIncludeHeader(); + $includeHeader->setKey('host'); + $includeHeader->setEquals('test.oss-cn-beijing-internal.aliyuncs.com'); + $websiteCondition->addIncludeHeader($includeHeader); + $includeHeader->setKey('host_two'); + $includeHeader->setEquals('demo.oss-cn-beijing-internal.aliyuncs.com'); + $websiteCondition->addIncludeHeader($includeHeader); + $websiteCondition->setKeyPrefixEquals('abc/'); + $websiteCondition->setHttpErrorCodeReturnedEquals(404); + $routingRuleOne->setCondition($websiteCondition); + $websiteRedirect = new WebsiteRedirect(); + $websiteRedirect->setRedirectType(WebsiteRedirect::ALICDN); + $websiteRedirect->setProtocol(WebsiteRedirect::HTTP); + $websiteRedirect->setPassQueryString(false); +// $websiteRedirect->setReplaceKeyWith('prefix/${key}.jpg'); + $websiteRedirect->setEnableReplacePrefix(false); + $websiteRedirect->setHttpRedirectCode(301); + $routingRuleOne->setRedirect($websiteRedirect); + $websiteConfig->addRule($routingRuleOne); + + $routingRuleTwo = new WebsiteRoutingRule(); + $routingRuleTwo->setNumber(3); + $websiteCondition = new WebsiteCondition(); + $websiteCondition->setKeyPrefixEquals("abc/"); + $websiteCondition->setHttpErrorCodeReturnedEquals(404); + $routingRuleTwo->setCondition($websiteCondition); + + $websiteRedirect = new WebsiteRedirect(); + $websiteRedirect->setRedirectType(WebsiteRedirect::EXTERNAL); + $websiteRedirect->setProtocol(WebsiteRedirect::HTTPS); + $websiteRedirect->setHostName("demo.com"); + $websiteRedirect->setPassQueryString(false); +// $websiteRedirect->setReplaceKeyWith('prefix/${key}'); + $websiteRedirect->setEnableReplacePrefix(false); + $websiteRedirect->setHttpRedirectCode(302); + + $routingRuleTwo->setRedirect($websiteRedirect); + $websiteConfig->addRule($routingRuleTwo); + + + try { + $this->ossClient->putBucketWebsite($this->bucket, $websiteConfig); + } catch (OssException $e) { + var_dump($e->getMessage()); + $this->assertTrue(false); + } + + + try { + Common::waitMetaSync(); + $websiteConfig2 = $this->ossClient->getBucketWebsite($this->bucket); + $this->assertEquals($websiteConfig->serializeToXml(), $websiteConfig2->serializeToXml()); + } catch (OssException $e) { + $this->assertTrue(false); + } + + try { + Common::waitMetaSync(); + $this->ossClient->deleteBucketWebsite($this->bucket); + } catch (OssException $e) { + $this->assertTrue(false); + } + try { + Common::waitMetaSync(); + $websiteConfig3 = $this->ossClient->getBucketLogging($this->bucket); + $this->assertNotEquals($websiteConfig->serializeToXml(), $websiteConfig3->serializeToXml()); + } catch (OssException $e) { + $this->assertTrue(false); + } + } } diff --git a/tests/OSS/Tests/WebsiteConfigTest.php b/tests/OSS/Tests/WebsiteConfigTest.php index d04b42c1..5bf1bb74 100644 --- a/tests/OSS/Tests/WebsiteConfigTest.php +++ b/tests/OSS/Tests/WebsiteConfigTest.php @@ -3,7 +3,15 @@ namespace OSS\Tests; +use OSS\Model\WebsiteCondition; use OSS\Model\WebsiteConfig; +use OSS\Model\WebsiteIncludeHeader; +use OSS\Model\WebsiteIndexDocument; +use OSS\Model\WebsiteErrorDocument; +use OSS\Model\WebsiteMirrorHeaders; +use OSS\Model\WebsiteMirrorHeadersSet; +use OSS\Model\WebsiteRedirect; +use OSS\Model\WebsiteRoutingRule; class WebsiteConfigTest extends \PHPUnit\Framework\TestCase { @@ -26,17 +34,127 @@ class WebsiteConfigTest extends \PHPUnit\Framework\TestCase BBBB; + private $manyXml = << + + +index.html + + +error.html +404 + + + +1 + +abc/ +404 + + +Mirror +true +http://example.com/ +true +true +false + +true +myheader-key1 +myheader-key2 +myheader-key3 +myheader-key4 + +myheader-key5 +myheader-value5 + + + + + +2 + +abc/ +404 + +host +test.oss-cn-beijing-internal.aliyuncs.com + + + +AliCDN +false +http +example.com +prefix/!{key}.suffix +301 + + + + +BBBB; + + + private $oneRule = << + + +index.html +false +0 + + +error.html +404 + + + +1 + +examplebucket +404 + +host +test.oss-cn-beijing-internal.aliyuncs.com + + + +Mirror +true +https://www.example.com/ +true +true + +true +cache-control-one +pass-one +remove-one +test-two + +key1 +value1 + + +false +key.jpg + + + + +BBBB; + + public function testParseValidXml() { - $websiteConfig = new WebsiteConfig("index"); - $websiteConfig->parseFromXml($this->validXml); + $websiteConfig = new WebsiteConfig("index.html","errorDocument.html"); $this->assertEquals($this->cleanXml($this->validXml), $this->cleanXml($websiteConfig->serializeToXml())); } public function testParsenullXml() { - $websiteConfig = new WebsiteConfig(); - $websiteConfig->parseFromXml($this->nullXml); + $index = new WebsiteIndexDocument(""); + $error = new WebsiteErrorDocument(""); + $websiteConfig = new WebsiteConfig($index, $error); $this->assertTrue($this->cleanXml($this->nullXml) === $this->cleanXml($websiteConfig->serializeToXml()) || $this->cleanXml($this->nullXml2) === $this->cleanXml($websiteConfig->serializeToXml())); } @@ -44,8 +162,153 @@ public function testParsenullXml() public function testWebsiteConstruct() { $websiteConfig = new WebsiteConfig("index.html", "errorDocument.html"); - $this->assertEquals('index.html', $websiteConfig->getIndexDocument()); - $this->assertEquals('errorDocument.html', $websiteConfig->getErrorDocument()); + $this->assertEquals($this->cleanXml($this->validXml), $this->cleanXml($websiteConfig->serializeToXml())); + } + public function testParseValidXmlNew() + { + $index = new WebsiteIndexDocument("index.html"); + $error = new WebsiteErrorDocument("error.html"); + $websiteConfig = new WebsiteConfig($index, $error); + $websiteConfig->parseFromXml($this->validXml); + $this->assertEquals($this->cleanXml($this->validXml), $this->cleanXml($websiteConfig->serializeToXml())); + } + + public function testParsenullXmlNew() + { + $websiteConfig = new WebsiteConfig(); + $websiteConfig->parseFromXml($this->nullXml); + $this->assertTrue($this->cleanXml($this->nullXml) === $this->cleanXml($websiteConfig->serializeToXml()) || + $this->cleanXml($this->nullXml2) === $this->cleanXml($websiteConfig->serializeToXml())); + } + + + public function testParseManyRule() + { + + $index = new WebsiteIndexDocument('index.html'); + $error = new WebsiteErrorDocument('error.html',404); + $websiteConfig = new WebsiteConfig($index,$error); + + $routingRule = new WebsiteRoutingRule(); + $routingRule->setNumber(1); + $websiteCondition = new WebsiteCondition(); + $websiteCondition->setKeyPrefixEquals("abc/"); + $websiteCondition->setHttpErrorCodeReturnedEquals(404); + $routingRule->setCondition($websiteCondition); + $websiteRedirect = new WebsiteRedirect(); + $websiteRedirect->setRedirectType(WebsiteRedirect::MIRROR); + $websiteRedirect->setPassQueryString(true); + $websiteRedirect->setMirrorURL('http://example.com/'); + $websiteRedirect->setMirrorPassQueryString(true); + $websiteRedirect->setMirrorFollowRedirect(true); + $websiteRedirect->setMirrorCheckMd5(false); + + $mirrorHeaders = new WebsiteMirrorHeaders(); + // Whether to transparently transmit headers other than the following headers to the source station + + $mirrorHeaders->setPassAll(true); + $pass = 'myheader-key1'; + $passOne = 'myheader-key2'; + $mirrorHeaders->addPass($pass); + $mirrorHeaders->addPass($passOne); + $remove = 'myheader-key3'; + $removeOne = 'myheader-key4'; + $mirrorHeaders->addRemove($remove); + $mirrorHeaders->addRemove($removeOne); + + $set = new WebsiteMirrorHeadersSet(); + $set->setKey("myheader-key5"); + $set->setValue("myheader-value5"); + $mirrorHeaders->addSet($set); + + $websiteRedirect->setMirrorHeaders($mirrorHeaders); + $routingRule->setRedirect($websiteRedirect); + $websiteConfig->addRule($routingRule); + + $routingRule2 = new WebsiteRoutingRule(); + $routingRule2->setNumber(2); + $websiteCondition2 = new WebsiteCondition(); + $websiteCondition2->setKeyPrefixEquals("abc/"); + $websiteCondition2->setHttpErrorCodeReturnedEquals(404); + $includeHeader2 = new WebsiteIncludeHeader(); + $includeHeader2->setKey('host'); + $includeHeader2->setEquals('test.oss-cn-beijing-internal.aliyuncs.com'); + $websiteCondition2->addIncludeHeader($includeHeader2); + $routingRule2->setCondition($websiteCondition2); + + $websiteRedirect2 = new WebsiteRedirect(); + $websiteRedirect2->setRedirectType(WebsiteRedirect::ALICDN); + $websiteRedirect2->setPassQueryString(false); + $websiteRedirect2->setProtocol("http"); + $websiteRedirect2->setHostName("example.com"); + $websiteRedirect2->setReplaceKeyWith("prefix/!{key}.suffix"); + $websiteRedirect2->setHttpRedirectCode(301); + $routingRule2->setRedirect($websiteRedirect2); + $websiteConfig->addRule($routingRule2); + + + $this->assertTrue($this->cleanXml($this->manyXml) === $this->cleanXml($websiteConfig->serializeToXml())); + } + + public function testParseOneRule() + { + $index = new WebsiteIndexDocument('index.html',false,0); + $error = new WebsiteErrorDocument('error.html',404); + $websiteConfig = new WebsiteConfig($index,$error); + + $routingRule = new WebsiteRoutingRule(); + $routingRule->setNumber(1); + $websiteCondition = new WebsiteCondition(); + $includeHeader = new WebsiteIncludeHeader(); + $includeHeader->setKey('host'); + $includeHeader->setEquals('test.oss-cn-beijing-internal.aliyuncs.com'); + $websiteCondition->addIncludeHeader($includeHeader); + $websiteCondition->setKeyPrefixEquals("examplebucket"); + $websiteCondition->setHttpErrorCodeReturnedEquals(404); + + $websiteRedirect = new WebsiteRedirect(); + $websiteRedirect->setRedirectType(WebsiteRedirect::MIRROR); + $websiteRedirect->setMirrorURL('https://www.example.com/'); + $websiteRedirect->setPassQueryString(true); + $websiteRedirect->setMirrorPassQueryString(true); + $websiteRedirect->setMirrorCheckMd5(true); + + $mirrorHeaders = new WebsiteMirrorHeaders(); + // Whether to transparently transmit headers other than the following headers to the source station + + $mirrorHeaders->setPassAll(true); + $pass = 'cache-control-one'; + $passOne = 'pass-one'; + $mirrorHeaders->addPass($pass); + $mirrorHeaders->addPass($passOne); + $remove = 'remove-one'; + $removeOne = 'test-two'; + $mirrorHeaders->addRemove($remove); + $mirrorHeaders->addRemove($removeOne); + + $set = new WebsiteMirrorHeadersSet(); + $set->setKey("key1"); + $set->setValue("value1"); + $mirrorHeaders->addSet($set); + + $websiteRedirect->setMirrorHeaders($mirrorHeaders); + $websiteRedirect->setEnableReplacePrefix(false); + $websiteRedirect->setReplaceKeyWith("key.jpg"); + $routingRule->setRedirect($websiteRedirect); + $routingRule->setCondition($websiteCondition); + $websiteConfig->addRule($routingRule); + + $this->assertTrue($this->cleanXml($this->oneRule) === $this->cleanXml($websiteConfig->serializeToXml())); + } + + + public function testWebsiteConstructNew() + { + $index = new WebsiteIndexDocument("index.html"); + $error = new WebsiteErrorDocument("errorDocument.html"); + $websiteConfig = new WebsiteConfig($index, $error); + $this->assertEquals('index.html', $websiteConfig->getIndexDocument()->getSuffix()); + $this->assertEquals('errorDocument.html', $websiteConfig->getErrorDocument()->getKey()); $this->assertEquals($this->cleanXml($this->validXml), $this->cleanXml($websiteConfig->serializeToXml())); }