Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
89 changed files
with
19,709 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>MockNet</name> | ||
<comment>Project MockNet created by Buildship.</comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.buildship.core.gradleprojectbuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.buildship.core.gradleprojectnature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER) | ||
connection.project.dir= | ||
eclipse.preferences.version=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
## MockNet使用指南 | ||
|
||
### 框架介绍 | ||
通过 MockNet,不需要服务器相关知识就可以在 Android 本地创建服务器用于接口快速开发和网络测试。通过简单的代码就可以快速模拟后台返回数据,而不需要调整现有的网络接口。 | ||
|
||
### 效果演示 | ||
|
||
### 下载 | ||
|
||
### 使用方法 | ||
|
||
MockNet 中,对每一个请求及其响应称为一个 Connection,具体对应的类是 MockConnection | ||
|
||
#### 1. 初始化 | ||
``` java | ||
// 创建 MockNet | ||
MockNet mockNet = MockNet.create(); | ||
``` | ||
|
||
#### 2. 添加对请求的处理 | ||
|
||
* 通过 MockConnectionFactory 构建一个 MockConnection,createGeneralConnection(String url, String body) 会创建一个处理 GET 请求的 MockConnection,并返回 body,默认 Content-Type 是 "text/plain"。 | ||
|
||
``` java | ||
MockConnection conn = MockConnectionFactory.getInstance().createGeneralConnection("/*", "general connection"); | ||
mockNet.addConnection(conn); | ||
``` | ||
|
||
|
||
* 通过 MockConnectionFactory 构建一个 MockConnection,createGeneralConnection(String method, String url, String body) 会创建一个处理 method 指定的请求的 MockConnection,并返回 body,默认 Content-Type 是 "text/plain"。 | ||
|
||
``` java | ||
MockConnectio conn = MockConnectionFactory.getInstance().createGeneralConnection(MockConnection.POST, "/*", "general connection"); | ||
mockNet.addConnection(conn); | ||
``` | ||
|
||
* 通过 MockConnectionFactory 构建一个 MockConnection,createGeneralConnection(String method, String url, String body) 会创建一个处理 method 指定的请求的 MockConnection,并返回 body,默认 Content-Type 是 "text/plain"。 | ||
|
||
``` java | ||
MockConnection conn = MockConnectionFactory.getInstance().createGeneralConnection(MockConnection.POST, "/*", "general connection"); | ||
mockNet.addConnection(conn); | ||
``` | ||
|
||
* 添加返回 json 的请求处理 | ||
通过 MockConnectionFactory 构建一个 MockConnection,createGeneralConnection(String method, String url, String contentType, String body) 会创建一个处理 method 指定的请求的 MockConnection,并返回 body。 | ||
|
||
``` java | ||
MockConnection conn = MockConnectionFactory.getInstance().createGeneralConnection(MockConnection.POST, "/*", "text/json", "{'code':'success'}"); | ||
mockNet.addConnection(conn); | ||
|
||
``` | ||
|
||
* 自己构建 MockConnection | ||
通过 MockConnection.Builder 来创建 MockConnection,添加自定义的返回信息 | ||
|
||
``` java | ||
MockConnection conn = new MockConnection.Builder() | ||
.setMethod(MockConnection.GET) | ||
.setUrl("/test") | ||
.setResponseBody("text/json", "first test") | ||
.addResponseHeader("Content-Length", "" + "first test".length()) | ||
.addRequestHeader("Content-Length", "" + con.length) | ||
.setVerifyHeaders(true) | ||
.build(); | ||
mockNet.addConnection(conn); | ||
|
||
// 或者直接传入 builder | ||
MockConnection.Builder builder = new MockConnection.Builder() | ||
.setMethod(MockConnection.GET) | ||
.setUrl("/test") | ||
.setResponseBody("text/json", "first test") | ||
.addResponseHeader("Content-Length", "" + "first test".length()) | ||
.addRequestHeader("Content-Length", "" + con.length) | ||
.setVerifyHeaders(true); | ||
|
||
mockNet.addConnection(builder); | ||
``` | ||
|
||
#### 3. 启动服务 | ||
|
||
``` java | ||
mockNet.start(); | ||
``` | ||
|
||
#### 4. 关闭服务 | ||
|
||
``` java | ||
mockNet.stop(); | ||
``` | ||
|
||
#### 5. 链式调用 | ||
以上方法可以用链式调用来写,更方便一些 | ||
``` java | ||
|
||
MockNet mockNet = MockNet.create() | ||
.addConnection(MockConnectionFactory.getInstance() | ||
.createGeneralConnection("/*", "general connection")) | ||
.addConnection(MockConnectionFactory.getInstance() | ||
.createGeneralConnection(MockConnection.POST, "/*", "general connection")) | ||
.addConnection(MockConnectionFactory.getInstance() | ||
.createGeneralConnection(MockConnection.POST, "/*", "text/json", "{'code':'success'}")) | ||
.addConnection(new MockConnection.Builder() | ||
.setMethod(MockConnection.GET) | ||
.setUrl("/test") | ||
.setResponseBody("text/json", con, con.length) | ||
.addResponseHeader("Content-Length", "" + con.length) | ||
.addRequestHeader("Content-Length", "" + con.length) | ||
.setVerifyHeaders(true) | ||
) | ||
.start(); | ||
``` | ||
|
||
### 进阶使用 | ||
|
||
#### 1. 自定义 MockConnection 的使用 | ||
通过 MockConnection.Builder 生成 Builder,并通过 Builder 相关方法构建 MockConnection。Builder 常用的方法有: | ||
|
||
* setUrl(String url) | ||
设置请求 url | ||
|
||
* setMethod(String method) | ||
设置请求方法,参数选择有 MockConnection.GET,MockConnection.POST等 | ||
|
||
* setHttpVersion(String version) | ||
设置 http version | ||
|
||
* setResponseStatusCode(int code) | ||
设置响应状态码 例子: 200 | ||
|
||
* setResponseReasonPhrase(String phrase) | ||
设置响应原因 例: OK | ||
|
||
* setVerifyHeaders(boolean verify) | ||
设置是否验证请求头 | ||
|
||
* setRequestHeaders(Map<String, String> headers, boolean verifyHeaders) | ||
设置需要验证的请求头,verifyHeaders 为 true 时会开启请求头验证 | ||
|
||
* addRequestHeader(String name, String val) | ||
添加需要验证的请求头 | ||
|
||
* setVerifyParams(boolean verify) | ||
设置是否验证请求参数 | ||
|
||
* setRequestParams(Map<String, String> params, boolean verifyParams) | ||
设置需要验证的请求参数,verifyParams 为 true 时,会开启请求参数验证 | ||
|
||
* addRequestParam(String name, String val) | ||
添加需要验证的请求参数 | ||
|
||
* setResponseHeaders(Map<String, String> headers) | ||
设置响应头 | ||
|
||
* addResponseHeader(String name, String val) | ||
添加响应头 | ||
|
||
* addResponseHeader(Map<String, String> headers) | ||
添加响应头 | ||
|
||
|
||
* setResponseParams(Map<String, String> params) | ||
设置响应参数,以键值对的方式放在 body 中 | ||
|
||
* setResponseBody(String contentType, byte[] body, int len) | ||
设置响应 body | ||
|
||
* setResponseBody(String contentType, String body) | ||
设置响应 body | ||
|
||
* setResponseBody(String contentType, File file) | ||
设置响应 body | ||
|
||
* setBlockTime(int time) | ||
设置后台阻塞时间,可用于测试请求超时等情况 | ||
|
||
* isLog(boolean log) | ||
设置是否输出 log | ||
|
||
#### 2. 随机对请求做出响应 | ||
MockNet 内部通过 url 和 method(GET,POST等)来对请求做出区分,在 addConnection 时如果添加了相同的 url 和 method 的 MockConnection,会通过 IConnectionSelector 来选择其中之一进行返回,默认使用 RandomSelector 随机返回。 | ||
可以通过 实现 IConnectionSelector 接口并通过 MockNet.setSelector() 设置返回规则 | ||
|
||
#### 3. Log 设置 | ||
默认对每个 MockConnection 都会输出 Log 以帮助调试,如果想关闭 Log,可以在构建 MockConnection 时设置 isLog(false) | ||
|
||
MockNet 的 Log 输出是由 Logger 和 Printer 完成的,默认设置了 AndroidPrinter 和 JavaPrinter,如果想自定义 Log,可以实现 Printer 接口,并调用 Logger.init(yourPrinter) 设置,但是要在调用了 MockConnection.create() 之后,否则设置会被覆盖为默认设置 | ||
|
||
#### 4. 自定义 Handler 对请求响应进行处理 | ||
MockNet 中对 Request 和 Response 的处理采用了责任链模式(具体可查看整体架构中的介绍),通过添加 Handler 增加处理环节,框架自带的 Handler 有 BlockHandler,LogHandler,VerifyHeaderHandler,VerifyParamHandler,ConnectionHandler。 | ||
如果想增加自己的处理环节,请实现 Handler 接口,并通过 MockNet.addHandler(Handler h) 来设置。具体实现方法可参照框架默认实现的 Handler 代码。 | ||
|
||
#### 5. 支持 https | ||
支持 https 可以通过下面代码来开启: | ||
``` java | ||
MockNet mockNet = MockNet.create(); | ||
mockNet.start(ServerSocketFactory.createHttpsServerSocket(int port, String jksPath, String storePwd)); | ||
``` | ||
|
||
#### 6. 动态处理数据 | ||
为了简化使用方法和加快开发速度,默认只支持返回静态数据,暂时没有对数据进行动态处理。如果想动态处理请求数据,可以继承 RequestExecutor 接口并实现 execute 方法。在 execute 方法中对请求进行动态处理,并创建 Reponse 返回。 | ||
之后通过 Server 构造函数构造 Server 对象并传入实现 RequestExecutor 接口的类对象。 | ||
具体可以参考 MockRequestExecutor 的实现以及 Server 构造函数。 | ||
|
||
### 整体架构简析 | ||
![架构图](./proDocs/mocknet.png) | ||
整体架构可以分为两层,server 层和 application 层。 | ||
server 层主要做的是 socket 通信以及 Request 和 Response 的解析,解析结果会传入 RequestExecutor 中做处理。 | ||
application 层主要是对 request 的处理和对 response 的生成,MockRequestExecutor 实现了 RequestExecutor 接口,其中主要通过责任链模式来对 Request 处理。 | ||
|
||
### 其他 | ||
有关项目 bug 反馈或者希望项目增加什么功能或者单纯想和我交流,欢迎邮件交流或者添加 issue。 | ||
邮箱: zy5a59@outlook.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/> | ||
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>demo</name> | ||
<comment>Project demo created by Buildship.</comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.buildship.core.gradleprojectbuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
<nature>org.eclipse.buildship.core.gradleprojectnature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER) | ||
connection.project.dir=.. | ||
eclipse.preferences.version=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/> | ||
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>mocknet</name> | ||
<comment>Project mocknet created by Buildship.</comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.buildship.core.gradleprojectbuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
<nature>org.eclipse.buildship.core.gradleprojectnature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER) | ||
connection.project.dir=.. | ||
eclipse.preferences.version=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | ||
<!-- NewPage --> | ||
<html lang="en"> | ||
<head> | ||
<!-- Generated by javadoc (1.8.0_101) on Thu May 25 11:06:58 CST 2017 --> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
<title>All Classes (mocknet 1.0 API)</title> | ||
<meta name="date" content="2017-05-25"> | ||
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style"> | ||
<script type="text/javascript" src="script.js"></script> | ||
</head> | ||
<body> | ||
<h1 class="bar">All Classes</h1> | ||
<div class="indexContainer"> | ||
<ul> | ||
<li><a href="com/zy/mocknet/common/logger/AndroidPrinter.html" title="class in com.zy.mocknet.common.logger" target="classFrame">AndroidPrinter</a></li> | ||
<li><a href="com/zy/mocknet/server/bean/BasicRule.html" title="class in com.zy.mocknet.server.bean" target="classFrame">BasicRule</a></li> | ||
<li><a href="com/zy/mocknet/application/handler/BlockHandler.html" title="class in com.zy.mocknet.application.handler" target="classFrame">BlockHandler</a></li> | ||
<li><a href="com/zy/mocknet/Client.html" title="class in com.zy.mocknet" target="classFrame">Client</a></li> | ||
<li><a href="com/zy/mocknet/application/handler/ConnectionHandler.html" title="class in com.zy.mocknet.application.handler" target="classFrame">ConnectionHandler</a></li> | ||
<li><a href="com/zy/mocknet/application/ConnectionStore.html" title="class in com.zy.mocknet.application" target="classFrame">ConnectionStore</a></li> | ||
<li><a href="com/zy/mocknet/application/handler/Handler.html" title="class in com.zy.mocknet.application.handler" target="classFrame">Handler</a></li> | ||
<li><a href="com/zy/mocknet/application/handler/chain/HandlerChain.html" title="class in com.zy.mocknet.application.handler.chain" target="classFrame">HandlerChain</a></li> | ||
<li><a href="com/zy/mocknet/server/bean/Headers.html" title="class in com.zy.mocknet.server.bean" target="classFrame">Headers</a></li> | ||
<li><a href="com/zy/mocknet/application/selector/HeadSelector.html" title="class in com.zy.mocknet.application.selector" target="classFrame">HeadSelector</a></li> | ||
<li><a href="com/zy/mocknet/application/selector/IConnectionSelector.html" title="interface in com.zy.mocknet.application.selector" target="classFrame"><span class="interfaceName">IConnectionSelector</span></a></li> | ||
<li><a href="com/zy/mocknet/common/logger/JavaPrinter.html" title="class in com.zy.mocknet.common.logger" target="classFrame">JavaPrinter</a></li> | ||
<li><a href="com/zy/mocknet/common/logger/Logger.html" title="class in com.zy.mocknet.common.logger" target="classFrame">Logger</a></li> | ||
<li><a href="com/zy/mocknet/application/handler/LogHandler.html" title="class in com.zy.mocknet.application.handler" target="classFrame">LogHandler</a></li> | ||
<li><a href="com/zy/mocknet/Main.html" title="class in com.zy.mocknet" target="classFrame">Main</a></li> | ||
<li><a href="com/zy/mocknet/application/MockConnection.html" title="class in com.zy.mocknet.application" target="classFrame">MockConnection</a></li> | ||
<li><a href="com/zy/mocknet/application/MockConnection.Builder.html" title="class in com.zy.mocknet.application" target="classFrame">MockConnection.Builder</a></li> | ||
<li><a href="com/zy/mocknet/application/MockConnectionFactory.html" title="class in com.zy.mocknet.application" target="classFrame">MockConnectionFactory</a></li> | ||
<li><a href="com/zy/mocknet/MockNet.html" title="class in com.zy.mocknet" target="classFrame">MockNet</a></li> | ||
<li><a href="com/zy/mocknet/common/logger/MockPrinter.html" title="class in com.zy.mocknet.common.logger" target="classFrame">MockPrinter</a></li> | ||
<li><a href="com/zy/mocknet/application/MockRequestExecutor.html" title="class in com.zy.mocknet.application" target="classFrame">MockRequestExecutor</a></li> | ||
<li><a href="com/zy/mocknet/server/bean/Parameters.html" title="class in com.zy.mocknet.server.bean" target="classFrame">Parameters</a></li> | ||
<li><a href="com/zy/mocknet/common/logger/Printer.html" title="interface in com.zy.mocknet.common.logger" target="classFrame"><span class="interfaceName">Printer</span></a></li> | ||
<li><a href="com/zy/mocknet/application/selector/RandomSelector.html" title="class in com.zy.mocknet.application.selector" target="classFrame">RandomSelector</a></li> | ||
<li><a href="com/zy/mocknet/application/handler/chain/RealHandlerChain.html" title="class in com.zy.mocknet.application.handler.chain" target="classFrame">RealHandlerChain</a></li> | ||
<li><a href="com/zy/mocknet/server/bean/Request.html" title="class in com.zy.mocknet.server.bean" target="classFrame">Request</a></li> | ||
<li><a href="com/zy/mocknet/server/bean/RequestBody.html" title="class in com.zy.mocknet.server.bean" target="classFrame">RequestBody</a></li> | ||
<li><a href="com/zy/mocknet/server/RequestExecutor.html" title="interface in com.zy.mocknet.server" target="classFrame"><span class="interfaceName">RequestExecutor</span></a></li> | ||
<li><a href="com/zy/mocknet/server/RequestRunnable.html" title="class in com.zy.mocknet.server" target="classFrame">RequestRunnable</a></li> | ||
<li><a href="com/zy/mocknet/server/bean/Response.html" title="class in com.zy.mocknet.server.bean" target="classFrame">Response</a></li> | ||
<li><a href="com/zy/mocknet/server/bean/ResponseBody.html" title="class in com.zy.mocknet.server.bean" target="classFrame">ResponseBody</a></li> | ||
<li><a href="com/zy/mocknet/server/Server.html" title="class in com.zy.mocknet.server" target="classFrame">Server</a></li> | ||
<li><a href="com/zy/mocknet/server/ServerSocketFactory.html" title="class in com.zy.mocknet.server" target="classFrame">ServerSocketFactory</a></li> | ||
<li><a href="com/zy/mocknet/server/ThreadPool.html" title="class in com.zy.mocknet.server" target="classFrame">ThreadPool</a></li> | ||
<li><a href="com/zy/mocknet/application/handler/TmpHandler.html" title="class in com.zy.mocknet.application.handler" target="classFrame">TmpHandler</a></li> | ||
<li><a href="com/zy/mocknet/common/Utils.html" title="class in com.zy.mocknet.common" target="classFrame">Utils</a></li> | ||
<li><a href="com/zy/mocknet/application/handler/VerifyHeaderHandler.html" title="class in com.zy.mocknet.application.handler" target="classFrame">VerifyHeaderHandler</a></li> | ||
<li><a href="com/zy/mocknet/application/handler/VerifyParamHandler.html" title="class in com.zy.mocknet.application.handler" target="classFrame">VerifyParamHandler</a></li> | ||
</ul> | ||
</div> | ||
</body> | ||
</html> |
Oops, something went wrong.