-
Notifications
You must be signed in to change notification settings - Fork 24
Add support for mocking services #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 12 commits
b419565
bba06c9
e254f78
2a35017
ec59917
be88f40
6f25a0a
b7b815d
4e42609
cf7cd71
73d4235
822be83
7b00fe0
d7379f5
2660153
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,7 +53,7 @@ function createLoader() { | |
| //if parent id is a consumer, i.e. was registered with a prefix, | ||
| //strip the prefix off since that's not part of the module name | ||
| parentId = parentId.indexOf('.') > -1 ? parentId.substring(parentId.indexOf('.') + 1) : parentId; | ||
|
|
||
|
||
| var mod = subRequire(id, parentId); | ||
| moduleMap[id] = mod; | ||
| dependencyMap[id] = normalizeServiceNames(di.getParamNames(mod.init)); | ||
|
|
@@ -486,11 +486,11 @@ function createLoader() { | |
| } | ||
|
|
||
| /* | ||
| * Strips the prefix + suffix underscores from the service name, allowing you to inject the service | ||
| * without having to think of an alternative name. For example, you can inject the logger service | ||
| * Strips the prefix + suffix underscores from the service name, allowing you to inject the service | ||
| * without having to think of an alternative name. For example, you can inject the logger service | ||
| * as "_logger_" and still be free to assign the service to the variable "logger". | ||
| * | ||
| * camelCamel strings are then coverted into dash-case. Based off of: | ||
| * | ||
| * camelCamel strings are then coverted into dash-case. Based off of: | ||
| * https://github.com/epeli/underscore.string/blob/master/dasherize.js | ||
| */ | ||
| function normalizeServiceName(str) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,15 @@ var modulePath = {}; //maintain a list of the paths where we resolved files - ne | |
| * but also provides a way to load submodules by passing the optional parentId argument. | ||
| */ | ||
| module.exports = function (id, parentId) { | ||
| // load mock if appropriate | ||
| if (global.__mockServices && | ||
| _.includes(global.__mockServices, path.basename(id, path.extname(id))) | ||
| ) { | ||
| var mockPath = path.resolve(global.__appDir, 'test', 'bos-mocks', 'services', path.basename(id)); | ||
| if (mockPath) { | ||
|
||
| id = mockPath; | ||
| } | ||
| } | ||
|
|
||
| if (id.indexOf('.js') > -1) { | ||
| return loadJsFile(id); | ||
|
|
@@ -108,4 +117,4 @@ function loadFromParent(id, parentId) { | |
| var path = _.findWhere(parentMod.children, {exports: mod}).id; | ||
| modulePath[id] = path; | ||
| return mod; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "express": { | ||
| "port": "5000" | ||
| }, | ||
|
|
||
| "cluster": { | ||
| "maxWorkers": 1 | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| * Copyright (c) 2015-2016 PointSource, LLC. | ||
| * MIT Licensed | ||
| */ | ||
| var service1; | ||
| var service2; | ||
| var serviceModule; | ||
| exports.init = function (logger, petService1, petService2, petServiceModule) { | ||
| service1 = petService1; | ||
| service2 = petService2; | ||
| serviceModule = petServiceModule; | ||
| }; | ||
|
|
||
| exports.getPets1 = function (req, res, next) { | ||
| res.send(service1.getPets()); | ||
| }; | ||
|
|
||
| exports.getPets2 = function (req, res, next) { | ||
| res.send(service2.getPets()); | ||
| }; | ||
|
|
||
| exports.getPets3 = function (req, res, next) { | ||
| res.send(serviceModule.getPets()); | ||
| }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /* | ||
| * Copyright (c) 2015-2016 PointSource, LLC. | ||
| * MIT Licensed | ||
| */ | ||
| exports.init = function(logger) { | ||
| logger.info('Pet Service1 initialized'); | ||
| }; | ||
|
|
||
| exports.getPets = function() { | ||
| return { | ||
| id: 1, | ||
| name: 'service1 pet' | ||
| }; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /* | ||
| * Copyright (c) 2015-2016 PointSource, LLC. | ||
| * MIT Licensed | ||
| */ | ||
| exports.init = function(logger) { | ||
| logger.info('Pet Service2 initialized'); | ||
| }; | ||
|
|
||
| exports.getPets = function() { | ||
| return { | ||
| id: 2, | ||
| name: 'service2 pet' | ||
| }; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| { | ||
| "swagger": "2.0", | ||
| "info": { | ||
| "version": "1.0.0", | ||
| "title": "Swagger Petstore", | ||
| "description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification", | ||
| "termsOfService": "http://helloreverb.com/terms/", | ||
| "contact": { | ||
| "name": "Wordnik API Team", | ||
| "email": "[email protected]", | ||
| "url": "http://madskristensen.net" | ||
| }, | ||
| "license": { | ||
| "name": "MIT", | ||
| "url": "http://github.com/gruntjs/grunt/blob/master/LICENSE-MIT" | ||
| } | ||
| }, | ||
| "host": "petstore.swagger.wordnik.com", | ||
| "basePath": "/api", | ||
| "schemes": [ | ||
| "http" | ||
| ], | ||
| "consumes": [ | ||
| "application/json" | ||
| ], | ||
| "produces": [ | ||
| "application/json" | ||
| ], | ||
| "paths": { | ||
| "/pets1": { | ||
| "get": { | ||
| "description": "Returns a user based on a single ID, if the user does not have access to the pet", | ||
| "operationId": "getPets1", | ||
| "produces": [ | ||
| "application/json", | ||
| "application/xml", | ||
| "text/xml", | ||
| "text/html" | ||
| ], | ||
| "responses": { | ||
| "200": { | ||
| "description": "pet response", | ||
| "schema": { | ||
| "$ref": "#/definitions/pet" | ||
| } | ||
| }, | ||
| "default": { | ||
| "description": "unexpected error", | ||
| "schema": { | ||
| "$ref": "#/definitions/errorModel" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "/pets2": { | ||
| "get": { | ||
| "description": "Returns a user based on a single ID, if the user does not have access to the pet", | ||
| "operationId": "getPets2", | ||
| "produces": [ | ||
| "application/json", | ||
| "application/xml", | ||
| "text/xml", | ||
| "text/html" | ||
| ], | ||
| "responses": { | ||
| "200": { | ||
| "description": "pet response", | ||
| "schema": { | ||
| "$ref": "#/definitions/pet" | ||
| } | ||
| }, | ||
| "default": { | ||
| "description": "unexpected error", | ||
| "schema": { | ||
| "$ref": "#/definitions/errorModel" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "/pets3": { | ||
| "get": { | ||
| "description": "Returns a user based on a single ID, if the user does not have access to the pet", | ||
| "operationId": "getPets3", | ||
| "produces": [ | ||
| "application/json", | ||
| "application/xml", | ||
| "text/xml", | ||
| "text/html" | ||
| ], | ||
| "responses": { | ||
| "200": { | ||
| "description": "pet response", | ||
| "schema": { | ||
| "$ref": "#/definitions/pet" | ||
| } | ||
| }, | ||
| "default": { | ||
| "description": "unexpected error", | ||
| "schema": { | ||
| "$ref": "#/definitions/errorModel" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "definitions": { | ||
| "pet": { | ||
| "required": [ | ||
| "id", | ||
| "name" | ||
| ], | ||
| "properties": { | ||
| "id": { | ||
| "type": "integer", | ||
| "format": "int64" | ||
| }, | ||
| "name": { | ||
| "type": "string" | ||
| }, | ||
| "tag": { | ||
| "type": "string" | ||
| } | ||
| } | ||
| }, | ||
| "newPet": { | ||
| "allOf": [ | ||
| { | ||
| "$ref": "#/definitions/pet" | ||
| }, | ||
| { | ||
| "required": [ | ||
| "name" | ||
| ], | ||
| "properties": { | ||
| "id": { | ||
| "type": "integer", | ||
| "format": "int64" | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| }, | ||
| "errorModel": { | ||
| "required": [ | ||
| "code", | ||
| "message" | ||
| ], | ||
| "properties": { | ||
| "code": { | ||
| "type": "integer", | ||
| "format": "int32" | ||
| }, | ||
| "message": { | ||
| "type": "string" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would:
work?
Then it can be expanded to support middleware in the future
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I like this better.