Lloyd Brookes
9 years ago
11 changed files with 114 additions and 123 deletions
-
2bin/cli.js
-
2extend/cache-control/server.js
-
2extend/live-reload-optional/server.js
-
2extend/live-reload/server.js
-
51lib/local-web-server.js
-
2lib/middleware-stack.js
-
2package.json
-
0test/proxy/file.txt
-
1test/proxy/one.html
-
84test/proxy/rewrite-proxy.js
-
89test/rewrite-proxy.js
@ -0,0 +1 @@ |
|||
one |
@ -0,0 +1,84 @@ |
|||
'use strict' |
|||
const test = require('tape') |
|||
const request = require('req-then') |
|||
const LocalWebServer = require('../../') |
|||
const http = require('http') |
|||
|
|||
function checkResponse (t, status, body) { |
|||
return function (response) { |
|||
if (status) t.strictEqual(response.res.statusCode, status) |
|||
if (body) t.ok(body.test(response.data), 'correct data') |
|||
} |
|||
} |
|||
|
|||
function fail (t) { |
|||
return function (err) { |
|||
t.fail('failed: ' + err.stack) |
|||
} |
|||
} |
|||
|
|||
test('rewrite: proxy', function (t) { |
|||
t.plan(2) |
|||
const ws = new LocalWebServer() |
|||
ws.addRewrite([ |
|||
{ from: '/test/*', to: 'http://registry.npmjs.org/$1' } |
|||
]) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/test/') |
|||
.then(checkResponse(t, 200, /db_name/)) |
|||
.then(server.close.bind(server)) |
|||
.catch(fail(t)) |
|||
}) |
|||
}) |
|||
|
|||
test('rewrite: proxy, POST', function (t) { |
|||
t.plan(1) |
|||
const ws = new LocalWebServer() |
|||
ws.addRewrite([ |
|||
{ from: '/test/*', to: 'http://registry.npmjs.org/' } |
|||
]) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/test/', { data: {} }) |
|||
.then(checkResponse(t, 405)) |
|||
.then(server.close.bind(server)) |
|||
.catch(fail(t)) |
|||
}) |
|||
}) |
|||
|
|||
test('rewrite: proxy, two url tokens', function (t) { |
|||
t.plan(2) |
|||
const ws = new LocalWebServer() |
|||
ws.addRewrite([ |
|||
{ from: '/:package/:version', to: 'http://registry.npmjs.org/:package/:version' } |
|||
]) |
|||
const server = ws.getServer() |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/command-line-args/1.0.0') |
|||
.then(checkResponse(t, 200, /command-line-args/)) |
|||
.then(server.close.bind(server)) |
|||
.catch(fail(t)) |
|||
}) |
|||
}) |
|||
|
|||
test('rewrite: proxy with port', function (t) { |
|||
t.plan(2) |
|||
const ws1 = new LocalWebServer() |
|||
ws1.addStatic(__dirname) |
|||
|
|||
const ws2 = new LocalWebServer() |
|||
ws2.addRewrite([ |
|||
{ from: '/test/*', to: 'http://localhost:9000/$1' } |
|||
]) |
|||
const server1 = ws1.getServer() |
|||
const server2 = ws2.getServer() |
|||
server1.listen(9000, () => { |
|||
server2.listen(8100, () => { |
|||
request('http://localhost:8100/test/file.txt') |
|||
.then(checkResponse(t, 200, /one/)) |
|||
.then(server1.close.bind(server1)) |
|||
.then(server2.close.bind(server2)) |
|||
}) |
|||
}) |
|||
}) |
@ -1,89 +0,0 @@ |
|||
'use strict' |
|||
const test = require('tape') |
|||
const request = require('req-then') |
|||
const localWebServer = require('../') |
|||
const http = require('http') |
|||
|
|||
function launchServer (app, options) { |
|||
options = options || {} |
|||
const path = `http://localhost:8100${options.path || '/'}` |
|||
const server = http.createServer(app.callback()) |
|||
return server.listen(options.port || 8100, () => { |
|||
const req = request(path, options.reqOptions) |
|||
if (options.onSuccess) req.then(options.onSuccess) |
|||
if (!options.leaveOpen) req.then(() => server.close()) |
|||
req.catch(err => console.error('LAUNCH ERROR', err.stack)) |
|||
}) |
|||
} |
|||
|
|||
function checkResponse (t, status, body) { |
|||
return function (response) { |
|||
if (status) t.strictEqual(response.res.statusCode, status) |
|||
if (body) t.ok(body.test(response.data)) |
|||
} |
|||
} |
|||
|
|||
test('rewrite: proxy', function (t) { |
|||
t.plan(2) |
|||
const app = localWebServer({ |
|||
log: { format: 'none' }, |
|||
static: { root: __dirname + '/fixture/rewrite' }, |
|||
rewrite: [ { from: '/test/*', to: 'http://registry.npmjs.org/$1' } ] |
|||
}) |
|||
launchServer(app, { path: '/test/', onSuccess: response => { |
|||
t.strictEqual(response.res.statusCode, 200) |
|||
t.ok(/db_name/.test(response.data)) |
|||
}}) |
|||
}) |
|||
|
|||
test('rewrite: proxy, POST', function (t) { |
|||
t.plan(1) |
|||
const app = localWebServer({ |
|||
log: { format: 'none' }, |
|||
static: { root: __dirname + '/fixture/rewrite' }, |
|||
rewrite: [ { from: '/test/*', to: 'http://registry.npmjs.org/' } ] |
|||
}) |
|||
const server = http.createServer(app.callback()) |
|||
server.listen(8100, () => { |
|||
request('http://localhost:8100/test/', { data: {} }) |
|||
.then(checkResponse(t, 405)) |
|||
.then(server.close.bind(server)) |
|||
}) |
|||
}) |
|||
|
|||
test('rewrite: proxy, two url tokens', function (t) { |
|||
t.plan(2) |
|||
const app = localWebServer({ |
|||
log: { format: 'none' }, |
|||
rewrite: [ { from: '/:package/:version', to: 'http://registry.npmjs.org/:package/:version' } ] |
|||
}) |
|||
launchServer(app, { path: '/command-line-args/1.0.0', onSuccess: response => { |
|||
t.strictEqual(response.res.statusCode, 200) |
|||
t.ok(/command-line-args/.test(response.data)) |
|||
}}) |
|||
}) |
|||
|
|||
test('rewrite: proxy with port', function (t) { |
|||
t.plan(2) |
|||
const one = localWebServer({ |
|||
log: { format: 'none' }, |
|||
static: { root: __dirname + '/fixture/one' } |
|||
}) |
|||
const two = localWebServer({ |
|||
log: { format: 'none' }, |
|||
static: { root: __dirname + '/fixture/spa' }, |
|||
rewrite: [ { from: '/test/*', to: 'http://localhost:9000/$1' } ] |
|||
}) |
|||
const server1 = http.createServer(one.callback()) |
|||
const server2 = http.createServer(two.callback()) |
|||
server1.listen(9000, () => { |
|||
server2.listen(8100, () => { |
|||
request('http://localhost:8100/test/file.txt').then(response => { |
|||
t.strictEqual(response.res.statusCode, 200) |
|||
t.ok(/one/.test(response.data)) |
|||
server1.close() |
|||
server2.close() |
|||
}) |
|||
}) |
|||
}) |
|||
}) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue