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

Stream depending on params for S3.getObject #97

Open
wawanco opened this issue Jul 28, 2017 · 0 comments
Open

Stream depending on params for S3.getObject #97

wawanco opened this issue Jul 28, 2017 · 0 comments

Comments

@wawanco
Copy link

wawanco commented Jul 28, 2017

Hi there,

In my lib I have the following call

s3.getObject({ Bucket: bucket, Key: key }).createReadStream();

I would like to be able to mock to s3.getObject method and to have different stream depending on the key passed in parameter.

The kind of code I would like to write in my unit test is like

AWS.mock('S3', 'getObject', (params) => {
            if (params.Key === 'key1') {
                return 'body1';
            } else {
               return 'body2';
            }
});

Obviously it is not the way the lib work for now. Ideally I would have liked to have the following implementation of createReadStream in the mockServiceMethod method:

createReadStream() {
                const stream = new Readable();
                stream._read = function (size) {
                    if (typeof (replace) === 'string' || Buffer.isBuffer(replace)) {
                        this.push(replace);
                    } else if (typeof (replace) === 'function') {
                        this.push(replace(userArgs[0]));
                    }
                    this.push(null);
                };
                return stream;
            },

The part I want to add is if (typeof (replace) === 'function') {this.push(replace(userArgs[0]));}

Does it make sense to you ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant