diff --git a/src/handlers/file_handler.ts b/src/handlers/file_handler.ts index 6cb549f..06385ae 100644 --- a/src/handlers/file_handler.ts +++ b/src/handlers/file_handler.ts @@ -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)); } diff --git a/tests/filetest/test/default.js b/tests/filetest/test/default.js index 5fbe01f..7e442f5 100644 --- a/tests/filetest/test/default.js +++ b/tests/filetest/test/default.js @@ -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; @@ -58,4 +77,4 @@ describe("/default", () => { }); }) -}); \ No newline at end of file +});