forked from nestjs/nest
/
http.exception.spec.ts
157 lines (140 loc) 路 4.87 KB
/
http.exception.spec.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
import { expect } from 'chai';
import {
BadRequestException,
HttpException,
NotFoundException,
} from '../../exceptions';
describe('HttpException', () => {
it('should return a response as a string when input is a string', () => {
const message = 'My error message';
expect(new HttpException(message, 404).getResponse()).to.be.eql(
'My error message',
);
});
it('should return a response as an object when input is an object', () => {
const message = {
msg: 'My error message',
reason: 'this can be a human readable reason',
anything: 'else',
};
expect(new HttpException(message, 404).getResponse()).to.be.eql(message);
});
it('should return a message from a built-in exception as an object', () => {
const message = 'My error message';
expect(new BadRequestException(message).getResponse()).to.be.eql({
statusCode: 400,
error: 'Bad Request',
message: 'My error message',
});
});
it('should return an object even when the message is undefined', () => {
expect(new BadRequestException().getResponse()).to.be.eql({
statusCode: 400,
message: 'Bad Request',
});
});
it('should return a status code', () => {
expect(new BadRequestException().getStatus()).to.be.eql(400);
expect(new NotFoundException().getStatus()).to.be.eql(404);
});
it('should return a response', () => {
expect(new BadRequestException().getResponse()).to.be.eql({
message: 'Bad Request',
statusCode: 400,
});
expect(new NotFoundException().getResponse()).to.be.eql({
message: 'Not Found',
statusCode: 404,
});
});
it('should inherit from error', () => {
const error = new HttpException('', 400);
expect(error instanceof Error).to.be.true;
});
it('should be serializable', () => {
const message = 'Some Error';
const error = new HttpException(message, 400);
expect(`${error}`).to.be.eql(`HttpException: ${message}`);
});
describe('when "response" is an object', () => {
it('should use default message', () => {
const obj = { foo: 'bar' };
const error = new HttpException(obj, 400);
const badRequestError = new BadRequestException(obj);
expect(`${error}`).to.be.eql(`HttpException: Http Exception`);
expect(`${badRequestError}`).to.be.eql(
`BadRequestException: Bad Request Exception`,
);
expect(`${error}`.includes('[object Object]')).to.not.be.true;
expect(`${badRequestError}`.includes('[object Object]')).to.not.be.true;
});
describe('otherwise', () => {
it('should concat strings', () => {
const test = 'test message';
const error = new HttpException(test, 400);
expect(`${error}`).to.be.eql(`HttpException: ${test}`);
expect(`${error}`.includes('[object Object]')).to.not.be.true;
});
});
});
describe('createBody', () => {
describe('when object has been passed', () => {
it('should return expected object', () => {
const object = {
message: 'test',
};
expect(HttpException.createBody(object)).to.be.eql(object);
});
});
describe('when string has been passed', () => {
it('should return expected object', () => {
const error = 'test';
const status = 500;
const message = 'error';
expect(HttpException.createBody(message, error, status)).to.be.eql({
error,
message,
statusCode: status,
});
});
});
describe('when nil has been passed', () => {
it('should return expected object', () => {
const status = 500;
const error = 'error';
expect(HttpException.createBody(null, error, status)).to.be.eql({
message: error,
statusCode: status,
});
});
});
it('should not override pre-defined body if message is array', () => {
expect(
HttpException.createBody(['a', 'random', 'array'], 'error', 200),
).to.eql({
message: ['a', 'random', 'array'],
error: 'error',
statusCode: 200,
});
});
});
describe('initCause', () => {
it('configures a cause when message is an instance of error', () => {
const message = new Error('Some Error');
const error = new HttpException(message, 400);
expect(`${error}`).to.be.eql(`HttpException: ${message.message}`);
const { cause } = error;
expect(cause).to.be.eql(message);
});
it('configures a cause when message is a string and the options object is passed', () => {
const causeError = new Error('Some Error');
const customDescription = 'custom description';
const error = new HttpException(customDescription, 400, {
cause: causeError,
});
expect(`${error}`).to.be.eql(`HttpException: ${customDescription}`);
const { cause } = error;
expect(cause).to.be.eql(causeError);
});
});
});