insomnia/packages/insomnia-app/app/__mocks__/@grpc/grpc-js.ts
Filipe Freire f513d59f69
Add gRPC Headers (INS-362) (#3667) (#4244)
* Add gRPC Headers (INS-362) (#3667)

* Add grpc headers support to client, server and bidi streams

* Fix failing grpc unit checks

* Improvements after CR for #4244

* Fix lint issues

* Fix grpc header overflow and failing tests

* Fix illegal characters in metadata key validation

* Fix common-headers naming and add unit checks

* Fix wrong import format
2021-12-06 10:12:18 +00:00

102 lines
2.0 KiB
TypeScript

import { EventEmitter } from 'events';
const grpcJs = jest.requireActual('@grpc/grpc-js');
const mockCallWrite = jest.fn();
const mockCallEnd = jest.fn();
const mockCallCancel = jest.fn();
export const status = grpcJs.status;
class MockCall extends EventEmitter {
write(...args) {
mockCallWrite(...args);
}
end(...args) {
mockCallEnd(...args);
}
cancel(...args) {
mockCallCancel(...args);
}
}
let mockCall = new MockCall();
const makeMockCall = () => {
mockCall = new MockCall();
};
const getMockCall = () => mockCall;
const mockConstructor = jest.fn();
const mockMakeUnaryRequest = jest.fn();
const mockMakeClientStreamRequest = jest.fn();
const mockMakeServerStreamRequest = jest.fn();
const mockMakeBidiStreamRequest = jest.fn();
const mockCreateInsecure = jest.fn();
const mockCreateSsl = jest.fn();
export const grpcMocks = {
getMockCall,
mockConstructor,
mockMakeUnaryRequest,
mockMakeClientStreamRequest,
mockMakeServerStreamRequest,
mockMakeBidiStreamRequest,
mockCreateInsecure,
mockCreateSsl,
mockCallWrite,
mockCallEnd,
mockCallCancel,
};
class MockGrpcClient {
constructor(...args) {
mockConstructor(...args);
}
makeUnaryRequest(...args) {
mockMakeUnaryRequest(...args);
makeMockCall();
return getMockCall();
}
makeClientStreamRequest(...args) {
mockMakeClientStreamRequest(...args);
makeMockCall();
return getMockCall();
}
makeServerStreamRequest(...args) {
mockMakeServerStreamRequest(...args);
makeMockCall();
return getMockCall();
}
makeBidiStreamRequest(...args) {
mockMakeBidiStreamRequest(...args);
makeMockCall();
return getMockCall();
}
}
export function makeGenericClientConstructor() {
return MockGrpcClient;
}
export class Metadata {
/**
* Mock Metadata class to avoid TypeError: grpc.Metadata is not a constructor
*/
constructor() {
// Do nothing
}
}
export const credentials = {
createInsecure: mockCreateInsecure,
createSsl: mockCreateSsl,
};