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

Allow binary responses in getObject #107

Closed
wants to merge 6 commits into from
Closed
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
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"globals": {
// Allow specific global types present in the k6 JS runtime natively
"ArrayBuffer": "readonly",
"Uint8Array": "readonly",
"Set": "readonly",
"console": "readonly",
Expand Down
2 changes: 1 addition & 1 deletion localstack/init/ready.d/s3.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ BUCKET="test-jslib-aws"
# The localstack exposes its init script in the `/etc/localstack/init` folder.
# For convinience we've setup a `testdata` folder in the same directory.
testdata_folder="/etc/localstack/init/testdata/s3"
testdata_files="bonjour.txt tschuss.txt delete.txt"
testdata_files="bonjour.txt tschuss.txt delete.txt z-load-impact.png"

# Create the test-jslib-aws bucket
awslocal s3api create-bucket \
Expand Down
Binary file added localstack/init/testdata/s3/z-load-impact.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@babel/plugin-transform-block-scoping": "^7.14.1",
"@babel/preset-env": "^7.4.4",
"@babel/preset-typescript": "^7.16.7",
"@types/k6": "^0.45.0",
"@types/k6": "^0.51.0",
"@types/uuid": "^3.4.0",
"@types/webpack": "^5.28.0",
"@typescript-eslint/eslint-plugin": "^6.11.0",
Expand Down
18 changes: 13 additions & 5 deletions src/internal/s3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { bytes } from 'k6'
import { parseHTML } from 'k6/html'
import http, { RefinedResponse, ResponseType } from 'k6/http'

Expand Down Expand Up @@ -62,6 +61,7 @@ export class S3Client extends AWSClient {

const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
headers: signedRequest.headers,
responseType: 'text',
})
this._handle_error('ListBuckets', res)

Expand Down Expand Up @@ -121,6 +121,7 @@ export class S3Client extends AWSClient {

const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
headers: signedRequest.headers,
responseType: 'text',
})
this._handle_error('ListObjectsV2', res)

Expand Down Expand Up @@ -164,11 +165,16 @@ export class S3Client extends AWSClient {
*
* @param {string} bucketName - The bucket name containing the object.
* @param {string} objectKey - Key of the object to get.
* @param {string} responseType - The desired interpretation of the response body ('text' or 'binary').
* @return {S3Object} - returns the content of the fetched S3 Object.
* @throws {S3ServiceError}
* @throws {InvalidSignatureError}
*/
async getObject(bucketName: string, objectKey: string): Promise<S3Object> {
async getObject(
bucketName: string,
objectKey: string,
responseType: string = 'text'
): Promise<S3Object> {
// Prepare request
const method = 'GET'

Expand All @@ -184,6 +190,7 @@ export class S3Client extends AWSClient {

const res = await http.asyncRequest(method, signedRequest.url, null, {
headers: signedRequest.headers,
responseType: responseType as ResponseType,
})
this._handle_error('GetObject', res)

Expand Down Expand Up @@ -344,6 +351,7 @@ export class S3Client extends AWSClient {

const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
headers: signedRequest.headers,
responseType: 'text',
})
this._handle_error('CreateMultipartUpload', res)

Expand Down Expand Up @@ -537,7 +545,7 @@ export class S3Object {
etag: string
size: number
storageClass: StorageClass
data?: string | bytes | null
data?: string | ArrayBuffer | null

/**
* Create an S3 Object
Expand All @@ -547,15 +555,15 @@ export class S3Object {
* @param {string} etag - S3 object's etag
* @param {number} size - S3 object's size
* @param {StorageClass} storageClass - S3 object's storage class
* @param {string | bytes | null} data=null - S3 Object's data
* @param {string | ArrayBuffer | null} data=null - S3 Object's data
*/
constructor(
key: string,
lastModified: number,
etag: string,
size: number,
storageClass: StorageClass,
data?: string | bytes | null
data?: string | ArrayBuffer | null
) {
this.key = key
this.lastModified = lastModified
Expand Down
4 changes: 4 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ const testData = {
key: 'delete.txt',
body: 'Delete me in a test!',
},
{
key: 'z-load-impact.png',
body: open('../localstack/init/testdata/s3/z-load-impact.png', 'b'),
},
],
},

Expand Down
15 changes: 13 additions & 2 deletions tests/internal/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function s3TestSuite(data) {
s3Client.endpoint = s3Endpoint

await asyncDescribe('s3.listBuckets', async (expect) => {
let buckets;
let buckets
// Act
buckets = await s3Client.listBuckets()

Expand All @@ -28,10 +28,11 @@ export async function s3TestSuite(data) {

// Assert
expect(objects).to.be.an('array')
expect(objects).to.have.lengthOf(3)
expect(objects).to.have.lengthOf(4)
expect(objects[0].key).to.equal('bonjour.txt')
expect(objects[1].key).to.equal('delete.txt')
expect(objects[2].key).to.equal('tschuss.txt')
expect(objects[3].key).to.equal('z-load-impact.png')
})

await asyncDescribe('s3.getObject', async (expect) => {
Expand All @@ -46,6 +47,12 @@ export async function s3TestSuite(data) {
data.s3.testObjects[1].key
)

const gotBinaryObject = await s3Client.getObject(
data.s3.testBucketName,
data.s3.testObjects[3].key,
'binary'
)

let getObjectFromNonExistingBucketError
try {
await s3Client.getObject('non-existent-bucket', data.s3.testObjects[0].key)
Expand All @@ -67,6 +74,10 @@ export async function s3TestSuite(data) {
expect(gotSecondObject).to.be.an('object')
expect(gotSecondObject.key).to.equal(data.s3.testObjects[1].key)
expect(gotSecondObject.data).to.equal(data.s3.testObjects[1].body)
expect(gotBinaryObject).to.be.an('object')
expect(gotBinaryObject.data).to.be.an.instanceof(ArrayBuffer)
expect(gotBinaryObject.key).to.equal(data.s3.testObjects[3].key)
expect(gotBinaryObject.data.byteLength).to.equal(data.s3.testObjects[3].body.byteLength)
expect(getObjectFromNonExistingBucketError).to.not.be.undefined
expect(getObjectFromNonExistingBucketError).to.be.an.instanceOf(S3ServiceError)
expect(getNonExistingObjectError).to.not.be.undefined
Expand Down
Loading