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 1 commit
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 @@ -1509,7 +1509,7 @@ public StorageClass getStorageClass() {
* (CORS)</a>
*/
public List<Cors> getCors() {
return cors;
return cors != null ? cors : ImmutableList.<Cors>of();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave this field as is and revert change. There are potentially 4 cases which are considered here and we don't want to mess them up.

  1. Cors are set and field selector is selected then returns not-null.
  2. Cors are set but field selector isn't selected then returns null.
  3. Cors are not set and field selector is selected then returns null.
  4. Cors are not set and field selector isn't selected then returns null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

/**
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().size()).isEqualTo(1);
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().size()).isEqualTo(0);
}
}
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,34 @@ 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();
Bucket bucket =
storage.create(BucketInfo.newBuilder(bucketName).setCors(ImmutableList.of(cors)).build());
assertThat(bucket.getCors().size()).isEqualTo(1);
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 updatedBucket = bucket.toBuilder().setCors(ImmutableList.<Cors>of()).build().update();
assertThat(updatedBucket.getCors().size()).isEqualTo(0);
} finally {
RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
}
}
}