How to use createMiddleware method in apimocker

Best JavaScript code snippet using apimocker

createMiddleware-test.js

Source:createMiddleware-test.js Github

copy

Full Screen

...37 const engine = { save: sinon.stub().resolves() };38 const store = { getState: sinon.spy() };39 const next = sinon.spy();40 const action = { type: 'dummy' };41 createMiddleware(engine)(store)(next)(action);42 next.should.have.been.calledWith(action);43 });44 it('should return the result of next', () => {45 const engine = { save: sinon.stub().resolves() };46 const store = { getState: sinon.spy() };47 const next = sinon.stub().returns('nextResult');48 const action = { type: 'dummy' };49 const result = createMiddleware(engine)(store)(next)(action);50 result.should.equal('nextResult');51 });52 it('should ignore blacklisted actions', () => {53 const engine = { save: sinon.spy() };54 const store = {};55 const next = sinon.spy();56 const action = { type: 'IGNORE_ME' };57 createMiddleware(engine, ['IGNORE_ME'])(store)(next)(action);58 engine.save.should.not.have.been.called;59 });60 it('should ignore non-whitelisted actions', () => {61 const engine = { save: sinon.spy() };62 const store = {};63 const next = sinon.spy();64 const action = { type: 'IGNORE_ME' };65 createMiddleware(engine, [], ['ALLOWED'])(store)(next)(action);66 engine.save.should.not.have.been.called;67 });68 it('should process whitelisted actions', () => {69 const engine = { save: sinon.stub().resolves() };70 const store = { getState: sinon.spy() };71 const next = sinon.spy();72 const action = { type: 'ALLOWED' };73 createMiddleware(engine, [], ['ALLOWED'])(store)(next)(action);74 engine.save.should.have.been.called;75 });76 it('should allow whitelist function', () => {77 const engine = { save: sinon.stub().resolves() };78 const store = { getState: sinon.spy() };79 const next = sinon.spy();80 const action = { type: 'ALLOWED' };81 const whitelistFn = () => true;82 createMiddleware(engine, [], whitelistFn)(store)(next)(action);83 engine.save.should.have.been.called;84 });85 it('should ignore actions if the whitelist function returns false', () => {86 const engine = { save: sinon.stub().resolves() };87 const store = { getState: sinon.spy() };88 const next = sinon.spy();89 const action = { type: 'ALLOWED' };90 const whitelistFn = () => false;91 createMiddleware(engine, [], whitelistFn)(store)(next)(action);92 engine.save.should.not.have.been.called;93 });94 it('should pass the whole action to the whitelist function', (done) => {95 const engine = { save: sinon.stub().resolves() };96 const store = { getState: sinon.spy() };97 const next = sinon.spy();98 const action = { type: 'ALLOWED' };99 const whitelistFn = (checkAction) => {100 checkAction.should.deep.equal(action);101 done();102 };103 createMiddleware(engine, [], whitelistFn)(store)(next)(action);104 });105 describeConsoleWarnInNonProduction(106 'should not process functions',107 () => {108 const engine = { save: sinon.stub().resolves() };109 const store = { getState: sinon.spy() };110 const next = sinon.spy();111 const action = () => {};112 createMiddleware(engine)(store)(next)(action);113 engine.save.should.not.have.been.called;114 },115 (msg) => {116 msg.should.contain('ACTION IGNORED!');117 msg.should.contain('but received a function');118 }119 );120 describeConsoleWarnInNonProduction(121 'should not process strings',122 () => {123 const engine = { save: sinon.stub().resolves() };124 const store = { getState: sinon.spy() };125 const next = sinon.spy();126 const action = 'haha';127 createMiddleware(engine)(store)(next)(action);128 engine.save.should.not.have.been.called;129 },130 (msg) => {131 msg.should.contain('ACTION IGNORED!');132 msg.should.contain('but received: haha');133 }134 );135 describeConsoleWarnInNonProduction(136 'should not process objects without a type',137 () => {138 const engine = { save: sinon.stub().resolves() };139 const store = { getState: sinon.spy() };140 const next = sinon.spy();141 const action = { noType: 'damn it' };142 createMiddleware(engine)(store)(next)(action);143 engine.save.should.not.have.been.called;144 },145 (msg) => {146 msg.should.contain('ACTION IGNORED!');147 msg.should.contain('objects should have a type property');148 }149 );150 describeConsoleWarnInNonProduction(151 'should warn about action on both black- and whitelist',152 () => {153 const engine = {};154 createMiddleware(engine, ['A'], ['A']);155 }156 );157 it('should pass the current state to engine.save', () => {158 const engine = { save: sinon.stub().resolves() };159 const state = { x: 42 };160 const store = { getState: sinon.stub().returns(state) };161 const next = sinon.spy();162 const action = { type: 'dummy' };163 createMiddleware(engine)(store)(next)(action);164 engine.save.should.have.been.calledWith(state);165 });166 it('should trigger a SAVE action after engine.save', (done) => {167 const engine = { save: sinon.stub().resolves() };168 const state = { x: 42 };169 const store = {170 getState: sinon.stub().returns(state),171 dispatch: sinon.spy()172 };173 const next = sinon.spy();174 const action = { type: 'dummy' };175 createMiddleware(engine)(store)(next)(action);176 setTimeout(() => {177 const saveAction = { payload: state, type: SAVE };178 store.dispatch.should.have.been.calledWith(saveAction);179 done();180 }, 5);181 });182 it('should add the parent action as meta.origin to the saveAction', (done) => {183 process.env.NODE_ENV = 'develop';184 const engine = { save: sinon.stub().resolves() };185 const state = { x: 42 };186 const store = {187 getState: sinon.stub().returns(state),188 dispatch: sinon.spy()189 };190 const next = sinon.spy();191 const action = { type: 'dummy' };192 createMiddleware(engine)(store)(next)(action);193 setTimeout(() => {194 const saveAction = { payload: state, type: SAVE, meta: { origin: action } };195 store.dispatch.should.have.been.calledWith(saveAction);196 done();197 }, 5);198 });199 it('should do nothing if engine.save fails', () => {200 const engine = { save: sinon.stub().rejects() };201 const store = { getState: sinon.spy() };202 const next = sinon.spy();203 const action = { type: 'dummy' };204 createMiddleware(engine)(store)(next)(action);205 });206 it('should always ignore SAVE action', () => {207 const engine = { save: sinon.spy() };208 const store = {};209 const next = sinon.spy();210 const action = { type: SAVE };211 createMiddleware(engine)(store)(next)(action);212 engine.save.should.not.have.been.called;213 });214 it('should always ignore LOAD action', () => {215 const engine = { save: sinon.spy() };216 const store = {};217 const next = sinon.spy();218 const action = { type: LOAD };219 createMiddleware(engine)(store)(next)(action);220 engine.save.should.not.have.been.called;221 });...

Full Screen

Full Screen

index.js

Source:index.js Github

copy

Full Screen

...17 * @param {*} opts 18 */19export default function (app, router, opts) {20 //Menus21 router.get('/authorizations/menus', createMiddleware(Menu.findMenuOfCurrentUser));2223 //Operations24 router.get('/authorizations/operations', createMiddleware(Operation.findOperationOfCurrentUser));2526 //Organizations27 router.get('/authorizations/organizations', createMiddleware(Organization.findOrg));28 router.get('/authorizations/assigned_organizations', createMiddleware(Organization.findOrganizationsAssignToSelf));29 router.post('/authorizations/organizations', createMiddleware(Organization.createOrg));30 router.put('/authorizations/organizations/:id', createMiddleware(Organization.updateOrg));31 router.delete('/authorizations/organizations/:id?', createMiddleware(Organization.deleteOrg));3233 router.get('/authorizations/organizations/:organizationId/roles', createMiddleware(Organization.findRoleAssignToOrg));34 router.get('/authorizations/organizations/:organizationId/not_assigned/roles', createMiddleware(Organization.findRoleNotAssignToOrg));35 router.post('/authorizations/organizations/:organizationId/roles', createMiddleware(Organization.assignRoleToOrg));36 router.delete('/authorizations/organizations/:organizationId/roles/:id?', createMiddleware(Organization.removeRoleFromOrg)); // record id or roleId?3738 router.get('/authorizations/organizations/:organizationId/users', createMiddleware(Organization.findUserInOrg));39 router.get('/authorizations/organizations/:organizationId/not_in/users', createMiddleware(Organization.findUserNotInOrg));40 router.post('/authorizations/organizations/:organizationId/users', createMiddleware(Organization.addUserToOrg));41 router.delete('/authorizations/organizations/:organizationId/users/:id?', createMiddleware(Organization.removeUseFromOrg));4243 //Positions 44 router.get('/authorizations/positions', createMiddleware(Position.findPosition));45 router.get('/authorizations/assigned_positions', createMiddleware(Position.findPositionsAssignToSelf));46 router.post('/authorizations/positions', createMiddleware(Position.createPosition));47 router.put('/authorizations/positions/:id', createMiddleware(Position.updatePosition));48 router.delete('/authorizations/positions/:id?', createMiddleware(Position.deletePosition));4950 router.get('/authorizations/positions/:positionId/roles', createMiddleware(Position.findRoleAssignToPosition));51 router.get('/authorizations/positions/:positionId/not_assigned/roles', createMiddleware(Position.findRoleNotAssignToPosition));52 router.post('/authorizations/positions/:positionId/roles', createMiddleware(Position.assignRoleToPosition));53 router.delete('/authorizations/positions/:positionId/roles/:id?', createMiddleware(Position.removeRoleFromPosition));5455 router.get('/authorizations/positions/:positionId/users', createMiddleware(Position.findUserInPosition));56 router.get('/authorizations/positions/:positionId/not_in/users', createMiddleware(Position.findUserNotInPosition));57 router.post('/authorizations/positions/:positionId/users', createMiddleware(Position.addUserToPosition));58 router.delete('/authorizations/positions/:positionId/users/:id?', createMiddleware(Position.removeUseFromPosition));5960 //Roles61 router.get('/authorizations/roles', createMiddleware(Role.findRole));62 router.get('/authorizations/assigned_roles', createMiddleware(Role.findRolesAssignToSelf));63 router.post('/authorizations/roles', createMiddleware(Role.createRole));64 router.put('/authorizations/roles/:id', createMiddleware(Role.updateRole));65 router.delete('/authorizations/roles/:id?', createMiddleware(Role.deleteRole));6667 router.get('/authorizations/roles/:roleId/menus', createMiddleware(Role.findMenuOfRole));68 router.post('/authorizations/roles/:roleId/menus', createMiddleware(Role.assignMenuToRole));69 router.delete('/authorizations/roles/:roleId/menus/:id?', createMiddleware(Role.removeMenuFromRole));70 router.get('/authorizations/roles/:roleId/operations', createMiddleware(Role.findOperationOfRole));71 router.post('/authorizations/roles/:roleId/operations', createMiddleware(Role.assignOperationToRole));72 router.delete('/authorizations/roles/:roleId/operations/:id?', createMiddleware(Role.removeOperationFromRole));73 router.get('/authorizations/roles/:roleId/organizations', createMiddleware(Role.findOrgBindToRole));74 router.get('/authorizations/roles/:roleId/not_bind/organizations', createMiddleware(Role.findOrgNotBindToRole));75 //@see Organization.assignRoleToOrg76 router.post('/authorizations/roles/:roleId/organizations', createMiddleware(Role.bindRoleToOrg));77 //@see Organization.removeRoleFromeOrg78 router.delete('/authorizations/roles/:roleId/organizations/:id?', createMiddleware(Role.unbindRoleAndOrg));7980 router.get('/authorizations/roles/:roleId/positions', createMiddleware(Role.findPositionBindToRole));81 router.get('/authorizations/roles/:roleId/not_bind/positions', createMiddleware(Role.findPositionNotBindToRole));82 //@see Position.asignRoleToPosition83 router.post('/authorizations/roles/:roleId/positions', createMiddleware(Role.bindRoleToPosition));84 //@see Position.removeRoleFromePosition85 router.delete('/authorizations/roles/:roleId/positions/:id?', createMiddleware(Role.unbindRoleAndPosition));8687 router.get('/authorizations/roles/:roleId/users', createMiddleware(Role.findUserBindToRole));88 router.get('/authorizations/roles/:roleId/not_bind/users', createMiddleware(Role.findUserNotBindToRole));89 router.post('/authorizations/roles/:roleId/users', createMiddleware(Role.bindRoleToUser));90 router.delete('/authorizations/roles/:roleId/users/:id?', createMiddleware(Role.unbindRoleAndUser));9192 //Users93 router.get('/authorizations/users/:id?', createMiddleware(User.findUser));94 router.post('/authorizations/users', createMiddleware(User.createUser));95 router.put('/authorizations/users/:id', createMiddleware(User.updateUser));96 router.delete('/authorizations/users/:id', createMiddleware(User.deleteUser));97 router.post('/authorizations/users/enable/:id?', createMiddleware(User.enable));98 router.post('/authorizations/users/disable/:id?', createMiddleware(User.disable));99 //Extention100 router.get('/authorizations/users/:id/extention', createMiddleware(User.findUserExtention));101 router.put('/authorizations/users/:id/extention', createMiddleware(User.updateUserExtention));102103104 router.get('/authorizations/users/:userId/roles', createMiddleware(User.findRoleAssignToUser))105 router.get('/authorizations/users/:userId/not_assigned/roles', createMiddleware(User.findRoleNotAssignToUser))106 //@see Role.bindRoleToUser107 router.post('/authorizations/users/:userId/roles', createMiddleware(User.bindRoleToUser))108 //@see Role.unbindRoleAndUser109 router.delete('/authorizations/users/:userId/roles/:id?', createMiddleware(User.unbindRoleAndUser))110111 router.get('/authorizations/users/:userId/organizations', createMiddleware(User.findOrgAssignToUser))112 router.get('/authorizations/users/:userId/not_assigned/organizations', createMiddleware(User.findOrgNotAssignToUser))113 router.post('/authorizations/users/:userId/organizations', createMiddleware(User.bindUserToOrg))114 router.delete('/authorizations/users/:userId/organizations/:id?', createMiddleware(User.unbindUserAndOrg))115116 router.get('/authorizations/users/:userId/positions', createMiddleware(User.findPositionAssignToUser))117 router.get('/authorizations/users/:userId/not_assigned/positions', createMiddleware(User.findPositionNotAssignToUser))118 router.post('/authorizations/users/:userId/positions', createMiddleware(User.bindUserToPosition))119 router.delete('/authorizations/users/:userId/positions/:id?', createMiddleware(User.unbindUserAndPosition))120121 router.get('/authorizations/users/:userId/menus', createMiddleware(User.findMenuOfUser))122 router.get('/authorizations/users/:userId/operations', createMiddleware(User.findOperationOfUser))123124 // 125 router.get('/organizations/:organizationId/users', createMiddleware(OrganizationResource.findUsersOfOrganization)) ...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var apimocker = require('apimocker');2var path = require('path');3var express = require('express');4var app = express();5var apiMocker = apimocker.createMiddleware(path.join(__dirname, 'config'), path.join(__dirname, 'mocks'));6app.use(apiMocker);7app.listen(3000);8console.log('Listening on port 3000');9{10 "config": {11 {12 }13 }14}15 {16 },17 {18 }

Full Screen

Using AI Code Generation

copy

Full Screen

1var apiMocker = require('apimocker');2var express = require('express');3var app = express();4app.use(apiMocker('/api', 'mocks/api'));5app.listen(3000);6{7 "scripts": {8 },9}10Content-Type: application/json; charset=utf-811{12 "scripts": {13 },14}

Full Screen

Using AI Code Generation

copy

Full Screen

1var createMiddleware = require('apimocker').createMiddleware;2var app = require('express')();3app.use(createMiddleware({4 defaultResponseHeaders: {5 }6}));7app.listen(3000);8var createServer = require('apimocker').createServer;9var server = createServer({10 defaultResponseHeaders: {11 }12});13server.listen(3000);14var createServer = require('apimocker').createServer;15var server = createServer({16 defaultResponseHeaders: {17 },18 responseHandler: function (req, res, next) {19 res.json({success: true, data: res.data});20 }21});22server.listen(3000);23var createServer = require('apimocker').createServer;24var server = createServer({25 defaultResponseHeaders: {26 },27 responseHandler: function (req, res

Full Screen

Using AI Code Generation

copy

Full Screen

1var apimocker = require('apimocker');2apimocker.createMiddleware({3});4var express = require('express');5var app = express();6app.use(apimocker.middleware);7app.listen(3000);8var http = require('http');9var server = http.createServer(apimocker.middleware);10server.listen(3000);11var connect = require('connect');12var app = connect();13app.use(apimocker.middleware);14http.createServer(app).listen(3000);15var koa = require('koa');16var app = koa();17app.use(apimocker.middleware);18app.listen(3000);19var restify = require('restify');20var server = restify.createServer();21server.use(apimocker.middleware);22server.listen(3000);23var hapi = require('hapi');24var server = new hapi.Server();25server.connection();26server.register({ register: apimocker.middleware }, function (err) {27 if (err) {28 console.log('Failed to load plugin:', err);29 }30});31server.start();32sails.hooks.http.app.use(apimocker.middleware);33var restana = require('restana');34var app = restana();35app.use(apimocker.middleware);36app.start(3000);37var express = require('express');38var app = express();39app.use(apimocker.middleware);40app.listen(3000);41var fastify = require('fastify');42var app = fastify();43app.use(apimocker.middleware);44app.listen(3000);45var micro = require('micro');46var app = micro();47app.use(apimocker.middleware);48app.listen(3000);49var polka = require('polka');50var app = polka();51app.use(apimocker.middleware);52app.listen(3000);

Full Screen

Using AI Code Generation

copy

Full Screen

1var apimocker = require('apimocker');2var express = require('express');3var app = express();4var middleware = apimocker.createMiddleware({5});6app.use(middleware);7app.listen(4000, function() {8 console.log('server started');9});

Full Screen

Using AI Code Generation

copy

Full Screen

1var apimocker = require('apimocker');2var path = require('path');3var app = require('express')();4var middleware = apimocker.createMiddleware({5 src: path.resolve(__dirname, './mocks'),6 proxy: path.resolve(__dirname, './proxy.json')7});8app.use(middleware);9app.listen(3000, function () {10});11module.exports = {12 response: function (req, res) {13 res.json([14 {id: 1, name: 'John Doe'},15 {id: 2, name: 'Jane Doe'}16 ]);17 }18};19{20 {21 }22}

Full Screen

Using AI Code Generation

copy

Full Screen

1const {createMiddleware} = require('apimocker');2const middleware = createMiddleware({3});4module.exports = middleware;5const middleware = require('./test');6const express = require('express');7const app = express();8app.use(middleware);9app.listen(3000, () => console.log('Server started on port 3000'));10{11 "request": {12 },13 "response": {14 {15 },16 {17 }18 }19}20 {21 },22 {23 }

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run apimocker automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful