fix: return contenttype header value string with parameters

This commit is contained in:
Thomas Dupont
2022-07-05 13:06:18 +02:00
committed by Joachim Van Herwegen
parent e0954cf2a7
commit 311f8756ec
12 changed files with 122 additions and 30 deletions

View File

@@ -10,6 +10,7 @@ const baseUrl = `http://localhost:${port}`;
const documents = [
[ '/turtle', 'text/turtle', '# Test' ],
[ '/markdown', 'text/markdown', '# Test' ],
[ '/plain', 'text/plain; charset=utf-8', '# Test' ],
];
const cases: [string, string, string][] = [
@@ -27,6 +28,8 @@ const cases: [string, string, string][] = [
[ '/markdown', 'text/html', 'text/html,*/*;q=0.8' ],
[ '/markdown', 'text/html', 'text/markdown;q=0.1, text/html;q=0.9' ],
[ '/markdown', 'application/octet-stream', 'application/octet-stream' ],
[ '/plain', 'text/plain; charset=utf-8', 'text/plain' ],
[ '/plain', 'text/plain; charset=utf-8', 'text/plain;q=0.1, text/markdown;q=0.9' ],
];
describe('Content negotiation', (): void => {

View File

@@ -0,0 +1,43 @@
import { createResponse } from 'node-mocks-http';
import { ContentTypeMetadataWriter } from '../../../../../src/http/output/metadata/ContentTypeMetadataWriter';
import { RepresentationMetadata } from '../../../../../src/http/representation/RepresentationMetadata';
import type { HttpResponse } from '../../../../../src/server/HttpResponse';
describe('A ContentTypeMetadataWriter', (): void => {
const writer = new ContentTypeMetadataWriter();
let response: HttpResponse;
beforeEach(async(): Promise<void> => {
response = createResponse() as HttpResponse;
});
it('adds no header if there is no relevant metadata.', async(): Promise<void> => {
const metadata = new RepresentationMetadata();
await expect(writer.handle({ response, metadata })).resolves.toBeUndefined();
expect(response.getHeaders()).toEqual({ });
});
it('adds a Content-Type header with parameters if present.', async(): Promise<void> => {
const metadata = new RepresentationMetadata('text/plain; charset=utf-8');
await expect(writer.handle({ response, metadata })).resolves.toBeUndefined();
expect(response.getHeaders()).toEqual({
'content-type': 'text/plain; charset=utf-8',
});
const metadata2 = new RepresentationMetadata('text/plain; charset="utf-8"');
await expect(writer.handle({ response, metadata: metadata2 })).resolves.toBeUndefined();
expect(response.getHeaders()).toEqual({
'content-type': 'text/plain; charset=utf-8',
});
});
it('adds a Content-Type header without parameters.', async(): Promise<void> => {
const metadata = new RepresentationMetadata('text/plain');
await expect(writer.handle({ response, metadata })).resolves.toBeUndefined();
expect(response.getHeaders()).toEqual({
'content-type': 'text/plain',
});
});
});

View File

@@ -2,6 +2,7 @@ import 'jest-rdf';
import type { BlankNode } from 'n3';
import { DataFactory } from 'n3';
import type { NamedNode, Quad } from 'rdf-js';
import { ContentType } from '../../../../src';
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
import { CONTENT_TYPE_TERM, SOLID_META, RDFS } from '../../../../src/util/Vocabularies';
const { defaultGraph, literal, namedNode, quad } = DataFactory;
@@ -320,13 +321,13 @@ describe('A RepresentationMetadata', (): void => {
it('has a shorthand for Content-Type as object.', async(): Promise<void> => {
expect(metadata.contentType).toBeUndefined();
expect(metadata.contentTypeObject).toBeUndefined();
metadata.contentTypeObject = {
value: 'text/plain',
parameters: {
metadata.contentTypeObject = new ContentType(
'text/plain',
{
charset: 'utf-8',
test: 'value1',
},
};
);
expect(metadata.contentTypeObject).toEqual({
value: 'text/plain',
parameters: {

View File

@@ -1,6 +1,6 @@
import type { HttpResponse } from '../../../src/server/HttpResponse';
import { BadRequestHttpError } from '../../../src/util/errors/BadRequestHttpError';
import {
import { ContentType,
addHeader,
hasScheme,
matchesAuthorizationScheme,
@@ -11,8 +11,7 @@ import {
parseAcceptLanguage,
parseContentType,
parseForwarded,
parseLinkHeader,
} from '../../../src/util/HeaderUtil';
parseLinkHeader } from '../../../src/util/HeaderUtil';
describe('HeaderUtil', (): void => {
describe('#parseAccept', (): void => {
@@ -469,4 +468,21 @@ describe('HeaderUtil', (): void => {
expect(hasScheme('wss://example.com', 'http', 'WSS')).toBeTruthy();
});
});
describe('A ContentType instance', (): void => {
it('can serialize to a correct header value string with parameters.', (): void => {
const contentType: ContentType = new ContentType(
'text/plain',
{
charset: 'utf-8',
extra: 'test',
},
);
expect(contentType.toHeaderValueString()).toBe('text/plain; charset=utf-8; extra=test');
});
it('can serialize to a correct header value string without parameters.', (): void => {
const contentType: ContentType = new ContentType('text/plain');
expect(contentType.toHeaderValueString()).toBe('text/plain');
});
});
});