realm-object-server
Version:
350 lines • 14.3 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const TestServer_1 = require("../TestServer");
const decorators_1 = require("../decorators");
const superagent = require("superagent");
const WebSocket = require("uws");
describe("Server Decorators Tests", function () {
let server;
beforeEach(() => __awaiter(this, void 0, void 0, function* () {
server = new TestServer_1.TestServer();
}));
afterEach(() => __awaiter(this, void 0, void 0, function* () {
if (server) {
yield server.shutdown();
}
}));
it("should be able to parse body", function (done) {
const json = {
user: {
name: "Ruth",
age: 78
}
};
let SampleService = class SampleService {
post(incomingBody) {
chai_1.expect(incomingBody).to.deep.eq(json);
return incomingBody;
}
postWithKeyPath(userJSON) {
chai_1.expect(userJSON).to.deep.eq(json["user"]);
return userJSON;
}
postWithDeepKeyPath(userJSON) {
chai_1.expect(userJSON).to.eq(json["user"]["name"]);
return userJSON;
}
};
__decorate([
decorators_1.Post("/a"),
__param(0, decorators_1.Body()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], SampleService.prototype, "post", null);
__decorate([
decorators_1.Post("/b"),
__param(0, decorators_1.Body("user")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], SampleService.prototype, "postWithKeyPath", null);
__decorate([
decorators_1.Post("/c"),
__param(0, decorators_1.Body("user.name")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], SampleService.prototype, "postWithDeepKeyPath", null);
SampleService = __decorate([
decorators_1.BaseRoute("/sample")
], SampleService);
server.addService(new SampleService());
server.start()
.then(() => {
return superagent.post(`${server.url}/sample/a`)
.send(json);
})
.then(() => {
return superagent.post(`${server.url}/sample/b`)
.send(json);
})
.then(() => {
return superagent.post(`${server.url}/sample/c`)
.send(json);
})
.then(() => {
done();
})
.catch(err => done(err));
});
it("should be able to parse query strings", function (done) {
let SampleService = class SampleService {
a(query) {
chai_1.expect(query).to.deep.eq({
price: "20",
category: "toys"
});
return {};
}
b(category) {
chai_1.expect(category).to.eq("toys");
return {};
}
};
__decorate([
decorators_1.Get("/a"),
__param(0, decorators_1.Query()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], SampleService.prototype, "a", null);
__decorate([
decorators_1.Get("/b"),
__param(0, decorators_1.Query("category")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String]),
__metadata("design:returntype", void 0)
], SampleService.prototype, "b", null);
SampleService = __decorate([
decorators_1.BaseRoute("/sample")
], SampleService);
server.addService(new SampleService());
server.start()
.then(() => {
return superagent.get(`${server.url}/sample/a?price=20&category=toys`);
})
.then(() => {
return superagent.get(`${server.url}/sample/b?price=20&category=toys`);
})
.then(() => {
done();
})
.catch(err => {
done(err);
});
});
it("should be able to parse headers", function (done) {
const headers = {
"Cool-Header": "something"
};
let SampleService = class SampleService {
a(incomingHeaders) {
chai_1.expect(incomingHeaders["cool-header"]).to.equal(headers["Cool-Header"]);
return incomingHeaders;
}
b(coolHeader, token) {
chai_1.expect(coolHeader).to.equal(headers["Cool-Header"]);
return { coolHeader, token };
}
};
__decorate([
decorators_1.Get("/a"),
__param(0, decorators_1.Headers()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], SampleService.prototype, "a", null);
__decorate([
decorators_1.Get("/b"),
__param(0, decorators_1.Headers("Cool-Header")), __param(1, decorators_1.Headers("Authorization")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String, Object]),
__metadata("design:returntype", void 0)
], SampleService.prototype, "b", null);
SampleService = __decorate([
decorators_1.BaseRoute("/sample")
], SampleService);
server.addService(new SampleService());
server.start()
.then(() => {
return superagent.get(`${server.url}/sample/a`)
.set(headers);
})
.then((res) => {
return superagent.get(`${server.url}/sample/b`)
.set(headers);
})
.then(() => {
done();
})
.catch(err => done(err));
});
it("should be able to parse params", function (done) {
let SampleService = class SampleService {
a(params) {
chai_1.expect(params).to.deep.equal({
category: "toys",
product: "ball"
});
return {};
}
b(category, product) {
chai_1.expect(category).to.eq("toys");
chai_1.expect(product).to.eq("ball");
return {};
}
};
__decorate([
decorators_1.Get("/a/:category/:product"),
__param(0, decorators_1.Params()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], SampleService.prototype, "a", null);
__decorate([
decorators_1.Get("/b/:category/:product"),
__param(0, decorators_1.Params("category")), __param(1, decorators_1.Params("product")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String, String]),
__metadata("design:returntype", void 0)
], SampleService.prototype, "b", null);
SampleService = __decorate([
decorators_1.BaseRoute("/sample")
], SampleService);
server.addService(new SampleService());
server.start()
.then(() => {
return superagent.get(`${server.url}/sample/a/toys/ball`);
})
.then(() => {
return superagent.get(`${server.url}/sample/b/toys/ball`);
})
.then(() => {
done();
})
.catch(err => done(err));
});
it("should be able to parse params", function (done) {
let SampleService = class SampleService {
a(params) {
chai_1.expect(params).to.deep.equal({
category: "toys",
product: "ball"
});
return {};
}
b(category, product) {
chai_1.expect(category).to.equal("toys");
chai_1.expect(product).to.equal("ball");
return {};
}
};
__decorate([
decorators_1.Get("/a/:category/:product"),
__param(0, decorators_1.Params()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], SampleService.prototype, "a", null);
__decorate([
decorators_1.Get("/b/:category/:product"),
__param(0, decorators_1.Params("category")), __param(1, decorators_1.Params("product")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String, String]),
__metadata("design:returntype", void 0)
], SampleService.prototype, "b", null);
SampleService = __decorate([
decorators_1.BaseRoute("/sample")
], SampleService);
server.addService(new SampleService());
server.start()
.then(() => {
return superagent.get(`${server.url}/sample/a/toys/ball`);
})
.then(() => {
return superagent.get(`${server.url}/sample/b/toys/ball`);
})
.then(() => {
done();
})
.catch(err => done(err));
});
it("should be able to register multiple websocket routes", function () {
return __awaiter(this, void 0, void 0, function* () {
let SampleService = class SampleService {
constructor() {
this.wss = new WebSocket.Server({ noServer: true });
}
stop() {
this.wss.close();
}
a(req, socket, head) {
this.wss.handleUpgrade(req, socket, head, (client) => {
client.send("hello from A");
});
}
b(req, socket, head) {
this.wss.handleUpgrade(req, socket, head, (client) => {
client.send("hello from B");
});
}
};
__decorate([
decorators_1.Stop(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], SampleService.prototype, "stop", null);
__decorate([
decorators_1.Upgrade("/a"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object, Object]),
__metadata("design:returntype", void 0)
], SampleService.prototype, "a", null);
__decorate([
decorators_1.Upgrade("/b"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object, Object]),
__metadata("design:returntype", void 0)
], SampleService.prototype, "b", null);
SampleService = __decorate([
decorators_1.BaseRoute("/sample")
], SampleService);
server.addService(new SampleService());
yield server.start();
yield new Promise((resolve, reject) => {
const ws = new WebSocket(`ws://${server.address}/sample/a`);
ws.on("message", (msg) => {
chai_1.expect(msg).to.equal("hello from A");
resolve();
});
ws.on("close", () => {
reject(new Error("Socket closed before receiving a message from A"));
});
});
yield new Promise((resolve, reject) => {
const ws = new WebSocket(`ws://${server.address}/sample/b`);
ws.on("message", (msg) => {
chai_1.expect(msg).to.equal("hello from B");
resolve();
});
ws.on("close", () => {
reject(new Error("Socket closed before receiving a message from B"));
});
});
});
});
});
//# sourceMappingURL=server-decorators.spec.js.map