Search in sources :

Example 1 with BucketCrossOriginConfiguration

use of com.qcloud.cos.model.BucketCrossOriginConfiguration in project cos-java-sdk-v5 by tencentyun.

the class CORSTest method setBucketCORSTest.

@Test
public void setBucketCORSTest() throws InterruptedException {
    if (!judgeUserInfoValid()) {
        return;
    }
    BucketCrossOriginConfiguration bucketCORS = new BucketCrossOriginConfiguration();
    List<CORSRule> corsRules = new ArrayList<>();
    CORSRule corsRule = new CORSRule();
    corsRule.setId("set-bucket-cors-test");
    corsRule.setAllowedMethods(AllowedMethods.PUT, AllowedMethods.GET, AllowedMethods.HEAD);
    corsRule.setAllowedHeaders("x-cos-grant-full-control");
    corsRule.setAllowedOrigins("http://mail.qq.com", "http://www.qq.com", "http://video.qq.com");
    corsRule.setExposedHeaders("x-cos-request-id");
    corsRule.setMaxAgeSeconds(60);
    corsRules.add(corsRule);
    bucketCORS.setRules(corsRules);
    cosclient.setBucketCrossOriginConfiguration(bucket, bucketCORS);
    Thread.sleep(5000L);
    BucketCrossOriginConfiguration corsGet = cosclient.getBucketCrossOriginConfiguration(bucket);
    assertEquals(1, corsGet.getRules().size());
    CORSRule corsRuleGet = corsGet.getRules().get(0);
    assertEquals(3, corsRuleGet.getAllowedMethods().size());
    assertEquals(1, corsRuleGet.getAllowedHeaders().size());
    assertEquals(3, corsRuleGet.getAllowedOrigins().size());
    assertEquals(1, corsRuleGet.getExposedHeaders().size());
    assertEquals(60, corsRuleGet.getMaxAgeSeconds());
    cosclient.deleteBucketCrossOriginConfiguration(bucket);
}
Also used : BucketCrossOriginConfiguration(com.qcloud.cos.model.BucketCrossOriginConfiguration) CORSRule(com.qcloud.cos.model.CORSRule) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with BucketCrossOriginConfiguration

use of com.qcloud.cos.model.BucketCrossOriginConfiguration in project cos-java-sdk-v5 by tencentyun.

the class CORSTest method putAndGetNotExistBucketCORSTest.

@Test
public void putAndGetNotExistBucketCORSTest() {
    if (!judgeUserInfoValid()) {
        return;
    }
    String bucketName = "not-exist-" + bucket;
    BucketCrossOriginConfiguration bucketCORS = new BucketCrossOriginConfiguration();
    List<CORSRule> corsRules = new ArrayList<>();
    bucketCORS.setRules(corsRules);
    try {
        cosclient.setBucketCrossOriginConfiguration(bucketName, bucketCORS);
    } catch (CosServiceException cse) {
        assertEquals(4, cse.getStatusCode() / 100);
    }
    try {
        cosclient.getBucketCrossOriginConfiguration(bucketName);
    } catch (CosServiceException cse) {
        assertEquals(404, cse.getStatusCode() / 100);
    }
}
Also used : BucketCrossOriginConfiguration(com.qcloud.cos.model.BucketCrossOriginConfiguration) CosServiceException(com.qcloud.cos.exception.CosServiceException) CORSRule(com.qcloud.cos.model.CORSRule) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

BucketCrossOriginConfiguration (com.qcloud.cos.model.BucketCrossOriginConfiguration)2 CORSRule (com.qcloud.cos.model.CORSRule)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 CosServiceException (com.qcloud.cos.exception.CosServiceException)1