Skip to content

Commit

Permalink
Updated version
Browse files Browse the repository at this point in the history
  • Loading branch information
teone committed Mar 21, 2016
2 parents a57d9ab + 572e020 commit 3a2d0fb
Show file tree
Hide file tree
Showing 16 changed files with 372 additions and 74 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"generators": true
},
"env": {
"browser": true,
"node": true,
"es6": true
},
Expand All @@ -26,7 +25,8 @@
"object-shorthand": [0, "never"],
"wrap-iife": [2, "any"],
"no-loop-func": 0,
"no-console": 0
"no-console": 0,
"padded-blocks": 0
},
"globals" :{
"require": true
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
npm-debug.log
local-mocks/
.idea/
43 changes: 19 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Minimum usage: `easy-mocker -c config.json -d folder/`
| `-c`* | `path/to/config.json` | Path to api definition |
| `-d`* | `path/to/mock-directory` | Path to mock folder |
| `-p` | `port` | Webserver port |
| `-u` | `null` | Return different file for different user (to be implemented) |
| `-u` | `null` | Return different models for different user |

_Options marked with * are mandatory.mv m_

Expand All @@ -28,20 +28,25 @@ Minimum usage: `easy-mocker -c config.json -d folder/`
This is a sample structure for a configuration file:

```
[
{
"url": "users",
"base": "api/",
"methods": ["GET"],
"param": "id"
{
"auth": {
"headerField": "x-randomField" //optional (default to `x-userid`)
},
{
"url": "posts",
"base": "api/",
"methods": ["GET", "POST"],
"param": "id"
}
]
"endpoints": [
{
"url": "users",
"base": "api/",
"methods": ["GET"],
"param": "id"
},
{
"url": "posts",
"base": "api/",
"methods": ["GET", "POST"],
"param": "id"
}
]
}
```

That will generate the following endpoints:
Expand All @@ -59,13 +64,3 @@ You can provide data to be loaded as a starting point for your development serve

Examples for this files can be found in `spec/mocks`, anyway they are plain `JSON` arrays of objects.

## TODO

- Create and endpont (or command to restore the original data)
- Add a delay option to slow down responses
- How to handle similar endpoints (`api/user/devices`, `api/other/devices`)? Now thery'll look for the same definition file `devices.json`. Probably adding an optional `filename` to the `config` should work.
- Handle users
- Add config options to allow specify a header different form `x-userid` to identify user
- If `-u` is set create `login` and `logout` endpoint


13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easy-mocker",
"version": "1.2.1",
"version": "1.3.0",
"keywords": [
"server",
"cli",
Expand All @@ -20,12 +20,12 @@
},
"author": "Matteo Scandolo",
"homepage": "https://github.com/teone/easy-mocker",
"bugs": {
"url" : "https://github.com/teone/easy-mocker/issues"
"bugs": {
"url": "https://github.com/teone/easy-mocker/issues"
},
"repository" :{
"type" : "git",
"url" : "https://github.com/teone/easy-mocker"
"repository": {
"type": "git",
"url": "https://github.com/teone/easy-mocker"
},
"license": "MIT",
"dependencies": {
Expand All @@ -41,6 +41,7 @@
"eslint": "1.10.3",
"eslint-config-airbnb": "^5.0.1",
"mocha": "^2.4.5",
"mockery": "^1.4.0",
"nodemon": "^1.8.1",
"supertest": "^1.1.0"
}
Expand Down
23 changes: 22 additions & 1 deletion spec/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,41 @@
'use strict';

const request = require('supertest');
const app = require('../src/server');
const chai = require('chai');
const expect = chai.expect;
const mockery = require('mockery');
const path = require('path');
let memory;
let app;

describe('From the base config', () => {
beforeEach((done) => {
mockery.enable({
warnOnReplace: false,
warnOnUnregistered: false,
useCleanCache: true,
});
mockery.resetCache();
const configMock = {
user: false,
definitionFile: path.join(__dirname, './config/base.json'),
mockDir: path.join(__dirname, './mocks/base/'),
};
mockery.registerMock('./lib/config', configMock);
mockery.registerMock('./config', configMock);
app = require('../src/server');
memory = require('../src/lib/in_memory');
memory.setup()
.then(() => {
done();
});
});

afterEach(() => {
mockery.deregisterMock('./config');
mockery.disable();
});

describe('when quering items', () => {
it('should return an array', (done) => {
request(app)
Expand Down
2 changes: 1 addition & 1 deletion spec/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

describe('given a mocks folder', () => {
it('should fill the in-memory storage with base data', (done) => {
memory.loadBaseData(path.join(__dirname, './mocks/'))
memory.loadBaseData(path.join(__dirname, './mocks/base/'))
.then(() => {
expect(memory.memoryStorage).to.have.property('users').with.length(2);
expect(memory.memoryStorage).to.have.property('posts').with.length(0);
Expand Down
30 changes: 16 additions & 14 deletions spec/config/base.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
[
{
"url": "users",
"base": "api/",
"methods": ["GET", "POST", "DELETE"],
"param": "id"
},
{
"url": "posts",
"base": "api/",
"methods": ["GET", "POST"],
"param": "id"
}
]
{
"endpoints": [
{
"url": "users",
"base": "api/",
"methods": ["GET", "POST", "DELETE"],
"param": "id"
},
{
"url": "posts",
"base": "api/",
"methods": ["GET", "POST"],
"param": "id"
}
]
}
13 changes: 13 additions & 0 deletions spec/config/users_enabled.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"auth": {
"headerField": "x-randomField"
},
"endpoints": [
{
"url": "posts",
"base": "api/",
"methods": ["GET", "POST", "DELETE"],
"param": "id"
}
]
}
File renamed without changes.
22 changes: 22 additions & 0 deletions spec/mocks/users_enabled/posts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"id": 1,
"name": "Jhon Snow",
"user": 1
},
{
"id": 2,
"name": "Igritte",
"user": 1
},
{
"id": 3,
"name": "Tyrion Lannister",
"user": 2
},
{
"id": 4,
"name": "Brienne of Tarth",
"user": 2
}
]
Loading

0 comments on commit 3a2d0fb

Please sign in to comment.