@ohos-ports/emulator 0.1.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +80 -0
- package/bin/emulator +21 -0
- package/index.js +1 -0
- package/lib/emulator.js +35 -0
- package/lib/routes.js +73 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# Emulator
|
|
2
|
+
|
|
3
|
+
A simple server for emulating an API.
|
|
4
|
+
|
|
5
|
+
Install through npm:
|
|
6
|
+
|
|
7
|
+
npm install -g emulator
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
### API response simulation
|
|
12
|
+
|
|
13
|
+
Emulator is a simple server that allows for rapid prototyping of client-side applications.
|
|
14
|
+
|
|
15
|
+
Make any request:
|
|
16
|
+
|
|
17
|
+
http://localhost:3000/profile?user=me
|
|
18
|
+
|
|
19
|
+
$.ajax('http://localhost:3000/profile', {
|
|
20
|
+
data: {user: 'me'}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
And get the same response data:
|
|
24
|
+
|
|
25
|
+
{user: 'me'}
|
|
26
|
+
|
|
27
|
+
Or specify a different response:
|
|
28
|
+
|
|
29
|
+
http://localhost:3000/profile?user=me&response[user]=you
|
|
30
|
+
|
|
31
|
+
$.ajax('http://localhost:3000/profile', {
|
|
32
|
+
data: {
|
|
33
|
+
user: 'me',
|
|
34
|
+
response: {
|
|
35
|
+
user: 'you'
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
And get the response you specified:
|
|
41
|
+
|
|
42
|
+
{user: 'you'}
|
|
43
|
+
|
|
44
|
+
### Other routes
|
|
45
|
+
|
|
46
|
+
Emulator also includes other helper routes that may be useful in your application.
|
|
47
|
+
|
|
48
|
+
#### Redirects
|
|
49
|
+
|
|
50
|
+
Redirect to a different url:
|
|
51
|
+
|
|
52
|
+
http://localhost:3000/redirect/me
|
|
53
|
+
|
|
54
|
+
http://localhost:3000/redirect?redirect=me
|
|
55
|
+
|
|
56
|
+
These routes both redirect to `http://localhost:3000/me`
|
|
57
|
+
|
|
58
|
+
#### Simulate high latentcy or long response times
|
|
59
|
+
|
|
60
|
+
Force the server to simulate long response times:
|
|
61
|
+
|
|
62
|
+
http://localhost:3000/wait/4000?user=me
|
|
63
|
+
|
|
64
|
+
This request would wait 4 seconds and then respond with:
|
|
65
|
+
|
|
66
|
+
{user: me}
|
|
67
|
+
|
|
68
|
+
#### Test error codes
|
|
69
|
+
|
|
70
|
+
Test how your application would respond to error codes:
|
|
71
|
+
|
|
72
|
+
http://localhost:3000/500
|
|
73
|
+
|
|
74
|
+
http://localhost:3000/404
|
|
75
|
+
|
|
76
|
+
Responds with a 500 (Internal Server Error) code and a 404 (Not Found) code, respectively
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
MIT
|
package/bin/emulator
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Module dependencies.
|
|
5
|
+
*/
|
|
6
|
+
var path = require('path'),
|
|
7
|
+
fs = require('fs'),
|
|
8
|
+
program = require('commander');
|
|
9
|
+
|
|
10
|
+
program
|
|
11
|
+
.version(require('../package.json').version)
|
|
12
|
+
.usage('[options]')
|
|
13
|
+
.option('-p, --port', 'specify a port')
|
|
14
|
+
.option('-C, --cors', 'disable cross-origin resource sharing')
|
|
15
|
+
.option('-R, --redirect', 'disable redirect route')
|
|
16
|
+
.option('-W, --wait', 'disable wait route')
|
|
17
|
+
.option('-S, --status', 'disable http status code route')
|
|
18
|
+
.parse(process.argv);
|
|
19
|
+
|
|
20
|
+
var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');
|
|
21
|
+
require(lib + '/emulator')(program);
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./lib/emulator')();
|
package/lib/emulator.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
var http = require('http'),
|
|
2
|
+
router = require('router')()
|
|
3
|
+
routes = require('./routes');
|
|
4
|
+
|
|
5
|
+
module.exports = function(config) {
|
|
6
|
+
config || (config = {});
|
|
7
|
+
var port = config.port || 3000;
|
|
8
|
+
|
|
9
|
+
router.get('/favicon.icon', function(req, res) {
|
|
10
|
+
res.writeHead(200, {'Content-Type': 'image/x-icon'} );
|
|
11
|
+
res.end();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
if (!config.cors) router.all('*', routes.crossdomain);
|
|
15
|
+
if (!config.redirect) router.all(/^\/redirect\/*(\S+)*/, routes.redirect);
|
|
16
|
+
if (!config.wait) router.all('/wait/:time', routes.wait);
|
|
17
|
+
if (!config.status) router.all(/(10[012]|20[0-8]|226|30[0-8]|4[01][\d]|42[02-689]|431|44[49]|45[01]|49[4-79]|50[\d]|51[01]|522|59[89])/, routes.statusCode);
|
|
18
|
+
|
|
19
|
+
router.all('*', routes.response);
|
|
20
|
+
|
|
21
|
+
http.createServer(function(req, res) {
|
|
22
|
+
router(req, res, function(err) {
|
|
23
|
+
if (err) {
|
|
24
|
+
res.statusCode = 500;
|
|
25
|
+
res.end(err.message);
|
|
26
|
+
} else {
|
|
27
|
+
res.statusCode = 404;
|
|
28
|
+
res.end();
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}).listen(port, function() {
|
|
32
|
+
console.log('Emulator running on port '+ port);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
package/lib/routes.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
var url = require('url'),
|
|
2
|
+
qs = require('qs');
|
|
3
|
+
|
|
4
|
+
var routes = {}
|
|
5
|
+
|
|
6
|
+
routes.crossdomain = function(req, res, next) {
|
|
7
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
8
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
|
|
9
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
10
|
+
if (req.method == 'OPTIONS') return res.end();
|
|
11
|
+
next();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
routes.statusCode = function(req, res){
|
|
15
|
+
var code = req.params[0];
|
|
16
|
+
res.writeHead(code);
|
|
17
|
+
res.end();
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
routes.redirect = function(req, res) {
|
|
21
|
+
var route = req.params[0];
|
|
22
|
+
|
|
23
|
+
var fn = function(data) {
|
|
24
|
+
var redirect = data.redirect || '/';
|
|
25
|
+
if (!redirect.match(/https?:\/\//)) {
|
|
26
|
+
redirect = (redirect[0] == '/') ? redirect : '/' + redirect;
|
|
27
|
+
redirect = 'http://' + req.headers.host + redirect;
|
|
28
|
+
}
|
|
29
|
+
res.writeHead(302, {
|
|
30
|
+
'Location' : redirect
|
|
31
|
+
});
|
|
32
|
+
res.end();
|
|
33
|
+
}
|
|
34
|
+
if (route) {
|
|
35
|
+
fn({redirect: route})
|
|
36
|
+
} else {
|
|
37
|
+
parseData(req, fn)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
routes.wait = function(req, res) {
|
|
42
|
+
var wait = req.params.time;
|
|
43
|
+
setTimeout(function() {
|
|
44
|
+
routes.response(req, res);
|
|
45
|
+
}, wait);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
routes.response = function(req, res) {
|
|
49
|
+
parseData(req, function(data) {
|
|
50
|
+
res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'});
|
|
51
|
+
var responseBody = data.response || data;
|
|
52
|
+
res.write(JSON.stringify(responseBody));
|
|
53
|
+
res.end();
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function parseData(req, fn) {
|
|
58
|
+
var queryData = ''
|
|
59
|
+
if (req.method == 'GET') {
|
|
60
|
+
queryData = qs.parse(url.parse(req.url).query);
|
|
61
|
+
fn(queryData);
|
|
62
|
+
} else {
|
|
63
|
+
req.on('data', function(data) {
|
|
64
|
+
queryData += data;
|
|
65
|
+
})
|
|
66
|
+
req.on('end', function() {
|
|
67
|
+
queryData = qs.parse(queryData);
|
|
68
|
+
fn(queryData);
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
module.exports = routes;
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ohos-ports/emulator",
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "A simple server for emulating an API",
|
|
5
|
+
"author": "Josh Vermaire <joshvermaire@gmail.com>",
|
|
6
|
+
"preferGlobal": true,
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/joshvermaire/emulator"
|
|
10
|
+
},
|
|
11
|
+
"main": "index.js",
|
|
12
|
+
"bin": {
|
|
13
|
+
"emulator": "./bin/emulator"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js",
|
|
17
|
+
"bin/",
|
|
18
|
+
"lib/",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"keywords": [
|
|
22
|
+
"prototyping",
|
|
23
|
+
"api",
|
|
24
|
+
"response",
|
|
25
|
+
"error"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"router": "^1.0.0",
|
|
29
|
+
"qs": "^6.0.0",
|
|
30
|
+
"commander": "^2.0.0"
|
|
31
|
+
},
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/joshvermaire/emulator/issues"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">= 0.6.x"
|
|
39
|
+
}
|
|
40
|
+
}
|