Skip to content
This repository was archived by the owner on Mar 28, 2022. It is now read-only.

Commit 734e83e

Browse files
authored
Merge pull request #6 from mocks-server/v1.2.0
V1.2.0
2 parents cc8deeb + 78f5d00 commit 734e83e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2404
-271
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [To be deprecated]
8-
- Remove "/features" api path.
8+
- Remove deprecated "/mocks" api path.
99

1010
## [unreleased]
1111
### Added
1212
### Changed
1313
### Fixed
1414
### Removed
1515

16+
## [1.2.0] - 2019-12-24
17+
### Added
18+
- Add "adminApiPath" option, which allows to change the new API path ("/admin").
19+
- Add "adminApiDeprecatedPaths" option, which allows to disable the deprecated API path ("/mocks").
20+
- Add new api resources under "/admin" path.
21+
1622
## [1.1.0] - 2019-12-07
1723
### Changed
1824
- Upgrade "@mocks-server" core dependency. Use new "path" option.

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,30 @@ This is __very useful when running acceptance tests, as you can change the behav
1515

1616
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].
1717

18+
## Options
19+
20+
* `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.
21+
* `adminApiDeprecatedPaths` - Boolean option, disables deprecated "/mocks" api path, which is still enabled by default.
22+
23+
Read more about [how to define options for the mocks-server plugins here](https://www.mocks-server.org/docs/configuration-options).
24+
1825
## API Resources
1926

2027
Available api resources are:
2128

22-
* `GET` `/mocks/behaviors/current` Returns current behavior.
23-
* `PUT` `/mocks/behaviors/current` Set current behavior.
24-
* Request body example: `{ "name": "behavior-name" }`
25-
* `GET` `/mocks/settings` Returns current server settings.
26-
* Response body example: `{ "delay": 0 }`
27-
* `PUT` `/mocks/settings` Changes current server settings.
29+
* `GET` `/admin/about` Returns plugin information.
30+
* Response body example: `{ "version": "1.2.0" }`
31+
* `GET` `/admin/behaviors` Returns behaviors collection.
32+
* `GET` `/admin/behaviors/:name` Returns an specific behavior.
33+
* `GET` `/admin/fixtures` Returns fixtures collection.
34+
* `GET` `/admin/fixtures/:id` Returns an specific fixture.
35+
* `GET` `/admin/settings` Returns current server settings.
36+
* Response body example: `{ "delay": 0, behavior: "foo-behavior", path: "mocks" }`
37+
* `PATCH` `/admin/settings` Changes current server settings.
2838
* Request body example: `{ "delay": 3000 }`
2939

40+
> Deprecated api resources under "/mocks" api path are still available.
41+
3042
## Contributing
3143

3244
Contributors are welcome.

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ Unless required by applicable law or agreed to in writing, software distributed
1111

1212
"use strict";
1313

14-
const PluginAdminApi = require("./src/Api");
14+
const PluginAdminApi = require("./src/Plugin");
1515

1616
module.exports = PluginAdminApi;

jest.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ module.exports = {
2626

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

3130
// The test environment that will be used for testing
3231
testEnvironment: "node"

package-lock.json

Lines changed: 47 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mocks-server/plugin-admin-api",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "Plugin for Mocks Server. Provides a REST API for administrating settings, fixtures and behaviors",
55
"keywords": [
66
"mocks-server-plugin",
@@ -35,10 +35,12 @@
3535
"@mocks-server/core": "^1.1.0"
3636
},
3737
"dependencies": {
38+
"@mocks-server/admin-api-paths": "1.0.1",
39+
"@hapi/boom": "8.0.1",
3840
"express": "4.17.1"
3941
},
4042
"devDependencies": {
41-
"@mocks-server/core": "^1.1.0",
43+
"@mocks-server/core": "^1.2.0",
4244
"coveralls": "^3.0.7",
4345
"eslint": "6.6.0",
4446
"eslint-config-prettier": "6.5.0",

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
sonar.organization=mocks-server
22
sonar.projectKey=mocks-server-plugin-admin-api
3-
sonar.projectVersion=1.1.0
3+
sonar.projectVersion=1.2.0
44

55
sonar.javascript.file.suffixes=.js
66
sonar.sourceEncoding=UTF-8

src/About.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2019 Javier Brea
3+
Copyright 2019 XbyOrange
4+
5+
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
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
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.
10+
*/
11+
12+
"use strict";
13+
14+
const express = require("express");
15+
16+
const { PLUGIN_NAME } = require("./constants");
17+
const { version } = require("../package.json");
18+
19+
class AboutApi {
20+
constructor(core) {
21+
this._core = core;
22+
this._tracer = core.tracer;
23+
this._fixtures = this._core.fixtures;
24+
this._router = express.Router();
25+
this._router.get("/", this.getAbout.bind(this));
26+
}
27+
28+
getAbout(req, res) {
29+
this._tracer.verbose(`${PLUGIN_NAME}: Sending about | ${req.id}`);
30+
res.status(200);
31+
res.send({
32+
version
33+
});
34+
}
35+
36+
get router() {
37+
return this._router;
38+
}
39+
}
40+
41+
module.exports = AboutApi;

src/Behaviors.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,48 @@ Unless required by applicable law or agreed to in writing, software distributed
1212
"use strict";
1313

1414
const express = require("express");
15+
const Boom = require("@hapi/boom");
16+
17+
const { PLUGIN_NAME } = require("./constants");
1518

1619
class BehaviorsApi {
1720
constructor(core) {
18-
this._tracer = core.tracer;
1921
this._core = core;
22+
this._tracer = core.tracer;
23+
this._behaviors = this._core.behaviors;
2024
this._router = express.Router();
2125
this._router.get("/", this.getCollection.bind(this));
22-
this._router.get("/current", this.getCurrent.bind(this));
23-
this._router.put("/current", this.putCurrent.bind(this));
26+
this._router.get("/:name", this.getModel.bind(this));
2427
}
2528

26-
getCurrent(req, res) {
27-
this._tracer.verbose(`Sending current behavior | ${req.id}`);
28-
res.status(200);
29-
res.send(this._core.behaviors.currentFromCollection);
29+
_parseModel(behavior) {
30+
return {
31+
name: behavior.name,
32+
fixtures: behavior.fixtures.map(fixture => fixture.id),
33+
extendedFrom: behavior.extendedFrom
34+
};
3035
}
3136

32-
putCurrent(req, res) {
33-
const newCurrent = req.body.name;
34-
this._tracer.verbose(`Changing current behavior to "${newCurrent}" | ${req.id}`);
35-
this._core.settings.set("behavior", newCurrent);
36-
this.getCurrent(req, res);
37+
_parseCollection() {
38+
return this._behaviors.collection.map(this._parseModel);
3739
}
3840

3941
getCollection(req, res) {
40-
this._tracer.verbose(`Sending behaviors collection | ${req.id}`);
42+
this._tracer.verbose(`${PLUGIN_NAME}: Sending behaviors | ${req.id}`);
4143
res.status(200);
42-
res.send(this._core.behaviors.collection);
44+
res.send(this._parseCollection());
45+
}
46+
47+
getModel(req, res, next) {
48+
const name = req.params.name;
49+
this._tracer.verbose(`${PLUGIN_NAME}: Sending behavior ${name} | ${req.id}`);
50+
const foundBehavior = this._behaviors.collection.find(behavior => behavior.name === name);
51+
if (foundBehavior) {
52+
res.status(200);
53+
res.send(this._parseModel(foundBehavior));
54+
} else {
55+
next(Boom.notFound(`Behavior with name "${name}" was not found`));
56+
}
4357
}
4458

4559
get router() {

0 commit comments

Comments
 (0)