From 306b854c595774afbe0308ad9dbd4e70ccca166e Mon Sep 17 00:00:00 2001 From: rridley Date: Tue, 3 Oct 2023 20:39:46 -0400 Subject: [PATCH] Add text/plain headers where needed + tests --- packages/cli/src/routeGeneration/templates/express.hbs | 3 ++- packages/cli/src/routeGeneration/templates/hapi.hbs | 5 ++++- tests/integration/express-server.spec.ts | 2 ++ tests/integration/hapi-server.spec.ts | 1 + tests/integration/koa-server.spec.ts | 1 + 5 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/routeGeneration/templates/express.hbs b/packages/cli/src/routeGeneration/templates/express.hbs index 9fb3a0cce..6d69cbdb5 100644 --- a/packages/cli/src/routeGeneration/templates/express.hbs +++ b/packages/cli/src/routeGeneration/templates/express.hbs @@ -208,7 +208,8 @@ export function RegisterRoutes(app: Router) { if (data && typeof data.pipe === 'function' && data.readable && typeof data._read === 'function') { response.status(statusCode || 200) data.pipe(response); - } else if (data && typeof data === "string") { + } else if (data && typeof data === 'string') { + response.type('txt'); response.status(statusCode || 200).send(data); } else if (data !== null && data !== undefined) { response.status(statusCode || 200).json(data); diff --git a/packages/cli/src/routeGeneration/templates/hapi.hbs b/packages/cli/src/routeGeneration/templates/hapi.hbs index 9d7c2e316..f6af4c0d5 100644 --- a/packages/cli/src/routeGeneration/templates/hapi.hbs +++ b/packages/cli/src/routeGeneration/templates/hapi.hbs @@ -298,7 +298,6 @@ export function RegisterRoutes(server: any) { if (h.__isTsoaResponded) { return h.__isTsoaResponded; } - let response = data !== null && data !== undefined ? h.response(data).code(200) : h.response("").code(204); @@ -307,6 +306,10 @@ export function RegisterRoutes(server: any) { response.header(name, headers[name]); }); + if (data && typeof data == 'string') { + response.header('content-type', 'text/plain'); + } + if (statusCode) { response.code(statusCode); } diff --git a/tests/integration/express-server.spec.ts b/tests/integration/express-server.spec.ts index 63a5b8b76..140a28624 100644 --- a/tests/integration/express-server.spec.ts +++ b/tests/integration/express-server.spec.ts @@ -246,6 +246,8 @@ describe('Express Server', () => { it('returns string responses', () => { return verifyGetRequest(`${basePath}/GetTest/StringValue`, (_err, res) => { + expect(res.status).to.equal(200); + expect(res.headers['content-type']).to.equal('text/plain; charset=utf-8'); expect(res.text).to.equal('FOO'); return; }); diff --git a/tests/integration/hapi-server.spec.ts b/tests/integration/hapi-server.spec.ts index 90b9d0863..447fa19c3 100644 --- a/tests/integration/hapi-server.spec.ts +++ b/tests/integration/hapi-server.spec.ts @@ -50,6 +50,7 @@ describe('Hapi Server', () => { it('returns string responses', () => { return verifyGetRequest(`${basePath}/GetTest/StringValue`, (_err, res) => { + expect(res.get('content-type')).to.eq('text/plain; charset=utf-8'); expect(res.text).to.equal('FOO'); return; }); diff --git a/tests/integration/koa-server.spec.ts b/tests/integration/koa-server.spec.ts index 90df45432..c1ac7e832 100644 --- a/tests/integration/koa-server.spec.ts +++ b/tests/integration/koa-server.spec.ts @@ -61,6 +61,7 @@ describe('Koa Server', () => { it('returns string responses', () => { return verifyGetRequest(`${basePath}/GetTest/StringValue`, (_err, res) => { + expect(res.header['content-type']).to.equal('text/plain; charset=utf-8'); expect(res.text).to.equal('FOO'); return; });