Co-authored-by: Andrew Eisenberg <aeisenberg@github.com> Co-authored-by: Henry Mercer <henrymercer@github.com>
160 lines
6.2 KiB
JavaScript
160 lines
6.2 KiB
JavaScript
"use strict";
|
|
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const http = __importStar(require("http"));
|
|
const http_terminator_1 = require("http-terminator");
|
|
const service_twirp_1 = require("../__mocks__/service.twirp");
|
|
const service_1 = require("../__mocks__/service");
|
|
const http_client_1 = require("../http.client");
|
|
const errors_1 = require("../errors");
|
|
describe("Twirp Clients", () => {
|
|
let httpTerminator;
|
|
let server;
|
|
beforeEach(() => {
|
|
const twirpServer = service_twirp_1.createHaberdasherServer({
|
|
MakeHat(ctx, request) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
return service_1.Hat.create({
|
|
id: "1",
|
|
name: "cap",
|
|
color: "blue",
|
|
inches: 100,
|
|
variants: [],
|
|
});
|
|
});
|
|
},
|
|
FindHat(ctx, request) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
return request;
|
|
});
|
|
},
|
|
ListHat(ctx, request) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
return request;
|
|
});
|
|
}
|
|
});
|
|
server = http.createServer(twirpServer.httpHandler());
|
|
httpTerminator = http_terminator_1.createHttpTerminator({
|
|
server,
|
|
});
|
|
});
|
|
it("can call methods using the JSON client", (done) => {
|
|
const port = 9999;
|
|
server.listen(port, () => __awaiter(void 0, void 0, void 0, function* () {
|
|
const client = new service_twirp_1.HaberdasherClientJSON(http_client_1.NodeHttpRPC({
|
|
baseUrl: "http://localhost:9999/twirp",
|
|
}));
|
|
const hat = yield client.MakeHat({
|
|
inches: 1,
|
|
});
|
|
expect(hat).toEqual({
|
|
id: "1",
|
|
color: "blue",
|
|
inches: 100,
|
|
name: "cap",
|
|
variants: [],
|
|
});
|
|
yield httpTerminator.terminate();
|
|
done();
|
|
}));
|
|
});
|
|
it("can call methods using the Protobuf client", (done) => {
|
|
const port = 9999;
|
|
server.listen(port, () => __awaiter(void 0, void 0, void 0, function* () {
|
|
const client = new service_twirp_1.HaberdasherClientProtobuf(http_client_1.NodeHttpRPC({
|
|
baseUrl: "http://localhost:9999/twirp",
|
|
}));
|
|
const hat = yield client.MakeHat({
|
|
inches: 1,
|
|
});
|
|
expect(hat).toEqual({
|
|
id: "1",
|
|
color: "blue",
|
|
inches: 100,
|
|
name: "cap",
|
|
variants: [],
|
|
});
|
|
yield httpTerminator.terminate();
|
|
done();
|
|
}));
|
|
});
|
|
it("will return a TwripError when a error occur", (done) => {
|
|
const twirpServer = service_twirp_1.createHaberdasherServer({
|
|
MakeHat(ctx, request) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
const error = new errors_1.InternalServerError("error");
|
|
error.withMeta("test", "msg");
|
|
error.withMeta("test2", "msg2");
|
|
throw error;
|
|
});
|
|
},
|
|
FindHat(ctx, request) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
return request;
|
|
});
|
|
},
|
|
ListHat(ctx, request) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
return request;
|
|
});
|
|
}
|
|
});
|
|
server = http.createServer(twirpServer.httpHandler());
|
|
httpTerminator = http_terminator_1.createHttpTerminator({
|
|
server,
|
|
});
|
|
const port = 9999;
|
|
server.listen(port, () => __awaiter(void 0, void 0, void 0, function* () {
|
|
const client = new service_twirp_1.HaberdasherClientProtobuf(http_client_1.NodeHttpRPC({
|
|
baseUrl: "http://localhost:9999/twirp",
|
|
}));
|
|
let err;
|
|
try {
|
|
yield client.MakeHat({
|
|
inches: 1,
|
|
});
|
|
}
|
|
catch (e) {
|
|
err = e;
|
|
}
|
|
expect(err).toBeInstanceOf(errors_1.TwirpError);
|
|
const twirpErr = err;
|
|
expect(twirpErr.code).toEqual(errors_1.TwirpErrorCode.Internal);
|
|
expect(twirpErr.msg).toEqual("error");
|
|
expect(twirpErr.meta).toEqual({
|
|
test: "msg",
|
|
test2: "msg2"
|
|
});
|
|
yield httpTerminator.terminate();
|
|
done();
|
|
}));
|
|
});
|
|
});
|