Skip to content

Commit

Permalink
add test for big file
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Jan 20, 2025
1 parent 3aede8d commit 536939b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 29 deletions.
13 changes: 12 additions & 1 deletion tests/filetest/src/controllers/file_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
assign,
file,
FileProcessor,
getSingleton
getSingleton,
http
} from "fortjs";

import * as Path from "path";
Expand Down Expand Up @@ -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);
}
}
27 changes: 27 additions & 0 deletions tests/filetest/test/common.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 = '<html>\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 '</html>' to avoid exceeding the size
content += 'A'; // You can replace 'A' with any text you prefer
}

// Add the closing HTML tag
content += '\n</html>';

// 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;
})
}
29 changes: 28 additions & 1 deletion tests/filetest/test/file_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ let {
forbiddenText,
methodNotAllowedMsg,
badRequestMsg,
removeSpaceAndNewLine
removeSpaceAndNewLine,
createHtmlTextFile
} = require('./common');
const fs = require('fs').promises;

describe("/file", () => {

Expand Down Expand Up @@ -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('</html>');

// 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);
})
});
30 changes: 3 additions & 27 deletions tests/filetest/test/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<html>\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 '</html>' to avoid exceeding the size
content += 'A'; // You can replace 'A' with any text you prefer
}

// Add the closing HTML tag
content += '\n</html>';

// 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", () => {

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 536939b

Please sign in to comment.