Skip to content

Commit

Permalink
Merge pull request #74 from H4ad/fix/alb-stringify-headers
Browse files Browse the repository at this point in the history
fix(alb): alb not working when header value is not string
  • Loading branch information
H4ad authored Dec 20, 2022
2 parents 7ab7087 + 10d72ac commit 29b8320
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
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

0 comments on commit 29b8320

Please sign in to comment.