Skip to content

Commit

Permalink
add check for big files
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Jan 20, 2025
1 parent fea73ed commit 3aede8d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
3 changes: 2 additions & 1 deletion tests/filetest/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ build
deploy_key
dist

upload.png
upload.png
ignorefile.*;
1 change: 0 additions & 1 deletion tests/filetest/src/controllers/file_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ export class FileController extends Controller {

@worker(HTTP_METHOD.Get)
async getCookie() {

const result = textResult(this.cookie.getCookie('hello'));
console.log(result);
return result;
Expand Down
12 changes: 7 additions & 5 deletions tests/filetest/test/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ let {
isProduction
} = require('./common');




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

it('default path', (done) => {
Expand All @@ -22,7 +25,6 @@ describe("/default", () => {
done();
});


})

it("/default", done => {
Expand All @@ -38,7 +40,7 @@ describe("/default", () => {
let etagVal;
function testWithEtag(done) {
const req = request.get('/assets/').accept(browserAccept).set('if-none-match', etagVal);
console.log("request", req);
// console.log("request", req);
req.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(304);
Expand All @@ -57,18 +59,18 @@ describe("/default", () => {
expect(res).to.have.header('Etag');
expect(res).to.have.header('last-modified');
etagVal = res.headers['etag'];
console.log("etagVal", etagVal);
// console.log("etagVal", res.headers);
// }
// else {
// expect(res).to.not.have.header('Etag');
// }
// console.log("data", res.text);
expect(res.text).to.include('</html>');
expect(res.header['x-powered-by']).to.equal('MyFort');
testWithEtag(done);
});
})



it("/file/getCookie", done => {
request.get('/file/getCookie').accept(browserAccept).end((err, res) => {
expect(err).to.be.null;
Expand Down
59 changes: 56 additions & 3 deletions tests/filetest/test/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@ let {
removeSpaceAndNewLine
} = 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", () => {

it('default path', (done) => {
Expand All @@ -20,8 +49,6 @@ describe("/static", () => {
expect(res.header['x-powered-by']).to.equal('MyFort');
done();
});


})

it("/index.html", done => {
Expand All @@ -43,4 +70,30 @@ describe("/static", () => {
done();
});
})
});

let etagVal;

it('big file test', async () => {
const filePath = await createHtmlTextFile(10);
const fileName = path.basename(filePath);
let res = await request.get(`/static/${fileName}`).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(`/static/${fileName}`).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);
})
});

0 comments on commit 3aede8d

Please sign in to comment.