Skip to content

Commit

Permalink
(pouchdb/express-pouchdb#232) - Modernize pouchdb-vhost
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-de-vries authored and gr2m committed Jul 3, 2017
1 parent 89229bf commit f8d9447
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = function (PouchDB) {
var hostHeader = splitHost(headers.Host || headers.host || '');
var match, targetParts;
for (var unsplittedVHost in vhosts) {
/* istanbul ignore else */
if (vhosts.hasOwnProperty(unsplittedVHost)) {
var vhost = splitHost(unsplittedVHost);
targetParts = splitUrl(vhosts[unsplittedVHost]);
Expand Down
111 changes: 111 additions & 0 deletions test/features.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Based on: https://github.com/apache/couchdb/blob/5c94e815e60dc53db735243e0d532b03bbe319c3/test/etap/160-vhosts.t
Doesn't include the following subtests:
- doesn't make any sense in the current context:
- test_vhost_request_with_global()
- test_vhost_request_with_oauth
- an undocumented part of the api that can be duplicated with rewrites
- test_vhost_request_path1
- test_vhost_request_path2
- test_vhost_request_path3
- double with 'regular request'?
- test_vhost_request_to_root
- double with 'vhost request'
- testVHostRequestPath
*/

import {setup, teardown, PouchDB} from './utils';
let db;

describe('sync vhost tests', () => {
const vhosts = {
'example.com': '/test',
'*.example.com': '/test/_design/doc1/_rewrite',
'example.com/test': '/test',
'example1.com': '/test/_design/doc1/_rewrite/',
':appname.:dbname.example1.com': '/:dbname/_design/:appname/_rewrite/',
':dbname.example1.com': '/:dbname',
'*.example2.com': '/*',
'*.example2.com/abc': '/*',
'*/abc': '/test/_design/doc1/_show/test',
'example3.com': '/'
};

beforeEach(async () => {
db = setup();
await db.put({value: 666}, 'doc1');
await db.put({
shows: {
test: `function (doc, req) {
return {
json: {
requested_path: '/' + req.requested_path.join('/'),
path: '/' + req.path.join('/')
}
};
}`
},
rewrites: [{
from: '/',
to: '_show/test'
}]
}, '_design/doc1');
});
afterEach(teardown);

function vhost(req) {
req.raw_path = req.raw_path || '/';
return PouchDB.virtualHost(req, vhosts);
}
function resolve(req) {
req.raw_path = req.raw_path || '/';
return PouchDB.resolveVirtualHost(req, vhosts);
}

it('regular request', async () => {
// with no host headers, no vhost should be used
await resolve({}).should.equal('/');
});

it('vhost request', async () => {
const info = await vhost({headers:{host: 'example.com'}});
info.should.have.property('db_name');
});

it('vhost request with QS', async () => {
const doc = await vhost({raw_path: '/doc1?revs_info=true', headers: {host: 'example.com'}});
doc.should.have.property('_revs_info');
});

it('vhost requested path', async () => {
const resp = await vhost({headers: {host: 'example1.com'}});
resp.json.requested_path.should.equal('/');
});

it('vhost requested path path', async () => {
const resp = await vhost({headers: {Host: 'example1.com'}});
resp.json.path.should.equal('/test/_design/doc1/_show/test');
});

it('vhost request with wildcard', async () => {
const resp = await vhost({headers: {host: 'test.example.com'}});
resp.json.path.should.equal('/test/_design/doc1/_show/test');
});

it('vhost request replace var', async () => {
const info = await vhost({headers: {host: 'test.example1.com'}});
info.should.have.property('db_name');
});

it('vhost request replace var1', async () => {
const resp = await vhost({headers: {Host: 'doc1.test.example1.com'}});
resp.json.path.should.equal('/test/_design/doc1/_show/test');
});

it('vhost request replace wildcard', async () => {
const info = await vhost({headers: {host: 'test.example2.com'}});
info.should.have.property('db_name');
});
});
9 changes: 9 additions & 0 deletions test/signatures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {PouchDB} from './utils';

describe('signatures', () => {
it('vhost', () => {
const promise = PouchDB.virtualHost({raw_path: '/'}, {}, () => {});
promise.then.should.be.ok;
promise.catch.should.be.ok;
});
});
10 changes: 10 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import stuff from 'pouchdb-plugin-helper/testutils';
import Show from 'pouchdb-show';
import Rewrite from 'pouchdb-rewrite';
import VirtualHost from '../';

stuff.PouchDB.plugin(Show);
stuff.PouchDB.plugin(Rewrite);
VirtualHost(stuff.PouchDB);

module.exports = stuff;

0 comments on commit f8d9447

Please sign in to comment.