模拟服务器数据格式进行客户端调试用。方便本地调试试挫从而与服务器定具体的数据格式。

基本配置

1. Mac 的 jdk 环境配置
2. 下载 moco 服务 jar 包
3. 在本地新建一个文件夹将下载好的 jar 包拖入,并且同目录下创建一个 xxx.json 文件;
4. 验证是否ok:xxx.json 我命名为 localServer.json,其数据如下

1
2
3
4
5
6
7
[
{
"response": {
"text": "This server ok!"
}
}
]

同路径下终端执行命令启动服务

1
$ java -jar moco-runner-0.10.2-standalone.jar start -p 8080 -c localServer.json.json

浏览器打开 http://localhost:8080/ 验证是否ok。

Get / Post 请求

request 请求有14个固定的属性: methodheadersjsonfactoryuritextcookiesxpathsjson_pathsversionfilequeriespath_resourceforms。一定要遵循这些方法。 常用的 method(请求方式),headers(heads参数),uri(url地址),file(指定调用的请求文件), queries(请求带参),forms(表单内容)。

response 响应有12个固定属性:statusattachmentheadersversionfactoryfiletextproxycookiesjsonlatencypath_resource

Get

1. 修改 localServer.json 文件,模拟无请求参数 Get 请求:

1
2
3
4
5
6
7
8
9
10
11
[
{
"request": {
"method": "get",
"uri": "/getMethod"
},
"response": {
"text": "Get method!"
}
}
]

同上一样启动服务。浏览器 http://localhost:8080/getMethod 验证即可。

2. 修改 localServer.json 文件,模拟带请求参数的 Get 请求:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[
{
"request": {
"method" : "get",
"uri": "/getMethodWithParams",
"queries": {
"param1": "1",
"param2": "2"
}
},
"response": {
"text": "This is a method with params!"
}
}
]

同上一样启动服务。浏览器

1
http://localhost:8080/getMethodWithParams?param1=1&param2=2

验证即可。

Post

1. 修改 localServer.json 文件,写一个带有headers,body的Post 请求方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[
{
"request": {
"method": "post",
"uri": "/postMethod",
"headers": {
"content-type": "application/json",
"sessionid": "e566288ba77de98d"
},
"forms": {
"name": "zerocc",
"password": "123456"
}
},
"response": {
"text": "This is a POST Method!"
}
}
]

2. 调试使用 AFN 请求数据,真机的话需连接同一 WiFi 通过 IP 地址访问,URL 为 IP地址 + 端口 + URI;模拟器 http://localhost:8080/postMethod 还是可以访问的。具体demo :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#import "ViewController.h"
#import "AFNetworking.h"
[object Object]
@interface ViewController ()
[object Object]
@end
[object Object]
@implementation ViewController
[object Object]
- (void)viewDidLoad {
[super viewDidLoad];

[self postHTTPNetwork];
}
[object Object]
- (void)postHTTPNetwork {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"content-type"];
[manager.requestSerializer setValue:@"e566288ba77de98d" forHTTPHeaderField:@"sessionid"];
NSDictionary *paramDic = @{@"name":@"zerocc",
@"password":@"123456"};
[manager POST:@"http://172.21.184.223:8080/postMethod" parameters:paramDic progress:^(NSProgress * _Nonnull uploadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *receiveStr = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"receiveStr:::%@",receiveStr);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error");
}];
}

3. 服务器响应过程,终端显示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
25 Mar 2016 18:31:05 [nioEventLoopGroup-3-6] INFO  Request received:
[object Object]
POST /postMethod HTTP/1.1
Host: 172.21.184.223:8080
sessionid: e566288ba77de98d
Content-Type: application/json
Connection: keep-alive
Accept: */*
User-Agent: TestLocalServer/1.0 (iPhone; iOS 9.2.1; Scale/2.00)
Accept-Language: zh-Hans-CN;q=1
Content-Length: 27
Accept-Encoding: gzip, deflate
[object Object]
name=zerocc&password=123456
[object Object]
25 Mar 2016 18:31:05 [nioEventLoopGroup-3-6] INFO Response return:
[object Object]
HTTP/1.1 200
Content-Length: 22
Content-Type: text/plain; charset=utf-8
[object Object]
This is a POST Method!

全局灵活配置

在 moco-server jar 包目录下创建 serverConfig.json, 同时创建 login 和 user 两个文件夹。

  • 在login文件夹内创建 login.json 和 login_response.json 两个文件。
  • 在asset文件夹内创建 user.json和 user_response.json两个文件。

1. serverConfig.json 文件:

1
2
3
4
5
6
7
8
[
{
"include": "login/login.json"
},
{
"include": "user/user.json"
}
]

2. login.json输入以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[
{
"request": {
"uri": "/assetApp/login",
"method": "get",
"queries": {
"username": "zerocc",
"password": "123456"
}
},
"response": {
"file": "./login/login_response.json"
}
}
]

3. user.json内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[
{
"request": {
"uri": "/assetApp/assetList",
"method": "post",
"headers": {
"content-type": "application/json",
"sessionid": "e566288ba77de98d"
}
},
"response": {
"file": "./user/user_response.json"
}
}
]

4. user_response.json 内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[
{
"id":@"140437656"
"friend_name": "zeroa",
"location": "上海",
"sex": "男"
},
{
"id":@"140437657"
"friend_name": "zerob",
"location": "北京",
"sex": "男"
},
{
"id":@"140437658"
"friend_name": "zeroc",
"location": "深圳",
"sex": "女"
}
]

启动服务 jar 包目录下 终端指令

1
$ java -jar moco-runner-0.10.2-standalone.jar start -p 8080 -g serverConfig.json

参考相关