From 9d4041a32e9457109765f6244bad58ade95782aa Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Wed, 5 Dec 2018 14:16:44 +0000 Subject: [PATCH] Create option to allow Origin header for upload initiation Fixes #2755. --- .../UploadObjectTest.cs | 14 ++++++++++++++ .../UploadObjectOptionsTest.cs | 8 ++++---- .../Google.Cloud.Storage.V1/UploadObjectOptions.cs | 12 +++++++++++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1.IntegrationTests/UploadObjectTest.cs b/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1.IntegrationTests/UploadObjectTest.cs index ce25bc1144b5..c3572e161623 100644 --- a/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1.IntegrationTests/UploadObjectTest.cs +++ b/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1.IntegrationTests/UploadObjectTest.cs @@ -52,6 +52,20 @@ public void SimpleUpload() ValidateData(_fixture.MultiVersionBucket, name, source); } + [Fact] + public void CorsUpload() + { + var name = IdGenerator.FromGuid(); + var contentType = "application/octet-stream"; + var source = GenerateData(100); + var options = new UploadObjectOptions { Origin = "http://nodatime.org" }; + var result = _fixture.Client.UploadObject(_fixture.MultiVersionBucket, name, contentType, source, options); + Assert.Equal(_fixture.MultiVersionBucket, result.Bucket); + Assert.Equal(name, result.Name); + Assert.Equal(contentType, result.ContentType); + ValidateData(_fixture.MultiVersionBucket, name, source); + } + [Fact] public void UploadWithObject() { diff --git a/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1.Tests/UploadObjectOptionsTest.cs b/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1.Tests/UploadObjectOptionsTest.cs index 31ee746e488c..70a2541e1f5d 100644 --- a/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1.Tests/UploadObjectOptionsTest.cs +++ b/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1.Tests/UploadObjectOptionsTest.cs @@ -54,7 +54,7 @@ public void InvalidChunkSize(int chunkSize) [Fact] public void ModifyMediaUpload_DefaultOptions() { - var upload = new InsertMediaUpload(new DummyService(), null, "bucket", new MemoryStream(), null); + var upload = new CustomMediaUpload(new DummyService(), null, "bucket", new MemoryStream(), null); var options = new UploadObjectOptions(); options.ModifyMediaUpload(upload); Assert.Equal(ResumableUpload.DefaultChunkSize, upload.ChunkSize); @@ -71,7 +71,7 @@ public void ModifyMediaUpload_DefaultOptions() [Fact] public void ModifyMediaUpload_AllOptions_PositiveMatch() { - var upload = new InsertMediaUpload(new DummyService(), null, "bucket", new MemoryStream(), null); + var upload = new CustomMediaUpload(new DummyService(), null, "bucket", new MemoryStream(), null); var options = new UploadObjectOptions { ChunkSize = UploadObjectOptions.MinimumChunkSize * 3, @@ -96,7 +96,7 @@ public void ModifyMediaUpload_AllOptions_PositiveMatch() [Fact] public void ModifyMediaUpload_AllOptions_NegativeMatch() { - var upload = new InsertMediaUpload(new DummyService(), null, "bucket", new MemoryStream(), null); + var upload = new CustomMediaUpload(new DummyService(), null, "bucket", new MemoryStream(), null); var options = new UploadObjectOptions { ChunkSize = UploadObjectOptions.MinimumChunkSize * 3, @@ -117,7 +117,7 @@ public void ModifyMediaUpload_AllOptions_NegativeMatch() [Fact] public void ModifyMediaUpload_MatchNotMatchConflicts() { - var upload = new InsertMediaUpload(new DummyService(), null, "bucket", new MemoryStream(), null); + var upload = new CustomMediaUpload(new DummyService(), null, "bucket", new MemoryStream(), null); Assert.Throws(() => { var options = new UploadObjectOptions { IfGenerationMatch = 1L, IfGenerationNotMatch = 2L }; diff --git a/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1/UploadObjectOptions.cs b/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1/UploadObjectOptions.cs index d69963e7a87e..021312b5a233 100644 --- a/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1/UploadObjectOptions.cs +++ b/apis/Google.Cloud.Storage.V1/Google.Cloud.Storage.V1/UploadObjectOptions.cs @@ -123,7 +123,13 @@ public int? ChunkSize /// public UploadValidationMode? UploadValidationMode { get; set; } - internal void ModifyMediaUpload(InsertMediaUpload upload) + /// + /// If set, an Origin header is included when initiating the resumable upload. This allows for Cross-Origin + /// Resource Sharing, as documented at https://cloud.google.com/storage/docs/cross-origin. + /// + public string Origin { get; set; } + + internal void ModifyMediaUpload(CustomMediaUpload upload) { // Note the use of ArgumentException here, as this will basically be the result of invalid // options being passed to a public method. @@ -175,6 +181,10 @@ internal void ModifyMediaUpload(InsertMediaUpload upload) { upload.KmsKeyName = KmsKeyName; } + if (Origin != null) + { + upload.Options.ModifySessionInitiationRequest += message => message.Headers.Add("Origin", Origin); + } } } }