diff --git a/tests/filetest/src/controllers/file_controller.js b/tests/filetest/src/controllers/file_controller.js index 60472da..9817c38 100644 --- a/tests/filetest/src/controllers/file_controller.js +++ b/tests/filetest/src/controllers/file_controller.js @@ -12,7 +12,8 @@ import { assign, file, FileProcessor, - getSingleton + getSingleton, + http } from "fortjs"; import * as Path from "path"; @@ -121,4 +122,14 @@ export class FileController extends Controller { console.log(result); return result; } + + @http.get('/bigfile') + // @worker(HTTP_METHOD.Get) + // @route("/bigfile") + async getBigFile() { + const filePath = Path.join(__dirname, '../static', 'ignorefile.big_html_file.html'); + // console.log("file", filePath); + // return textResult(filePath); + return fileResult(filePath); + } } diff --git a/tests/filetest/test/common.js b/tests/filetest/test/common.js index 4d6ceb0..55ff04c 100644 --- a/tests/filetest/test/common.js +++ b/tests/filetest/test/common.js @@ -1,5 +1,7 @@ let chai = require('chai'); let chaiHttp = require('chai-http'); +const fs = require('fs').promises; +const path = require('path'); chai.use(chaiHttp); const url = 'http://localhost:4000'; exports.url = url; @@ -15,3 +17,28 @@ exports.expect = chai.expect; exports.browserAccept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"; exports.isProduction = process.env.NODE_ENV === "production" exports.isDevelopment = process.env.NODE_ENV === "development" +exports.createHtmlTextFile = (sizeInMB, fileName = 'big_html_file.html') => { + // Calculate the number of characters needed to reach approximately the desired size + const sizeInBytes = sizeInMB * 1024 * 1024; // Convert MB to bytes + let content = '\n'; // Start with the opening HTML tag + + // Fill content with random text or a repeating pattern + while (content.length < sizeInBytes - 7) { // Subtract 7 for the length of '' to avoid exceeding the size + content += 'A'; // You can replace 'A' with any text you prefer + } + + // Add the closing HTML tag + content += '\n'; + + // Slice the content to the exact desired size (in case the length slightly exceeds the size) + // content = content.slice(0, sizeInBytes); + + // Write content to a file + const filePath = path.join(process.cwd(), 'static', 'ignorefile.' + fileName); + return fs.writeFile( + filePath, + content + ).then(_ => { + return filePath; + }) +} diff --git a/tests/filetest/test/file_test.js b/tests/filetest/test/file_test.js index 216e1f9..582809f 100644 --- a/tests/filetest/test/file_test.js +++ b/tests/filetest/test/file_test.js @@ -7,8 +7,10 @@ let { forbiddenText, methodNotAllowedMsg, badRequestMsg, - removeSpaceAndNewLine + removeSpaceAndNewLine, + createHtmlTextFile } = require('./common'); +const fs = require('fs').promises; describe("/file", () => { @@ -147,4 +149,29 @@ describe("/file", () => { done(); }); }) + + it("/bigfile", async () => { + const filePath = await createHtmlTextFile(10); + + let res = await request.get('/file/bigfile').accept(browserAccept); + expect(res).to.have.status(200); + expect(res).to.have.header('content-type', 'text/html'); + expect(res.header['x-powered-by']).to.equal('MyFort'); + + expect(res).to.have.header('Etag'); + expect(res).to.have.header('last-modified'); + etagVal = res.headers['etag']; + expect(res.text).to.include(''); + + // check after etag + res = await request.get(`/file/bigfile`).accept(browserAccept).set('if-none-match', etagVal); + 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'); + + + // delete file + await fs.unlink(filePath); + }) }); diff --git a/tests/filetest/test/static.js b/tests/filetest/test/static.js index c70dde2..bd6f919 100644 --- a/tests/filetest/test/static.js +++ b/tests/filetest/test/static.js @@ -7,37 +7,13 @@ let { forbiddenText, methodNotAllowedMsg, badRequestMsg, - removeSpaceAndNewLine + removeSpaceAndNewLine, + createHtmlTextFile } = require('./common'); const fs = require('fs').promises; const path = require('path'); -function createHtmlTextFile(sizeInMB) { - // Calculate the number of characters needed to reach approximately the desired size - const sizeInBytes = sizeInMB * 1024 * 1024; // Convert MB to bytes - let content = '\n'; // Start with the opening HTML tag - - // Fill content with random text or a repeating pattern - while (content.length < sizeInBytes - 7) { // Subtract 7 for the length of '' to avoid exceeding the size - content += 'A'; // You can replace 'A' with any text you prefer - } - - // Add the closing HTML tag - content += '\n'; - - // Slice the content to the exact desired size (in case the length slightly exceeds the size) - // content = content.slice(0, sizeInBytes); - - // Write content to a file - const filePath = path.join(process.cwd(), 'static', 'ignorefile.big_html_file.html'); - return fs.writeFile( - filePath, - content - ).then(_ => { - return filePath; - }) -} describe("/static", () => { @@ -74,7 +50,7 @@ describe("/static", () => { let etagVal; it('big file test', async () => { - const filePath = await createHtmlTextFile(10); + const filePath = await createHtmlTextFile(10, 'temp_big_file.html'); const fileName = path.basename(filePath); let res = await request.get(`/static/${fileName}`).accept(browserAccept); expect(res).to.have.status(200);