Skip to content

Commit

Permalink
test: remove deprecated dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Sep 10, 2024
1 parent ef1c715 commit 040cb20
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 414 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@
},
"devDependencies": {
"chai": "^5.1.1",
"electrode-server": "^2.2.2",
"gulp": "^3.8.11",
"gulp-eslint": "^0.9.0",
"node-fetch": "^3.3.2",
"prettier": "^2.0.1",
"run-verify": "^1.2.2",
"vitest": "^2.0.5"
},
"prettier": {
Expand Down
103 changes: 49 additions & 54 deletions test/onrequest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,64 @@ import { describe, it, expect } from 'vitest';
import { Redbird } from '../'; // Adjust the import path if necessary
import { expect } from 'chai';
import fetch from 'node-fetch';
import { createServer } from 'http';

const { asyncVerify, runFinally } = require('run-verify');
const electrodeServer = require('electrode-server');
const TEST_PORT = 3000;

describe('onRequest hook', function () {
function setupTestRoute(handler) {
return electrodeServer().then((server) => {
server.route({
method: 'get',
path: '/test',
handler,
});
return server;
});
}

it('should be able to modify headers for a route', () => {
let server;
it('should be able to modify headers for a route', async () => {
let proxy;
let serverReq;
let proxyReq;
let saveProxyHeaders;

return asyncVerify(
() => {
return setupTestRoute((req) => {
serverReq = req;
return 'hello test';
});
const promiseServer = testServer();

let target;
proxy = Redbird({ bunyan: false, port: 18999 });
proxy.register({
src: 'localhost/x',
target: `http://localhost:${TEST_PORT}/test`,
onRequest: (req, res, tgt) => {
proxyReq = req;
saveProxyHeaders = Object.assign({}, req.headers);
req.headers.foo = 'bar';
delete req.headers.blah;
target = tgt;
},
(s) => {
server = s;
let target;
proxy = Redbird({ bunyan: false, port: 18999 });
proxy.register({
src: 'localhost/x',
target: 'http://localhost:3000/test',
onRequest: (req, res, tgt) => {
proxyReq = req;
saveProxyHeaders = Object.assign({}, req.headers);
req.headers.foo = 'bar';
delete req.headers.blah;
target = tgt;
},
});
});

return fetch('http://localhost:18999/x', {
headers: {
blah: 'xyz',
},
}).then((res) => {
expect(res.status).to.equal(200);
expect(target).to.exist;
expect(saveProxyHeaders).to.exist;
expect(saveProxyHeaders.blah).to.equal('xyz');
expect(serverReq).to.exist;
expect(serverReq.headers.foo).to.equal('bar');
expect(serverReq.headers.blah).to.equal(undefined);
return target;
});
const res = await fetch('http://localhost:18999/x', {
headers: {
blah: 'xyz',
},
runFinally(() => proxy && proxy.close()),
runFinally(() => server && server.stop())
);
});
expect(res.status).to.equal(200);
expect(target).to.exist;
expect(saveProxyHeaders).to.exist;
expect(saveProxyHeaders.blah).to.equal('xyz');

const req = await promiseServer;
expect(req).to.exist;
expect(req.headers.foo).to.equal('bar');
expect(req.headers.blah).to.equal(undefined);

await proxy.close();
});
});

function testServer() {
return new Promise(function (resolve, reject) {
const server = createServer(function (req, res) {
res.write('');
res.end();
server.close((err) => {
if (err) {
return reject(err);
}
resolve(req);
});
});

server.listen(TEST_PORT);
});
}
Loading

0 comments on commit 040cb20

Please sign in to comment.