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

nock.recorder.rec does not record when stream not put into flowing mode #2185

Open
att14 opened this issue Apr 19, 2021 · 5 comments
Open

nock.recorder.rec does not record when stream not put into flowing mode #2185

att14 opened this issue Apr 19, 2021 · 5 comments
Labels
pull request welcome Recorder related to recording fixtures or Nock Back

Comments

@att14
Copy link

att14 commented Apr 19, 2021

What is the expected behavior?
nock.recorder.play() should contain the recorded version of the request to GetObject.

What is the actual behavior?
Request does not appear in array returned from nock.recorder.play().

How to reproduce the issue

const nock = require('nock');
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3');

const s3 = new S3Client();
const bucket = '';
const key = '';

async function main() {
  nock.recorder.rec({ dont_print: true });

  try {
    await s3.send(new GetObjectCommand({ Bucket: bucket, Key: 'invalid' }));
  } catch { }

  const response = await s3.send(new GetObjectCommand({ Bucket: bucket, Key: key }));
  console.log(response.Body.statusCode);

  console.log(nock.recorder.play());
}

main()
  .then(() => process.exit(0))
  .catch((err) => {
      console.log(err);
      process.exit(1);
  })

Fill in bucket and key and run. key must be a valid and present S3 key.

With a valid bucket and key the output is:

200
[
  '\n' +
    `nock('https://test-onelivex.s3.us-east-2.amazonaws.com:443', {"encodedQueryParams":true})\n` +
    "  .get('/invalid')\n" +
    '  .query({"x-id":"GetObject"})\n' +
    '  .reply(404, "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>\\n<Error><Code>NoSuchKey</Code><Message>The specified key does not exist.</Message><Key>invalid</Key><RequestId>6XB4DKCEPRQPWWY0</RequestId><HostId>pHwvM3hwyAKmDqYq6z6uBRIFos+UhBwM9hMAdOBbuTSXUaM3seP1yTV0C93o3RKE152McDWBnvE=</HostId></Error>", [\n' +
    "  'x-amz-request-id',\n" +
    "  '6XB4DKCEPRQPWWY0',\n" +
    "  'x-amz-id-2',\n" +
    "  'pHwvM3hwyAKmDqYq6z6uBRIFos+UhBwM9hMAdOBbuTSXUaM3seP1yTV0C93o3RKE152McDWBnvE=',\n" +
    "  'Content-Type',\n" +
    "  'application/xml',\n" +
    "  'Transfer-Encoding',\n" +
    "  'chunked',\n" +
    "  'Date',\n" +
    "  'Mon, 19 Apr 2021 00:48:04 GMT',\n" +
    "  'Server',\n" +
    "  'AmazonS3'\n" +
    ']);'
]

Versions

Software Version(s)
Nock 0.21.1
@aws-sdk/client-s3 3.13.0
Node v15.12.0
@paulmelnikow
Copy link
Member

Hi! Just wagering a guess that this is not a problem with nock. I’d guess an invalid bucket and key is erroring out in the AWS SDK before the HTTP request is made. Maybe you can catch the exception and see.

@att14
Copy link
Author

att14 commented Apr 19, 2021

You can see the response is 200 when logged by console.log(response.Body.statusCode). It is the first line of the output.

@att14
Copy link
Author

att14 commented Apr 19, 2021

@paulmelnikow thanks for your extremely fast response earlier. I have been looking into this some more and found that adding res.on('data', (chunk) => {}); within the callback of overridenRequest resolves the problem. The reason I did this we because the res.once('end', ...) event was never being hit, so I started seeing which events were and noticed that anytime I added data it would actually record the request. Looking into readable streams I noticed this in the documentation:

If a Readable is switched into flowing mode and there are no consumers available to handle the data, that data will be lost. This can occur, for instance, when the readable.resume() method is called without a listener attached to the 'data' event, or when a 'data' event handler is removed from the stream.

All Readable streams begin in paused mode but can be switched to flowing mode in one of the following ways:

  • Adding a 'data' event handler.
  • Calling the stream.resume() method.
  • Calling the stream.pipe() method to send the data to a Writable.

I guess in this case the stream is never put into flowing mode, so unless the client actually consumes the stream nock won't record anything.

@att14 att14 changed the title nock.recorder.rec does not record AWS S3 V3 GetObject when object exists nock.recorder.rec does not record when stream not put into flowing mode Jul 6, 2021
@gr2m gr2m added the Recorder related to recording fixtures or Nock Back label Nov 8, 2021
@gr2m
Copy link
Member

gr2m commented Nov 8, 2021

@att14 could you start a pull request with a test replicating the issue? That'd be a great start to address it

att14 added a commit to onelive-dev/nock that referenced this issue Nov 30, 2021
@att14
Copy link
Author

att14 commented Nov 30, 2021

Okay, #2257 has been created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pull request welcome Recorder related to recording fixtures or Nock Back
Projects
None yet
Development

No branches or pull requests

3 participants