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

fix(alb): alb not working when header value is not string #74

Merged
merged 1 commit into from
Dec 20, 2022
Merged
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
2 changes: 1 addition & 1 deletion src/core/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function getFlattenedHeadersMap(

if (Array.isArray(headerValue))
commaDelimitedHeaders[newKey] = headerValue.join(separator);
else commaDelimitedHeaders[newKey] = headerValue || '';
else commaDelimitedHeaders[newKey] = String(headerValue ?? '');
}

return commaDelimitedHeaders;
Expand Down
3 changes: 3 additions & 0 deletions test/core/headers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ describe('getFlattenedHeadersMap', () => {
'Accept-Language': 'en-US,en;q=0.9',
Host: undefined,
'Content-Type': '',
'Content-Length': 40 as unknown as string,
},
{
'Accept-Encoding': ['gzip'],
'Accept-Language': ['en-US', 'en;q=0.9'],
Host: undefined,
'Content-Type': '',
'Content-Length': [40] as unknown as string[],
},
];

Expand All @@ -31,6 +33,7 @@ describe('getFlattenedHeadersMap', () => {
expect(flattenedHeaders).toHaveProperty('Accept-Encoding');
expect(flattenedHeaders['Accept-Encoding']).toEqual('gzip');
expect(flattenedHeaders['Accept-Language']).toEqual('en-US,en;q=0.9');
expect(flattenedHeaders['Content-Length']).toEqual('40');
}
});

Expand Down
34 changes: 34 additions & 0 deletions test/issues/alb-express-static/alb-express-static.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { resolve } from 'path';
import type { ALBResult } from 'aws-lambda';
import express from 'express';
import { AlbAdapter } from '../../../src/adapters/aws/index';
import { ExpressFramework } from '../../../src/frameworks/express/index';
import { DefaultHandler } from '../../../src/handlers/default/index';
import { PromiseResolver } from '../../../src/resolvers/promise/index';
import { ServerlessAdapter } from '../../../src/index';
import { createAlbEvent } from '../../adapters/aws/utils/alb-event';

describe('ALB rejecting response when uses express.static because', () => {
it('returns some headers that are not string', async () => {
const app = express();

app.use(express.static(resolve(__dirname)));

const handler = ServerlessAdapter.new(app)
.setHandler(new DefaultHandler())
.setFramework(new ExpressFramework())
.setResolver(new PromiseResolver())
.addAdapter(new AlbAdapter())
.build();

const albEvent = createAlbEvent('GET', '/robots.txt');

const response = (await handler(albEvent, {})) as ALBResult;

for (const header of Object.keys(response.headers || {})) {
expect(`typeof ${header}: ${typeof response.headers![header]}`).toBe(
`typeof ${header}: string`,
);
}
});
});
1 change: 1 addition & 0 deletions test/issues/alb-express-static/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test