Skip to content

Commit

Permalink
feat: Closes #14 #12 .
Browse files Browse the repository at this point in the history
  • Loading branch information
temberature committed Apr 29, 2019
1 parent 0778305 commit e758740
Show file tree
Hide file tree
Showing 8 changed files with 2,727 additions and 2,686 deletions.
3 changes: 2 additions & 1 deletion lib/anymock.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const options = {
throttle: 10000,
forceProxyHttps: true,
wsIntercept: false, // 不开启websocket代理
silent: false
silent: false,
dangerouslyIgnoreUnauthorized: true
};
const proxyServer = new AnyProxy.ProxyServer(options);

Expand Down
10 changes: 8 additions & 2 deletions lib/anyproxy.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ module.exports = function () {
const searchParams = url.searchParams;
const callback = searchParams.get('callback') || new URLSearchParams(requestDetail.requestData.toString()).get('callback');

const rt = mock(url.href, searchParams, callback)
let rt = mock(url.href, searchParams, callback)

if (rt && rt.match) {
rt.response.header["Access-Control-Allow-Origin"] = requestDetail.requestOptions.headers["Origin"] || "*"
rt.response.header["Access-Control-Allow-Credentials"] = true
rt.response.header["Access-Control-Allow-Headers"] = "platform"
return {
response: rt.response
};
Expand All @@ -23,7 +27,9 @@ module.exports = function () {
* beforeSendResponse(requestDetail, responseDetail) {
// console.log(requestDetail.requestOptions.headers["Origin"]);
const newResponse = responseDetail.response;
newResponse.header["Access-Control-Allow-Origin"] = requestDetail.requestOptions.headers["Origin"] || "*";
if (!newResponse.header["Access-Control-Allow-Origin"] && !newResponse.header["access-control-allow-origin"]) {
newResponse.header["Access-Control-Allow-Origin"] = requestDetail.requestOptions.headers["Origin"] || "*";
}
return {
response: newResponse
};
Expand Down
4 changes: 2 additions & 2 deletions lib/createRes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ function createRes(callbackName, mockMap) {
}
} else if (!type) {
rt = JSON.stringify(body);
type = 'application/json'
} else {
rt = body;
}
return {
statusCode: 200,
header: {
"Content-Type": type || "text/plain",
"Access-Control-Allow-Origin": "*"
"Content-Type": type || "text/plain"
},
body: rt
};
Expand Down
29 changes: 19 additions & 10 deletions lib/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,34 @@ function mock(url, searchParams, callbackName) {
const matchMock = mocks.filter(mock => {
return startsWith(url, mock.URL || mock.url);
})[0];
if (!matchMock || matchMock.enabled === 0) {
if (!matchMock || matchMock.enabled === 0 || matchMock.enabled === '') {
return;
}
const matchURL = normalizeUrl(matchMock.URL || matchMock.url);
if (is.string(matchMock.enabled)) {
const local = matchMock.enabled
if (fs.existsSync(local)) {
let enabled = matchMock.enabled
if (is.string(enabled)) {
enabled = [enabled]
}
if (is.string(enabled[0]) && enabled[0].includes('/')) {
let body = enabled.reduce((a, local) => {
if (fs.existsSync(local)) {
return a + fs.readFileSync(local, {
encoding: "utf-8"
}) + '\n'
} else {
throw new Error('local file not exists ' + local)
}

}, '')
if (body) {
match = true;
response = {
statusCode: 200,
header: {
"Content-Type": contentTypeMap[path.extname(local)],
"Content-Type": contentTypeMap[path.extname(enabled[0])] || 'text/plain',
},
body: fs.readFileSync(local, {
encoding: "utf-8"
})
body: body
};
} else {
throw new Error('local file not exists ' + local)
}
} else {
const MOCKS = loadJsonFile.sync("./mock/mocks.json");
Expand Down
Loading

0 comments on commit e758740

Please sign in to comment.