Skip to content

Commit 8a3173c

Browse files
committed
Refactor health check tests to use async and fix file content test by setting response content type explicitly
1 parent baf3542 commit 8a3173c

File tree

2 files changed

+12
-27
lines changed

2 files changed

+12
-27
lines changed

lib/handlers/health-check.js

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class HealthCheckHandler {
4343
try {
4444
const content = await fs.readFile(this.filePath);
4545
if (content.length === 0) throw Error("File is empty");
46+
res.set('Content-Type', 'text/html');
4647
res.send(content);
4748
} catch (e) {
4849
logger.error(`*** HEALTH CHECK FAILURE: while reading file '${this.filePath}' got ${e}`);

test/handlers/health-check-tests.js

+11-27
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,10 @@ describe('Health checks', () => {
3434
const app = express();
3535
app.use('/hc', new healthCheck.HealthCheckHandler().handle);
3636

37-
it('should respond with OK', () => {
38-
return chai.request(app)
39-
.get('/hc')
40-
.then(res => {
41-
res.should.have.status(200);
42-
res.text.should.be.eql('Everything is awesome');
43-
})
44-
.catch(function (err) {
45-
throw err;
46-
});
37+
it('should respond with OK', async () => {
38+
const res = await chai.request(app).get('/hc');
39+
res.should.have.status(200);
40+
res.text.should.be.eql('Everything is awesome');
4741
});
4842
});
4943

@@ -64,25 +58,15 @@ describe('Health checks on disk', () => {
6458
mockfs.restore();
6559
});
6660

67-
it('should respond with 500 when file not found', () => {
68-
return chai.request(app)
69-
.get('/hc')
70-
.then(res => res.should.have.status(500))
71-
.catch(function (err) {
72-
throw err;
73-
});
61+
it('should respond with 500 when file not found', async () => {
62+
const res = await chai.request(app).get('/hc');
63+
res.should.have.status(500);
7464
});
7565

7666

77-
it('should respond with OK and file contents when found', () => {
78-
return chai.request(app)
79-
.get('/hc2')
80-
.then(res => {
81-
res.should.have.status(200);
82-
// Stupid chai http doesn't work with `send()` and promises it seems (!!)
83-
})
84-
.catch(function (err) {
85-
throw err;
86-
});
67+
it('should respond with OK and file contents when found', async () => {
68+
const res = await chai.request(app).get('/hc2');
69+
res.should.have.status(200);
70+
res.text.should.be.eql('Everything is fine');
8771
});
8872
});

0 commit comments

Comments
 (0)