Skip to content

Commit

Permalink
Create option to allow Origin header for upload initiation
Browse files Browse the repository at this point in the history
Fixes #2755.
  • Loading branch information
jskeet committed Dec 6, 2018
1 parent e5dfd1d commit 9d4041a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<InsertMediaUpload>.DefaultChunkSize, upload.ChunkSize);
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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<ArgumentException>(() =>
{
var options = new UploadObjectOptions { IfGenerationMatch = 1L, IfGenerationNotMatch = 2L };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ public int? ChunkSize
/// </remarks>
public UploadValidationMode? UploadValidationMode { get; set; }

internal void ModifyMediaUpload(InsertMediaUpload upload)
/// <summary>
/// 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.
/// </summary>
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.
Expand Down Expand Up @@ -175,6 +181,10 @@ internal void ModifyMediaUpload(InsertMediaUpload upload)
{
upload.KmsKeyName = KmsKeyName;
}
if (Origin != null)
{
upload.Options.ModifySessionInitiationRequest += message => message.Headers.Add("Origin", Origin);
}
}
}
}

0 comments on commit 9d4041a

Please sign in to comment.