aws-sdk-client-mock
aws-sdk-client-mock copied to clipboard
S3 Presign
S3 presign is done using a helper library @aws-sdk/s3-request-presigner
this seems like something that would need a helper library or additional documentation to mock properly.
It would be good to see a way to mock presign requests. I'm happy to help with this.
I looked at it and I think mocking it is not (easily) possible right now with the lib.
Presign is done by a middleware. getSignedUrl()
is creating the middleware and makes the send()
call. Middleware intercepts the command, generates signed URL, and returns the result instead of proceeding with actually sending the command to AWS. See: https://github.com/aws/aws-sdk-js-v3/blob/main/packages/s3-request-presigner/src/getSignedUrl.ts
The mocking lib does not mock middlewares, but only client send()
command. Mocking middleware can be especially hard in this case, since it's created locally in the getSignedUrl()
function.
I think the way to go is to mock the whole getSignedUrl()
function.
Feel free to submit a PR for this!
Yeah so at the moment my strategy has been to mock getSignedUrl()
and this works but it's a pretty basic mock. I'll look into it but might take some time. I'll post here if I have any updates.
Yeah, this functionality would be great.
An alternative solution is to mock your URL command to return a {presigned: require('url').parse('http://url')}
. At least, it worked for me on PutObject.
The best solution I've found is from the referenced issue:
const presigner = require('@aws-sdk/s3-request-presigner/dist-cjs/getSignedUrl') // eslint-disable-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment
sinon.stub(presigner, 'getSignedUrl').resolves('https://s3.amazonaws.com/path/to/object')
I get an error with this (sinon.stub)
TypeError: Descriptor for property getSignedUrl is non-configurable and non-writable
This is how I've done using jest:
const presigner = require('@aws-sdk/s3-request-presigner/dist-cjs/getSignedUrl')
jest.spyOn(presigner, 'getSignedUrl').mockResolvedValueOnce(signedUrlMockValue)
Thanks for the simple solution @jhecking 🙌
This is how I've done using jest:
const presigner = require('@aws-sdk/s3-request-presigner/dist-cjs/getSignedUrl') jest.spyOn(presigner, 'getSignedUrl').mockResolvedValueOnce(signedUrlMockValue)
Thanks for the simple solution @jhecking 🙌
This works great, but you need to expose (fake) AWS credentials, or otherwise it will complain.
CredentialsProviderError: Could not load credentials from any providers
Similar behaviour can be achieved for createPresignedPost
from s3-presigned-post.
const postPresigner = require('@aws-sdk/s3-presigned-post/dist-cjs/createPresignedPost.js')
jest.spyOn(postPresigner, 'createPresignedPost').mockResolvedValue({ url: 'mockValue',
fields: {
key: 'mockValue',
Policy: 'mockValue',
'X-Amz-Signature': 'mockValue',
'X-Amz-Security-Token': 'mockValue',
bucket: 'mockValue',
'X-Amz-Algorithm': 'mockValue',
'X-Amz-Credential': 'mockValue',
'X-Amz-Date': 'mockValue',
} })
This is how I've done using jest:
const presigner = require('@aws-sdk/s3-request-presigner/dist-cjs/getSignedUrl') jest.spyOn(presigner, 'getSignedUrl').mockResolvedValueOnce(signedUrlMockValue)
Thanks for the simple solution @jhecking 🙌
Thank you. I had to change it, but your approach helped me.
My solution:
jest.mock("@aws-sdk/s3-request-presigner", () => {
return {
getSignedUrl: jest.fn().mockResolvedValueOnce(signedUrlMockValue),
};
});
This is how I've done using jest:
const presigner = require('@aws-sdk/s3-request-presigner/dist-cjs/getSignedUrl') jest.spyOn(presigner, 'getSignedUrl').mockResolvedValueOnce(signedUrlMockValue)
Thanks for the simple solution @jhecking 🙌
This solution doesn't work anymore starting from SDK v3.495.0 unfortunately.
Is there any PR open to support getSignedUrl mock ? I am completely blocked as work around discussed above as well not working with new version of sdk