UNPKG

5.89 kBJavaScriptView Raw
1/**
2 * Created by tanxiangyuan on 2016/6/24.
3 */
4var http = require('http'),
5 projectId = 4858,
6 mockHost = /m\.(jdpay|wangyin)\.com/,
7 options = {
8 hostname: 'rap.taobao.org',
9 path: '/api/queryModel.do?projectId=' + projectId
10 },
11 interfaceList = [];
12
13var req = http.request(options, (res) => {
14 res.setEncoding('utf8');
15 res.on('data', (chunk) => {
16 console.log(`BODY: ${chunk}`);
17 JSON.parse(chunk).model.moduleList.forEach(function (mod) {
18 mod.pageList.forEach(function (page) {
19 page.interfaceList.forEach(function (face) {
20 interfaceList.push(face.reqUrl);
21 });
22 });
23 });
24 console.log(`interface list : ${JSON.stringify(interfaceList)}`);
25 });
26 res.on('end', () => {
27 console.log('No more data in response.')
28 })
29});
30
31req.on('error', (e) => {
32 console.log(`problem with request: ${e.message}`);
33});
34req.end();
35
36function filterable(req) {
37 var mock = false;
38 if (req.headers.host.match(mockHost)) {
39 var path = req.url.substring(req.url.indexOf(req.headers.host) + req.headers.host.length);
40 for (var i = interfaceList.length; i > 0; i--) {
41 mock = req.url.startsWith('/' + interfaceList[i - 1]) || path.startsWith('/' + interfaceList[i - 1]);
42 }
43 }
44 return mock;
45}
46
47module.exports = {
48 /*
49 These functions will overwrite the default ones, write your own when necessary.
50 Comments in Chinese are nothing but a translation of key points. Be relax if you dont understand.
51 致中文用户:中文注释都是只摘要,必要时请参阅英文文档。欢迎提出修改建议。
52 */
53 summary: function () {
54 return "this is a anymock rule for AnyProxy";
55 },
56
57
58 //=======================
59 //when getting a request from user
60 //收到用户请求之后
61 //=======================
62
63 //是否截获https请求
64 //should intercept https request, or it will be forwarded to real server
65 shouldInterceptHttpsReq: function (req) {
66 return true;
67 },
68
69 //是否在本地直接发送响应(不再向服务器发出请求)
70 //whether to intercept this request by local logic
71 //if the return value is true, anyproxy will call dealLocalResponse to get response data and will not send request to remote server anymore
72 //req is the user's request sent to the proxy server
73 shouldUseLocalResponse: function (req, reqBody) {
74 return false;
75 },
76
77 //如果shouldUseLocalResponse返回true,会调用这个函数来获取本地响应内容
78 //you may deal the response locally instead of sending it to server
79 //this function be called when shouldUseLocalResponse returns true
80 //callback(statusCode,resHeader,responseData)
81 //e.g. callback(200,{"content-type":"text/html"},"hello world")
82 dealLocalResponse: function (req, reqBody, callback) {
83 callback(statusCode, resHeader, responseData)
84 },
85
86
87 //=======================
88 //when ready to send a request to server
89 //向服务端发出请求之前
90 //=======================
91
92 //替换向服务器发出的请求协议(http和https的替换)
93 //replace the request protocol when sending to the real server
94 //protocol : "http" or "https"
95 replaceRequestProtocol: function (req, protocol) {
96 return filterable(req) ? 'http' : protocol;
97 },
98
99 //替换向服务器发出的请求参数(option)
100 //option is the configuration of the http request sent to remote server. You may refers to http://nodejs.org/api/http.html#http_http_request_options_callback
101 //you may return a customized option to replace the original one
102 //you should not overwrite content-length header in options, since anyproxy will handle it for you
103 replaceRequestOption: function (req, option) {
104 var newOption = option;
105 if (filterable(req)) {
106 newOption.hostname = 'rap.taobao.org';
107 newOption.path = '/mockjsdata/' + projectId + option.path;
108 newOption.port = '80';
109 }
110 return newOption;
111 },
112
113 //替换请求的body
114 //replace the request body
115 replaceRequestData: function (req, data) {
116 return data;
117 },
118
119
120 //=======================
121 //when ready to send the response to user after receiving response from server
122 //向用户返回服务端的响应之前
123 //=======================
124
125 //替换服务器响应的http状态码
126 //replace the statusCode before it's sent to the user
127 replaceResponseStatusCode: function (req, res, statusCode) {
128 var newStatusCode = statusCode;
129 return newStatusCode;
130 },
131
132 //替换服务器响应的http头
133 //replace the httpHeader before it's sent to the user
134 //Here header == res.headers
135 replaceResponseHeader: function (req, res, header) {
136 var newHeader = header;
137 return newHeader;
138 },
139
140 //替换服务器响应的数据
141 //replace the response from the server before it's sent to the user
142 //you may return either a Buffer or a string
143 //serverResData is a Buffer. for those non-unicode reponse , serverResData.toString() should not be your first choice.
144 replaceServerResDataAsync: function (req, res, serverResData, callback) {
145 callback(serverResData);
146 },
147
148 //Deprecated
149 // replaceServerResData: function(req,res,serverResData){
150 // return serverResData;
151 // },
152
153 //在请求返回给用户前的延迟时间
154 //add a pause before sending response to user
155 pauseBeforeSendingResponse: function (req, res) {
156 var timeInMS = 1; //delay all requests for 1ms
157 return timeInMS;
158 }
159
160};
\No newline at end of file