Skip to content

Commit

Permalink
feat: add support of null to remove the CORS configuration from bucket (
Browse files Browse the repository at this point in the history
#438)

* feat: add support of null to remove bucket CORS configuration

* feat: addresse review changes
  • Loading branch information
athakor committed Jul 22, 2020
1 parent e63feb7 commit f8a4b12
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
Expand Up @@ -1240,7 +1240,7 @@ Builder setMetageneration(Long metageneration) {

@Override
public Builder setCors(Iterable<Cors> cors) {
this.cors = cors != null ? ImmutableList.copyOf(cors) : null;
this.cors = cors != null ? ImmutableList.copyOf(cors) : ImmutableList.<Cors>of();
return this;
}

Expand Down
Expand Up @@ -886,4 +886,36 @@ public void testUpdateBucketLogging() {
assertThat(actualUpdatedBucket.getLogging().getLogBucket()).isNull();
assertThat(actualUpdatedBucket.getLogging().getLogObjectPrefix()).isNull();
}

@Test
public void testRemoveBucketCORS() {
initializeExpectedBucket(6);
List<Cors.Origin> origins = ImmutableList.of(Cors.Origin.of("http://cloud.google.com"));
List<HttpMethod> httpMethods = ImmutableList.of(HttpMethod.GET);
List<String> responseHeaders = ImmutableList.of("Content-Type");
Cors cors =
Cors.newBuilder()
.setOrigins(origins)
.setMethods(httpMethods)
.setResponseHeaders(responseHeaders)
.setMaxAgeSeconds(100)
.build();
BucketInfo bucketInfo = BucketInfo.newBuilder("b").setCors(ImmutableList.of(cors)).build();
Bucket bucket = new Bucket(serviceMockReturnsOptions, new BucketInfo.BuilderImpl(bucketInfo));
assertThat(bucket.getCors()).isNotNull();
assertThat(bucket.getCors().get(0).getMaxAgeSeconds()).isEqualTo(100);
assertThat(bucket.getCors().get(0).getMethods()).isEqualTo(httpMethods);
assertThat(bucket.getCors().get(0).getOrigins()).isEqualTo(origins);
assertThat(bucket.getCors().get(0).getResponseHeaders()).isEqualTo(responseHeaders);

// Remove bucket CORS configuration.
Bucket expectedUpdatedBucket = bucket.toBuilder().setCors(null).build();
expect(storage.getOptions()).andReturn(mockOptions).times(2);
expect(storage.update(expectedUpdatedBucket)).andReturn(expectedUpdatedBucket);
replay(storage);
initializeBucket();
Bucket updatedBucket = new Bucket(storage, new BucketInfo.BuilderImpl(expectedUpdatedBucket));
Bucket actualUpdatedBucket = updatedBucket.update();
assertThat(actualUpdatedBucket.getCors()).isEmpty();
}
}
Expand Up @@ -66,6 +66,7 @@
import com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleAction;
import com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleCondition;
import com.google.cloud.storage.CopyWriter;
import com.google.cloud.storage.Cors;
import com.google.cloud.storage.HmacKey;
import com.google.cloud.storage.HttpMethod;
import com.google.cloud.storage.PostPolicyV4;
Expand Down Expand Up @@ -3474,4 +3475,50 @@ public void testAutoContentTypeCreateFrom() throws IOException {
public void testAutoContentTypeWriter() throws IOException {
testAutoContentType("writer");
}

@Test
public void testRemoveBucketCORS() throws ExecutionException, InterruptedException {
String bucketName = RemoteStorageHelper.generateBucketName();
List<Cors.Origin> origins = ImmutableList.of(Cors.Origin.of("http://cloud.google.com"));
List<HttpMethod> httpMethods = ImmutableList.of(HttpMethod.GET);
List<String> responseHeaders = ImmutableList.of("Content-Type");
try {
Cors cors =
Cors.newBuilder()
.setOrigins(origins)
.setMethods(httpMethods)
.setResponseHeaders(responseHeaders)
.setMaxAgeSeconds(100)
.build();
storage.create(BucketInfo.newBuilder(bucketName).setCors(ImmutableList.of(cors)).build());

// case-1 : Cors are set and field selector is selected then returns not-null.
Bucket remoteBucket =
storage.get(bucketName, Storage.BucketGetOption.fields(BucketField.CORS));
assertThat(remoteBucket.getCors()).isNotNull();
assertThat(remoteBucket.getCors().get(0).getMaxAgeSeconds()).isEqualTo(100);
assertThat(remoteBucket.getCors().get(0).getMethods()).isEqualTo(httpMethods);
assertThat(remoteBucket.getCors().get(0).getOrigins()).isEqualTo(origins);
assertThat(remoteBucket.getCors().get(0).getResponseHeaders()).isEqualTo(responseHeaders);

// case-2 : Cors are set but field selector isn't selected then returns not-null.
remoteBucket = storage.get(bucketName);
assertThat(remoteBucket.getCors()).isNotNull();

// Remove CORS configuration from the bucket.
Bucket updatedBucket = remoteBucket.toBuilder().setCors(null).build().update();
assertThat(updatedBucket.getCors()).isNull();

// case-3 : Cors are not set and field selector is selected then returns null.
updatedBucket = storage.get(bucketName, Storage.BucketGetOption.fields(BucketField.CORS));
assertThat(updatedBucket.getCors()).isNull();

// case-4 : Cors are not set and field selector isn't selected then returns null.
updatedBucket = storage.get(bucketName);
assertThat(updatedBucket.getCors()).isNull();

} finally {
RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
}
}
}

0 comments on commit f8a4b12

Please sign in to comment.