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

Create option to allow Origin header for upload initiation #2756

Merged
merged 1 commit into from
Dec 6, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}