Skip to content

Commit

Permalink
getDownloadUrl and getMetadata at the root of a bucket throws proper …
Browse files Browse the repository at this point in the history
…error message (#445)
  • Loading branch information
fredzqm committed May 20, 2019
1 parent 47d41b9 commit a7b46ec
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
3 changes: 3 additions & 0 deletions firebase-storage/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
and are not mentioned in our documentation.
- [internal] Updated the SDK initialization process and removed usages of
deprecated methods.
- [changed] Added validation to `StorageReference.getDownloadUrl` and
`StorageReference.getMetadata` to return an error if the reference is the
root of the bucket.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ class GetDownloadUrlTask implements Runnable {

this.storageRef = storageRef;
this.pendingResult = pendingResult;

if (storageRef.getRoot().getName().equals(storageRef.getName())) {
throw new IllegalArgumentException(
"getDownloadUrl() is not supported at the root of the bucket.");
}
FirebaseStorage storage = this.storageRef.getStorage();
sender =
new ExponentialBackoffSender(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class GetMetadataTask implements Runnable {

this.mStorageRef = storageRef;
this.mPendingResult = pendingResult;
if (storageRef.getRoot().getName().equals(storageRef.getName())) {
throw new IllegalArgumentException(
"getMetadata() is not supported at the root of the bucket.");
}

FirebaseStorage storage = mStorageRef.getStorage();
mSender =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ public void badURL2() throws Exception {
@Test
public void downloadUrl() throws Exception {
MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("downloadUrl", true);
final StorageReference ref = FirebaseStorage.getInstance().getReference("flubbertest.txt");

Task<StringBuilder> task = TestCommandHelper.testDownloadUrl();
Task<StringBuilder> task = TestCommandHelper.testDownloadUrl(ref);
for (int i = 0; i < 3000; i++) {
Robolectric.flushForegroundThreadScheduler();
if (task.isComplete()) {
Expand All @@ -277,4 +278,30 @@ public void downloadUrl() throws Exception {
}
Assert.fail();
}

@Test
public void downloadUrlBucketRoot() throws Exception {
final StorageReference ref = FirebaseStorage.getInstance().getReference();

boolean thrown = false;
try {
TestCommandHelper.testDownloadUrl(ref);
} catch (IllegalArgumentException e) {
thrown = true;
}
Assert.assertTrue(thrown);
}

@Test
public void getMetadataBucketRoot() throws Exception {
final StorageReference ref = FirebaseStorage.getInstance().getReference();

boolean thrown = false;
try {
TestCommandHelper.getMetadata(ref);
} catch (IllegalArgumentException e) {
thrown = true;
}
Assert.assertTrue(thrown);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public class TestCommandHelper {

private static final Executor executor = ExecutorProviderHelper.getInstance();

public static Task<StringBuilder> testDownloadUrl() {
final StorageReference ref = FirebaseStorage.getInstance().getReference("flubbertest.txt");

public static Task<StringBuilder> testDownloadUrl(StorageReference ref) {
TaskCompletionSource<StringBuilder> result = new TaskCompletionSource<>();
StringBuilder builder = new StringBuilder();
builder.append("Getting Download Url.\n");
Expand All @@ -54,7 +52,7 @@ public static Task<StringBuilder> testDownloadUrl() {
return result.getTask();
}

private static Task<StringBuilder> getMetadata(StorageReference ref) {
static Task<StringBuilder> getMetadata(StorageReference ref) {
TaskCompletionSource<StringBuilder> result = new TaskCompletionSource<>();
StringBuilder builder = new StringBuilder();
builder.append("Getting Metadata.\n");
Expand Down

0 comments on commit a7b46ec

Please sign in to comment.