Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 18 additions & 56 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions packages/insomnia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@
"@apideck/better-ajv-errors": "^0.3.6",
"@apidevtools/swagger-parser": "10.1.1",
"ajv": "^8.17.1",
"@bufbuild/protobuf": "^1.10.0",
"@connectrpc/connect": "^1.6.1",
"@connectrpc/connect-node": "^1.6.1",
"@bufbuild/protobuf": "2.11.0",
"@connectrpc/connect": "2.1.1",
"@customerio/cdp-analytics-browser": "^0.3.9",
"@faker-js/faker": "9.7.0",
"@fortawesome/fontawesome-svg-core": "^6.7.2",
Expand All @@ -64,7 +63,7 @@
"@seald-io/nedb": "^4.1.1",
"@segment/analytics-node": "2.2.1",
"@sentry/electron": "^6.5.0",
"@stoplight/spectral-core": "^1.20.0",
"@stoplight/spectral-core": "1.21.0",
"@stoplight/spectral-formats": "^1.8.2",
"@stoplight/spectral-ruleset-bundler": "1.6.3",
"@stoplight/spectral-rulesets": "^1.22.0",
Expand Down
152 changes: 73 additions & 79 deletions packages/insomnia/src/main/ipc/__tests__/grpc.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import type { AnyMessage, MethodInfo, PartialMessage, ServiceType } from '@bufbuild/protobuf';
import type { UnaryResponse } from '@connectrpc/connect';
import { createConnectTransport } from '@connectrpc/connect-node';
import * as grpcReflection from 'grpc-reflection-js';
import protobuf from 'protobufjs';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { beforeEach, describe, expect, it, type Mock, vi } from 'vitest';

import { loadMethodsFromReflection } from '../grpc';

vi.mock('grpc-reflection-js');
vi.mock('@connectrpc/connect-node');

describe('loadMethodsFromReflection', () => {
describe('one service reflection', () => {
beforeEach(() => {
// we want to test that the values that are passed to axios are returned in the config key
(grpcReflection.Client as unknown as vi.Mock).mockImplementation(() => ({
listServices: () => Promise.resolve(['FooService']),
fileContainingSymbol: async () => {
const parsed = protobuf.parse(`
(grpcReflection.Client as unknown as Mock).mockImplementation(function() {
return {
listServices: () => Promise.resolve(['FooService']),
fileContainingSymbol: async () => {
const parsed = protobuf.parse(`
syntax = "proto3";

message FooRequest {
Expand All @@ -31,16 +28,18 @@ describe('loadMethodsFromReflection', () => {
service FooService {
rpc Foo (FooRequest) returns (FooResponse);
}`);
return parsed.root;
},
}));
return parsed.root;
},
};
});
});

it('parses methods', async () => {
const methods = await loadMethodsFromReflection({
url: 'foo.com',
metadata: [],
reflectionApi: { enabled: false, apiKey: '', url: '', module: '' },
rejectUnauthorized: true,
});
expect(methods).toStrictEqual([
{
Expand All @@ -57,10 +56,11 @@ describe('loadMethodsFromReflection', () => {
describe('format service reflection', () => {
beforeEach(() => {
// we want to test that the values that are passed to axios are returned in the config key
(grpcReflection.Client as unknown as vi.Mock).mockImplementation(() => ({
listServices: () => Promise.resolve(['FooService']),
fileContainingSymbol: async () => {
const parsed = protobuf.parse(`
(grpcReflection.Client as unknown as Mock).mockImplementation(function() {
return {
listServices: () => Promise.resolve(['FooService']),
fileContainingSymbol: async () => {
const parsed = protobuf.parse(`
syntax = "proto3";

message FooRequest {
Expand All @@ -74,16 +74,18 @@ describe('loadMethodsFromReflection', () => {
service FooService {
rpc format (FooRequest) returns (FooResponse);
}`);
return parsed.root;
},
}));
return parsed.root;
},
};
});
});

it('parses methods', async () => {
const methods = await loadMethodsFromReflection({
url: 'foo.com',
metadata: [],
reflectionApi: { enabled: false, apiKey: '', url: '', module: '' },
rejectUnauthorized: true,
});
expect(methods).toStrictEqual([
{
Expand All @@ -100,10 +102,11 @@ describe('loadMethodsFromReflection', () => {
describe('multiple service reflection', () => {
beforeEach(() => {
// we want to test that the values that are passed to axios are returned in the config key
(grpcReflection.Client as unknown as vi.Mock).mockImplementation(() => ({
listServices: () => Promise.resolve(['FooService', 'BarService']),
fileContainingSymbol: async () => {
const parsed = protobuf.parse(`
(grpcReflection.Client as unknown as Mock).mockImplementation(function() {
return {
listServices: () => Promise.resolve(['FooService', 'BarService']),
fileContainingSymbol: async () => {
const parsed = protobuf.parse(`
syntax = "proto3";

message FooRequest {
Expand All @@ -129,16 +132,18 @@ describe('loadMethodsFromReflection', () => {
service BarService {
rpc Bar (BarRequest) returns (BarResponse);
}`);
return parsed.root;
},
}));
return parsed.root;
},
};
});
});

it('parses methods', async () => {
const methods = await loadMethodsFromReflection({
url: 'foo-bar.com',
metadata: [],
reflectionApi: { enabled: false, apiKey: '', url: '', module: '' },
rejectUnauthorized: true,
});
expect(methods).toStrictEqual([
{
Expand All @@ -161,60 +166,49 @@ describe('loadMethodsFromReflection', () => {

describe('buf reflection api', () => {
it('loads module', async () => {
(createConnectTransport as unknown as vi.Mock).mockImplementation(options => {
expect(options.baseUrl).toStrictEqual('https://buf.build');
return {
async unary(
service: ServiceType,
method: MethodInfo,
_: AbortSignal | undefined,
__: number | undefined,
header: HeadersInit | undefined,
input: PartialMessage<AnyMessage>,
): Promise<UnaryResponse> {
expect(new Headers(header).get('Authorization')).toStrictEqual('Bearer TEST_KEY');
expect(input).toStrictEqual({ module: 'buf.build/connectrpc/eliza' });
return {
service: service,
method: method,
header: new Headers(),
trailer: new Headers(),
stream: false,
// Output of running `buf curl https://buf.build/buf.reflect.v1beta1.FileDescriptorSetService/GetFileDescriptorSet --data '{"module": "buf.build/connectrpc/eliza"}' --schema buf.build/bufbuild/reflect -H 'Authorization: Bearer buf-token'`
message: method.O.fromJsonString(
'{"fileDescriptorSet":{"file":[{"name":"connectrpc/eliza/v1/eliza.proto","package":"connectrpc.eliza.v1","messageType":[{"name":"SayRequest","field":[{"name":"sentence","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"sentence"}]},{"name":"SayResponse","field":[{"name":"sentence","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"sentence"}]},{"name":"ConverseRequest","field":[{"name":"sentence","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"sentence"}]},{"name":"ConverseResponse","field":[{"name":"sentence","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"sentence"}]},{"name":"IntroduceRequest","field":[{"name":"name","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"name"}]},{"name":"IntroduceResponse","field":[{"name":"sentence","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"sentence"}]}],"service":[{"name":"ElizaService","method":[{"name":"Say","inputType":".connectrpc.eliza.v1.SayRequest","outputType":".connectrpc.eliza.v1.SayResponse","options":{"idempotencyLevel":"NO_SIDE_EFFECTS"}},{"name":"Converse","inputType":".connectrpc.eliza.v1.ConverseRequest","outputType":".connectrpc.eliza.v1.ConverseResponse","options":{},"clientStreaming":true,"serverStreaming":true},{"name":"Introduce","inputType":".connectrpc.eliza.v1.IntroduceRequest","outputType":".connectrpc.eliza.v1.IntroduceResponse","options":{},"serverStreaming":true}]}],"syntax":"proto3"}]},"version":"233fca715f49425581ec0a1b660be886"}',
),
};
},
};
const bufReflectJson = '{"fileDescriptorSet":{"file":[{"name":"connectrpc/eliza/v1/eliza.proto","package":"connectrpc.eliza.v1","messageType":[{"name":"SayRequest","field":[{"name":"sentence","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"sentence"}]},{"name":"SayResponse","field":[{"name":"sentence","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"sentence"}]},{"name":"ConverseRequest","field":[{"name":"sentence","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"sentence"}]},{"name":"ConverseResponse","field":[{"name":"sentence","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"sentence"}]},{"name":"IntroduceRequest","field":[{"name":"name","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"name"}]},{"name":"IntroduceResponse","field":[{"name":"sentence","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"sentence"}]}],"service":[{"name":"ElizaService","method":[{"name":"Say","inputType":".connectrpc.eliza.v1.SayRequest","outputType":".connectrpc.eliza.v1.SayResponse","options":{"idempotencyLevel":"NO_SIDE_EFFECTS"}},{"name":"Converse","inputType":".connectrpc.eliza.v1.ConverseRequest","outputType":".connectrpc.eliza.v1.ConverseResponse","options":{},"clientStreaming":true,"serverStreaming":true},{"name":"Introduce","inputType":".connectrpc.eliza.v1.IntroduceRequest","outputType":".connectrpc.eliza.v1.IntroduceResponse","options":{},"serverStreaming":true}]}],"syntax":"proto3"}]},"version":"233fca715f49425581ec0a1b660be886"}';
const fetchMock = vi.spyOn(globalThis, 'fetch').mockImplementation(async (input, init) => {
expect(String(input)).toStrictEqual('https://buf.build/buf.reflect.v1beta1.FileDescriptorSetService/GetFileDescriptorSet');
expect(init?.method).toStrictEqual('POST');
expect(JSON.parse(String(init?.body))).toStrictEqual({ module: 'buf.build/connectrpc/eliza' });
expect(new Headers(init?.headers as HeadersInit).get('Authorization')).toStrictEqual('Bearer TEST_KEY');
return new Response(bufReflectJson, {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
});
const methods = await loadMethodsFromReflection({
url: 'foo.com',
metadata: [],
reflectionApi: {
enabled: true,
apiKey: 'TEST_KEY',
url: 'https://buf.build',
module: 'buf.build/connectrpc/eliza',
},
});
expect(methods).toStrictEqual([
{
example: undefined,
fullPath: '/connectrpc.eliza.v1.ElizaService/Say',
type: 'unary',
},
{
example: undefined,
fullPath: '/connectrpc.eliza.v1.ElizaService/Converse',
type: 'bidi',
},
{
example: undefined,
fullPath: '/connectrpc.eliza.v1.ElizaService/Introduce',
type: 'server',
},
]);
try {
const methods = await loadMethodsFromReflection({
url: 'foo.com',
metadata: [],
rejectUnauthorized: true,
reflectionApi: {
enabled: true,
apiKey: 'TEST_KEY',
url: 'https://buf.build',
module: 'buf.build/connectrpc/eliza',
},
});
expect(methods).toStrictEqual([
{
example: undefined,
fullPath: '/connectrpc.eliza.v1.ElizaService/Say',
type: 'unary',
},
{
example: undefined,
fullPath: '/connectrpc.eliza.v1.ElizaService/Converse',
type: 'bidi',
},
{
example: undefined,
fullPath: '/connectrpc.eliza.v1.ElizaService/Introduce',
type: 'server',
},
]);
} finally {
fetchMock.mockRestore();
}
});
});
});
Loading
Loading