|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { BadRequestException, HttpException, NotFoundException } from '../../exceptions'; |
| 3 | + |
| 4 | +describe('HttpException', () => { |
| 5 | + it('should return a message as a string when input is a string', () => { |
| 6 | + const message: string = 'My error message'; |
| 7 | + expect(new HttpException(message, 404).message).to.be.eql('My error message'); |
| 8 | + }); |
| 9 | + |
| 10 | + it('should return a message as an object when input is an object', () => { |
| 11 | + const message: object = { |
| 12 | + msg: 'My error message', |
| 13 | + reason: 'this can be a human readable reason', |
| 14 | + anything: 'else', |
| 15 | + }; |
| 16 | + expect(new HttpException(message, 404).message).to.be.eql(message); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should return a message from a built-in exception as an object', () => { |
| 20 | + const message: string = 'My error message'; |
| 21 | + expect(new BadRequestException(message).message).to.be.eql({ |
| 22 | + statusCode: 400, |
| 23 | + error: 'Bad Request', |
| 24 | + message: 'My error message', |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should return an object even when the message is undefined', () => { |
| 29 | + expect(new BadRequestException().message).to.be.eql({statusCode: 400, error: 'Bad Request'}); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should return a status code', () => { |
| 33 | + expect(new BadRequestException().getStatus()).to.be.eql(400); |
| 34 | + expect(new NotFoundException().getStatus()).to.be.eql(404); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should return a response', () => { |
| 38 | + expect(new BadRequestException().getResponse()).to.be.eql({ |
| 39 | + error: 'Bad Request', |
| 40 | + statusCode: 400, |
| 41 | + }); |
| 42 | + expect(new NotFoundException().getResponse()).to.be.eql({ |
| 43 | + error: 'Not Found', |
| 44 | + statusCode: 404, |
| 45 | + }); |
| 46 | + }); |
| 47 | +}); |
0 commit comments