-
Notifications
You must be signed in to change notification settings - Fork 121
/
ErrorToTemplateConverter.test.ts
180 lines (165 loc) · 9.09 KB
/
ErrorToTemplateConverter.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
import { ErrorToTemplateConverter } from '../../../../src/storage/conversion/ErrorToTemplateConverter';
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
import { errorTermsToMetadata } from '../../../../src/util/errors/HttpErrorUtil';
import { resolveModulePath } from '../../../../src/util/PathUtil';
import { readableToString } from '../../../../src/util/StreamUtil';
import type { TemplateEngine } from '../../../../src/util/templates/TemplateEngine';
describe('An ErrorToTemplateConverter', (): void => {
const identifier = { path: 'http://test.com/error' };
const mainTemplatePath = '/templates/main.html';
const codeTemplatesPath = '/templates/codes';
const extension = '.html';
const contentType = 'text/html';
const errorCode = 'E0001';
const cause = new Error('cause');
let templateEngine: jest.Mocked<TemplateEngine>;
let converter: ErrorToTemplateConverter;
const preferences = {};
beforeEach(async(): Promise<void> => {
templateEngine = {
handleSafe: jest.fn().mockReturnValue(Promise.resolve('<html>')),
} as any;
converter = new ErrorToTemplateConverter(templateEngine,
{ mainTemplatePath, codeTemplatesPath, extension, contentType });
});
it('supports going from errors to the given content type.', async(): Promise<void> => {
await expect(converter.getOutputTypes('internal/error')).resolves.toEqual({ 'text/html': 1 });
});
it('works with non-HTTP errors.', async(): Promise<void> => {
const error = new Error('error text');
const representation = new BasicRepresentation([ error ], 'internal/error', false);
const prom = converter.handle({ identifier, representation, preferences });
await expect(prom).resolves.toBeDefined();
const result = await prom;
expect(result.binary).toBe(true);
expect(result.metadata.contentType).toBe('text/html');
await expect(readableToString(result.data)).resolves.toBe('<html>');
expect(templateEngine.handleSafe).toHaveBeenCalledTimes(1);
expect(templateEngine.handleSafe).toHaveBeenLastCalledWith({
contents: { name: 'Error', message: 'error text', stack: error.stack },
template: { templateFile: mainTemplatePath },
});
});
it('calls the template engine with all HTTP error fields.', async(): Promise<void> => {
const error = new BadRequestHttpError('error text', { cause });
const representation = new BasicRepresentation([ error ], 'internal/error', false);
const prom = converter.handle({ identifier, representation, preferences });
templateEngine.handleSafe.mockRejectedValueOnce(new Error('error-specific template not found'));
await expect(prom).resolves.toBeDefined();
const result = await prom;
expect(result.binary).toBe(true);
expect(result.metadata.contentType).toBe('text/html');
await expect(readableToString(result.data)).resolves.toBe('<html>');
expect(templateEngine.handleSafe).toHaveBeenCalledTimes(2);
expect(templateEngine.handleSafe).toHaveBeenNthCalledWith(1, {
contents: {},
template: { templatePath: '/templates/codes', templateFile: 'H400.html' },
});
expect(templateEngine.handleSafe).toHaveBeenNthCalledWith(2, {
contents: { name: 'BadRequestHttpError', message: 'error text', stack: error.stack, cause: error.cause },
template: { templateFile: mainTemplatePath },
});
});
it('only adds stack if it is defined.', async(): Promise<void> => {
const error = new BadRequestHttpError('error text');
delete error.stack;
const representation = new BasicRepresentation([ error ], 'internal/error', false);
const prom = converter.handle({ identifier, representation, preferences });
templateEngine.handleSafe.mockRejectedValueOnce(new Error('error-specific template not found'));
await expect(prom).resolves.toBeDefined();
const result = await prom;
expect(result.binary).toBe(true);
expect(result.metadata.contentType).toBe('text/html');
await expect(readableToString(result.data)).resolves.toBe('<html>');
expect(templateEngine.handleSafe).toHaveBeenCalledTimes(2);
expect(templateEngine.handleSafe).toHaveBeenNthCalledWith(1, {
contents: {},
template: { templatePath: '/templates/codes', templateFile: 'H400.html' },
});
expect(templateEngine.handleSafe).toHaveBeenNthCalledWith(2, {
contents: { name: 'BadRequestHttpError', message: 'error text' },
template: { templateFile: mainTemplatePath },
});
});
it('adds additional information if an error code description is found.', async(): Promise<void> => {
const metadata = errorTermsToMetadata({ key: 'val' });
const error = new BadRequestHttpError('error text', { errorCode, metadata });
const representation = new BasicRepresentation([ error ], 'internal/error', false);
const prom = converter.handle({ identifier, representation, preferences });
await expect(prom).resolves.toBeDefined();
const result = await prom;
expect(result.binary).toBe(true);
expect(result.metadata.contentType).toBe('text/html');
await expect(readableToString(result.data)).resolves.toBe('<html>');
expect(templateEngine.handleSafe).toHaveBeenCalledTimes(2);
expect(templateEngine.handleSafe).toHaveBeenNthCalledWith(1, {
contents: { key: 'val' },
template: { templatePath: '/templates/codes', templateFile: 'E0001.html' },
});
expect(templateEngine.handleSafe).toHaveBeenNthCalledWith(2, {
contents: { name: 'BadRequestHttpError', message: 'error text', stack: error.stack, description: '<html>' },
template: { templateFile: mainTemplatePath },
});
});
it('sends an empty object for additional error code parameters if none are defined.', async(): Promise<void> => {
const error = new BadRequestHttpError('error text', { errorCode });
const representation = new BasicRepresentation([ error ], 'internal/error', false);
const prom = converter.handle({ identifier, representation, preferences });
await expect(prom).resolves.toBeDefined();
const result = await prom;
expect(result.binary).toBe(true);
expect(result.metadata.contentType).toBe('text/html');
await expect(readableToString(result.data)).resolves.toBe('<html>');
expect(templateEngine.handleSafe).toHaveBeenCalledTimes(2);
expect(templateEngine.handleSafe).toHaveBeenNthCalledWith(1, {
contents: {},
template: { templatePath: '/templates/codes', templateFile: 'E0001.html' },
});
expect(templateEngine.handleSafe).toHaveBeenNthCalledWith(2, {
contents: { name: 'BadRequestHttpError', message: 'error text', stack: error.stack, description: '<html>' },
template: { templateFile: mainTemplatePath },
});
});
it('converts errors with a code as usual if no corresponding template is found.', async(): Promise<void> => {
const error = new BadRequestHttpError('error text', { errorCode: 'invalid' });
const representation = new BasicRepresentation([ error ], 'internal/error', false);
const prom = converter.handle({ identifier, representation, preferences });
templateEngine.handleSafe.mockRejectedValueOnce(new Error('error-specific template not found'));
await expect(prom).resolves.toBeDefined();
const result = await prom;
expect(result.binary).toBe(true);
expect(result.metadata.contentType).toBe('text/html');
await expect(readableToString(result.data)).resolves.toBe('<html>');
expect(templateEngine.handleSafe).toHaveBeenCalledTimes(2);
expect(templateEngine.handleSafe).toHaveBeenNthCalledWith(1, {
contents: {},
template: { templatePath: '/templates/codes', templateFile: 'invalid.html' },
});
expect(templateEngine.handleSafe).toHaveBeenNthCalledWith(2, {
contents: { name: 'BadRequestHttpError', message: 'error text', stack: error.stack },
template: { templateFile: mainTemplatePath },
});
});
it('has default template options.', async(): Promise<void> => {
const metadata = errorTermsToMetadata({ key: 'val' });
converter = new ErrorToTemplateConverter(templateEngine);
const error = new BadRequestHttpError('error text', { errorCode, metadata });
const representation = new BasicRepresentation([ error ], 'internal/error', false);
const prom = converter.handle({ identifier, representation, preferences });
await expect(prom).resolves.toBeDefined();
const result = await prom;
expect(result.binary).toBe(true);
expect(result.metadata.contentType).toBe('text/markdown');
await expect(readableToString(result.data)).resolves.toBe('<html>');
expect(templateEngine.handleSafe).toHaveBeenCalledTimes(2);
expect(templateEngine.handleSafe).toHaveBeenNthCalledWith(1, {
contents: { key: 'val' },
template: { templatePath: resolveModulePath('templates/error/descriptions/'), templateFile: 'E0001.md.hbs' },
});
expect(templateEngine.handleSafe).toHaveBeenNthCalledWith(2, {
contents: { name: 'BadRequestHttpError', message: 'error text', stack: error.stack, description: '<html>' },
template: { templateFile: resolveModulePath('templates/error/main.md.hbs') },
});
});
});