实际工作中,业务模块之间大多存在依赖关系的,例:B模块依赖A模块的触发,如果A模块处于阻断性、很难触发、已触发无法再次触发或不想被触发时,B模块是无法单独进行调试或测试的,这时可以借助mitmproxy工具实现前后端双向篡改,重定向A模块的url,mock A模块成功的响应数据,即A模块被假定成功触发,实际并未触发到服务器,这时我们就可进行B模块的调试或测试工作。
测试需求:
拉钩app-我的页面-活动banner
1、活动banner接口500了,活动banner不展示,无法测试活动详情页
2、活动banner已进入1次,活动banner不展示,无法测试活动详情页
3、至尊VIP充值5w,才能看到活动banner,真实充值不太可能,活动banner不展示,无法测试活动详情页
抓取banner(BAT大佬都在看的搞笑学习法)接口信息,或者参照接口文档接口信息
GET https://gate.lagou.com/v1/neirong/edu/ads/ad?type=1 HTTP/2.0
{
"content": {
"adAppHref": "https://edu.lagou.com/growth/sem/offer.html?utm_source=lagouedu&utm_medium=wodetab-banner&utm_campaign=%E5%A4%A7%E8%AF%BE%E7%BA%BF%E7%B4%A2&_channel_track_key=yKyKbQxA&lgec_type=website&lgec_sign=9A0FAD8D532161C641529DEDC5C30AFC",
"adAppImage": "https://s0.lgstatic.com/i/image6/M00/66/38/Cgp9HWGwLI6AIYWSAAMK4ADi3Jw315.png",
"adId": 169,
"adName": "大厂私教offer计划",
"adPcHref": "",
"adPcImage": "",
"adType": null
},
"message": "操作成功",
"state": 1,
"uiMessage": null
}
篡改接口地址https://gate.lagou.com/v1/neirong/edu/ads/ad?type=1为https://www.baidu.com,让app发起请求时,不请求拉钩服务器,重定向到百度服务器
class Demo:
def request(self, flow):
# 匹配指定的url
if 'https://gate.lagou.com/v1/neirong/edu/ads/ad?type=' in flow.request.url:
flow.request.url='https://www.baidu.com/'
addons = [
Demo()
]
由于前段识别到接口返回的数据是百度响应的数据格式,无法正常识别,故不展示banner了
我们想让banner展示出来,需要根据接口文档模拟正确的响应数据,让前端可以正常识别
common | Description |
---|---|
response = flow.response | |
response.status_code | 响应码 |
response.text | 文本 (同下) |
response.content | Bytes 类型 |
response.headers | 响应头 |
response.cookies | 响应 cookie |
response.set_text() | 修改响应的文本 |
response.get_text() | 文本 (同上) |
flow.response= flow.response.make(404) | 响应 404 |
import json, os
response_data = {
"content": {
"adAppHref": "https://edu.lagou.com/growth/sem/offer.html?utm_source=lagouedu&utm_medium=wodetab-banner&utm_campaign=%E5%A4%A7%E8%AF%BE%E7%BA%BF%E7%B4%A2&_channel_track_key=yKyKbQxA&lgec_type=website&lgec_sign=9A0FAD8D532161C641529DEDC5C30AFC",
"adAppImage": "https://s0.lgstatic.com/i/image6/M00/66/38/Cgp9HWGwLI6AIYWSAAMK4ADi3Jw315.png",
"adId": 169,
"adName": "测试",
"adPcHref": "",
"adPcImage": "",
"adType": 'null'
},
"message": "操作成功",
"state": 1,
"uiMessage": 'null'
}
class Demo:
def request(self, flow):
# 匹配指定的url
if 'https://gate.lagou.com/v1/neirong/edu/ads/ad?type=' in flow.request.url:
flow.request.url='https://www.baidu.com/'
def response(self, flow):
# 匹配指定的url
if flow.request.url.startswith ( 'https://www.baidu.com/' ):
# 设置响应据
flow.response.set_text ( json.dumps ( response_data ) )
addons = [
Demo()
]
再次app请求,接口响应数据是我们脚本里设置的
banner展示出来了,说明篡改的响应数据生效了,前段识别成功了,这时可以进入活动详情进行测试了,达到我们想要的效果了