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

Set the CacheControl property on stored blobs #6297

Merged
merged 4 commits into from
Aug 17, 2018
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.
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 @@ -318,7 +318,7 @@ public async Task SaveFileAsync(string folderName, string fileName, Stream file,
}

blob.Properties.ContentType = GetContentType(folderName);
blob.Properties.CacheControl = CoreConstants.DefaultCacheControl;
blob.Properties.CacheControl = GetCacheControl(folderName);
await blob.SetPropertiesAsync();
}

Expand Down Expand Up @@ -550,6 +550,7 @@ private static string GetContentType(string folderName)
case CoreConstants.PackageReadMesFolderName:
return CoreConstants.TextContentType;

case CoreConstants.ContentFolderName:
case CoreConstants.RevalidationFolderName:
Copy link
Member

Choose a reason for hiding this comment

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

Why this addition? A test should cover it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The the content type tests skipped ContentFolderName, which caused it to fail my new test. I'll fix the content type test.

case CoreConstants.StatusFolderName:
return CoreConstants.JsonContentType;
Expand All @@ -563,6 +564,32 @@ private static string GetContentType(string folderName)
}
}

private static string GetCacheControl(string folderName)
{
switch (folderName)
{
case CoreConstants.PackagesFolderName:
return CoreConstants.DefaultCacheControl;
Copy link
Member

Choose a reason for hiding this comment

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

nit: PackageCacheControl instead of Default


case CoreConstants.PackageBackupsFolderName:
case CoreConstants.UploadsFolderName:
case CoreConstants.ValidationFolderName:
case CoreConstants.SymbolPackagesFolderName:
case CoreConstants.SymbolPackageBackupsFolderName:
case CoreConstants.DownloadsFolderName:
case CoreConstants.PackageReadMesFolderName:
case CoreConstants.ContentFolderName:
case CoreConstants.RevalidationFolderName:
case CoreConstants.StatusFolderName:
case CoreConstants.UserCertificatesFolderName:
return null;

default:
throw new InvalidOperationException(
String.Format(CultureInfo.CurrentCulture, "The folder name {0} is not supported.", folderName));
}
}

private async Task<ICloudBlobContainer> PrepareContainer(string folderName, bool isPublic)
{
var container = _client.GetContainerReference(folderName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,6 @@ public async Task WillUploadThePackageFileToTheBlob()

await service.SaveFileAsync(CoreConstants.PackagesFolderName, "theFileName", fakePackageFile);

Assert.Equal(CoreConstants.DefaultCacheControl, fakeBlob.Object.Properties.CacheControl);
fakeBlob.Verify();
}

Expand Down Expand Up @@ -513,6 +512,41 @@ public async Task WillSetTheBlobContentType(string folderName, string contentTyp
Assert.Equal(contentType, fakeBlob.Object.Properties.ContentType);
fakeBlob.Verify(x => x.SetPropertiesAsync());
}

[Theory]
[FolderNamesData]
public async Task WillSetTheBlobControlCacheOnPackagesFolder(string folderName)
{
var fakeBlobClient = new Mock<ICloudBlobClient>();
var fakeBlobContainer = new Mock<ICloudBlobContainer>();
fakeBlobContainer.Setup(x => x.CreateIfNotExistAsync()).Returns(Task.FromResult(0));
fakeBlobContainer.Setup(x => x.SetPermissionsAsync(It.IsAny<BlobContainerPermissions>())).Returns(Task.FromResult(0));
var fakeBlob = new Mock<ISimpleCloudBlob>();
fakeBlobClient.Setup(x => x.GetContainerReference(It.IsAny<string>())).Returns(fakeBlobContainer.Object);
fakeBlobContainer.Setup(x => x.GetBlobReference(It.IsAny<string>())).Returns(fakeBlob.Object);
fakeBlob.Setup(x => x.Properties).Returns(new BlobProperties());
fakeBlob.Setup(x => x.Uri).Returns(new Uri("http://theUri"));
fakeBlob.Setup(x => x.DeleteIfExistsAsync()).Returns(Task.FromResult(0));
fakeBlob.Setup(x => x.SetPropertiesAsync()).Returns(Task.FromResult(0));
var service = CreateService(fakeBlobClient: fakeBlobClient);
var fakePackageFile = new MemoryStream();
fakeBlob.Setup(x => x.UploadFromStreamAsync(fakePackageFile, true)).Returns(Task.FromResult(0)).Verifiable();

await service.SaveFileAsync(folderName, "theFileName", fakePackageFile);

fakeBlob.Verify();

if (folderName == CoreConstants.PackagesFolderName)
{
Assert.Equal(CoreConstants.DefaultCacheControl, fakeBlob.Object.Properties.CacheControl);
}
else
{
Assert.Null(fakeBlob.Object.Properties.CacheControl);
}

fakeBlob.Verify(x => x.SetPropertiesAsync());
}
}

public class TheSaveFileWithAccessConditionMethod
Expand Down