twirp-rpc-client
Version:
Typescript twirp client built for use with ts-proto
145 lines (144 loc) • 6.78 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const protobuf_client_1 = __importStar(require("./protobuf-client"));
const nock_1 = __importDefault(require("nock"));
const mockServiceUrl = 'https://example.com/twirp';
const client = protobuf_client_1.default({ url: mockServiceUrl });
const requestData = Buffer.from('request-data');
const responseData = Buffer.from('response-data');
describe('TwirpProtobufClient', () => {
afterAll(() => {
nock_1.default.restore();
});
test('requests twirp service correctly', async () => {
const scope = nock_1.default(mockServiceUrl)
.post('/twitch.twirp.example.Haberdasher/MakeHat', requestData)
.reply(200, responseData);
scope.on('request', (req) => {
expect(req['headers']['accept']).toEqual('application/protobuf,application/json');
expect(req['headers']['content-type']).toEqual('application/protobuf');
expect(req['requestBodyBuffers'][0]).toEqual(requestData);
});
await expect(client.request('twitch.twirp.example.Haberdasher', 'MakeHat', requestData)).resolves.toEqual(responseData);
});
test('passes through correct header configuration', async () => {
const configuredClient = protobuf_client_1.default({
url: mockServiceUrl,
headers: {
'x-example-header': 'test',
},
auth: {
username: 'username',
password: 'password',
},
});
const scope = nock_1.default(mockServiceUrl)
.post('/twitch.twirp.example.Haberdasher/MakeHat', requestData)
.reply(200, responseData);
scope.on('request', (req) => {
// expect(req['headers']['authorization']).toEqual('Basic dXNlcm5hbWU6cGFzc3dvcmQ=')
expect(req['headers']['x-example-header']).toEqual('test');
expect(req['headers']['content-type']).toEqual('application/protobuf');
expect(req['headers']['accept']).toEqual('application/protobuf,application/json');
});
await expect(configuredClient.request('twitch.twirp.example.Haberdasher', 'MakeHat', requestData)).resolves.toEqual(responseData);
});
test('correctly sets timeout, and throws a timeout error', async () => {
const configuredClient = protobuf_client_1.default({
url: mockServiceUrl,
timeout: 250,
});
const scope = nock_1.default(mockServiceUrl)
.post('/twitch.twirp.example.Haberdasher/MakeHat', requestData)
.delay(500)
.reply(200, responseData);
await expect(configuredClient.request('twitch.twirp.example.Haberdasher', 'MakeHat', requestData)).rejects.toBeInstanceOf(Error);
});
test('correctly maps invalid_argument twirp error', async () => {
const scope = nock_1.default(mockServiceUrl).post('/twitch.twirp.example.Haberdasher/MakeHat', requestData).reply(400, {
code: 'invalid_argument',
msg: 'field is required',
});
await client
.request('twitch.twirp.example.Haberdasher', 'MakeHat', requestData)
.then(() => fail('should throw an exception'))
.catch((error) => {
expect(error).toBeInstanceOf(protobuf_client_1.TwirpError);
expect(error.code).toEqual(protobuf_client_1.TwirpErrorCode.InvalidArgument);
expect(error.msg).toEqual('field is required');
});
});
test('correctly maps meta data for twirp errors', async () => {
const scope = nock_1.default(mockServiceUrl)
.post('/twitch.twirp.example.Haberdasher/MakeHat', requestData)
.reply(412, {
code: 'failed_precondition',
msg: 'precondition failed',
meta: {
nested: 'data',
},
});
await client
.request('twitch.twirp.example.Haberdasher', 'MakeHat', requestData)
.then(() => fail('should throw an exception'))
.catch((error) => {
expect(error).toBeInstanceOf(protobuf_client_1.TwirpError);
expect(error.code).toEqual(protobuf_client_1.TwirpErrorCode.FailedPrecondition);
expect(error.meta).toEqual({
nested: 'data',
});
});
});
test('falls back to http status code when no error code is provided', async () => {
const scope = nock_1.default(mockServiceUrl)
.post('/twitch.twirp.example.Haberdasher/MakeHat', requestData)
.reply(412, {
msg: 'precondition failed',
meta: {
nested: 'data',
},
});
await client
.request('twitch.twirp.example.Haberdasher', 'MakeHat', requestData)
.then(() => fail('should throw an exception'))
.catch((error) => {
expect(error).toBeInstanceOf(protobuf_client_1.TwirpError);
expect(error.code).toEqual(protobuf_client_1.TwirpErrorCode.FailedPrecondition);
expect(error.meta).toEqual({
nested: 'data',
});
});
});
test('handles an empty error response', async () => {
const scope = nock_1.default(mockServiceUrl).post('/twitch.twirp.example.Haberdasher/MakeHat', requestData).reply(412);
await client
.request('twitch.twirp.example.Haberdasher', 'MakeHat', requestData)
.then(() => fail('should throw an exception'))
.catch((error) => {
expect(error).toBeInstanceOf(protobuf_client_1.TwirpError);
expect(error.code).toEqual(protobuf_client_1.TwirpErrorCode.FailedPrecondition);
});
});
});
;