http://www.b.com http://www.a.com http://www.a.com GET PUT POST x-oss-test x-oss-test2 x-oss-test2 x-oss-test3 x-oss-test1 x-oss-test1 x-oss-test2 10 http://www.b.com GET x-oss-test x-oss-test1 110 false BBBB; private $validXml2 = << http://www.b.com http://www.a.com http://www.a.com GET PUT POST x-oss-test x-oss-test2 x-oss-test2 x-oss-test3 x-oss-test1 x-oss-test1 x-oss-test2 10 BBBB; private $validXml3 = << http://www.b.com http://www.a.com http://www.a.com GET PUT POST x-oss-test 10 true BBBB; public function testParseValidXml() { $corsConfig = new CorsConfig(); $corsConfig->parseFromXml($this->validXml); $this->assertEquals($this->cleanXml($this->validXml), $this->cleanXml($corsConfig->serializeToXml())); $this->assertNotNull($corsConfig->getRules()); $rules = $corsConfig->getRules(); $this->assertNotNull($rules[0]->getAllowedHeaders()); $this->assertNotNull($rules[0]->getAllowedMethods()); $this->assertNotNull($rules[0]->getAllowedOrigins()); $this->assertNotNull($rules[0]->getExposeHeaders()); $this->assertNotNull($rules[0]->getMaxAgeSeconds()); } public function testParseValidXml2() { $corsConfig = new CorsConfig(); $corsConfig->parseFromXml($this->validXml2); $this->assertEquals($this->cleanXml($this->validXml2), $this->cleanXml($corsConfig->serializeToXml())); } public function testParseValidXml3() { $corsConfig = new CorsConfig(); $corsConfig->parseFromXml($this->validXml3); $this->assertEquals($this->cleanXml($this->validXml3), $this->cleanXml($corsConfig->serializeToXml())); $this->assertTrue($corsConfig->getResponseVary()); } public function testResponseValidXml3() { $response = new ResponseCore(array(), $this->validXml, 200); $result = new GetCorsResult($response); $this->assertTrue($result->isOK()); $this->assertNotNull($result->getData()); $this->assertNotNull($result->getRawResponse()); $this->assertNotNull($result->getRawResponse()->body); $corsConfig = $result->getData(); $this->assertEquals($this->cleanXml($this->validXml), $this->cleanXml($corsConfig->serializeToXml())); $this->assertNotNull($corsConfig->getRules()); $rules = $corsConfig->getRules(); $this->assertNotNull($rules[0]->getAllowedHeaders()); $this->assertNotNull($rules[0]->getAllowedMethods()); $this->assertNotNull($rules[0]->getAllowedOrigins()); $this->assertNotNull($rules[0]->getExposeHeaders()); $this->assertNotNull($rules[0]->getMaxAgeSeconds()); $this->assertFalse($corsConfig->getResponseVary()); } public function testResponseValidXml4() { $response = new ResponseCore(array(), $this->validXml3, 200); $result = new GetCorsResult($response); $this->assertTrue($result->isOK()); $this->assertNotNull($result->getData()); $this->assertNotNull($result->getRawResponse()); $this->assertNotNull($result->getRawResponse()->body); $corsConfig = $result->getData(); $this->assertEquals($this->cleanXml($this->validXml3), $this->cleanXml($corsConfig->serializeToXml())); $this->assertNotNull($corsConfig->getRules()); $rules = $corsConfig->getRules(); $this->assertNotNull($rules[0]->getAllowedHeaders()); $this->assertNotNull($rules[0]->getAllowedMethods()); $this->assertNotNull($rules[0]->getAllowedOrigins()); $this->assertNotNull($rules[0]->getExposeHeaders()); $this->assertNotNull($rules[0]->getMaxAgeSeconds()); $this->assertTrue($corsConfig->getResponseVary()); } public function testCreateCorsConfigFromMoreThan10Rules() { $corsConfig = new CorsConfig(); $rule = new CorsRule(); for ($i = 0; $i < CorsConfig::OSS_MAX_RULES; $i += 1) { $corsConfig->addRule($rule); } try { $corsConfig->addRule($rule); $this->assertFalse(true); } catch (OssException $e) { $this->assertEquals($e->getMessage(), "num of rules in the config exceeds self::OSS_MAX_RULES: " . strval(CorsConfig::OSS_MAX_RULES)); } } public function testCreateCorsConfigParamAbsent() { $corsConfig = new CorsConfig(); $rule = new CorsRule(); $corsConfig->addRule($rule); try { $xml = $corsConfig->serializeToXml(); $this->assertFalse(true); } catch (OssException $e) { $this->assertEquals($e->getMessage(), "maxAgeSeconds is not set in the Rule"); } } public function testCreateCorsConfigFromScratch() { $corsConfig = new CorsConfig(); $rule = new CorsRule(); $rule->addAllowedHeader("x-oss-test"); $rule->addAllowedHeader("x-oss-test2"); $rule->addAllowedHeader("x-oss-test2"); $rule->addAllowedHeader("x-oss-test3"); $rule->addAllowedOrigin("http://www.b.com"); $rule->addAllowedOrigin("http://www.a.com"); $rule->addAllowedOrigin("http://www.a.com"); $rule->addAllowedMethod("GET"); $rule->addAllowedMethod("PUT"); $rule->addAllowedMethod("POST"); $rule->addExposeHeader("x-oss-test1"); $rule->addExposeHeader("x-oss-test1"); $rule->addExposeHeader("x-oss-test2"); $rule->setMaxAgeSeconds(10); $corsConfig->addRule($rule); $this->assertEquals($this->cleanXml($this->validXml2), $this->cleanXml($corsConfig->serializeToXml())); $this->assertEquals($this->cleanXml($this->validXml2), $this->cleanXml(strval($corsConfig))); } private function cleanXml($xml) { return str_replace("\n", "", str_replace("\r", "", $xml)); } }