mac搭建测试服务器

 代码可以参考:

https://github.com/BigShow1949/MyServe  这里也有jar包

一、下载一个jar包

点击链接下载服务器端【moco服务端】

https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.10.2/moco-runner-0.10.2-standalone.jar 


二、简单的测试

1.在桌面建立一个文件夹,将刚刚下载的jar包拖进去

2.创建一个json文件


testServe

在teseServe.json文件中写入

【注意不要使用文本编辑器打开编辑,会有符号不识别在运行的时候程序会报错,请使用XCode打开编辑】

[  { "response":{      "text":"First Blood!"}}]

3.打开终端输入以下命令

cd  /Users/bean/Desktop/testServe      【打开文件夹】

java -jar moco-runner-0.10.2-standalone.jar start -p 8080 -c testServe.json

【注意:8080是端口号,可以随意定义数字】

4.终端就会看到如下窗口就代表成功了


 

5.用浏览器打开  http://localhost:8080/

显示如下图


First Blood

三、Get请求

修改一下刚刚的json里面的内容

[

{  "response":{"text":"First Blood!"}},

{  "request":{ "method" :"get", "uri":"/getTest"}, 

"response":{ "text":"First Get!"}

}

]

在浏览器里输入http://localhost:8080/getTest


First Get

request 请求

有14个固定的属性:

method,headers,json,factory,uri,text,cookies,xpaths,

json_paths,version,file,queries,path_resource,forms。

一定要遵循这些方法。

常用的method(请求方式),headers(heads参数),uri(url地址),file(指定调用的请求文件),queries(请求带参),forms(表单内容)。

response 响应

有12个固定属性:

status,attachment,headers,version,factory,file,text,proxy,cookies,json,latency,path_resource。

tip:response里的text可以故意写错,然后终端会有提醒,告诉你返回的属性有哪些.

四、带参数的方法

同理修改json文件

{"request":

{"uri":"/getTestWithParams",

"queries":{  "param1":"1",  "param2":"2"}

}, 

"response":{ "text":"This is a method with params!"}

}

浏览器输入链接http://localhost:8080/getTestWithParams?param1=1&param2=2

五、Post请求

修改json

{    "request":{ "method" :"post",        "uri":"/postMethod",        "headers" :{            "content-type" :"application/json",            "sessionid":"e566288ba77de98d"},        "forms" :{          "name" :"zhangsan",          "password" :"123456"}},    "response":{        "text":"This is a POST Method!"}}


六、AFN请求

在网页输入: http://localhost:8080/assetApp/login?password=123456&username=zhangsan

 1     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
 2     
 3     manager.responseSerializer = [AFHTTPResponseSerializer serializer];
 4     manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"text/json",@"text/html",@"application/json",@"text/plain", nil];
 5     NSString *url=@"http://10.100.70.234:8080/assetApp/login";
 6     NSDictionary *dic = @{@"username":@"zhangsan",
 7                           @"password":@"123456"};
 8     [manager GET:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
 9         NSLog(@"responseObject = %@", responseObject);
10     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
11         NSLog(@"error = %@", error);
12     }];

注意:

1)这里不要再写成localhost了, 不然会连不上服务器, 写自己笔记本的ip就ok了

2)请求参数不要写错了,比如username写成name,不然网页啥也没有, AFN请求会报400.

参考:

http://www.jianshu.com/p/cb7eb3bf272c

http://www.jianshu.com/p/638da64422a0

原文地址:https://www.cnblogs.com/bigshow1949/p/6183616.html