2014-07-04 22:09:03 +00:00
|
|
|
/* Copyright (c) 2013 StrongLoop, Inc.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var url = require('url');
|
2014-07-10 18:49:09 +00:00
|
|
|
var urlJoin = require('../lib/url-join');
|
2014-07-04 22:09:03 +00:00
|
|
|
var loopback = require('loopback');
|
2014-07-09 22:38:05 +00:00
|
|
|
var express = require('express');
|
2014-07-10 18:49:09 +00:00
|
|
|
var swagger = require('../lib/swagger');
|
2014-07-04 22:09:03 +00:00
|
|
|
|
|
|
|
var request = require('supertest');
|
|
|
|
var expect = require('chai').expect;
|
|
|
|
|
|
|
|
describe('swagger definition', function() {
|
|
|
|
describe('basePath', function() {
|
2014-07-05 19:32:00 +00:00
|
|
|
// No basepath on resource doc in 1.2
|
|
|
|
it('no longer exists on resource doc', function(done) {
|
2014-07-09 22:38:05 +00:00
|
|
|
var app = mountSwagger();
|
2014-07-04 22:09:03 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
var getReq = getSwaggerResources(app);
|
2014-07-04 22:09:03 +00:00
|
|
|
getReq.end(function(err, res) {
|
2014-07-05 19:32:00 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
expect(res.body.basePath).to.equal(undefined);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
it('is "http://{host}/api" by default', function(done) {
|
|
|
|
var app = mountSwagger();
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
var getReq = getAPIDeclaration(app, 'products');
|
2014-07-05 19:32:00 +00:00
|
|
|
getReq.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2014-07-09 22:38:05 +00:00
|
|
|
expect(res.body.basePath).to.equal(url.resolve(getReq.url, '/api'));
|
2014-07-05 19:32:00 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-07-04 22:09:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('is "http://{host}/{basePath}" when basePath is a path', function(done){
|
2014-07-09 22:38:05 +00:00
|
|
|
var app = mountSwagger({ basePath: '/api-root'});
|
2014-07-04 22:09:03 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
var getReq = getAPIDeclaration(app, 'products');
|
2014-07-04 22:09:03 +00:00
|
|
|
getReq.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var apiRoot = url.resolve(getReq.url, '/api-root');
|
|
|
|
expect(res.body.basePath).to.equal(apiRoot);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
it('infers API basePath from app', function(done){
|
|
|
|
var app = mountSwagger({}, {apiRoot: '/custom-api-root'});
|
|
|
|
|
|
|
|
var getReq = getAPIDeclaration(app, 'products');
|
|
|
|
getReq.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var apiRoot = url.resolve(getReq.url, '/custom-api-root');
|
|
|
|
expect(res.body.basePath).to.equal(apiRoot);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-07-04 22:09:03 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
it('is reachable when explorer mounting location is changed', function(done){
|
|
|
|
var explorerRoot = '/erforscher';
|
|
|
|
var app = mountSwagger({}, {explorerRoot: explorerRoot});
|
2014-07-04 22:09:03 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
var getReq = getSwaggerResources(app, explorerRoot, 'products');
|
2014-07-04 22:09:03 +00:00
|
|
|
getReq.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
2014-07-09 22:38:05 +00:00
|
|
|
expect(res.body.basePath).to.be.a('string');
|
2014-07-04 22:09:03 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model definition attributes', function() {
|
|
|
|
it('Properly defines basic attributes', function(done) {
|
2014-07-09 22:38:05 +00:00
|
|
|
var app = mountSwagger();
|
|
|
|
|
|
|
|
var getReq = getAPIDeclaration(app, 'products');
|
|
|
|
getReq.end(function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var data = res.body.models.product;
|
2014-07-04 22:09:03 +00:00
|
|
|
expect(data.id).to.equal('product');
|
2014-07-10 16:32:58 +00:00
|
|
|
expect(data.required.sort()).to.eql(['aNum', 'foo'].sort());
|
2014-07-04 22:09:03 +00:00
|
|
|
expect(data.properties.foo.type).to.equal('string');
|
|
|
|
expect(data.properties.bar.type).to.equal('string');
|
|
|
|
expect(data.properties.aNum.type).to.equal('number');
|
|
|
|
// These will be Numbers for Swagger 2.0
|
|
|
|
expect(data.properties.aNum.minimum).to.equal('1');
|
|
|
|
expect(data.properties.aNum.maximum).to.equal('10');
|
|
|
|
// Should be Number even in 1.2
|
|
|
|
expect(data.properties.aNum.defaultValue).to.equal(5);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
function getSwaggerResources(app, restPath, classPath) {
|
2014-07-04 22:09:03 +00:00
|
|
|
return request(app)
|
2014-07-10 18:49:09 +00:00
|
|
|
.get(urlJoin(restPath || '/explorer', '/resources', classPath || ''))
|
2014-07-04 22:09:03 +00:00
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
}
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
function getAPIDeclaration(app, className) {
|
2014-07-10 18:49:09 +00:00
|
|
|
return getSwaggerResources(app, '', urlJoin('/', className));
|
2014-07-09 22:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function mountSwagger(options, addlOptions) {
|
|
|
|
addlOptions = addlOptions || {};
|
|
|
|
var app = createLoopbackAppWithModel(addlOptions.apiRoot);
|
|
|
|
var swaggerApp = express();
|
|
|
|
swagger(app, swaggerApp, options);
|
|
|
|
app.use(addlOptions.explorerRoot || '/explorer', swaggerApp);
|
|
|
|
return app;
|
2014-07-04 22:09:03 +00:00
|
|
|
}
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
function createLoopbackAppWithModel(apiRoot) {
|
2014-07-04 22:09:03 +00:00
|
|
|
var app = loopback();
|
|
|
|
|
|
|
|
var Product = loopback.Model.extend('product', {
|
|
|
|
foo: {type: 'string', required: true},
|
|
|
|
bar: 'string',
|
|
|
|
aNum: {type: 'number', min: 1, max: 10, required: true, default: 5}
|
|
|
|
});
|
|
|
|
Product.attachTo(loopback.memory());
|
|
|
|
app.model(Product);
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
// Simulate a restApiRoot set in config
|
|
|
|
app.set('restApiRoot', apiRoot || '/api');
|
|
|
|
app.use(app.get('restApiRoot'), loopback.rest());
|
2014-07-05 19:32:00 +00:00
|
|
|
|
2014-07-04 22:09:03 +00:00
|
|
|
return app;
|
|
|
|
}
|
|
|
|
});
|