MockServer工具-Moco应用

软测小生

共 2784字,需浏览 6分钟

 · 2020-12-18

前言

前段时间介绍了Mock基本知识和Moco工具简单使用,今天重点介绍小编在实际业务所遇到的Moco应用场景。

Nginx代理

1.将域名转发到本地端口

将域名转发到本地另一个端口上

server{  listen 80;  server_name  sogou.com;  index  index.php index.html index.htm;  location / {    proxy_pass  http://127.0.0.1:8080; # 转发规则    proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求    proxy_set_header X-Real-IP $remote_addr;    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  }}

访问http://sogou.com时 就会转发到本地8080端口

2.将域名转发到另一个域名

server{  listen 80;  server_name  sogou.com;  index  index.php index.html index.htm;  location / {    proxy_pass  http://www.sogou.com;    proxy_set_header Host $proxy_host;    proxy_set_header X-Real-IP $remote_addr;    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  }}
访问http://sogou.com时就会转发
http://www.sogou.com


3.本地一个端口转发到另一个端口或另一个域名

server{  listen 80;  server_name 127.0.0.1; # 公网ip  index  index.php index.html index.htm;  location / {    proxy_pass  http://127.0.0.1:8080; # 或 http://www.sogou.com    proxy_set_header Host $proxy_host;    proxy_set_header X-Real-IP $remote_addr;    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  }}

访问http://127.0.0.1 就会转发本地8080端口或者访问http://www.sogou.com


4.加/ 与不加/

在配置proxy_pass代理转发时,如果后面的url加/,表示绝对根路径;如果没有/,表示相对路径

1. 加 /

server_name sogou.comlocation /data/ {proxy_pass http://127.0.0.1/;}

访问http://sogou.com/data/index.html 会转发到 http://127.0.0.1/index.html

2.不加/

server_name sogou.comlocation /data/ {proxy_pass http://127.0.0.1;}

访问 http://sogou.com/data/index.html 会转发到 http://127.0.0.1/data/index.html


Moco部署

1.确定DSP请求方式、请求头部、Response

例如:

response.json

[  {    "description": "pinYinAdx",    "request":{        "method": "get",        "headers": {        "content-type": "application/json"      }    },    "response": {      "file": "pinYin.response"    }  }]

pinYin.response

{  "id""51f7eef525d66c11f0e53b12eacd6a8a",   ...}

2.部署并启动Moco

  • 通过https://github.com/dreamhead/moco 获取moco jar包

  • 后台运行Moco服务

    nohup java -jar moco-runner-1.1.0-standalone.jar http -p 4000 -c response.json  &

业务应用

1.nginx.conf配置


server {      server_name sogou.dsp.ipinyinu.com; location /sogou { proxy_pass http://10.168.15.1:4000; } location / {                  proxy_pass http://10.168.15.3:4000;            } }

2.配置目标机器Host

10.168.15.1 sogou.dsp.ipinyou.com 


3. 当请求域名为sogou.dsp.ipinyin.com ,且uri 包含/sogou,转发到已经部署好的MockServer




文章合集

Selenium Appium  | Jenkins  |  Jmeter 

软件测试方法汇总 Postman接口参数化 | 测试用例设计 | App抓包

视频教程

Selenium | Appium | Jenkins | Jmeter


AWS与Docker

如何使用AWS EC2+Docker+JMeter构建分布式负载测试基础架构
Docker容器数据持久化和容器网桥连接
Docker删除image和container
Docker与VM虚拟机的区别以及Docker的特点



END



觉得不错,可以“点赞”和“在看”,或者转发留言


精彩的内容要和朋友分享
微信群:
软件自动化测试交流群
已创建,公号回复入群即可获取入群二维码。

浏览 30
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报