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

Cache does not decode compressed responses properly #2105

Open
2 tasks done
michael42 opened this issue Aug 10, 2022 · 0 comments
Open
2 tasks done

Cache does not decode compressed responses properly #2105

michael42 opened this issue Aug 10, 2022 · 0 comments

Comments

@michael42
Copy link

Describe the bug

  • Node.js version: v16.16.0
  • OS & version: Linux 5.15

We noticed that got returned the wrong response for every second request, when making requests with enabled caching.

Actual behavior

Apparently, when making requests with the cache option, got does not decode the cached response properly (based on the Content-Encoding header of the cached response), but instead uses the Content-Encoding-header of the 304 Not Modified response.

Expected behavior

got should be able to cache and re-use cached response, even if the 304 response is sent with a different Content-Encoding header.

Code to reproduce

import http from 'node:http';
import zlib from 'node:zlib';
import got from 'got';

const gzipData = zlib.gzipSync('test');
const etag = '"asdf"';

const server = http
  .createServer((req, res) => {
    if (req.headers['if-none-match'] === etag) {
      res.writeHead(304); // not, no Content-Encoding header
      res.end();
      return;
    }

    res.writeHead(200, { 'content-encoding': 'gzip', etag });
    res.write(gzipData);
    res.end();
  })
  .listen(2345);

const map = new Map();
for (let i = 0; i < 4; i++) {
  const result = await got.get({
    url: 'http://localhost:2345',
    cache: map,
    responseType: 'text',
  });
  console.log("response", i, result.body.slice(0, 32));
}

server.close();

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.
@szmarczak szmarczak changed the title got with caching enabled does not decode compressed cached responses properly Cache does not decode compressed responses properly Aug 10, 2022
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