Skip to content

Commit

Permalink
fix `Resource with the 304 status code and the 'content-length: x' he…
Browse files Browse the repository at this point in the history
…ader does not load` (close DevExpress#1602) (DevExpress#1638)

* fix `Resource with the 304 status code and the 'content-length: x' header does not load` (close DevExpress#1602)

* fix tests

* fix review issue
  • Loading branch information
LavrovArtem authored and AndreyBelym committed Feb 28, 2019
1 parent 8a3de6a commit 2e59c12
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/request-pipeline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ const stages = {
ctx.onResponseEventDataWithoutBody.push({ rule, opts: configureResponseEvent.opts });
});

ctx.destRes.pipe(ctx.res);
if (ctx.contentInfo.isNotModified)
ctx.res.end();
else
ctx.destRes.pipe(ctx.res);

if (ctx.onResponseEventDataWithoutBody.length) {
ctx.res.on('finish', () => {
const responseInfo = requestEventInfo.createResponseInfo(ctx);
Expand All @@ -117,7 +121,7 @@ const stages = {
}
}
else
ctx.res.end('');
ctx.res.end();

return;
}
Expand Down
40 changes: 40 additions & 0 deletions test/server/proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3338,5 +3338,45 @@ describe('Proxy', () => {
testRedirectRequestStatusCode(202, false)
]);
});

it('Should not pipe the request with the 304 status code (Not Modified) (GH-1602)', () => {
const server = net.createServer(socket => {
socket.on('data', () => {
socket.write([
'HTTP/1.1 304 Not Modified',
'Content-Length: 5',
'',
''
].join('\r\n'));
});
});

return getFreePort()
.then(port => new Promise(resolve => server.listen(port, () => resolve(port))))
.then(port => new Promise((resolve, reject) => {
const proxyUrl = proxy.openSession(`http://127.0.0.1:${port}/`, session);
const reqOptions = Object.assign(urlLib.parse(proxyUrl), {
method: 'GET',
headers: { 'if-none-match': 'NQQ6Iyi1ttEATRNQs+U9yQ==' }
});

const req = http.request(reqOptions, res => {
const chunks = [];

res.on('data', chunk => chunks.push(chunk));
res.on('end', () => resolve({ res, body: Buffer.concat(chunks).toString() }));
});

req.on('error', reject);
req.setTimeout(1500, () => reject('timeout'));
req.end();
}))
.then(({ res, body }) => {
expect(res.statusCode).eql(304);
expect(res.headers['content-length']).eql('5');
expect(body).eql('');
server.close();
});
});
});
});

0 comments on commit 2e59c12

Please sign in to comment.