Skip to content

Commit

Permalink
Merge pull request #51 from Pchelolo/docs2
Browse files Browse the repository at this point in the history
Doc handling improvements
  • Loading branch information
gwicke committed Aug 3, 2016
2 parents 6bb41c0 + b44378f commit aaa3f2d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
43 changes: 29 additions & 14 deletions lib/hyperswitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,43 @@ HyperSwitch.prototype.makeChild = function(req, options) {
return new HyperSwitch(this, req, options);
};

function getDocBasePath(req, spec) {
if (req.params.domain === req.headers.host.replace(/:[0-9]+$/, '')
&& spec['x-host-basePath']) {
// This is a host-based request. Set an appropriate base path.
return spec['x-host-basePath'];
}
return req.uri.toString().replace(/\/$/, '');
}
// A default listing handler for URIs that end in / and don't have any
// handlers associated with it otherwise.
HyperSwitch.prototype.defaultListingHandler = function(match, hyper, req) {
var rq = req.query;
if (rq.spec !== undefined
&& match.value.specRoot && !match.value.specRoot['x-listing']) {
var spec = Object.assign({}, match.value.specRoot, {
// Set the base path dynamically
basePath: req.uri.toString().replace(/\/$/, '')
});

if (req.params.domain === req.headers.host.replace(/:[0-9]+$/, '')) {
// This is a host-based request. Set an appropriate base path.
spec.basePath = spec['x-host-basePath'] || spec.basePath;
}

return P.resolve({
status: 200,
body: spec
body: Object.assign({}, match.value.specRoot, {
// Set the base path dynamically
basePath: getDocBasePath(req, match.value.specRoot)
})
});
} else if (rq.doc !== undefined
&& (match.value.specRoot && !match.value.specRoot['x-listing'] || rq.path)) {
} else if (rq.path ||
(match.value.specRoot
&& !match.value.specRoot['x-listing']
&& match.value.specRoot.basePath === req.uri.toString().replace(/\/$/, '')
&& /\btext\/html\b/.test(req.headers.accept))) {
// If there's ane query parameters except ?path - redirect to the basePath
if (Object.keys(req.query).filter(function(paramName) {
return paramName !== 'path';
}).length) {
return {
status: 301,
headers: {
location: getDocBasePath(req, match.value.specRoot) + '/'
}
};
}
// Return swagger UI & load spec from /?spec
if (!req.query.path) {
req.query.path = '/index.html';
Expand All @@ -149,7 +164,7 @@ HyperSwitch.prototype.defaultListingHandler = function(match, hyper, req) {
})
.map(function(api) {
return '<li><a href="' + encodeURIComponent(api)
+ '/?doc">' + api + '</a></li>';
+ '/">' + api + '</a></li>';
}).join('\n')
+ '</ul>';
html += "<h3>JSON listing</h3><p>To retrieve a regular JSON listing, you can either "
Expand Down
6 changes: 3 additions & 3 deletions lib/swaggerUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function staticServe(hyper, req) {
if (reqPath === '/index.html') {
// Rewrite the HTML to use a query string
body = body.toString()
.replace(/((?:src|href)=['"])/g, '$1?doc=&path=')
.replace(/((?:src|href)=['"])/g, '$1?path=')
// Some self-promotion
.replace(/<a id="logo".*?<\/a>/,
'<a id="logo" href="https://www.mediawiki.org/wiki/RESTBase">RESTBase</a>')
Expand All @@ -40,7 +40,7 @@ function staticServe(hyper, req) {
if (/\.js$/.test(reqPath)) {
contentType = 'text/javascript';
body = body.toString()
.replace(/underscore\-min\.map/, '?doc=&path=lib/underscore-min.map');
.replace(/underscore\-min\.map/, '?path=lib/underscore-min.map');
} else if (/\.png$/.test(reqPath)) {
contentType = 'image/png';
} else if (/\.map$/.test(reqPath)) {
Expand All @@ -49,7 +49,7 @@ function staticServe(hyper, req) {
contentType = 'application/x-font-ttf';
} else if (/\.css$/.test(reqPath)) {
contentType = 'text/css';
body = body.toString().replace(/\.\.\/(images|fonts)\//g, '?doc&path=$1/');
body = body.toString().replace(/\.\.\/(images|fonts)\//g, '?path=$1/');
}
return P.resolve({
status: 200,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hyperswitch",
"version": "0.6.0",
"version": "0.6.1",
"description": "REST API creation framework",
"main": "index.js",
"scripts": {
Expand Down
23 changes: 14 additions & 9 deletions test/hyperswitch/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,25 @@ describe('Documentation handling', function() {

it('should retrieve the swagger-ui main page', function() {
return preq.get({
uri: server.hostPort + '/v1/?doc'
uri: server.hostPort + '/v1/',
headers: {
accept: 'text/html'
}
})
.then(function(res) {
assert.deepEqual(res.status, 200);
assert.contentType(res, 'text/html');
assert.deepEqual(/<html/.exec(res.body)[0], '<html');
})
.catch(function (e) {
console.log(e);
});
});
});

it('should retrieve all dependencies of the swagger-ui main page', function() {
return preq.get({ uri: server.hostPort + '/v1/?doc' })
return preq.get({
uri: server.hostPort + '/v1/',
headers: {
accept: 'text/html'
}
})
.then(function(res) {
var assertions = [];
var linkRegex = /<link\s[^>]*href=["']([^"']+)["']/g;
Expand Down Expand Up @@ -86,7 +91,7 @@ describe('Documentation handling', function() {

it('should throw error for static serve', function() {
return preq.get({
uri: server.hostPort + '/v1/?doc=&path=/this_is_no_a_path',
uri: server.hostPort + '/v1/?path=/this_is_no_a_path',
headers: {
accept: 'text/html'
}
Expand All @@ -100,7 +105,7 @@ describe('Documentation handling', function() {

it('should disallow unsecure relative paths for static serve', function() {
return preq.get({
uri: server.hostPort + '/v1/?doc=&path=../../../Test',
uri: server.hostPort + '/v1/?path=../../../Test',
headers: {
accept: 'text/html'
}
Expand All @@ -126,7 +131,7 @@ describe('Documentation handling', function() {

it('should not allow doc requests to sys', function () {
return preq.get({
uri: server.hostPort + '/sys/?doc=',
uri: server.hostPort + '/sys/',
headers: {
accept: 'text/html'
}
Expand Down

0 comments on commit aaa3f2d

Please sign in to comment.