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

Add header extraction test based on spec #21

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 4 additions & 5 deletions src/httpbis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ export function extractHeader({ headers }: RequestLike | ResponseLike, header: s
if (!allowMissing && !key) {
throw new Error(`Unable to extract header "${header}" from message`);
}
let val = key ? headers[key] ?? '' : '';
if (Array.isArray(val)) {
val = val.join(', ');
}
return val.toString().replace(/\s+/g, ' ');
const val = key ? headers[key] ?? '' : '';
return (!Array.isArray(val) ? [val] : val).map((v) => {
return v.toString().trim().replace(/\n+\s*/g, ' ') || ' ';
}).join(', ');
}

function populateDefaultParameters(parameters: Parameters) {
Expand Down
44 changes: 27 additions & 17 deletions test/httpbis/httpbis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,41 @@ import { expect } from 'chai';
describe('httpbis', () => {
describe('.extractHeader', () => {
const headers = {
'testheader': 'test',
'test-header-1': 'test1',
'Test-Header-2': 'test2',
'test-Header-3': 'test3',
'TEST-HEADER-4': 'test4',
'Host': 'www.example.com',
'Date': 'Tue, 20 Apr 2021 02:07:56 GMT',
'X-OWS-Header': ' Leading and trailing whitespace. ',
'X-Obs-Fold-Header': 'Obsolete\nline folding.',
'Cache-Control': ['max-age=60', ' must-revalidate'],
'Example-Dict': ' a=1, b=2;x=1;y=2, c=(a b c)',
};
Object.entries(headers).forEach(([headerName, expectedValue]) => {
it(`successfully extracts a matching header (${headerName})`, () => {
expect(extractHeader({ headers } as unknown as RequestLike, headerName)).to.equal(expectedValue);
});
it(`successfully extracts a lower cased header (${headerName})`, () => {
expect(extractHeader({ headers } as unknown as RequestLike, headerName.toLowerCase())).to.equal(expectedValue);
});
it(`successfully extracts an upper cased header (${headerName})`, () => {
expect(extractHeader({ headers } as unknown as RequestLike, headerName.toUpperCase())).to.equal(expectedValue);
});
it('correctly extracts headers', () => {
const expected = {
'host': 'www.example.com',
'date': 'Tue, 20 Apr 2021 02:07:56 GMT',
'x-ows-header': 'Leading and trailing whitespace.',
'x-obs-fold-header': 'Obsolete line folding.',
'cache-control': 'max-age=60, must-revalidate',
'example-dict': 'a=1, b=2;x=1;y=2, c=(a b c)',
};
const parsed = Object.keys(headers).reduce((accum, name) => {
return Object.assign(accum, {
[name.toLowerCase()]: extractHeader({ headers } as unknown as RequestLike, name),
});
}, {});
expect(parsed).to.deep.equal(expected);
});
it('extracts an empty header', () => {
const parsed = extractHeader({ headers: { 'x-empty-header': '' } } as unknown as RequestLike, 'x-empty-header');
expect(parsed).to.equal(' ');
});
it('allows missing headers to return by default', () => {
expect(extractHeader({ headers } as unknown as RequestLike, 'missing')).to.equal('');
expect(extractHeader({ headers } as unknown as RequestLike, 'missing')).to.equal(' ');
});
it('throws on missing headers', () => {
expect(() => extractHeader({ headers } as unknown as RequestLike, 'missing', { allowMissing: false })).to.throw(Error, 'Unable to extract header "missing" from message');
});
it('does not throw on missing headers', () => {
expect(extractHeader({ headers } as unknown as RequestLike, 'missing', { allowMissing: true })).to.equal('');
expect(extractHeader({ headers } as unknown as RequestLike, 'missing', { allowMissing: true })).to.equal(' ');
});
});
describe('.extractComponent', () => {
Expand Down