This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
214 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
languages: | ||
JavaScript: true | ||
exclude_paths: | ||
- "examples/*.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
var http = require('http') | ||
var https = require('https') | ||
var version = require('../package.json').version | ||
|
||
module.exports = createServer | ||
|
||
function createServer(opts, router) { | ||
var handler = serverHandler(router) | ||
|
||
var server = opts.ssl | ||
? https.createServer(opts.ssl, handler) | ||
: http.createServer(handler) | ||
|
||
server.listen(opts.port, opts.host) | ||
return server | ||
} | ||
|
||
function serverHandler(router) { | ||
return function (req, res) { | ||
res.setHeader('Server', 'rocky ' + version) | ||
router(req, res, function (err) { | ||
if (err) { | ||
return reply(res, 500, err) | ||
} | ||
reply(res, 404, 'No route configured') | ||
}) | ||
} | ||
} | ||
|
||
function reply(res, code, body) { | ||
res.writeHead(code, { 'Content-Type': 'application/json' }) | ||
res.write(JSON.stringify({ message: body })) | ||
res.end() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,171 @@ | ||
const http = require('http') | ||
const supertest = require('supertest') | ||
const expect = require('chai').expect | ||
const rocky = require('../') | ||
|
||
const ports = { target: 9890, proxy: 9891 } | ||
const ports = { target: 9890, proxy: 9891, replay: 9892 } | ||
const baseUrl = 'http://127.0.0.1' | ||
const proxyUrl = baseUrl + ':' + ports.proxy | ||
const targetUrl = baseUrl + ':' + ports.target | ||
const replayUrl = baseUrl + ':' + ports.replay | ||
const noop = function () {} | ||
|
||
suite('rocky', function () { | ||
var proxy = null | ||
var proxy, replay, server | ||
|
||
afterEach(function () { | ||
proxy.server.close() | ||
beforeEach(function () { | ||
proxy = replay = server = null | ||
}) | ||
|
||
afterEach(function (done) { | ||
if (replay) replay.close() | ||
if (server) server.close() | ||
if (proxy) proxy.server.close() | ||
setTimeout(done, 10) | ||
}) | ||
|
||
test('simple forward', function (done) { | ||
proxy = rocky() | ||
.forward('http://127.0.0.1:' + ports.target) | ||
.forward(targetUrl) | ||
.listen(ports.proxy) | ||
|
||
server = createTestServer(assert) | ||
|
||
proxy.get('/test') | ||
http.get(proxyUrl + '/test', noop) | ||
|
||
function assert(req, res) { | ||
expect(req.url).to.be.equal('/test') | ||
expect(res.statusCode).to.be.equal(200) | ||
done() | ||
} | ||
}) | ||
|
||
test('forward and replay', function (done) { | ||
proxy = rocky() | ||
.forward(targetUrl) | ||
.replay(replayUrl) | ||
.listen(ports.proxy) | ||
|
||
proxy.get('/test') | ||
|
||
createTestServer(assert) | ||
replay = createReplayServer(assertReplay) | ||
server = createTestServer(assert) | ||
|
||
http.get(targetUrl + '/test', noop) | ||
supertest(proxyUrl) | ||
.get('/test') | ||
.expect(200) | ||
.expect('Content-Type', 'application/json') | ||
.expect({ 'hello': 'world' }) | ||
.end(done) | ||
|
||
function assert(req, res) { | ||
expect(req.url).to.be.equal('/test') | ||
expect(res.statusCode).to.be.equal(200) | ||
done() | ||
} | ||
|
||
function assertReplay(req, res) { | ||
expect(req.url).to.be.equal('/test') | ||
expect(res.statusCode).to.be.equal(204) | ||
} | ||
}) | ||
|
||
test('forward and replay to multiple backends', function (done) { | ||
proxy = rocky() | ||
.forward(targetUrl) | ||
.replay(replayUrl) | ||
.replay(replayUrl) | ||
.replay(replayUrl) | ||
.listen(ports.proxy) | ||
|
||
proxy.get('/test') | ||
|
||
replay = createReplayServer(assertReplay) | ||
server = createTestServer(assert) | ||
|
||
supertest(proxyUrl) | ||
.get('/test') | ||
.expect(200) | ||
.expect('Content-Type', 'application/json') | ||
.expect({ 'hello': 'world' }) | ||
.end(noop) | ||
|
||
function assert(req, res) { | ||
expect(req.url).to.be.equal('/test') | ||
expect(res.statusCode).to.be.equal(200) | ||
} | ||
|
||
var asserts = 0 | ||
function assertReplay(req, res) { | ||
expect(req.url).to.be.equal('/test') | ||
expect(res.statusCode).to.be.equal(204) | ||
|
||
asserts += 1 | ||
if (asserts === 3) { | ||
done() | ||
} | ||
} | ||
}) | ||
|
||
test('forward and replay payload data', function (done) { | ||
proxy = rocky() | ||
.forward(targetUrl) | ||
.replay(replayUrl) | ||
.listen(ports.proxy) | ||
|
||
proxy.post('/test') | ||
|
||
replay = createReplayServer(assertReplay) | ||
server = createTestServer(assert) | ||
|
||
supertest(proxyUrl) | ||
.post('/test') | ||
.send({ hello: 'world' }) | ||
.expect(200) | ||
.expect('Content-Type', 'application/json') | ||
.expect({ 'hello': 'world' }) | ||
.end(done) | ||
|
||
function assert(req, res) { | ||
expect(req.url).to.be.equal('/test') | ||
expect(res.statusCode).to.be.equal(200) | ||
expect(req.body).to.match(/hello/) | ||
} | ||
|
||
var asserts = 0 | ||
function assertReplay(req, res) { | ||
expect(req.url).to.be.equal('/test') | ||
expect(res.statusCode).to.be.equal(204) | ||
expect(req.body).to.match(/hello/) | ||
} | ||
}) | ||
|
||
}) | ||
|
||
function createTestServer(assert) { | ||
return createServer(ports.target, 200, assert) | ||
} | ||
|
||
function createReplayServer(assert) { | ||
return createServer(ports.replay, 204, assert) | ||
} | ||
|
||
function createServer(port, code, assert) { | ||
var server = http.createServer(function (req, res) { | ||
res.writeHead(200) | ||
res.end() | ||
assert(req, res) | ||
res.writeHead(code, { 'Content-Type': 'application/json' }) | ||
res.write(JSON.stringify({ 'hello': 'world' })) | ||
|
||
var body = '' | ||
req.on('data', function (data) { | ||
body += data | ||
}) | ||
req.on('end', function () { | ||
req.body = body | ||
assert(req, res) | ||
res.end() | ||
}) | ||
}) | ||
server.listen(ports.target) | ||
|
||
server.listen(port) | ||
return server | ||
} |