Skip to content

Commit 94e3ef6

Browse files
committed
add tests
lets see if we're there :)
1 parent 509d731 commit 94e3ef6

File tree

5 files changed

+208
-5
lines changed

5 files changed

+208
-5
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
pipeInterface: pipeInterface,
3+
middlewareInterface: middlewareInterface
4+
}
5+
6+
function pipeInterface(ctx, next) {
7+
ctx.statusCode = 200;
8+
ctx.headers = {
9+
'content-type': 'application/json',
10+
'x-interface': 'pipe'
11+
};
12+
next(null, { interface: 'pipe' });
13+
}
14+
15+
function middlewareInterface(req, res, next) {
16+
res.setHeader('x-interface', 'middleware');
17+
res.json({ interface: "middleware" });
18+
}

test/assets/project/api/swagger/swagger.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,45 @@ paths:
314314
content-type:
315315
type: integer
316316
schema: {}
317+
/controller_interface_auto_detected_as_pipe:
318+
x-swagger-router-controller: overrides_ctrl_interface_pipe
319+
x-controller-interface: auto-detect
320+
get:
321+
description: well, what do you know...
322+
operationId: pipeInterface
323+
responses:
324+
200:
325+
description: Whatever
326+
schema: {}
327+
/controller_interface_auto_detected_as_middleware:
328+
x-swagger-router-controller: overrides_ctrl_interface_pipe
329+
x-controller-interface: auto-detect
330+
get:
331+
description: well, what do you know...
332+
operationId: middlewareInterface
333+
responses:
334+
200:
335+
description: Whatever
336+
schema: {}
337+
/controller_interface_on_path_cascades:
338+
x-swagger-router-controller: overrides_ctrl_interface_pipe
339+
x-controller-interface: pipe
340+
get:
341+
operationId: pipeInterface
342+
responses:
343+
200:
344+
description: Whatever
345+
schema: {}
346+
/controller_interface_on_operation_cascades:
347+
x-swagger-router-controller: overrides_ctrl_interface_pipe
348+
x-controller-interface: pipe
349+
get:
350+
x-controller-interface: middleware
351+
operationId: middlewareInterface
352+
responses:
353+
200:
354+
description: Whatever
355+
schema: {}
317356
definitions:
318357
HelloWorldResponse:
319358
type: object
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# values in the swagger hash are system configuration for swagger-node
2+
swagger:
3+
4+
fittingsDirs: [ api/fittings ]
5+
defaultPipe: null
6+
swaggerControllerPipe: swagger_controllers # defines the standard processing pipe for controllers
7+
8+
# values defined in the bagpipes key are the bagpipes pipes and fittings definitions
9+
# (see https://github.com/apigee-127/bagpipes)
10+
bagpipes:
11+
12+
_router:
13+
name: swagger_router
14+
mockMode: false
15+
mockControllersDirs: [ api/mocks ]
16+
controllersDirs: [ api/controllers ]
17+
controllersInterface: auto-detect
18+
19+
_swagger_validate:
20+
name: swagger_validator
21+
validateResponse: true
22+
23+
_swagger_security:
24+
name: swagger_security
25+
securityHandlersModule: api/helpers/securityHandlers
26+
27+
# pipe for all swagger-node controllers
28+
swagger_controllers:
29+
- onError: json_error_handler
30+
- cors
31+
- swagger_params_parser
32+
- _swagger_security
33+
- _swagger_validate
34+
- express_compatibility
35+
- _router
36+
37+
# pipe to serve swagger (endpoint is in swagger.yaml)
38+
swagger_raw:
39+
name: swagger_raw

test/fittings/swagger_raw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('swagger_raw', function() {
2020
delete(filteredSwagger.paths['/invalid_header']);
2121

2222
// hokey algorithm, but at least it's different than the one it's testing
23-
var OMIT = ['x-swagger-router-controller', 'x-swagger-pipe', 'x-hidden', 'x-private'];
23+
var OMIT = ['x-swagger-router-controller', 'x-swagger-pipe', 'x-hidden', 'x-private', 'x-controller-interface'];
2424
_.forEach(filteredSwagger.paths, function(element, name) {
2525
filteredSwagger.paths[name] = _.omit(element, OMIT);
2626
_.forEach(filteredSwagger.paths[name], function(element, subName) {

test/index.js

Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,9 @@ describe('index', function() {
156156
delete process.env.NODE_CONFIG_DIR;
157157
});
158158

159-
it('should use the configured router interface', function(done) {
159+
it('should use pipe interface when _router.controllersInterface is set to `pipe`', function(done) {
160160
var config = _.clone(DEFAULT_PROJECT_CONFIG);
161-
162-
process.env.NODE_CONFIG_DIR = path.resolve(DEFAULT_PROJECT_ROOT, "config_pipe");
161+
config.configDir = path.resolve(DEFAULT_PROJECT_ROOT, "config_pipe");
163162

164163
SwaggerRunner.create(config, function(err, runner) {
165164
if (err) { return done(err); }
@@ -181,8 +180,116 @@ describe('index', function() {
181180
done();
182181
});
183182
});
184-
185183
});
184+
185+
it('should use pipe interface when _router.controllersInterface is set to `auto` and operation.length is 2', function(done) {
186+
var config = _.clone(DEFAULT_PROJECT_CONFIG);
187+
config.configDir = path.resolve(DEFAULT_PROJECT_ROOT, "config_auto");
188+
189+
SwaggerRunner.create(config, function(err, runner) {
190+
if (err) { return done(err); }
191+
runner.config.swagger.bagpipes.should.have.property('swagger_controllers');
192+
193+
var app = require('connect')();
194+
runner.connectMiddleware().register(app);
195+
196+
var request = require('supertest');
197+
198+
request(app)
199+
.get('/controller_interface_auto_detected_as_pipe')
200+
.set('Accept', 'application/json')
201+
.expect(200)
202+
.expect('Content-Type', /json/)
203+
.expect('x-interface', /pipe/)
204+
.end(function(err, res) {
205+
should.not.exist(err, err && err.stack);
206+
res.body.should.eql({ interface: "pipe" });
207+
done();
208+
});
209+
});
210+
});
211+
212+
it('should use middleware interface when _router.controllersInterface is set to `auto` and operation.length is 3', function(done) {
213+
var config = _.clone(DEFAULT_PROJECT_CONFIG);
214+
config.configDir = path.resolve(DEFAULT_PROJECT_ROOT, "config_auto");
215+
216+
SwaggerRunner.create(config, function(err, runner) {
217+
if (err) { return done(err); }
218+
runner.config.swagger.bagpipes.should.have.property('swagger_controllers');
219+
220+
var app = require('connect')();
221+
runner.connectMiddleware().register(app);
222+
223+
var request = require('supertest');
224+
225+
request(app)
226+
.get('/controller_interface_auto_detected_as_middleware')
227+
.set('Accept', 'application/json')
228+
.expect(200)
229+
.expect('Content-Type', /json/)
230+
.expect('x-interface', /middleware/)
231+
.end(function(err, res) {
232+
should.not.exist(err, err && err.stack);
233+
res.body.should.eql({ interface: "middleware" });
234+
done();
235+
});
236+
});
237+
});
238+
239+
it('should use adhere to cascading directgive `x-interface-type` found on path', function(done) {
240+
var config = _.clone(DEFAULT_PROJECT_CONFIG);
241+
config.configDir = path.resolve(DEFAULT_PROJECT_ROOT, "config_auto");
242+
243+
SwaggerRunner.create(config, function(err, runner) {
244+
if (err) { return done(err); }
245+
runner.config.swagger.bagpipes.should.have.property('swagger_controllers');
246+
247+
var app = require('connect')();
248+
runner.connectMiddleware().register(app);
249+
250+
var request = require('supertest');
251+
252+
request(app)
253+
.get('/controller_interface_on_path_cascades')
254+
.set('Accept', 'application/json')
255+
.expect(200)
256+
.expect('Content-Type', /json/)
257+
.expect('x-interface', /pipe/)
258+
.end(function(err, res) {
259+
should.not.exist(err, err && err.stack);
260+
res.body.should.eql({ interface: "pipe" });
261+
done();
262+
});
263+
});
264+
});
265+
266+
it('should use adhere to cascading directgive `x-interface-type` found on operation over one found on path', function(done) {
267+
var config = _.clone(DEFAULT_PROJECT_CONFIG);
268+
config.configDir = path.resolve(DEFAULT_PROJECT_ROOT, "config_auto");
269+
270+
SwaggerRunner.create(config, function(err, runner) {
271+
if (err) { return done(err); }
272+
runner.config.swagger.bagpipes.should.have.property('swagger_controllers');
273+
274+
var app = require('connect')();
275+
runner.connectMiddleware().register(app);
276+
277+
var request = require('supertest');
278+
279+
request(app)
280+
.get('/controller_interface_on_operation_cascades')
281+
.set('Accept', 'application/json')
282+
.expect(200)
283+
.expect('Content-Type', /json/)
284+
.expect('x-interface', /middleware/)
285+
.end(function(err, res) {
286+
should.not.exist(err, err && err.stack);
287+
res.body.should.eql({ interface: "middleware" });
288+
done();
289+
});
290+
});
291+
});
292+
186293

187294
it('should fail without callback', function() {
188295
(function() { SwaggerRunner.create(DEFAULT_PROJECT_CONFIG) }).should.throw('callback is required');

0 commit comments

Comments
 (0)