Skip to content

Commit

Permalink
Merge pull request #49 from smollweide/develop
Browse files Browse the repository at this point in the history
v0.16.0
  • Loading branch information
smollweide authored Jul 9, 2017
2 parents ac76327 + 7af8e7c commit a6481b0
Show file tree
Hide file tree
Showing 38 changed files with 631 additions and 108 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules
/tmp
npm-debug.log
/func-imported
package-lock.json
11 changes: 7 additions & 4 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/demo/rest
demo/rest
/demo/func
demo/func
/demo/func-imported
demo/func-imported
/demo/func2
demo/func2
/demo/public
demo/public
/demo/rest
demo/rest
/test
test
/doc
Expand All @@ -14,7 +18,6 @@ doc
src/swagger
/tmp
tmp
/demo/func-imported
demo/func-imported
npm-debug.log
.eslintrc
package-lock.json
6 changes: 6 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var responseFuncPath = __dirname + '/func-imported';
mockServer({
restPath: dest,
dirName: __dirname,
uiPath: '/',
funcPath: [
__dirname + '/func',
__dirname + '/func2',
Expand All @@ -32,6 +33,11 @@ mockServer({
return 'success';
}
},
expressMiddleware: [
function (express) {
return ['/public', express.static(__dirname + '/public')];
}
],
swaggerImport: {
protocol: 'http',
authUser: undefined,
Expand Down
1 change: 1 addition & 0 deletions demo/public/js/javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hello world');
31 changes: 31 additions & 0 deletions demo/rest/products/#{productCode}#static/GET/.store.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"validation": {
"example.png": {
"counter": 0
},
"success": {
"counter": 0
},
"document.txt": {
"counter": 0
},
"example.gif": {
"counter": 0
},
"example.jpg": {
"counter": 0
},
"example.xml": {
"counter": 0
},
"javascript.js": {
"counter": 0
},
"success.json": {
"counter": 0
},
"styles.css": {
"counter": 0
}
}
}
24 changes: 24 additions & 0 deletions demo/rest/products/#{productCode}#static/GET/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"desc": "Returns product image",
"security": [],
"protected": false,
"status": "open",
"request": {
"uri": {
"parameters": [
{
"name": "productCode",
"required": true,
"type": "string",
"desc": "Product identifier"
}
]
}
},
"response": {
"statusCode": 200,
"schema": {
"type": "application/json"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hi
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
120 changes: 120 additions & 0 deletions demo/rest/products/#{productCode}#static/GET/mock/example.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.</description>
</book>
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-09-10</publish_date>
<description>The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.</description>
</book>
<book id="bk106">
<author>Randall, Cynthia</author>
<title>Lover Birds</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-09-02</publish_date>
<description>When Carla meets Paul at an ornithology
conference, tempers fly as feathers get ruffled.</description>
</book>
<book id="bk107">
<author>Thurman, Paula</author>
<title>Splish Splash</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-11-02</publish_date>
<description>A deep sea diver finds true love twenty
thousand leagues beneath the sea.</description>
</book>
<book id="bk108">
<author>Knorr, Stefan</author>
<title>Creepy Crawlies</title>
<genre>Horror</genre>
<price>4.95</price>
<publish_date>2000-12-06</publish_date>
<description>An anthology of horror stories about roaches,
centipedes, scorpions and other insects.</description>
</book>
<book id="bk109">
<author>Kress, Peter</author>
<title>Paradox Lost</title>
<genre>Science Fiction</genre>
<price>6.95</price>
<publish_date>2000-11-02</publish_date>
<description>After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.</description>
</book>
<book id="bk110">
<author>O'Brien, Tim</author>
<title>Microsoft .NET: The Programming Bible</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-09</publish_date>
<description>Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.</description>
</book>
<book id="bk111">
<author>O'Brien, Tim</author>
<title>MSXML3: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-01</publish_date>
<description>The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.</description>
</book>
<book id="bk112">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
<description>Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.</description>
</book>
</catalog>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hello world');
3 changes: 3 additions & 0 deletions demo/rest/products/#{productCode}#static/GET/mock/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.a-button {
border: 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions demo/rest/products/#{productCode}/GET/.store.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
},
"deep-validation": {
"counter": 0
},
"deep-validation.json": {
"counter": 1
}
}
}
2 changes: 1 addition & 1 deletion doc/readme-collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# Collections

Collections allow you to store your current selected responses and share it with your team members.
For that you can use the UI or the both [CLI commands](/readme.md#L113).
For that you can use the UI or the both [CLI commands](/readme.md#L121).
25 changes: 25 additions & 0 deletions doc/readme-express-middleware.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Express Middleware

For static content delivery beside the mock response itself, you can define express middleware.
Read more in [express documentation](http://expressjs.com/en/4x/api.html#app.use).

Each entry needs to be a function witch returns the `app.use` arguments.

```
expressMiddleware: [
function (express) {
return ['/public', express.static(__dirname + '/public')];
},
function (express) {
return ['/dist', express.static(__dirname + '/dist')];
}
],
```

Express result:
```
app.use('/public', express.static(__dirname + '/public'));
app.use('/dist', express.static(__dirname + '/dist'));
```

[example](/demo/index.js#L37)
17 changes: 4 additions & 13 deletions doc/readme-folder-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ The _#_ represents a slash in the url path of your response. In other words, the
If you want to use dynamic path segments, you can enclose them in curly brackets, as shown above for the _#path#{param}_ folder. The node-mock-server looks for a success response called success-1 if the requested url is /path/1, success-2 is for /path/2 etc., and success-default is for all other cases. For examples how the actual value can be used inside responses, look at the success files for _products_ [in the demo](https://github.com/smollweide/node-mock-server/blob/master/demo/rest/products/%23%7BproductCode%7D/GET/mock).

### Method folders
A path folder with _#_ must contain one or more method folder, e.g. _GET_ or _POST_ which require to contain a _mock_ directory in turn - that directory will later contain the actual API responses.
A path folder with _#_ must contain one or more method folders, e.g. _GET_ or _POST_ which require to contain a _mock_ directory in turn - that directory will later contain the actual API responses.

### Description of Response
The file _desc.json_ is optional and can be used to describe the response to a method call on a resource in more detail. Consider the sample _desc.json_ below.
```
{
Expand Down Expand Up @@ -62,16 +63,6 @@ The file _desc.json_ is optional and can be used to describe the response to a m
```
The example above provides a descriptive string in the `desc` attribute and specifies expected requests and possible responses.

The files _request_schema.json_ and _response_schema.json_ are optional and may contain json schema definitions for your requests and responses which allow for validation.

## Using the node-mock-server UI

Once you have defined a resource path with a method folder containing a mock directory, you can continue to work with the new resource in the UI at http://localhost:3003 (or at the port you have defined via options). Hit F5 in the UI to see new resources after adding them in the file system.

Click the GET button which appears after the /foo resource. A popup appears that shows the possible responses and allows to add new responses. The node-mock-server can respond through a middleware, so *middleware* is always available.
### JSON Schema for Request and Response

Hit *+Add* to add a new response and call it `success`. Your favorite JSON editor is fired up where you can enter the response.

After storing the response, you need to select the radiobutton in front of your new *success* response. A file _response.txt_ will be created alongside your possible responses which stores your decision.

You can also add an 'error' response and use the radiobutton to define whether the resource should respond with a success or error response. For an error response the default HTTP status code is 500, but you can specify a different status code by suffixing the name of your error response with the appropriate status code, like error-401.
The files _request_schema.json_ and _response_schema.json_ are optional and may contain json schema definitions for your requests and responses which allow for validation.
2 changes: 1 addition & 1 deletion doc/readme-middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
For validation and self controlled responses you can select middleware as response.
The middleware function will be called, in case of "middleware" is selected. It's response can be a string which will continue with the given string as selected response. If you return null or undefined you have to implement the response by yourself.

[example](/demo/index.js#L22)
[example](/demo/index.js#L23)

## Parameter[0] serverOptions

Expand Down
18 changes: 18 additions & 0 deletions doc/readme-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ Default value: `3001`

A number value that defines the application port.

#### options.uiPath
Type: `string`
Default value: `/`

A string value that defines the path for the node-mock-server UI.

#### options.privateKey
Type: `String`

Expand Down Expand Up @@ -104,6 +110,18 @@ Optional
A object including the middleware functions.
Read [middleware.md](/doc/readme-middleware.md) for details.

#### options.expressMiddleware
Type: `Array<Function<Array<path: string, callback: Function>>>`
Optional

A array of functions that returns the express `app.use` arguments.
Read [express middleware documentation](http://expressjs.com/en/api.html#app.use) for details.
examples:
* `expressMiddleware: [ function () { return ['/public', express.static('/public')]; } ]`
* `expressMiddleware: [ function () { return ['/public', function (req, res, next) {}]; } ]`
* `expressMiddleware: [ function () { return [function (req, res, next) {}] } ]`
* `expressMiddleware: [ function () { return function (req, res, next) {}; } ]`

#### options.swaggerImport
Type: `Object`
Optional
Expand Down
19 changes: 19 additions & 0 deletions doc/readme-ui-documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Using the node-mock-server UI

The mock server UI is available at _http://localhost:3003_ (or at the port you have defined via options).

If the service you want to mock provides a Swagger definition and you have provided a swagger definition, you may perform an import via the small triangle button in the head of the UI page.

In the same location you also find general preferences such as a response delay and you can trigger a validation of all responses.

In addition to Swagger imports, you may also [define a resource path with a ReST method folder containing a mock directory](https://github.com/smollweide/node-mock-server/blob/master/doc/readme-folder-structure.md) manually by creating the appropriate folders in the file system. Once a resource has been defined that way, you can continue to work with the resource in the UI. Refresh the browser to see new resources after you have added them in the file system.

The UI shows you a list of known resources. Alongside each resource there are buttons which allow you to work with the responses for that method.

Click the button for a ReST method (e.g. GET) to define possible responses for that method. A popup appears that shows the possible responses and allows to add new responses. Initially there are no `success` and `error` responses defined, therefore you only see a `middleware` response. The node-mock-server can respond through a [middleware definition](https://github.com/smollweide/node-mock-server/blob/master/doc/readme-middleware.md), so *middleware* is always available.

Hit *+Add* to add a new response and call it `success`. Your favorite JSON editor is fired up where you can enter the response. It will be stored as _success.json_ inside the _mock_ directory.

After storing the response you can select the radiobutton in front of your new `success` response. A file _response.txt_ will be created alongside your possible responses, which stores your decision. All decisions can be captured, restored, reset and shared by means of a [collection](https://github.com/smollweide/node-mock-server/blob/master/doc/readme-collections.md).

You can also add an `error` response and use the radiobutton in front of it to specify that the resource should respond with an error response. For an error response the default HTTP status code is 500, but you can specify a different status code by suffixing the name of your error response with the appropriate status code, like error-401.
4 changes: 3 additions & 1 deletion lib/UserInterface.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

'use strict';

var mime = require('mime-types');
var Utils = require('./Utils.js');
var extend = require('util')._extend;
var ignoreInRestRoot = require('./constants/ignore-in-rest-root');
Expand Down Expand Up @@ -466,7 +467,8 @@ UserInterface.prototype = extend(UserInterface.prototype, {
}

mockItem.isSelected = (selected === nameMockItem);
mockItem.name = mockItem.file.replace('.json', '');
mockItem.name = mockItem.file;
mockItem.mime = mime.lookup(mockItem.file);
mockItem.isValidated = (validationResult !== undefined);

if (mockItem.isValidated) {
Expand Down
Loading

0 comments on commit a6481b0

Please sign in to comment.