Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tests #19

Merged
merged 3 commits into from
Mar 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"automock": false,
"verbose": true,
"testEnvironment": "node",
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testResultsProcessor": "<rootDir>/node_modules/ts-jest/coverageprocessor.js",
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
Expand Down
9 changes: 4 additions & 5 deletions src/jspm-hmr-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,16 @@ export function createServer(options: ServerOptions): JspmHmrServer {
}));
}


// Proxy
if (options.proxy) {
const proxyTarget = options.proxy;
const proxyRoute = options.proxyRoute || '*';

const proxy = httpProxy.createProxyServer();
const proxyServer = httpProxy.createProxyServer();
app.use(proxyRoute, (req, res) => {
req.url = `${req.baseUrl}${req.url}`;
proxy.web(req, res, { target: proxyTarget });
proxy.on('error', (err: Error) => {
req.url = `${req.originalUrl}`;
proxyServer.web(req, res, { target: proxyTarget });
proxyServer.on('error', (err: Error) => {
console.log('Proxy Server Error: ', err.message);
});
});
Expand Down
107 changes: 87 additions & 20 deletions test/jspm-hmr-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,108 @@ const TARGET_PORT = 8080;
const TARGET_ADDRESS = 'http://127.0.0.1:' + TARGET_PORT;
const SERVER_PORT = 3000;
const SERVER_ADDRESS = 'http://127.0.0.1:' + SERVER_PORT;
const PROXY_ROUTE = '*';

describe('testing jspmHmrServer features', () => {

describe('--proxy ' + TARGET_ADDRESS, () => {
describe(`--proxy ${TARGET_ADDRESS}`, () => {
it('target server should receive request through proxy server', (done) => {
const proxyServer = createServer({
path: './boilerplate/',
proxy: TARGET_ADDRESS,
disableHmr: true,
verbose: true,
}).listen(SERVER_PORT);

const proxyServer = createServer({
path: './boilerplate/',
proxy: TARGET_ADDRESS,
fallback: undefined,
disableHmr: true,
verbose: true,
const targetServer = http.createServer(function a(req, res) {
expect(req.headers.host).toEqual('127.0.0.1:3000');
targetServer.close(() => { console.log('target closed'); });
proxyServer.close(() => { console.log('proxy closed'); });
done();
}).listen(TARGET_PORT);

http.request(`${SERVER_ADDRESS}/`).end();
});
proxyServer.listen(SERVER_PORT);

test('target server should receive request through proxy server', (done) => {
const targetServer = http.createServer(function (req, res) {
expect(req.headers.host).toEqual('127.0.0.1:3000');
targetServer.close();
it('proxied request should preserve url params', (done) => {
const proxyServer = createServer({
path: './boilerplate/',
proxy: TARGET_ADDRESS,
disableHmr: true,
verbose: true,
}).listen(SERVER_PORT);

const targetServer = http.createServer((req, res) => {
expect(req.url).toEqual('/api/search?param=value');
targetServer.close(() => { console.log('target closed'); });
proxyServer.close(() => { console.log('proxy closed'); });
done();
}).listen(TARGET_PORT);;
}).listen(TARGET_PORT);

http.request(`${SERVER_ADDRESS}/api/search?param=value`).end();
});
});

http.request(SERVER_ADDRESS, function () { /* */ }).end();
})
describe(`--proxy ${TARGET_ADDRESS} --proxyRoute /api`, () => {
it('should not proxy request if not contain "/api/" route', (done) => {
const proxyServer = createServer({
path: './boilerplate/',
proxy: TARGET_ADDRESS,
proxyRoute: '/api',
disableHmr: true,
verbose: true,
}).listen(SERVER_PORT);

const targetServer = http.createServer(function handler(req, res) {
expect(handler).not.toHaveBeenCalled();
}).listen(TARGET_PORT);

http.request(`${SERVER_ADDRESS}/`, (res) => {
targetServer.close(() => { console.log('target closed'); });
proxyServer.close(() => { console.log('proxy closed'); });
done();
}).end();
});

it('should proxy request if contain "/api" route', (done) => {
const proxyServer = createServer({
path: './boilerplate/',
proxy: TARGET_ADDRESS,
proxyRoute: '/api',
disableHmr: true,
verbose: true,
}).listen(SERVER_PORT);

test('proxied request should preserve url params', (done) => {
const targetServer = http.createServer(function (req, res) {
expect(req.url).toEqual('/?parameter=test');
targetServer.close();
expect(req.url).toEqual('/api/');
targetServer.close(() => { console.log('target closed'); });
proxyServer.close(() => { console.log('proxy closed'); });
done();
}).listen(TARGET_PORT);

http.request(SERVER_ADDRESS + '/?parameter=test', function () { /* */ }).end();
http.request(`${SERVER_ADDRESS}/api/`).end();
});
});

describe(`--proxy ${TARGET_ADDRESS} --proxyRoute /api --fallback `, () => {
it('should proxy request even if fallback is enabled (proxy precenece over fallback)', (done) => {
const proxyServer = createServer({
path: './boilerplate/',
proxy: TARGET_ADDRESS,
proxyRoute: '/api',
fallback: true,
disableHmr: true,
verbose: true,
}).listen(SERVER_PORT);

const targetServer = http.createServer(function handler(req, res) {
expect(req.url).toEqual('/api/search?param=value');
targetServer.close(() => { console.log('target closed'); });
proxyServer.close(() => { console.log('proxy closed'); });
done();
}).listen(TARGET_PORT);

http.request(`${SERVER_ADDRESS}/api/search?param=value`).end();
});
});

});