Skip to content

Commit

Permalink
Merge pull request #6 from mocks-server/v1.2.0
Browse files Browse the repository at this point in the history
V1.2.0
  • Loading branch information
javierbrea authored Dec 24, 2019
2 parents cc8deeb + 78f5d00 commit 734e83e
Show file tree
Hide file tree
Showing 45 changed files with 2,404 additions and 271 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [To be deprecated]
- Remove "/features" api path.
- Remove deprecated "/mocks" api path.

## [unreleased]
### Added
### Changed
### Fixed
### Removed

## [1.2.0] - 2019-12-24
### Added
- Add "adminApiPath" option, which allows to change the new API path ("/admin").
- Add "adminApiDeprecatedPaths" option, which allows to disable the deprecated API path ("/mocks").
- Add new api resources under "/admin" path.

## [1.1.0] - 2019-12-07
### Changed
- Upgrade "@mocks-server" core dependency. Use new "path" option.
Expand Down
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,30 @@ This is __very useful when running acceptance tests, as you can change the behav

This plugin is included in the [main distribution of the Mocks Server project][main-distribution-url], so you can refer to the [official documentation website][website-url].

## Options

* `adminApiPath` - Base path for the administration api. Default is "/admin". You should change it only if there is any conflict with the api you are mocking.
* `adminApiDeprecatedPaths` - Boolean option, disables deprecated "/mocks" api path, which is still enabled by default.

Read more about [how to define options for the mocks-server plugins here](https://www.mocks-server.org/docs/configuration-options).

## API Resources

Available api resources are:

* `GET` `/mocks/behaviors/current` Returns current behavior.
* `PUT` `/mocks/behaviors/current` Set current behavior.
* Request body example: `{ "name": "behavior-name" }`
* `GET` `/mocks/settings` Returns current server settings.
* Response body example: `{ "delay": 0 }`
* `PUT` `/mocks/settings` Changes current server settings.
* `GET` `/admin/about` Returns plugin information.
* Response body example: `{ "version": "1.2.0" }`
* `GET` `/admin/behaviors` Returns behaviors collection.
* `GET` `/admin/behaviors/:name` Returns an specific behavior.
* `GET` `/admin/fixtures` Returns fixtures collection.
* `GET` `/admin/fixtures/:id` Returns an specific fixture.
* `GET` `/admin/settings` Returns current server settings.
* Response body example: `{ "delay": 0, behavior: "foo-behavior", path: "mocks" }`
* `PATCH` `/admin/settings` Changes current server settings.
* Request body example: `{ "delay": 3000 }`

> Deprecated api resources under "/mocks" api path are still available.
## Contributing

Contributors are welcome.
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Unless required by applicable law or agreed to in writing, software distributed

"use strict";

const PluginAdminApi = require("./src/Api");
const PluginAdminApi = require("./src/Plugin");

module.exports = PluginAdminApi;
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ module.exports = {

// The glob patterns Jest uses to detect test files
testMatch: ["**/test/unit/**/?(*.)+(spec|test).js?(x)"],
//testMatch: ["**/test/unit/core/Plugins.spec.js"],

// The test environment that will be used for testing
testEnvironment: "node"
Expand Down
67 changes: 47 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mocks-server/plugin-admin-api",
"version": "1.1.0",
"version": "1.2.0",
"description": "Plugin for Mocks Server. Provides a REST API for administrating settings, fixtures and behaviors",
"keywords": [
"mocks-server-plugin",
Expand Down Expand Up @@ -35,10 +35,12 @@
"@mocks-server/core": "^1.1.0"
},
"dependencies": {
"@mocks-server/admin-api-paths": "1.0.1",
"@hapi/boom": "8.0.1",
"express": "4.17.1"
},
"devDependencies": {
"@mocks-server/core": "^1.1.0",
"@mocks-server/core": "^1.2.0",
"coveralls": "^3.0.7",
"eslint": "6.6.0",
"eslint-config-prettier": "6.5.0",
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
sonar.organization=mocks-server
sonar.projectKey=mocks-server-plugin-admin-api
sonar.projectVersion=1.1.0
sonar.projectVersion=1.2.0

sonar.javascript.file.suffixes=.js
sonar.sourceEncoding=UTF-8
Expand Down
41 changes: 41 additions & 0 deletions src/About.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2019 Javier Brea
Copyright 2019 XbyOrange
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

"use strict";

const express = require("express");

const { PLUGIN_NAME } = require("./constants");
const { version } = require("../package.json");

class AboutApi {
constructor(core) {
this._core = core;
this._tracer = core.tracer;
this._fixtures = this._core.fixtures;
this._router = express.Router();
this._router.get("/", this.getAbout.bind(this));
}

getAbout(req, res) {
this._tracer.verbose(`${PLUGIN_NAME}: Sending about | ${req.id}`);
res.status(200);
res.send({
version
});
}

get router() {
return this._router;
}
}

module.exports = AboutApi;
42 changes: 28 additions & 14 deletions src/Behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,48 @@ Unless required by applicable law or agreed to in writing, software distributed
"use strict";

const express = require("express");
const Boom = require("@hapi/boom");

const { PLUGIN_NAME } = require("./constants");

class BehaviorsApi {
constructor(core) {
this._tracer = core.tracer;
this._core = core;
this._tracer = core.tracer;
this._behaviors = this._core.behaviors;
this._router = express.Router();
this._router.get("/", this.getCollection.bind(this));
this._router.get("/current", this.getCurrent.bind(this));
this._router.put("/current", this.putCurrent.bind(this));
this._router.get("/:name", this.getModel.bind(this));
}

getCurrent(req, res) {
this._tracer.verbose(`Sending current behavior | ${req.id}`);
res.status(200);
res.send(this._core.behaviors.currentFromCollection);
_parseModel(behavior) {
return {
name: behavior.name,
fixtures: behavior.fixtures.map(fixture => fixture.id),
extendedFrom: behavior.extendedFrom
};
}

putCurrent(req, res) {
const newCurrent = req.body.name;
this._tracer.verbose(`Changing current behavior to "${newCurrent}" | ${req.id}`);
this._core.settings.set("behavior", newCurrent);
this.getCurrent(req, res);
_parseCollection() {
return this._behaviors.collection.map(this._parseModel);
}

getCollection(req, res) {
this._tracer.verbose(`Sending behaviors collection | ${req.id}`);
this._tracer.verbose(`${PLUGIN_NAME}: Sending behaviors | ${req.id}`);
res.status(200);
res.send(this._core.behaviors.collection);
res.send(this._parseCollection());
}

getModel(req, res, next) {
const name = req.params.name;
this._tracer.verbose(`${PLUGIN_NAME}: Sending behavior ${name} | ${req.id}`);
const foundBehavior = this._behaviors.collection.find(behavior => behavior.name === name);
if (foundBehavior) {
res.status(200);
res.send(this._parseModel(foundBehavior));
} else {
next(Boom.notFound(`Behavior with name "${name}" was not found`));
}
}

get router() {
Expand Down
Loading

0 comments on commit 734e83e

Please sign in to comment.