Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Pchelolo/docs
Docs: Handle docs and listings in a more generic way
- Loading branch information
Showing
8 changed files
with
191 additions
and
33 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
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,117 @@ | ||
"use strict"; | ||
|
||
var assert = require('../utils/assert.js'); | ||
var Server = require('../utils/server.js'); | ||
var preq = require('preq'); | ||
var P = require('bluebird'); | ||
|
||
describe('Documentation handling', function() { | ||
var server = new Server('test/hyperswitch/docs_config.yaml'); | ||
|
||
before(function() { return server.start(); }); | ||
|
||
it('should list APIs using the generic listing handler', function() { | ||
return preq.get({ | ||
uri: server.hostPort + '/' | ||
}) | ||
.then(function(res) { | ||
assert.deepEqual(res.status, 200); | ||
assert.contentType(res, 'application/json'); | ||
assert.deepEqual(res.body, { | ||
items: [ 'v1' ] | ||
}); | ||
}); | ||
}); | ||
|
||
it('should retrieve the spec', function() { | ||
return preq.get({ | ||
uri: server.hostPort + '/v1/?spec' | ||
}) | ||
.then(function(res) { | ||
assert.deepEqual(res.status, 200); | ||
assert.contentType(res, 'application/json'); | ||
assert.deepEqual(res.body.swagger, '2.0'); | ||
}); | ||
}); | ||
|
||
it('should retrieve the swagger-ui main page', function() { | ||
return preq.get({ | ||
uri: server.hostPort + '/v1/?doc' | ||
}) | ||
.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' }) | ||
.then(function(res) { | ||
var assertions = []; | ||
var linkRegex = /<link\s[^>]*href=["']([^"']+)["']/g; | ||
var scriptRegex = /<script\s[^>]*src=["']([^"']+)["']/g; | ||
var match; | ||
while (match = linkRegex.exec(res.body)) { | ||
assertions.push(match[1]); | ||
} | ||
while (match = scriptRegex.exec(res.body)) { | ||
assertions.push(match[1]); | ||
} | ||
return P.all(assertions.map(function(path) { | ||
return preq.get({ uri: server.hostPort + '/' + path }) | ||
.then(function(res) { | ||
assert.deepEqual(res.status, 200); | ||
}); | ||
})); | ||
}); | ||
}); | ||
|
||
it('should retrieve API listing in html', function() { | ||
return preq.get({ | ||
uri: server.hostPort + '/', | ||
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'); | ||
}); | ||
}); | ||
|
||
it('should throw error for static serve', function() { | ||
return preq.get({ | ||
uri: server.hostPort + '/v1/?doc=&path=/this_is_no_a_path', | ||
headers: { | ||
accept: 'text/html' | ||
} | ||
}) | ||
.then(function() { | ||
throw new Error('Error should be thrown'); | ||
}, function(e) { | ||
assert.deepEqual(e.status, 404); | ||
}); | ||
}); | ||
|
||
it('should disallow unsecure relative paths for static serve', function() { | ||
return preq.get({ | ||
uri: server.hostPort + '/v1/?doc=&path=../../../Test', | ||
headers: { | ||
accept: 'text/html' | ||
} | ||
}) | ||
.then(function() { | ||
throw new Error('Error should be thrown'); | ||
}, function(e) { | ||
assert.deepEqual(e.status, 500); | ||
assert.deepEqual(e.body.detail, 'Error: Invalid path.'); | ||
}); | ||
}); | ||
|
||
after(function() { return server.stop(); }); | ||
}); |
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,28 @@ | ||
spec_root: &spec_root | ||
paths: | ||
/{api:v1}: | ||
x-modules: | ||
- type: inline | ||
spec: | ||
paths: | ||
/test: | ||
get: | ||
x-request-handler: | ||
- return_result: | ||
return: | ||
status: 200 | ||
body: 'test' | ||
|
||
# Finally, a standard service-runner config. | ||
info: | ||
name: hyperswitch | ||
|
||
services: | ||
- name: test_service | ||
module: ./lib/server | ||
conf: | ||
port: 12345 | ||
spec: *spec_root | ||
salt: secret | ||
default_page_size: 1 | ||
user_agent: HyperSwitch-testsuite |
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