Skip to content

Commit

Permalink
Add text/plain headers where needed + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keslerm committed Oct 4, 2023
1 parent df72c6a commit 306b854
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/cli/src/routeGeneration/templates/express.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/routeGeneration/templates/hapi.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/express-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
1 change: 1 addition & 0 deletions tests/integration/hapi-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
1 change: 1 addition & 0 deletions tests/integration/koa-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down

0 comments on commit 306b854

Please sign in to comment.