Skip to content

Commit

Permalink
Add test for invalid range in blob download request
Browse files Browse the repository at this point in the history
  • Loading branch information
cnaj committed Jan 13, 2025
1 parent 342cf86 commit 211f03a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/blob/RequestPolicy/RangePolicyFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { BaseRequestPolicy, WebResource } from "@azure/storage-blob";

// Create a policy factory with create() method provided
// In TypeScript, following factory class needs to implement Azure.RequestPolicyFactory type
export default class RangePolicyFactory {
// Constructor to accept parameters
private range: string;

constructor(range: any) {
this.range = range;
}

// create() method needs to create a new RequestIDPolicy object
create(nextPolicy: any, options: any) {
return new RangePolicy(nextPolicy, options, this.range);
}
}

// Create a policy by extending from Azure.BaseRequestPolicy
class RangePolicy extends BaseRequestPolicy {
private range: string;

constructor(nextPolicy: any, options: any, range: any) {
super(nextPolicy, options);
this.range = range;
}

// Customize HTTP requests and responses by overriding sendRequest
// Parameter request is Azure.WebResource type
async sendRequest(request: WebResource) {
request.headers.set("Range", `${this.range}`);

return await this._nextPolicy.sendRequest(request);
}
}
57 changes: 57 additions & 0 deletions tests/blob/apis/blob.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getUniqueName,
sleep
} from "../../testutils";
import RangePolicyFactory from "../RequestPolicy/RangePolicyFactory";

// Set true to enable debug log
configLogger(false);
Expand Down Expand Up @@ -302,6 +303,62 @@ describe("BlobAPIs", () => {
assert.fail();
});

it("download invalid range @loki @sql", async () => {
const pipeline = newPipeline(
new StorageSharedKeyCredential(
EMULATOR_ACCOUNT_NAME,
EMULATOR_ACCOUNT_KEY
),
{
retryOptions: { maxTries: 1 },
// Make sure socket is closed once the operation is done.
keepAliveOptions: { enable: false }
}
);
pipeline.factories.unshift(
new RangePolicyFactory("bytes=0--1")
);
const serviceClient = new BlobServiceClient(baseURL, pipeline);
const containerClient = serviceClient.getContainerClient(containerName);
const blobClient = containerClient.getBlobClient(blobName);

const result = await blobClient.download(0);
assert.deepStrictEqual(await bodyToString(result, content.length), content);
assert.equal(result.contentRange, undefined);
assert.equal(
result._response.request.headers.get("x-ms-client-request-id"),
result.clientRequestId
);
});

it("download partial range (via custom policy) @loki @sql", async () => {
const pipeline = newPipeline(
new StorageSharedKeyCredential(
EMULATOR_ACCOUNT_NAME,
EMULATOR_ACCOUNT_KEY
),
{
retryOptions: { maxTries: 1 },
// Make sure socket is closed once the operation is done.
keepAliveOptions: { enable: false }
}
);
pipeline.factories.unshift(
new RangePolicyFactory("bytes=0-4")
);
const serviceClient = new BlobServiceClient(baseURL, pipeline);
const containerClient = serviceClient.getContainerClient(containerName);
const blobClient = containerClient.getBlobClient(blobName);

const result = await blobClient.download(0);
assert.deepStrictEqual(await bodyToString(result, content.length), content.substring(0, 5));
assert.equal(result.contentRange, `bytes 0-4/${content.length}`);
assert.equal(
result._response.request.headers.get("x-ms-client-request-id"),
result.clientRequestId
);
});

it("get properties response should not set content-type @loki @sql", async () => {
const blobURL404 = containerClient.getBlobClient("UN_EXIST_BLOB_");
try {
Expand Down

0 comments on commit 211f03a

Please sign in to comment.