1.interfake 是什么?

Interfake is a tool which allows developers of client-side applications of any platform to easily create dummy HTTP APIs to develop against.

Interfake 能简便地创建虚假的 HTTP API,Interfake 是 NodeJs 开发的,是一款开源工具。

2.使用 interfake

2-1.NodeJs install

安装步骤

2-2.interfake install

cd /data1/hugang
npm install interfake --save

2-3.创建虚假 HTTP API

在 interfake 路径下(在上一步安装中会显示,我的项目路径为/data1/hugang/node_modules/interfake),新建一个 js 文件:my-first-interfake.js(最好以自己要模拟的接口名命名)

var Interfake = require('interfake');
var interfake = new Interfake();
interfake.get('/my-first-interfake').status(200).body({ result: 'true'});
interfake.listen(30002);

解释如下:

new Interfake(options): creates an Interfake object. 

get('/my-first-interfake'), get请求,url为my-first-interfake

status(200): 状态码为200

body({ result: 'true'}):设置接口返回的JSON内容为{ result: 'true'}

interfake.listen(30002): 监听端口30001

执行 js

node my-first-interfake.js

访问该 fake 接口:

curl http://10.13.1.139:30002/my-first-interfake -w %{http_code}     
{
  "result": "true"
}200

2-4.部分高级用法:

1.设置请求参数

query(queryParameters):

interfake.get('/my-first-interfake?id=1').query({id:2}).status(200).body({ result: 'true'});

/my-first-interfake?id=2 才生效, /my-first-interfake?id=1 不生效

[root@10 tmp]# curl http://10.13.1.139:30002/my-first-interfake
Cannot GET /my-first-interfake
[root@10 tmp]# 
[root@10 tmp]# curl http://10.13.1.139:30002/my-first-interfake?id=1
Cannot GET /my-first-interfake?id=1
[root@10 tmp]# 
[root@10 tmp]# curl http://10.13.1.139:30002/my-first-interfake?id=2
{
  "result": "true"
}[root@10 tmp]# 
2.设置延时

delay(milliseconds)

有 2 种格式

2.1 固定时间

延迟 1s

interfake.get('/my-first-interfake').status(200).body({ result: 'true'}).delay(1000);

2.2 在一定时间范围内

延迟 1s-2s 间

interfake.get('/my-first-interfake').status(200).body({ result: 'true'}).delay('1000..2000');
3.设置响应头信息

设置头 Date 为 2015

interfake.get('/my-first-interfake').status(200).body({ result: 'true'}).responseHeaders({Date: '2015'})

设置前接口返回的头信息, Date: Tue, 16 Feb 2016 06:54:41 GMT

[root@10 tmp]# curl http://10.13.1.139:30002/my-first-interfake -v
* About to connect() to 10.13.1.139 port 30002 (#0)
*   Trying 10.13.1.139... connected
* Connected to 10.13.1.139 (10.13.1.139) port 30002 (#0)
> GET /my-first-interfake HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 10.13.1.139:30002
> Accept: */*
> 
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET,PUT,POST,DELETE
< Access-Control-Allow-Headers: Content-Type
< Content-Type: application/json
< Date: Tue, 16 Feb 2016 06:54:41 GMT
< Connection: keep-alive
< Content-Length: 22
< 
{
  "result": "true"
* Connection #0 to host 10.13.1.139 left intact
* Closing connection #0
}[root@10 tmp]# 

设置头 Date 为 2015 后,返回 Date: 2015

[root@10 tmp]# curl http://10.13.1.139:30002/my-first-interfake -v
* About to connect() to 10.13.1.139 port 30002 (#0)
*   Trying 10.13.1.139... connected
* Connected to 10.13.1.139 (10.13.1.139) port 30002 (#0)
> GET /my-first-interfake HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 10.13.1.139:30002
> Accept: */*
> 
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET,PUT,POST,DELETE
< Access-Control-Allow-Headers: Content-Type
< Content-Type: application/json
< Date: 2015
< Connection: keep-alive
< Content-Length: 22
< 
{
  "result": "true"
* Connection #0 to host 10.13.1.139 left intact
* Closing connection #0
}
4.HTTP 请求的不同方式

get|post|put|patch|delete(url)

声明为 post 接口

my-second-interfake.js

var Interfake = require('interfake');
var interfake = new Interfake();
interfake.post('/my-second-interfake').status(200).body({ result: 'false'})
interfake.listen(30003);

执行 NodeJs

node my-second-interfake.js

只能以 post 方式请求接口

[root@10 tmp]# curl http://10.13.1.139:30003/my-second-interfake 
Cannot GET /my-second-interfake
[root@10 tmp]# 
[root@10 tmp]# 
[root@10 tmp]# 
[root@10 tmp]# curl http://10.13.1.139:30003/my-second-interfake -X POST
{
  "result": "false"
}
5.代理

proxy(url|options)

代理接口和原接口必须 http 请求方式一致

my-second-interfake.js 将 http 请求改成与代理接口一致的 get 方式

interfake.get('/my-second-interfake').status(200).body({ result: 'false'}).proxy('http://10.13.1.139:30002/my-first-interfake')

返回的结果是调用http://10.13.1.139:30002/my-first-interfake的结果

curl http://10.13.1.139:30003/my-second-interfake
{
  "result": "true"
}

wiki: https://github.com/basicallydan/interfake


↙↙↙阅读原文可查看相关链接,并与作者交流