Skip to content

Commit

Permalink
add test for if-none-match with etag in files
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Jan 20, 2025
1 parent fc3f37e commit fea73ed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/handlers/file_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ export class FileHandler {
weak: this.requestHandler.config.eTag.type === ETAG_TYPE.Weak
});
const response = this.requestHandler.response;
response.setHeader('Etag', eTagValue);
if (this.isClientHasFreshFile(lastModified, eTagValue)) { // client has fresh file
response.statusCode = HTTP_STATUS_CODE.NotModified;
response.end();
}
else {
response.setHeader('Etag', eTagValue);
response.setHeader('Last-Modified', lastModified);
this.sendFileAsResponse(filePath, getMimeTypeFromFileType(fileType));
}
Expand Down
23 changes: 21 additions & 2 deletions tests/filetest/test/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,40 @@ describe("/default", () => {
})

it("/assets/", done => {
let etagVal;
function testWithEtag(done) {
const req = request.get('/assets/').accept(browserAccept).set('if-none-match', etagVal);
console.log("request", req);
req.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(304);
expect(res).to.have.header('content-type', undefined);
expect(res).to.have.header('Etag');
expect(res.header['x-powered-by']).to.equal('MyFort');
done();
});
}

request.get('/assets/').accept(browserAccept).end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(200);
expect(res).to.have.header('content-type', 'text/html');
// if (isProduction) {
expect(res).to.have.header('Etag');
expect(res).to.have.header('last-modified');
etagVal = res.headers['etag'];
console.log("etagVal", etagVal);
// }
// else {
// expect(res).to.not.have.header('Etag');
// }
expect(res.header['x-powered-by']).to.equal('MyFort');
done();
testWithEtag(done);
});
})



it("/file/getCookie", done => {
request.get('/file/getCookie').accept(browserAccept).end((err, res) => {
expect(err).to.be.null;
Expand All @@ -58,4 +77,4 @@ describe("/default", () => {
});
})

});
});

0 comments on commit fea73ed

Please sign in to comment.