Skip to content

Commit

Permalink
Disregard K6_DISCARD_RESPONSE_BODIES (#114)
Browse files Browse the repository at this point in the history
* Add baseRequestParams to AWSClient

This allow us to ignore k6's discardResponseBodies in the context of
jslib-aws. As it is mostly a utility library, users have been mostly
about having the ability available. See #45 for details.

Using the baseRequestParams we set the default responseType for children
implementation, allowing us to effectively disregard the
http discardResponseBodies option. We can still, locally override it
where necessary, as in S3Client.getObject for instance.

* Adopt AWSClient.baseRequestParams in AWS client classes

* Update src/internal/client.ts

Co-authored-by: Joan López de la Franca Beltran <[email protected]>

---------

Co-authored-by: Joan López de la Franca Beltran <[email protected]>
  • Loading branch information
oleiade and joanlopez authored Sep 6, 2024
1 parent b312325 commit fbe41eb
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/internal/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RefinedResponse, ResponseType } from 'k6/http'
import { RefinedResponse, ResponseType, Params } from 'k6/http'

import { AWSConfig } from './config'
import { Endpoint } from './endpoint'
Expand Down Expand Up @@ -27,6 +27,22 @@ export class AWSClient {
readonly awsConfig: AWSConfig
readonly serviceName: string

// Because jslib-aws is mostly used as a way to setup or feed k6 tests, and
// we want the jslib-aws to be able to disregard k6's discardResponseBodies: meaning
// that for instance, even when setting discardResponseBodies to true in the k6 options, using
// s3.getObject still receives the underlying response body and returns data to the user.
//
// To achieve this, we set the responseType to 'text' in the baseRequestParams, as it
// will lead the http module to ignore the discardResponseBodies option.
//
// AWS Client classes can override this value if they want to receive the response body
// as a different type ('binary' for instance, e.g. S3Client.getObject).
//
// See #45: https://github.com/grafana/k6-jslib-aws/issues/45
readonly baseRequestParams: Params = {
responseType: 'text',
}

private _endpoint?: Endpoint

/**
Expand Down
1 change: 1 addition & 0 deletions src/internal/event-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class EventBridgeClient extends AWSClient {
)

const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, EventBridgeOperation.PutEvents)
Expand Down
1 change: 1 addition & 0 deletions src/internal/kinesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export class KinesisClient extends AWSClient {
)

const res = await http.asyncRequest('POST', signedRequest.url, signedRequest.body, {
...this.baseRequestParams,
headers: signedRequest.headers,
})

Expand Down
2 changes: 2 additions & 0 deletions src/internal/kms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class KMSClient extends AWSClient {
)

const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, KMSOperation.ListKeys)
Expand Down Expand Up @@ -112,6 +113,7 @@ export class KMSClient extends AWSClient {
)

const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, KMSOperation.GenerateDataKey)
Expand Down
1 change: 1 addition & 0 deletions src/internal/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class LambdaClient extends AWSClient {
)

const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res)
Expand Down
10 changes: 10 additions & 0 deletions src/internal/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class S3Client extends AWSClient {
)

const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, 'ListBuckets')
Expand Down Expand Up @@ -119,6 +120,7 @@ export class S3Client extends AWSClient {
)

const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, 'ListObjectsV2')
Expand Down Expand Up @@ -199,6 +201,7 @@ export class S3Client extends AWSClient {
}

const res = await http.asyncRequest(method, signedRequest.url, null, {
...this.baseRequestParams,
headers: signedRequest.headers,
responseType: responseType as ResponseType,
})
Expand Down Expand Up @@ -258,6 +261,7 @@ export class S3Client extends AWSClient {
)

const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, 'PutObject')
Expand Down Expand Up @@ -287,6 +291,7 @@ export class S3Client extends AWSClient {
)

const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, 'DeleteObject')
Expand Down Expand Up @@ -326,6 +331,7 @@ export class S3Client extends AWSClient {
)

const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
...this.baseRequestParams,
headers: signedRequest.headers,
})

Expand Down Expand Up @@ -360,6 +366,7 @@ export class S3Client extends AWSClient {
)

const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, 'CreateMultipartUpload')
Expand Down Expand Up @@ -410,6 +417,7 @@ export class S3Client extends AWSClient {
)

const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, 'UploadPart')
Expand Down Expand Up @@ -460,6 +468,7 @@ export class S3Client extends AWSClient {
)

const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, 'CompleteMultipartUpload')
Expand Down Expand Up @@ -494,6 +503,7 @@ export class S3Client extends AWSClient {
)

const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, 'AbortMultipartUpload')
Expand Down
5 changes: 5 additions & 0 deletions src/internal/secrets-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class SecretsManagerClient extends AWSClient {
)

const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, SecretsManagerOperation.ListSecrets)
Expand Down Expand Up @@ -100,6 +101,7 @@ export class SecretsManagerClient extends AWSClient {
)

const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
...this.baseRequestParams,
headers: signedRequest.headers,
})

Expand Down Expand Up @@ -160,6 +162,7 @@ export class SecretsManagerClient extends AWSClient {
// headers['X-Amz-Target'] = `${this.serviceName}.CreateSecret`

const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, SecretsManagerOperation.CreateSecret)
Expand Down Expand Up @@ -200,6 +203,7 @@ export class SecretsManagerClient extends AWSClient {
)

const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, SecretsManagerOperation.PutSecretValue)
Expand Down Expand Up @@ -249,6 +253,7 @@ export class SecretsManagerClient extends AWSClient {
)

const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, SecretsManagerOperation.DeleteSecret)
Expand Down
1 change: 1 addition & 0 deletions src/internal/sqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ export class SQSClient extends AWSClient {
)

const res = await http.asyncRequest('POST', signedRequest.url, signedRequest.body, {
...this.baseRequestParams,
headers: signedRequest.headers,
})

Expand Down
1 change: 1 addition & 0 deletions src/internal/ssm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class SystemsManagerClient extends AWSClient {
)

const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
...this.baseRequestParams,
headers: signedRequest.headers,
})
this.handleError(res, SystemsManagerOperation.GetParameter)
Expand Down

0 comments on commit fbe41eb

Please sign in to comment.