前言
fiddler可以抓包打斷點后,修改返回的內容,便於模擬各種返回結果。anyproxy也可以通過寫rule模塊規則,模擬返回狀態碼、頭部、body
beforeSendResponse
beforeSendResponse(requestDetail, responseDetail)
- AnyProxy向客戶端發送請求前,會調用
beforeSendResponse
,並帶上參數requestDetail
responseDetail
requestDetail
同beforeSendRequest
中的參數- responseDetail
response {object} 服務端的返回信息,包括statusCode header body三個字段
_res {object} 原始的服務端返回對象
舉例,請求 http://httpbin.org/get
時,responseDetail參數內容大致如下
{
response: { statusCode: 200, header: { 'Content-Type': 'image/gif', Connection: 'close', 'Cache-Control': '...' }, body: '...' }, _res: { /* ... */ } }
修改返回內容
以下幾種返回都是合法的
不做任何處理,返回null
return null;
修改返回的狀態碼
var newResponse = Object.assign({}, responseDetail.response); newResponse.statusCode = 404; return { response: newResponse };
修改返回的內容
var newResponse = Object.assign({}, responseDetail.response); newResponse.body += '--from anyproxy--'; return { response: newResponse };
rule案例
這里提供一些樣例,來講解規則模塊的常見用法
你可以通過 anyproxy --rule http://....js 來加載模塊並體驗
用curl發請求測試的方法如下
- 直接請求服務器:curl http://httpbin.org/
- 通過代理服務器請求:curl http://httpbin.org/ --proxy http://127.0.0.1:8001
返回本地json格式body
使用本地數據,攔截發送到 http://httpbin.org 的請求,使用本地數據代替服務端返回,sample_use_local_response.js規則
/* sample: intercept all requests toward httpbin.org, use a local response test: curl http://httpbin.org/user-agent --proxy http://127.0.0.1:8001 */ module.exports = { *beforeSendRequest(requestDetail) { const localResponse = { statusCode: 200, header: { 'Content-Type': 'application/json' }, body: '{"hello": "this is local response"}' }; if (requestDetail.url.indexOf('http://httpbin.org') === 0) { return { response: localResponse }; } }, };
加載rule規則的js文件
anyproxy -i --rule sample_use_local_response.js
使用curl測試http://httpbin.org
C:\Users\dell>curl http://httpbin.org/user-agent --proxy http://127.0.0.1:8001
{"hello": "this is local response"}
模擬返回狀態碼
模擬返回404狀態碼,sample_modify_response_statuscode.js規則如下
/* sample: modify all status code of http://httpbin.org/ to 404 test: curl -I 'http://httpbin.org/user-agent' --proxy http://127.0.0.1:8001 expected response: HTTP/1.1 404 Not Found */ module.exports = { *beforeSendResponse(requestDetail, responseDetail) { if (requestDetail.url.indexOf('http://httpbin.org') === 0) { const newResponse = responseDetail.response; newResponse.statusCode = 404; return { response: newResponse }; } } };
模擬返回頭部
修改返回的頭部,sample_modify_response_header.js規則如下
/* sample: modify response header of http://httpbin.org/user-agent test: curl -I 'http://httpbin.org/user-agent' --proxy http://127.0.0.1:8001 expected response: X-Proxy-By: AnyProxy */ module.exports = { *beforeSendResponse(requestDetail, responseDetail) { if (requestDetail.url.indexOf('http://httpbin.org/user-agent') === 0) { const newResponse = responseDetail.response; newResponse.header['X-Proxy-By'] = 'AnyProxy'; return { response: newResponse }; } } };
修改返回body
修改返回的body里面部分內容,sample_modify_response_data.js規則如下
/* sample: modify response data of http://httpbin.org/user-agent test: curl 'http://httpbin.org/user-agent' --proxy http://127.0.0.1:8001 expected response: { "user-agent": "curl/7.43.0" } -- AnyProxy Hacked! -- */ module.exports = { *beforeSendResponse(requestDetail, responseDetail) { if (requestDetail.url === 'http://httpbin.org/user-agent') { const newResponse = responseDetail.response; newResponse.body += '-- AnyProxy Hacked! --'; return new Promise((resolve, reject) => { setTimeout(() => { // delay the response for 5s resolve({ response: newResponse }); }, 5000); }); } }, };
更多學習資料參考https://github.com/alibaba/anyproxy/blob/master/docs/cn/src_doc.md