MockServer(API First)-构建独立的本地服务模拟环境

MockServer 可以帮助前端开发人员快速建立服务端的模拟环境;通过模拟环境,前端可以实施独立、稳定的测试,以尽早发现问题,并且在前后端集成前就解决掉。

  1. 要想模拟环境发挥出最大效用,首先得把 API 设计好,即:API First Strategy(API 优先策略);
  2. 一旦 API 设计完毕,则前后端可以并行开发;
  3. 假如你的 API 数据格式一致的话,比如都是 json,使用 mockserver 那是很方便的;
  4. 作者 James D Bloom;
    本文涉及到的三个软件包源码都托管在 Github:mockserver、mockserver-client-node、mockserver-grunt;@ Google +;
安装 node 和 npm

我们使用 node 和 npm 来安装、使用 mockserver,请首先安装好 node 和 npm;

安装 mockserver-grunt 包

npm install mockserver-grunt --save-dev

  • 安装这个包的时候,jar 包也会自动下载到 node_modules/mockserver-grunt 下;

    mockserver-netty-3.10.4-jar-with-dependencies.jar

  • java 运行这个 jar 包即启动了 mockserver;
安装 mockserver-client 包

npm install mockserver-client --save-dev

配置 MockServer(start-mockserver.js)
/* start-mockserver.js */
var mockserver = require('mockserver-grunt');
mockserver.start_mockserver({
    serverPort: 2080,
    proxyPort: 2090,
    verbose: true
});
  • 本地 serverPort 端口起一个 模拟 WebServer;
  • 本地 proxyPort 端口起一个代理服务器,这样可以直接透明代理一些 API 请求到远程服务器(因为通常并不需要在本地模拟所有 API 接口);
启动 MockServer
  • node start-mockserver.js 即可启动 mockserver;

java -Dfile.encoding=UTF-8 -Dmockserver.logLevel=WARN -jar node_modules/mockserver-grunt/mockserver-netty-3.10.1-jar-with-dependencies.jar -serverPort 2080 -proxyPort 2090

准备模拟接口数据
/* 准备模拟接口数据:demo-my-mock-responses.js */
/* 导入模拟接口数据 の 示例代码:node demo-my-mock-responses.js */
var mockServer = require('mockserver-client'),
    mockServerClient = mockServer.mockServerClient,
    proxyClient = mockServer.proxyClient;

var local = mockServerClient('localhost', 2080),
    localProxy = proxyClient('localhost', 2090);

local.reset();

local.mockSimpleResponse('/api/demo', {name:'value'}, 203);

local.mockAnyResponse({
  httpRequest: {
    method: 'GET',
    path: '/index.php',
    queryStringParameters: [
      { 'name': 'r', 'values': ['j'] },
      {
        'name': 'username',
        'values': ['michael']
      },
    ],
  },

  'httpResponse': {
    'statusCode': 200,
    'body': JSON.stringify({"username": "michael"}),
    'delay': {
      'timeUnit': 'MILLISECONDS',
      'value': 250
    }
  },
  'times': {
    'remainingTimes': 1,
    'unlimited':true
  }
});
  • expectation A:一个 mockSimpleResponse 就是一个 Expectation;
  • expectation B:一个 mockAnyResponse 也是一个 Expectation;
  • 从 API 角度看,每个 mockAnyResponse 就对应模拟了一个 API,当然可以使用正则;
  • 熟悉 Nginx 的,可以对照 mockAnyResponse 为一个 Location 的寻址规则;
导入模拟接口数据

node demo-my-mock-responses.js

使用本地模拟环境示例小结
  1. java 运行 mockserver;
  2. node demo-my-mock-responses.js
    node 执行 demo-my-mock-responses.js:
    注意:mock responses js 文件修改后,要重新 node 导入;
  3. curl 访问:curl -v -X GET 'http://localhost:2080/index.php?r=j&username=michael'

OK!到这里,你已经搞定了本文示例!

将每个 mockAnyResponse 独立成一个文件便于维护
  • my-mock-responses.js 主文件;
  • expes.mock/a08-r-city-list-forward.js 是一个独立的 mockAnyResponse;
  • 前后端共同设计出 API 接口;
  • 前端人员将每个接口实现为 mockAnyResponse 文件;
  • 前后端开发人员、测试开发人员都会阅读该 js 文件;
  • a08-r-city-list.md 文件继续保留;
参考
  • https://en.wikipedia.org/wiki/MockServer
  • http://blog.csdn.net/wxqee/article/details/50165581
  • http://apievangelist.com/2014/08/11/what-is-an-api-first-strategy-adding-some-dimensions-to-this-new-question/
  • http://www.programmableweb.com/news/how-to-design-great-apis-api-first-design-and-raml/how-to/2015/07/10
  • https://www.smartfile.com/blog/3-benefits-of-api-first-design/
单独安装 MockServer 的方法

MockServer 可以不依赖于 node 环境而单独运行,方法如下:

  • 下载 jar 包:mockserver-netty-3.10.4-jar-with-dependencies.jar ;
    wget http://search.maven.org/remotecontent?filepath=org/mock-server/mockserver-netty/3.10.4/mockserver-netty-3.10.4-jar-with-dependencies.jar
  • java 运行 MockServer;
    java -Dfile.encoding=UTF-8 -Dmockserver.logLevel=WARN -jar mockserver-netty-3.10.1-jar-with-dependencies.jar -serverPort 2080 -proxyPort 2090
  • 测试 MockServer 是否启动成功?
    使用 nc、telnet 等命令测试端口是否连通:nc -z -w 1 localhost 2080
  • 安装 java 运行环境
    如果没有 java,请安装 yum install java;当下 1.8 版本;
使用 grunt 启动 mockserver 的方法

mockserver-grunt 是 Grunt 的一个插件(plugin),除了可以使用 node 启动 mockserver 外,还可以通过 grunt 来运行 mockserver;
如果你使用 gulp 构建前端,当然就没有必要使用 grunt 了;

npm 包 mockserver-client 和 mockserver-grunt 的 安装日志
[git@i-rylroi1n wbs]$ npm i
npm WARN peerDependencies The peer dependency grunt@~0.4 included from mockserver-client will no
npm WARN peerDependencies longer be automatically installed to fulfill the peerDependency 
npm WARN peerDependencies in npm 3+. Your application will need to depend on it explicitly.
npm WARN peerDependencies The peer dependency grunt@~0.4 included from mockserver-grunt will no
npm WARN peerDependencies longer be automatically installed to fulfill the peerDependency 
npm WARN peerDependencies in npm 3+. Your application will need to depend on it explicitly.
npm WARN deprecated [email protected]: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated [email protected]: graceful-fs v3.0.0 and before will fail on node releases >= v7.0. Please update to graceful-fs@^4.0.0 as soon as possible. Use 'npm ls graceful-fs' to find it in the tree.
npm WARN deprecated [email protected]: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
[email protected] node_modules/grunt
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected])
└── [email protected] ([email protected], [email protected])

[email protected] node_modules/mockserver-client
└── [email protected] ([email protected], [email protected], [email protected])

[email protected] node_modules/mockserver-grunt
├── [email protected] ([email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])

你可能感兴趣的:(MockServer(API First)-构建独立的本地服务模拟环境)