mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Fully support Accept* headers
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { AcceptPreferenceParser } from '../../../../src/ldp/http/AcceptPreferenceParser';
|
||||
import { HttpRequest } from '../../../../src/server/HttpRequest';
|
||||
import { SimplePreferenceParser } from '../../../../src/ldp/http/SimplePreferenceParser';
|
||||
|
||||
describe('A SimplePreferenceParser', (): void => {
|
||||
const preferenceParser = new SimplePreferenceParser();
|
||||
describe('An AcceptPreferenceParser', (): void => {
|
||||
const preferenceParser = new AcceptPreferenceParser();
|
||||
|
||||
it('can handle all input.', async(): Promise<void> => {
|
||||
await expect(preferenceParser.canHandle()).resolves.toBeUndefined();
|
||||
@@ -14,7 +14,7 @@ describe('A SimplePreferenceParser', (): void => {
|
||||
|
||||
it('parses accept headers.', async(): Promise<void> => {
|
||||
await expect(preferenceParser.handle({ headers: { accept: 'audio/*; q=0.2, audio/basic' }} as HttpRequest))
|
||||
.resolves.toEqual({ type: [{ value: 'audio/*', weight: 0.2 }, { value: 'audio/basic', weight: 1 }]});
|
||||
.resolves.toEqual({ type: [{ value: 'audio/basic', weight: 1 }, { value: 'audio/*', weight: 0.2 }]});
|
||||
});
|
||||
|
||||
it('parses accept-charset headers.', async(): Promise<void> => {
|
||||
@@ -27,6 +27,11 @@ describe('A SimplePreferenceParser', (): void => {
|
||||
.resolves.toEqual({ datetime: [{ value: 'Tue, 20 Mar 2001 20:35:00 GMT', weight: 1 }]});
|
||||
});
|
||||
|
||||
it('parses accept-encoding headers.', async(): Promise<void> => {
|
||||
await expect(preferenceParser.handle({ headers: { 'accept-encoding': 'gzip;q=1.0, identity; q=0.5, *;q=0' }} as unknown as HttpRequest))
|
||||
.resolves.toEqual({ encoding: [{ value: 'gzip', weight: 1 }, { value: 'identity', weight: 0.5 }, { value: '*', weight: 0 }]});
|
||||
});
|
||||
|
||||
it('parses accept-language headers.', async(): Promise<void> => {
|
||||
await expect(preferenceParser.handle({ headers: { 'accept-language': 'da, en-gb;q=0.8, en;q=0.7' }} as HttpRequest))
|
||||
.resolves.toEqual({ language: [{ value: 'da', weight: 1 }, { value: 'en-gb', weight: 0.8 }, { value: 'en', weight: 0.7 }]});
|
||||
70
test/unit/util/AcceptParser.test.ts
Normal file
70
test/unit/util/AcceptParser.test.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { parseAccept, parseAcceptCharset, parseAcceptLanguage } from '../../../src/util/AcceptParser';
|
||||
|
||||
describe('AcceptParser', (): void => {
|
||||
describe('parseAccept function', (): void => {
|
||||
it('parses empty Accept headers.', async(): Promise<void> => {
|
||||
expect(parseAccept('')).toEqual([]);
|
||||
});
|
||||
|
||||
it('parses Accept headers with a single entry.', async(): Promise<void> => {
|
||||
expect(parseAccept('audio/basic')).toEqual([
|
||||
{ range: 'audio/basic', weight: 1, parameters: { mediaType: {}, extension: {}}},
|
||||
]);
|
||||
});
|
||||
|
||||
it('parses Accept headers with multiple entries.', async(): Promise<void> => {
|
||||
expect(parseAccept('audio/*; q=0.2, audio/basic')).toEqual([
|
||||
{ range: 'audio/basic', weight: 1, parameters: { mediaType: {}, extension: {}}},
|
||||
{ range: 'audio/*', weight: 0.2, parameters: { mediaType: {}, extension: {}}},
|
||||
]);
|
||||
});
|
||||
|
||||
it('parses complex Accept headers.', async(): Promise<void> => {
|
||||
expect(parseAccept('text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4,text/x-dvi; q=.8; mxb=100000; mxt')).toEqual([
|
||||
{ range: 'text/html', weight: 1, parameters: { mediaType: { level: '1' }, extension: {}}},
|
||||
{ range: 'text/x-dvi', weight: 0.8, parameters: { mediaType: {}, extension: { mxb: '100000', mxt: '' }}},
|
||||
{ range: 'text/html', weight: 0.7, parameters: { mediaType: {}, extension: {}}},
|
||||
{ range: 'text/html', weight: 0.4, parameters: { mediaType: { level: '2' }, extension: {}}},
|
||||
]);
|
||||
});
|
||||
|
||||
it('parses Accept headers with double quoted values.', async(): Promise<void> => {
|
||||
expect(parseAccept('audio/basic; param1="val" ; q=0.5 ;param2="\\\\\\"valid"')).toEqual([
|
||||
{ range: 'audio/basic', weight: 0.5, parameters: { mediaType: { param1: '"val"' }, extension: { param2: '"\\\\\\"valid"' }}},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseCharset function', (): void => {
|
||||
it('parses Accept-Charset headers.', async(): Promise<void> => {
|
||||
expect(parseAcceptCharset('iso-8859-5, unicode-1-1;q=0.8')).toEqual([
|
||||
{ range: 'iso-8859-5', weight: 1 },
|
||||
{ range: 'unicode-1-1', weight: 0.8 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseEncoding function', (): void => {
|
||||
it('parses empty Accept-Encoding headers.', async(): Promise<void> => {
|
||||
expect(parseAcceptCharset('')).toEqual([]);
|
||||
});
|
||||
|
||||
it('parses Accept-Encoding headers.', async(): Promise<void> => {
|
||||
expect(parseAcceptCharset('gzip;q=1.0, identity; q=0.5, *;q=0')).toEqual([
|
||||
{ range: 'gzip', weight: 1 },
|
||||
{ range: 'identity', weight: 0.5 },
|
||||
{ range: '*', weight: 0 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseLanguage function', (): void => {
|
||||
it('parses Accept-Language headers.', async(): Promise<void> => {
|
||||
expect(parseAcceptLanguage('da, en-gb;q=0.8, en;q=0.7')).toEqual([
|
||||
{ range: 'da', weight: 1 },
|
||||
{ range: 'en-gb', weight: 0.8 },
|
||||
{ range: 'en', weight: 0.7 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user