Skip to content
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

Proposal: JavaScript API for the ability to run mocker on existing server endpoint #610

Closed
SuperOleg39 opened this issue Apr 19, 2021 · 6 comments · Fixed by #616
Closed

Comments

@SuperOleg39
Copy link

SuperOleg39 commented Apr 19, 2021

Motivation

Current JS / CLI mountebank API immediately create and start new http server.
It means, if we want to test one NodeJS micro-service, we need to create and deploy two containers - one with our service, and one with mountebank.
If we have a k8s infrastructure, many applications, and dynamic deployed stands with automated testing, just one additional container with mountebank for every application complicates deployment and take many k8s resources.

But what if we can create mountebank instance on existing NodeJS application, at new endpoint?

Our micro-service can on demand run integration with mountebank, where we create new express endpoint, /mountebank for example, and pass req and res objects to special mountebank JS API.
After, this micro-service can replace http requests base URL to mocked API to http://localhost:3000/mountebank.

For SSR NodeJS applications, on client-side, will be available a relative URL /mountebank (nuances with a gateway proxy are possible).

API overview

Few examples, how new API may looks like

Nested express.js routers

Inspired by this reciepe

const express = require('express')
const { createRouter } = require('mountebank')

const app = express();

const mountebankRouter = await createRouter(options)

app.use('/mountebank/*', mountebankRouter)

express.js middeware

const express = require('express')
const { createMiddleware } = require('mountebank')

const app = express();

const mountebankMiddleware = await createMiddleware(options)

app.use('/mountebank/*', mountebankMiddleware)

express.js application

Example for clean http NodeJS server

const http = require('http')
const { createApp } = require('mountebank')

const mountebankApp = await createApp(options)

const server = createServer((req, res) => {
  if (req.url.startsWith('/mountebank')) {
    mountebankApp(req, res);
  }
});

server.listen(port);

Implementation

If this feature is discussible, and don't break some mountebank core concepts, i can start from a simple demo branch, for this proposal.

@dzotovwork
Copy link

Awesome proposal !!!! 👍

@vchuchkalov
Copy link

Hi @bbyars. What do you say?

@bbyars
Copy link
Owner

bbyars commented Apr 22, 2021 via email

@SuperOleg39
Copy link
Author

Thank you for the recommendations!

Will start working on this soon)

@SuperOleg39
Copy link
Author

@bbyars , hi!

Create PR, but not sure about all changes:

  • defaults implementation seems to be unfortunate
  • listenServer method may be unnecessary

Will waiting review)

@SuperOleg39
Copy link
Author

Need to create some publicPath option for static assets and links on mountebank web interface, because current relative path always see to current domain.
I think, will be better to create another PR)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants