Skip to content

Commit ca84be4

Browse files
committed
Create grpc-to-http-interceptor.spec.ts test file
1 parent 0d383c2 commit ca84be4

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

test/interceptors/grpc-to-http-interceptor.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpException } from "@nestjs/common";
1+
import { HttpException, HttpStatus } from "@nestjs/common";
22
import { Observable, throwError } from "rxjs";
33
import { GrpcNotFoundException, GrpcToHttpInterceptor } from "../../lib";
44

@@ -48,7 +48,7 @@ describe("GrpcToHttpInterceptor", () => {
4848
intercept$.subscribe({
4949
error: (err) => {
5050
expect(err).toBeInstanceOf(HttpException);
51-
expect(err.status).toEqual(404);
51+
expect(err.status).toEqual(HttpStatus.NOT_FOUND);
5252
done();
5353
},
5454
complete: () => done(),
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { NotFoundException } from "@nestjs/common";
2+
import { Observable, throwError } from "rxjs";
3+
import { HttpToGrpcInterceptor } from "../../lib";
4+
import { RpcException } from "@nestjs/microservices";
5+
import { status as GrpcStatusCode } from "@grpc/grpc-js";
6+
7+
const throwMockException = (
8+
Exception: new (...args: any[]) => any,
9+
): Observable<any> => {
10+
const exception = new Exception(Exception.name);
11+
return throwError(
12+
() =>
13+
new RpcException({
14+
message: exception.message,
15+
code: GrpcStatusCode.NOT_FOUND,
16+
}),
17+
);
18+
};
19+
20+
describe("HttpToGrpcInterceptor", () => {
21+
let interceptor: HttpToGrpcInterceptor;
22+
23+
beforeAll(() => {
24+
interceptor = new HttpToGrpcInterceptor();
25+
});
26+
27+
it("Should be defined", () => {
28+
expect(interceptor).toBeDefined();
29+
});
30+
31+
it("Should convert HTTP exceptions to gRPC exceptions", (done) => {
32+
const intercept$ = interceptor.intercept({} as any, {
33+
handle: () => throwMockException(NotFoundException),
34+
}) as Observable<any>;
35+
36+
intercept$.subscribe({
37+
error: (err) => {
38+
expect(err).toBeInstanceOf(RpcException);
39+
done();
40+
},
41+
complete: () => done(),
42+
});
43+
});
44+
45+
it("Should convert HTTP exceptions to gRPC exceptions", (done) => {
46+
const intercept$ = interceptor.intercept({} as any, {
47+
handle: () => throwMockException(NotFoundException),
48+
}) as Observable<any>;
49+
50+
intercept$.subscribe({
51+
error: (err) => {
52+
expect(err).toBeInstanceOf(RpcException);
53+
expect(err.error.code).toEqual(GrpcStatusCode.NOT_FOUND);
54+
done();
55+
},
56+
complete: () => done(),
57+
});
58+
});
59+
60+
it("Should contain the HTTP exception error message", (done) => {
61+
const intercept$ = interceptor.intercept({} as any, {
62+
handle: () => throwMockException(NotFoundException),
63+
}) as Observable<any>;
64+
65+
intercept$.subscribe({
66+
error: (err) => {
67+
expect(err).toBeInstanceOf(RpcException);
68+
expect(err.message).toEqual(NotFoundException.name);
69+
done();
70+
},
71+
complete: () => done(),
72+
});
73+
});
74+
});

0 commit comments

Comments
 (0)