Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BaseRock tests #6162

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions test/examples/cookie-sessions/index.sapient.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const assert = require('assert');
const request = require('supertest');
const app = require('../../../examples/cookie-sessions/index.js');

describe('Cookie Session App', function() {
it('should increment session count and return correct message', function(done) {
const agent = request.agent(app);

agent
.get('/')
.expect(200)
.expect('Content-Type', /text/)
.expect('viewed 1 times\n')
.end(function(err, res) {
if (err) return done(err);

// Make a second request to check if count increments
agent
.get('/')
.expect(200)
.expect('Content-Type', /text/)
.expect('viewed 2 times\n')
.end(done);
});
});

it('should start a new session for a new client', function(done) {
const agent = request.agent(app);

agent
.get('/')
.expect(200)
.expect('Content-Type', /text/)
.expect('viewed 1 times\n')
.end(function(err, res) {
if (err) return done(err);

// Create a new agent (simulating a new client) and make a request
const newAgent = request.agent(app);
newAgent
.get('/')
.expect(200)
.expect('Content-Type', /text/)
.expect('viewed 1 times\n')
.end(done);
});
});

it('should maintain session across multiple requests', function(done) {
const agent = request.agent(app);

agent
.get('/')
.expect(200)
.expect('viewed 1 times\n')
.end(function(err, res) {
if (err) return done(err);

agent
.get('/')
.expect(200)
.expect('viewed 2 times\n')
.end(function(err, res) {
if (err) return done(err);

agent
.get('/')
.expect(200)
.expect('viewed 3 times\n')
.end(done);
});
});
});
});
111 changes: 111 additions & 0 deletions test/examples/downloads/index.sapient.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const assert = require('assert');
const request = require('supertest');
const path = require('path');
const fs = require('fs');

// Import the Express app
const app = require('../../../examples/downloads/index.js');

describe('Express Downloads App', function() {
it('should return HTML with download links on GET /', function(done) {
request(app)
.get('/')
.expect('Content-Type', /html/)
.expect(200)
.end(function(err, res) {
if (err) return done(err);
assert(res.text.includes('Download <a href="/files/notes/groceries.txt">notes/groceries.txt</a>'));
assert(res.text.includes('Download <a href="/files/amazing.txt">amazing.txt</a>'));
assert(res.text.includes('Download <a href="/files/missing.txt">missing.txt</a>'));
assert(res.text.includes('Download <a href="/files/CCTV大赛上海分赛区.txt">CCTV大赛上海分赛区.txt</a>'));
done();
});
});

it('should download an existing file', function(done) {
const testFilePath = path.join(__dirname, '../../../examples/downloads/files/amazing.txt');
const testFileContent = 'This is an amazing test file.';

// Create a test file
fs.writeFileSync(testFilePath, testFileContent);

request(app)
.get('/files/amazing.txt')
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Content-Disposition', 'attachment; filename="amazing.txt"')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
assert.strictEqual(res.text, testFileContent);

// Clean up the test file
fs.unlinkSync(testFilePath);
done();
});
});

it('should return 404 for a non-existent file', function(done) {
request(app)
.get('/files/nonexistent.txt')
.expect(404)
.end(function(err, res) {
if (err) return done(err);
assert.strictEqual(res.text, 'Cant find that file, sorry!');
done();
});
});

it('should handle files with non-ASCII characters in the name', function(done) {
const fileName = 'CCTV大赛上海分赛区.txt';
const encodedFileName = encodeURIComponent(fileName);
const testFilePath = path.join(__dirname, '../../../examples/downloads/files', fileName);
const testFileContent = 'File with non-ASCII characters in the name.';

// Create a test file
fs.writeFileSync(testFilePath, testFileContent);

request(app)
.get(`/files/${encodedFileName}`)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(function(res) {
const contentDisposition = res.headers['content-disposition'];
assert(contentDisposition.startsWith('attachment; filename='), 'Content-Disposition should start with "attachment; filename="');
assert(contentDisposition.includes(fileName) || contentDisposition.includes(encodeURIComponent(fileName)), 'Content-Disposition should include the filename or its encoded version');
})
.expect(200)
.end(function(err, res) {
if (err) return done(err);
assert.strictEqual(res.text, testFileContent);

// Clean up the test file
fs.unlinkSync(testFilePath);
done();
});
});

it('should handle nested file paths', function(done) {
const testFilePath = path.join(__dirname, '../../../examples/downloads/files/notes/groceries.txt');
const testFileContent = 'Milk, Bread, Eggs';

// Ensure the directory exists
fs.mkdirSync(path.dirname(testFilePath), { recursive: true });

// Create a test file
fs.writeFileSync(testFilePath, testFileContent);

request(app)
.get('/files/notes/groceries.txt')
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Content-Disposition', 'attachment; filename="groceries.txt"')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
assert.strictEqual(res.text, testFileContent);

// Clean up the test file
fs.unlinkSync(testFilePath);
fs.rmdirSync(path.dirname(testFilePath));
done();
});
});
});
125 changes: 125 additions & 0 deletions test/examples/error-pages/index.sapient.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
const assert = require('assert');
const request = require('supertest');
const path = require('path');

describe('Error Pages Express App', function() {
let app;

beforeEach(function() {
delete require.cache[require.resolve('../../../examples/error-pages/index.js')];
app = require('../../../examples/error-pages/index.js');
});

it('should render index page', function(done) {
request(app)
.get('/')
.expect('Content-Type', /html/)
.expect(200)
.end(function(err, res) {
if (err) return done(err);
assert(res.text.includes('Error Pages'));
done();
});
});

it('should return 404 for non-existent route', function(done) {
request(app)
.get('/nonexistent')
.expect('Content-Type', /html/)
.expect(404)
.end(function(err, res) {
if (err) return done(err);
assert(res.text.includes('Cannot GET /nonexistent'));
done();
});
});

it('should handle 404 route', function(done) {
request(app)
.get('/404')
.expect('Content-Type', /html/)
.expect(404)
.end(function(err, res) {
if (err) return done(err);
assert(res.text.includes('Cannot GET /404'));
done();
});
});

it('should handle 403 route', function(done) {
request(app)
.get('/403')
.expect('Content-Type', /html/)
.expect(403)
.end(function(err, res) {
if (err) return done(err);
assert(res.text.includes('not allowed!'));
done();
});
});

it('should handle 500 route', function(done) {
request(app)
.get('/500')
.expect('Content-Type', /html/)
.expect(500)
.end(function(err, res) {
if (err) return done(err);
assert(res.text.includes('keyboard cat!'));
done();
});
});

it('should return JSON for 404 with Accept: application/json', function(done) {
request(app)
.get('/nonexistent')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(404)
.end(function(err, res) {
if (err) return done(err);
assert.deepStrictEqual(res.body, { error: 'Not found' });
done();
});
});

it('should return plain text for 404 with Accept: text/plain', function(done) {
request(app)
.get('/nonexistent')
.set('Accept', 'text/plain')
.expect('Content-Type', /text\/plain/)
.expect(404)
.end(function(err, res) {
if (err) return done(err);
assert.strictEqual(res.text, 'Not found');
done();
});
});

it('should disable verbose errors in production', function(done) {
const originalEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
const prodApp = require('../../../examples/error-pages/index.js');
assert.strictEqual(prodApp.get('verbose errors'), false);
process.env.NODE_ENV = originalEnv;
done();
});

it('should enable verbose errors in development', function(done) {
const originalEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';
const devApp = require('../../../examples/error-pages/index.js');
assert.strictEqual(devApp.get('verbose errors'), true);
process.env.NODE_ENV = originalEnv;
done();
});

it('should use logger middleware in non-test environment', function(done) {
const originalEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';
const devApp = require('../../../examples/error-pages/index.js');
assert(devApp._router.stack.some(layer => layer.name === 'logger'));
process.env.NODE_ENV = originalEnv;
done();
});
});
Loading