Add API mocks

This commit is contained in:
Oleg Shuralev 2019-12-24 17:40:43 +03:00
parent 0dc6b07ca7
commit 15397cb459
No known key found for this signature in database
GPG key ID: 0459DF80E1A2FD1B
12 changed files with 5253 additions and 0 deletions

62
api/.gitignore vendored Normal file
View file

@ -0,0 +1,62 @@
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules
jspm_packages
# Optional npm cache directory
.npm
# Optional REPL history
.node_repl_history
# 0x
profile-*
# mac files
.DS_Store
# vim swap files
*.swp
# webstorm
.idea
# vscode
.vscode
*code-workspace
# clinic
profile*
*clinic*
*flamegraph*
# generated code
examples/typescript-server.js
test/types/index.js

28
api/app.js Normal file
View file

@ -0,0 +1,28 @@
'use strict'
const path = require('path')
const AutoLoad = require('fastify-autoload')
module.exports = function (fastify, opts, next) {
// Place here your custom code!
// Do not touch the following lines
// This loads all plugins defined in plugins
// those should be support plugins that are reused
// through your application
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'plugins'),
options: Object.assign({}, opts)
})
// This loads all plugins defined in services
// define your routes in one of these
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'services'),
options: Object.assign({}, opts)
})
// Make sure to call next when done
next()
}

4949
api/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

26
api/package.json Normal file
View file

@ -0,0 +1,26 @@
{
"name": "api",
"version": "1.0.0",
"description": "",
"main": "app.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "tap test/**/*.test.js",
"start": "fastify start --log-level info app.js",
"dev": "fastify start --log-level info --pretty-logs --watch --port 3004 app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"fastify": "^2.0.0",
"fastify-plugin": "^1.5.0",
"fastify-autoload": "^1.0.0",
"fastify-cli": "^1.3.0"
},
"devDependencies": {
"tap": "^12.5.3"
}
}

16
api/plugins/README.md Normal file
View file

@ -0,0 +1,16 @@
# Plugins Folder
Plugins define behavior that is common to all the routes in your
application. Authentication, caching, templates, and all the other cross
cutting concerns should be handled by plugins placed in this folder.
Files in this folder are typically defined through the
[`fastify-plugin`](https://github.com/fastify/fastify-plugin) module,
making them non-encapsulated. They can define decorators and set hooks
that will then be used in the rest of your application.
Check out:
* [The hitchhiker's guide to plugins](https://github.com/fastify/fastify/blob/master/docs/Plugins-Guide.md)
* [Fastify decorators](https://www.fastify.io/docs/latest/Decorators/).
* [Fastify lifecycle](https://www.fastify.io/docs/latest/Lifecycle/).

21
api/plugins/support.js Normal file
View file

@ -0,0 +1,21 @@
'use strict'
const fp = require('fastify-plugin')
// the use of fastify-plugin is required to be able
// to export the decorators to the outer scope
module.exports = fp(function (fastify, opts, next) {
fastify.decorate('someSupport', function () {
return 'hugs'
})
next()
})
// If you prefer async/await, use the following
//
// module.exports = fp(async function (fastify, opts) {
// fastify.decorate('someSupport', function () {
// return 'hugs'
// })
// })

28
api/services/clasters.js Normal file
View file

@ -0,0 +1,28 @@
'use strict'
module.exports = function (fastify, opts, next) {
fastify.get('/clusters', function (request, reply) {
reply.send([
{
clusterId: 'wrYGf-csNgiGdK7B_ADF7Z',
displayName: 'fake.cluster',
default: true,
status: 'online',
brokerCount: 1,
onlinePartitionCount: 20,
topicCount: 2,
},
{
clusterId: 'dMMQx-WRh77BKYas_g2ZTz',
displayName: 'kafka-ui.cluster',
default: false,
status: 'offline',
brokerCount: 0,
onlinePartitionCount: 0,
topicCount: 0,
},
]);
});
next();
}

9
api/services/root.js Normal file
View file

@ -0,0 +1,9 @@
'use strict'
module.exports = function (fastify, opts, next) {
fastify.get('/', function (request, reply) {
reply.send({ root: true });
});
next();
}

34
api/test/helper.js Normal file
View file

@ -0,0 +1,34 @@
'use strict'
// This file contains code that we reuse
// between our tests.
const Fastify = require('fastify')
const fp = require('fastify-plugin')
const App = require('../app')
// Fill in this config with all the configurations
// needed for testing the application
function config () {
return {}
}
// automatically build and tear down our instance
function build (t) {
const app = Fastify()
// fastify-plugin ensures that all decorators
// are exposed for testing purposes, this is
// different from the production setup
app.register(fp(App), config())
// tear down our app after we are done
t.tearDown(app.close.bind(app))
return app
}
module.exports = {
config,
build
}

View file

@ -0,0 +1,26 @@
'use strict'
const { test } = require('tap')
const Fastify = require('fastify')
const Support = require('../../plugins/support')
test('support works standalone', (t) => {
t.plan(2)
const fastify = Fastify()
fastify.register(Support)
fastify.ready((err) => {
t.error(err)
t.equal(fastify.someSupport(), 'hugs')
})
})
// If you prefer async/await, use the following
//
// test('support works standalone', async (t) => {
// const fastify = Fastify()
// fastify.register(Support)
//
// await fastify.ready()
// t.equal(fastify.someSupport(), 'hugs')
// })

View file

@ -0,0 +1,27 @@
'use strict'
const { test } = require('tap')
const { build } = require('../helper')
test('example is loaded', (t) => {
t.plan(2)
const app = build(t)
app.inject({
url: '/example'
}, (err, res) => {
t.error(err)
t.equal(res.payload, 'this is an example')
})
})
// If you prefer async/await, use the following
//
// test('example is loaded', async (t) => {
// const app = build(t)
//
// const res = await app.inject({
// url: '/example'
// })
// t.equal(res.payload, 'this is an example')
// })

View file

@ -0,0 +1,27 @@
'use strict'
const { test } = require('tap')
const { build } = require('../helper')
test('default root route', (t) => {
t.plan(2)
const app = build(t)
app.inject({
url: '/'
}, (err, res) => {
t.error(err)
t.deepEqual(JSON.parse(res.payload), { root: true })
})
})
// If you prefer async/await, use the following
//
// test('default root route', async (t) => {
// const app = build(t)
//
// const res = await app.inject({
// url: '/'
// })
// t.deepEqual(JSON.parse(res.payload), { root: true })
// })