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

@@ -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: {