Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support of null to remove the CORS configuration from bucket #438

Merged
merged 2 commits into from Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}
}