@copperjs/copper
Version:
A lightweight chromium grid
166 lines • 7.74 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const sinon = require("sinon");
const httpProxy = require("http-proxy");
const fastify_1 = require("fastify");
const chai_1 = require("chai");
const grid_1 = require("./grid");
const gridRoutes_1 = require("./gridRoutes");
const sessionProxy_1 = require("./sessionProxy");
const sessionRoutes_1 = require("./sessionRoutes");
describe('grid', () => {
describe('gridRoutes', () => {
let fastifyInstance;
beforeEach(async () => {
fastifyInstance = fastify_1.fastify();
await fastifyInstance.register(gridRoutes_1.registerGridRoutes);
await fastifyInstance.ready();
});
afterEach(async () => {
await fastifyInstance.close();
});
it('should return server status', async () => {
const response = await fastifyInstance.inject({ method: 'GET', url: '/status' });
chai_1.expect(response.statusCode).to.equal(200);
chai_1.expect(response.body).to.equal(JSON.stringify({ ready: true, message: 'Copper Grid Is Ready' }));
});
it('should register node on grid', async () => {
const stub = sinon.stub(grid_1.grid, 'registerNode').resolves({ id: '1' });
const response = await fastifyInstance.inject({
method: 'POST',
url: '/node',
payload: { config: { port: 3000 } },
});
chai_1.expect(response.statusCode).to.equal(200);
chai_1.expect(response.body).to.equal(JSON.stringify({ status: 0, nodeId: '1' }));
chai_1.expect(stub).to.have.been.calledOnce;
chai_1.expect(stub).to.have.been.calledWith({ port: 3000, host: 'localhost' });
stub.restore();
});
it('should deregister node on grid', async () => {
const stub = sinon.stub(grid_1.grid, 'deregisterNode').resolves({});
const response = await fastifyInstance.inject({
method: 'DELETE',
url: '/node',
payload: { config: { port: 3000 } },
});
chai_1.expect(response.statusCode).to.equal(200);
chai_1.expect(response.body).to.equal(JSON.stringify({ status: 0 }));
chai_1.expect(stub).to.have.been.calledOnce;
chai_1.expect(stub).to.have.been.calledWith('localhost', 3000);
stub.restore();
});
afterEach(async () => {
await fastifyInstance.close();
});
});
describe('sessionProxy', () => {
let fastifyInstance;
let httpProxyStub;
let httpProxyWebStub;
let getNodeStub;
beforeEach(async () => {
httpProxyWebStub = sinon.stub().callsFake((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
let body = '';
req.on('readable', function () {
body += req.read();
});
req.on('end', function () {
res.end(body);
});
});
httpProxyStub = sinon.stub(httpProxy, 'createProxyServer').returns({ web: httpProxyWebStub });
getNodeStub = sinon.stub(grid_1.grid, 'getNode').returns({ id: '1', URL: 'url' });
fastifyInstance = fastify_1.fastify();
await fastifyInstance.register(sessionProxy_1.registerSessionProxy);
await fastifyInstance.ready();
});
afterEach(async () => {
await fastifyInstance.close();
httpProxyStub.restore();
getNodeStub.restore();
});
it('should proxy session requests', async () => {
const response = await fastifyInstance.inject({
method: 'GET',
url: '/session/1',
});
chai_1.expect(httpProxyWebStub).to.have.been.calledOnce;
chai_1.expect(httpProxyWebStub.getCall(0).args[2]).to.eql({ target: 'url' });
chai_1.expect(response.statusCode).to.equal(200);
});
it('should not parse requests with content-type application/json', async () => {
const response = await fastifyInstance.inject({
method: 'POST',
url: '/session/1/any',
headers: { 'Content-Type': 'application/json' },
payload: { foo: 'bar' },
});
chai_1.expect(response.statusCode).to.equal(200);
chai_1.expect(response.body).to.equal(JSON.stringify({ foo: 'bar' }));
});
it('should not parse requests with content-type text/plain', async () => {
const response = await fastifyInstance.inject({
method: 'POST',
url: '/session/1/any',
headers: { 'Content-Type': 'text/plain' },
payload: 'foo',
});
chai_1.expect(response.statusCode).to.equal(200);
chai_1.expect(response.body).to.equal('foo');
});
afterEach(async () => {
await fastifyInstance.close();
});
});
describe('sessionRoutes', () => {
let fastifyInstance;
beforeEach(async () => {
fastifyInstance = fastify_1.fastify();
await fastifyInstance.register(sessionRoutes_1.registerSessionRoutes);
await fastifyInstance.ready();
});
it('should return server status', async () => {
const response = await fastifyInstance.inject({ method: 'GET', url: '/status' });
chai_1.expect(response.statusCode).to.equal(200);
chai_1.expect(response.body).to.equal(JSON.stringify({ ready: true, message: 'Copper Grid Is Ready' }));
});
it('should list grid sessions', async () => {
const stub = sinon.stub(grid_1.grid, 'listSessions').returns([{ id: '1' }, { id: '2' }]);
const response = await fastifyInstance.inject({ method: 'GET', url: '/sessions' });
chai_1.expect(response.statusCode).to.equal(200);
chai_1.expect(response.body).to.equal(JSON.stringify({ status: 0, value: [{ id: '1' }, { id: '2' }] }));
chai_1.expect(stub).to.have.been.calledOnce;
stub.restore();
});
it('should create a session on the grid', async () => {
const stub = sinon.stub(grid_1.grid, 'createSession').resolves({ id: '1' });
const response = await fastifyInstance.inject({ method: 'POST', url: '/session' });
chai_1.expect(response.statusCode).to.equal(200);
chai_1.expect(response.body).to.equal(JSON.stringify({
status: 0,
value: {
id: '1',
webSocketDebuggerUrl: 'ws://localhost:80/ws/1',
'goog:chromeOptions': { debuggerAddress: 'localhost:80/ws/1' },
},
sessionId: '1',
}));
chai_1.expect(stub).to.have.been.calledOnce;
stub.restore();
});
it('should remove a session from the grid', async () => {
const stub = sinon.stub(grid_1.grid, 'removeSession').resolves({ id: '1' });
const response = await fastifyInstance.inject({ method: 'DELETE', url: '/session/1' });
chai_1.expect(response.statusCode).to.equal(200);
chai_1.expect(response.body).to.equal(JSON.stringify({ id: '1' }));
chai_1.expect(stub).to.have.been.calledOnce;
stub.restore();
});
afterEach(async () => {
await fastifyInstance.close();
});
});
});
//# sourceMappingURL=routes.test.js.map